github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/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 "strings" 22 23 "github.com/hyperledger/fabric/common/flogging" 24 "github.com/hyperledger/fabric/msp" 25 "github.com/hyperledger/fabric/peer/common" 26 ab "github.com/hyperledger/fabric/protos/orderer" 27 pb "github.com/hyperledger/fabric/protos/peer" 28 "github.com/spf13/cobra" 29 "golang.org/x/net/context" 30 "google.golang.org/grpc" 31 "google.golang.org/grpc/credentials" 32 ) 33 34 const channelFuncName = "channel" 35 36 var logger = flogging.MustGetLogger("channelCmd") 37 38 var ( 39 // join related variables. 40 genesisBlockPath string 41 42 // create related variables 43 chainID string 44 channelTxFile string 45 orderingEndpoint string 46 tls bool 47 caFile string 48 ) 49 50 // Cmd returns the cobra command for Node 51 func Cmd(cf *ChannelCmdFactory) *cobra.Command { 52 53 AddFlags(channelCmd) 54 channelCmd.AddCommand(joinCmd(cf)) 55 channelCmd.AddCommand(createCmd(cf)) 56 channelCmd.AddCommand(fetchCmd(cf)) 57 channelCmd.AddCommand(listCmd(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 flags.StringVarP(&orderingEndpoint, "orderer", "o", "", "Ordering service endpoint") 70 flags.BoolVarP(&tls, "tls", "", false, "Use TLS when communicating with the orderer endpoint") 71 flags.StringVarP(&caFile, "cafile", "", "", "Path to file containing PEM-encoded trusted certificate(s) for the ordering endpoint") 72 } 73 74 var channelCmd = &cobra.Command{ 75 Use: channelFuncName, 76 Short: fmt.Sprintf("%s specific commands.", channelFuncName), 77 Long: fmt.Sprintf("%s specific commands.", channelFuncName), 78 } 79 80 type BroadcastClientFactory func() (common.BroadcastClient, error) 81 82 // ChannelCmdFactory holds the clients used by ChannelCmdFactory 83 type ChannelCmdFactory struct { 84 EndorserClient pb.EndorserClient 85 Signer msp.SigningIdentity 86 BroadcastClient common.BroadcastClient 87 DeliverClient deliverClientIntf 88 BroadcastFactory BroadcastClientFactory 89 } 90 91 // InitCmdFactory init the ChannelCmdFactor with default clients 92 func InitCmdFactory(isOrdererRequired bool) (*ChannelCmdFactory, error) { 93 var err error 94 95 cmdFact := &ChannelCmdFactory{} 96 97 cmdFact.Signer, err = common.GetDefaultSigner() 98 if err != nil { 99 return nil, fmt.Errorf("Error getting default signer: %s", err) 100 } 101 102 cmdFact.BroadcastFactory = func() (common.BroadcastClient, error) { 103 return common.GetBroadcastClient(orderingEndpoint, tls, caFile) 104 } 105 106 if err != nil { 107 return nil, fmt.Errorf("Error getting broadcast client: %s", err) 108 } 109 110 //for join, we need the endorser as well 111 if isOrdererRequired { 112 cmdFact.EndorserClient, err = common.GetEndorserClient() 113 if err != nil { 114 return nil, fmt.Errorf("Error getting endorser client %s: %s", channelFuncName, err) 115 } 116 } else { 117 118 if len(strings.Split(orderingEndpoint, ":")) != 2 { 119 return nil, fmt.Errorf("Ordering service endpoint %s is not valid or missing", orderingEndpoint) 120 } 121 122 var opts []grpc.DialOption 123 // check for TLS 124 if tls { 125 if caFile != "" { 126 creds, err := credentials.NewClientTLSFromFile(caFile, "") 127 if err != nil { 128 return nil, fmt.Errorf("Error connecting to %s due to %s", orderingEndpoint, err) 129 } 130 opts = append(opts, grpc.WithTransportCredentials(creds)) 131 } 132 } else { 133 opts = append(opts, grpc.WithInsecure()) 134 } 135 conn, err := grpc.Dial(orderingEndpoint, opts...) 136 if err != nil { 137 return nil, err 138 } 139 140 client, err := ab.NewAtomicBroadcastClient(conn).Deliver(context.TODO()) 141 if err != nil { 142 fmt.Println("Error connecting:", err) 143 return nil, err 144 } 145 146 cmdFact.DeliverClient = newDeliverClient(client, chainID) 147 } 148 149 return cmdFact, nil 150 }