github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/peer/channel/channel.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package channel
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/hyperledger/fabric/msp"
    23  	"github.com/hyperledger/fabric/peer/common"
    24  	ab "github.com/hyperledger/fabric/protos/orderer"
    25  	pb "github.com/hyperledger/fabric/protos/peer"
    26  	"github.com/op/go-logging"
    27  	"github.com/spf13/cobra"
    28  	"github.com/spf13/viper"
    29  	"golang.org/x/net/context"
    30  	"google.golang.org/grpc"
    31  )
    32  
    33  const channelFuncName = "channel"
    34  
    35  var logger = logging.MustGetLogger("channelCmd")
    36  
    37  var (
    38  	// join related variables.
    39  	genesisBlockPath string
    40  
    41  	// create related variables
    42  	chainID       string
    43  	channelTxFile string
    44  )
    45  
    46  // Cmd returns the cobra command for Node
    47  func Cmd(cf *ChannelCmdFactory) *cobra.Command {
    48  	//the "peer.committer.enabled" flag should really go away...
    49  	//basically we need the orderer for create and join
    50  	if !viper.GetBool("peer.committer.enabled") || viper.GetString("peer.committer.ledger.orderer") == "" {
    51  		panic("orderer not provided")
    52  	}
    53  
    54  	AddFlags(channelCmd)
    55  	channelCmd.AddCommand(joinCmd(cf))
    56  	channelCmd.AddCommand(createCmd(cf))
    57  	channelCmd.AddCommand(fetchCmd(cf))
    58  
    59  	return channelCmd
    60  }
    61  
    62  // AddFlags adds flags for create and join
    63  func AddFlags(cmd *cobra.Command) {
    64  	flags := cmd.PersistentFlags()
    65  
    66  	flags.StringVarP(&genesisBlockPath, "blockpath", "b", common.UndefinedParamValue, "Path to file containing genesis block")
    67  	flags.StringVarP(&chainID, "chain", "c", common.UndefinedParamValue, "In case of a newChain command, the chain ID to create.")
    68  	flags.StringVarP(&channelTxFile, "file", "f", "", "Configuration transaction file generated by a tool such as configtxgen for submitting to orderer")
    69  }
    70  
    71  var channelCmd = &cobra.Command{
    72  	Use:   channelFuncName,
    73  	Short: fmt.Sprintf("%s specific commands.", channelFuncName),
    74  	Long:  fmt.Sprintf("%s specific commands.", channelFuncName),
    75  }
    76  
    77  type BroadcastClientFactory func() (common.BroadcastClient, error)
    78  
    79  // ChannelCmdFactory holds the clients used by ChannelCmdFactory
    80  type ChannelCmdFactory struct {
    81  	EndorserClient   pb.EndorserClient
    82  	Signer           msp.SigningIdentity
    83  	BroadcastClient  common.BroadcastClient
    84  	DeliverClient    deliverClientIntf
    85  	BroadcastFactory BroadcastClientFactory
    86  }
    87  
    88  // InitCmdFactory init the ChannelCmdFactor with default clients
    89  func InitCmdFactory(isOrdererRequired bool) (*ChannelCmdFactory, error) {
    90  	var err error
    91  
    92  	cmdFact := &ChannelCmdFactory{}
    93  
    94  	cmdFact.Signer, err = common.GetDefaultSigner()
    95  	if err != nil {
    96  		return nil, fmt.Errorf("Error getting default signer: %s", err)
    97  	}
    98  
    99  	cmdFact.BroadcastFactory = func() (common.BroadcastClient, error) {
   100  		return common.GetBroadcastClient()
   101  	}
   102  
   103  	if err != nil {
   104  		return nil, fmt.Errorf("Error getting broadcast client: %s", err)
   105  	}
   106  
   107  	//for join, we need the endorser as well
   108  	if isOrdererRequired {
   109  		cmdFact.EndorserClient, err = common.GetEndorserClient()
   110  		if err != nil {
   111  			return nil, fmt.Errorf("Error getting endorser client %s: %s", channelFuncName, err)
   112  		}
   113  	} else {
   114  		orderer := viper.GetString("peer.committer.ledger.orderer")
   115  		conn, err := grpc.Dial(orderer, grpc.WithInsecure())
   116  		if err != nil {
   117  			return nil, err
   118  		}
   119  
   120  		client, err := ab.NewAtomicBroadcastClient(conn).Deliver(context.TODO())
   121  		if err != nil {
   122  			fmt.Println("Error connecting:", err)
   123  			return nil, err
   124  		}
   125  
   126  		cmdFact.DeliverClient = newDeliverClient(client, chainID)
   127  	}
   128  
   129  	return cmdFact, nil
   130  }