github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/peer/channel/join.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 "errors" 21 "fmt" 22 "io/ioutil" 23 24 "github.com/hyperledger/fabric/core/scc/cscc" 25 "github.com/hyperledger/fabric/peer/common" 26 pcommon "github.com/hyperledger/fabric/protos/common" 27 pb "github.com/hyperledger/fabric/protos/peer" 28 putils "github.com/hyperledger/fabric/protos/utils" 29 "github.com/spf13/cobra" 30 "golang.org/x/net/context" 31 ) 32 33 const commandDescription = "Joins the peer to a chain." 34 35 func joinCmd(cf *ChannelCmdFactory) *cobra.Command { 36 // Set the flags on the channel start command. 37 return &cobra.Command{ 38 Use: "join", 39 Short: commandDescription, 40 Long: commandDescription, 41 RunE: func(cmd *cobra.Command, args []string) error { 42 return join(cmd, args, cf) 43 }, 44 } 45 } 46 47 //GBFileNotFoundErr genesis block file not found 48 type GBFileNotFoundErr string 49 50 func (e GBFileNotFoundErr) Error() string { 51 return fmt.Sprintf("genesis block file not found %s", string(e)) 52 } 53 54 //ProposalFailedErr proposal failed 55 type ProposalFailedErr string 56 57 func (e ProposalFailedErr) Error() string { 58 return fmt.Sprintf("proposal failed (err: %s)", string(e)) 59 } 60 61 func getJoinCCSpec() (*pb.ChaincodeSpec, error) { 62 if genesisBlockPath == common.UndefinedParamValue { 63 return nil, errors.New("Must supply genesis block file.") 64 } 65 66 gb, err := ioutil.ReadFile(genesisBlockPath) 67 if err != nil { 68 return nil, GBFileNotFoundErr(err.Error()) 69 } 70 71 // Build the spec 72 input := &pb.ChaincodeInput{Args: [][]byte{[]byte(cscc.JoinChain), gb}} 73 74 spec := &pb.ChaincodeSpec{ 75 Type: pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value["GOLANG"]), 76 ChaincodeId: &pb.ChaincodeID{Name: "cscc"}, 77 Input: input, 78 } 79 80 return spec, nil 81 } 82 83 func executeJoin(cf *ChannelCmdFactory) (err error) { 84 spec, err := getJoinCCSpec() 85 if err != nil { 86 return err 87 } 88 89 // Build the ChaincodeInvocationSpec message 90 invocation := &pb.ChaincodeInvocationSpec{ChaincodeSpec: spec} 91 92 creator, err := cf.Signer.Serialize() 93 if err != nil { 94 return fmt.Errorf("Error serializing identity for %s: %s", cf.Signer.GetIdentifier(), err) 95 } 96 97 var prop *pb.Proposal 98 prop, _, err = putils.CreateProposalFromCIS(pcommon.HeaderType_CONFIG, "", invocation, creator) 99 if err != nil { 100 return fmt.Errorf("Error creating proposal for join %s", err) 101 } 102 103 var signedProp *pb.SignedProposal 104 signedProp, err = putils.GetSignedProposal(prop, cf.Signer) 105 if err != nil { 106 return fmt.Errorf("Error creating signed proposal %s", err) 107 } 108 109 var proposalResp *pb.ProposalResponse 110 proposalResp, err = cf.EndorserClient.ProcessProposal(context.Background(), signedProp) 111 if err != nil { 112 return ProposalFailedErr(err.Error()) 113 } 114 115 if proposalResp == nil { 116 return ProposalFailedErr("nil proposal response") 117 } 118 119 if proposalResp.Response.Status != 0 && proposalResp.Response.Status != 200 { 120 return ProposalFailedErr(fmt.Sprintf("bad proposal response %d", proposalResp.Response.Status)) 121 } 122 123 fmt.Println("Peer joined the channel!") 124 125 return nil 126 } 127 128 func join(cmd *cobra.Command, args []string, cf *ChannelCmdFactory) error { 129 var err error 130 if cf == nil { 131 cf, err = InitCmdFactory(true) 132 if err != nil { 133 return err 134 } 135 } 136 return executeJoin(cf) 137 }