github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/peer/chaincode/upgrade.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 chaincodeUpgradeCmd *cobra.Command
    30  
    31  const upgrade_cmdname = "upgrade"
    32  
    33  // upgradeCmd returns the cobra command for Chaincode Upgrade
    34  func upgradeCmd(cf *ChaincodeCmdFactory) *cobra.Command {
    35  	chaincodeUpgradeCmd = &cobra.Command{
    36  		Use:       upgrade_cmdname,
    37  		Short:     "Upgrade chaincode.",
    38  		Long:      "Upgrade an existing chaincode with the specified one. The new chaincode will immediately replace the existing chaincode upon the transaction committed.",
    39  		ValidArgs: []string{"1"},
    40  		RunE: func(cmd *cobra.Command, args []string) error {
    41  			return chaincodeUpgrade(cmd, args, cf)
    42  		},
    43  	}
    44  
    45  	return chaincodeUpgradeCmd
    46  }
    47  
    48  //upgrade the command via Endorser
    49  func upgrade(cmd *cobra.Command, cf *ChaincodeCmdFactory) (*protcommon.Envelope, error) {
    50  	spec, err := getChaincodeSpec(cmd)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	cds, err := getChaincodeDeploymentSpec(spec, false)
    56  	if err != nil {
    57  		return nil, fmt.Errorf("Error getting chaincode code %s: %s", chainFuncName, err)
    58  	}
    59  
    60  	creator, err := cf.Signer.Serialize()
    61  	if err != nil {
    62  		return nil, fmt.Errorf("Error serializing identity for %s: %s", cf.Signer.GetIdentifier(), err)
    63  	}
    64  
    65  	prop, _, err := utils.CreateUpgradeProposalFromCDS(chainID, cds, creator, policyMarhsalled, []byte(escc), []byte(vscc))
    66  	if err != nil {
    67  		return nil, fmt.Errorf("Error creating proposal %s: %s", chainFuncName, err)
    68  	}
    69  	logger.Debugf("Get upgrade proposal for chaincode <%v>", spec.ChaincodeId)
    70  
    71  	var signedProp *pb.SignedProposal
    72  	signedProp, err = utils.GetSignedProposal(prop, cf.Signer)
    73  	if err != nil {
    74  		return nil, fmt.Errorf("Error creating signed proposal  %s: %s", chainFuncName, err)
    75  	}
    76  
    77  	proposalResponse, err := cf.EndorserClient.ProcessProposal(context.Background(), signedProp)
    78  	if err != nil {
    79  		return nil, fmt.Errorf("Error endorsing %s: %s", chainFuncName, err)
    80  	}
    81  	logger.Debugf("endorse upgrade proposal, get response <%v>", proposalResponse.Response)
    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  		logger.Debug("Get Signed envelope")
    90  		return env, nil
    91  	}
    92  
    93  	return nil, nil
    94  }
    95  
    96  // chaincodeUpgrade upgrades the chaincode. On success, the new chaincode
    97  // version is printed to STDOUT
    98  func chaincodeUpgrade(cmd *cobra.Command, args []string, cf *ChaincodeCmdFactory) error {
    99  	var err error
   100  	if cf == nil {
   101  		cf, err = InitCmdFactory(true, true)
   102  		if err != nil {
   103  			return err
   104  		}
   105  	}
   106  	defer cf.BroadcastClient.Close()
   107  
   108  	env, err := upgrade(cmd, cf)
   109  	if err != nil {
   110  		return err
   111  	}
   112  
   113  	if env != nil {
   114  		logger.Debug("Send signed envelope to orderer")
   115  		err = cf.BroadcastClient.Send(env)
   116  		return err
   117  	}
   118  
   119  	return nil
   120  }