github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/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 instantiate_cmdname = "instantiate"
    32  
    33  const instantiate_desc = "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:       instantiate_cmdname,
    39  		Short:     fmt.Sprint(instantiate_desc),
    40  		Long:      fmt.Sprint(instantiate_desc),
    41  		ValidArgs: []string{"1"},
    42  		RunE: func(cmd *cobra.Command, args []string) error {
    43  			return chaincodeDeploy(cmd, args, cf)
    44  		},
    45  	}
    46  
    47  	return chaincodeInstantiateCmd
    48  }
    49  
    50  //instantiate the command via Endorser
    51  func instantiate(cmd *cobra.Command, cf *ChaincodeCmdFactory) (*protcommon.Envelope, error) {
    52  	spec, err := getChaincodeSpec(cmd)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  
    57  	cds, err := getChaincodeDeploymentSpec(spec, false)
    58  	if err != nil {
    59  		return nil, fmt.Errorf("Error getting chaincode code %s: %s", chainFuncName, err)
    60  	}
    61  
    62  	creator, err := cf.Signer.Serialize()
    63  	if err != nil {
    64  		return nil, fmt.Errorf("Error serializing identity for %s: %s", cf.Signer.GetIdentifier(), err)
    65  	}
    66  
    67  	prop, _, err := utils.CreateDeployProposalFromCDS(chainID, cds, creator, policyMarhsalled, []byte(escc), []byte(vscc))
    68  	if err != nil {
    69  		return nil, fmt.Errorf("Error creating proposal  %s: %s", chainFuncName, err)
    70  	}
    71  
    72  	var signedProp *pb.SignedProposal
    73  	signedProp, err = utils.GetSignedProposal(prop, cf.Signer)
    74  	if err != nil {
    75  		return nil, fmt.Errorf("Error creating signed proposal  %s: %s", chainFuncName, err)
    76  	}
    77  
    78  	proposalResponse, err := cf.EndorserClient.ProcessProposal(context.Background(), signedProp)
    79  	if err != nil {
    80  		return nil, fmt.Errorf("Error endorsing %s: %s", chainFuncName, err)
    81  	}
    82  
    83  	if proposalResponse != nil {
    84  		// assemble a signed transaction (it's an Envelope message)
    85  		env, err := utils.CreateSignedTx(prop, cf.Signer, proposalResponse)
    86  		if err != nil {
    87  			return nil, fmt.Errorf("Could not assemble transaction, err %s", err)
    88  		}
    89  
    90  		return env, nil
    91  	}
    92  
    93  	return nil, nil
    94  }
    95  
    96  // chaincodeDeploy instantiates the chaincode. On success, the chaincode name
    97  // (hash) is printed to STDOUT for use by subsequent chaincode-related CLI
    98  // commands.
    99  func chaincodeDeploy(cmd *cobra.Command, args []string, cf *ChaincodeCmdFactory) error {
   100  	var err error
   101  	if cf == nil {
   102  		cf, err = InitCmdFactory(true, true)
   103  		if err != nil {
   104  			return err
   105  		}
   106  	}
   107  	defer cf.BroadcastClient.Close()
   108  	env, err := instantiate(cmd, cf)
   109  	if err != nil {
   110  		return err
   111  	}
   112  
   113  	if env != nil {
   114  		err = cf.BroadcastClient.Send(env)
   115  	}
   116  
   117  	return err
   118  }