github.com/defanghe/fabric@v2.1.1+incompatible/internal/peer/chaincode/instantiate.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package chaincode 8 9 import ( 10 "context" 11 "errors" 12 "fmt" 13 14 protcommon "github.com/hyperledger/fabric-protos-go/common" 15 pb "github.com/hyperledger/fabric-protos-go/peer" 16 "github.com/hyperledger/fabric/bccsp" 17 "github.com/hyperledger/fabric/protoutil" 18 "github.com/spf13/cobra" 19 ) 20 21 var chaincodeInstantiateCmd *cobra.Command 22 23 const instantiateCmdName = "instantiate" 24 25 const instantiateDesc = "Deploy the specified chaincode to the network." 26 27 // instantiateCmd returns the cobra command for Chaincode Deploy 28 func instantiateCmd(cf *ChaincodeCmdFactory, cryptoProvider bccsp.BCCSP) *cobra.Command { 29 chaincodeInstantiateCmd = &cobra.Command{ 30 Use: instantiateCmdName, 31 Short: fmt.Sprint(instantiateDesc), 32 Long: fmt.Sprint(instantiateDesc), 33 ValidArgs: []string{"1"}, 34 RunE: func(cmd *cobra.Command, args []string) error { 35 return chaincodeDeploy(cmd, args, cf, cryptoProvider) 36 }, 37 } 38 flagList := []string{ 39 "lang", 40 "ctor", 41 "name", 42 "channelID", 43 "version", 44 "policy", 45 "escc", 46 "vscc", 47 "collections-config", 48 "peerAddresses", 49 "tlsRootCertFiles", 50 "connectionProfile", 51 } 52 attachFlags(chaincodeInstantiateCmd, flagList) 53 54 return chaincodeInstantiateCmd 55 } 56 57 //instantiate the command via Endorser 58 func instantiate(cmd *cobra.Command, cf *ChaincodeCmdFactory) (*protcommon.Envelope, error) { 59 spec, err := getChaincodeSpec(cmd) 60 if err != nil { 61 return nil, err 62 } 63 64 cds, err := getChaincodeDeploymentSpec(spec, false) 65 if err != nil { 66 return nil, fmt.Errorf("error getting chaincode code %s: %s", chaincodeName, err) 67 } 68 69 creator, err := cf.Signer.Serialize() 70 if err != nil { 71 return nil, fmt.Errorf("error serializing identity: %s", err) 72 } 73 74 prop, _, err := protoutil.CreateDeployProposalFromCDS(channelID, cds, creator, policyMarshalled, []byte(escc), []byte(vscc), collectionConfigBytes) 75 if err != nil { 76 return nil, fmt.Errorf("error creating proposal %s: %s", chainFuncName, err) 77 } 78 79 var signedProp *pb.SignedProposal 80 signedProp, err = protoutil.GetSignedProposal(prop, cf.Signer) 81 if err != nil { 82 return nil, fmt.Errorf("error creating signed proposal %s: %s", chainFuncName, err) 83 } 84 85 // instantiate is currently only supported for one peer 86 proposalResponse, err := cf.EndorserClients[0].ProcessProposal(context.Background(), signedProp) 87 if err != nil { 88 return nil, fmt.Errorf("error endorsing %s: %s", chainFuncName, err) 89 } 90 91 if proposalResponse != nil { 92 // assemble a signed transaction (it's an Envelope message) 93 env, err := protoutil.CreateSignedTx(prop, cf.Signer, proposalResponse) 94 if err != nil { 95 return nil, fmt.Errorf("could not assemble transaction, err %s", err) 96 } 97 98 return env, nil 99 } 100 101 return nil, nil 102 } 103 104 // chaincodeDeploy instantiates the chaincode. On success, the chaincode name 105 // (hash) is printed to STDOUT for use by subsequent chaincode-related CLI 106 // commands. 107 func chaincodeDeploy(cmd *cobra.Command, args []string, cf *ChaincodeCmdFactory, cryptoProvider bccsp.BCCSP) error { 108 if channelID == "" { 109 return errors.New("The required parameter 'channelID' is empty. Rerun the command with -C flag") 110 } 111 // Parsing of the command line is done so silence cmd usage 112 cmd.SilenceUsage = true 113 114 var err error 115 if cf == nil { 116 cf, err = InitCmdFactory(cmd.Name(), true, true, cryptoProvider) 117 if err != nil { 118 return err 119 } 120 } 121 defer cf.BroadcastClient.Close() 122 env, err := instantiate(cmd, cf) 123 if err != nil { 124 return err 125 } 126 127 if env != nil { 128 err = cf.BroadcastClient.Send(env) 129 } 130 131 return err 132 }