github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/peer/chaincode/instantiate.go (about) 1 /* 2 Copyright IBM Corp. 2016 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 chaincode 18 19 import ( 20 "fmt" 21 22 protcommon "github.com/hyperledger/fabric/protos/common" 23 pb "github.com/hyperledger/fabric/protos/peer" 24 "github.com/hyperledger/fabric/protos/utils" 25 "github.com/spf13/cobra" 26 "golang.org/x/net/context" 27 ) 28 29 var chaincodeInstantiateCmd *cobra.Command 30 31 const instantiateCmdName = "instantiate" 32 33 const instantiateDesc = "Deploy the specified chaincode to the network." 34 35 // instantiateCmd returns the cobra command for Chaincode Deploy 36 func instantiateCmd(cf *ChaincodeCmdFactory) *cobra.Command { 37 chaincodeInstantiateCmd = &cobra.Command{ 38 Use: instantiateCmdName, 39 Short: fmt.Sprint(instantiateDesc), 40 Long: fmt.Sprint(instantiateDesc), 41 ValidArgs: []string{"1"}, 42 RunE: func(cmd *cobra.Command, args []string) error { 43 return chaincodeDeploy(cmd, args, cf) 44 }, 45 } 46 flagList := []string{ 47 "lang", 48 "ctor", 49 "name", 50 "channelID", 51 "version", 52 "policy", 53 "escc", 54 "vscc", 55 } 56 attachFlags(chaincodeInstantiateCmd, flagList) 57 58 return chaincodeInstantiateCmd 59 } 60 61 //instantiate the command via Endorser 62 func instantiate(cmd *cobra.Command, cf *ChaincodeCmdFactory) (*protcommon.Envelope, error) { 63 spec, err := getChaincodeSpec(cmd) 64 if err != nil { 65 return nil, err 66 } 67 68 cds, err := getChaincodeDeploymentSpec(spec, false) 69 if err != nil { 70 return nil, fmt.Errorf("Error getting chaincode code %s: %s", chainFuncName, err) 71 } 72 73 creator, err := cf.Signer.Serialize() 74 if err != nil { 75 return nil, fmt.Errorf("Error serializing identity for %s: %s", cf.Signer.GetIdentifier(), err) 76 } 77 78 prop, _, err := utils.CreateDeployProposalFromCDS(chainID, cds, creator, policyMarhsalled, []byte(escc), []byte(vscc)) 79 if err != nil { 80 return nil, fmt.Errorf("Error creating proposal %s: %s", chainFuncName, err) 81 } 82 83 var signedProp *pb.SignedProposal 84 signedProp, err = utils.GetSignedProposal(prop, cf.Signer) 85 if err != nil { 86 return nil, fmt.Errorf("Error creating signed proposal %s: %s", chainFuncName, err) 87 } 88 89 proposalResponse, err := cf.EndorserClient.ProcessProposal(context.Background(), signedProp) 90 if err != nil { 91 return nil, fmt.Errorf("Error endorsing %s: %s", chainFuncName, err) 92 } 93 94 if proposalResponse != nil { 95 // assemble a signed transaction (it's an Envelope message) 96 env, err := utils.CreateSignedTx(prop, cf.Signer, proposalResponse) 97 if err != nil { 98 return nil, fmt.Errorf("Could not assemble transaction, err %s", err) 99 } 100 101 return env, nil 102 } 103 104 return nil, nil 105 } 106 107 // chaincodeDeploy instantiates the chaincode. On success, the chaincode name 108 // (hash) is printed to STDOUT for use by subsequent chaincode-related CLI 109 // commands. 110 func chaincodeDeploy(cmd *cobra.Command, args []string, cf *ChaincodeCmdFactory) error { 111 var err error 112 if cf == nil { 113 cf, err = InitCmdFactory(true, true) 114 if err != nil { 115 return err 116 } 117 } 118 defer cf.BroadcastClient.Close() 119 env, err := instantiate(cmd, cf) 120 if err != nil { 121 return err 122 } 123 124 if env != nil { 125 err = cf.BroadcastClient.Send(env) 126 } 127 128 return err 129 }