github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/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 upgradeCmdName = "upgrade"
    32  
    33  // upgradeCmd returns the cobra command for Chaincode Upgrade
    34  func upgradeCmd(cf *ChaincodeCmdFactory) *cobra.Command {
    35  	chaincodeUpgradeCmd = &cobra.Command{
    36  		Use:       upgradeCmdName,
    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  	flagList := []string{
    45  		"lang",
    46  		"ctor",
    47  		"path",
    48  		"name",
    49  		"channelID",
    50  		"version",
    51  		"policy",
    52  		"escc",
    53  		"vscc",
    54  	}
    55  	attachFlags(chaincodeUpgradeCmd, flagList)
    56  
    57  	return chaincodeUpgradeCmd
    58  }
    59  
    60  //upgrade the command via Endorser
    61  func upgrade(cmd *cobra.Command, cf *ChaincodeCmdFactory) (*protcommon.Envelope, error) {
    62  	spec, err := getChaincodeSpec(cmd)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  
    67  	cds, err := getChaincodeDeploymentSpec(spec, false)
    68  	if err != nil {
    69  		return nil, fmt.Errorf("Error getting chaincode code %s: %s", chainFuncName, err)
    70  	}
    71  
    72  	creator, err := cf.Signer.Serialize()
    73  	if err != nil {
    74  		return nil, fmt.Errorf("Error serializing identity for %s: %s", cf.Signer.GetIdentifier(), err)
    75  	}
    76  
    77  	prop, _, err := utils.CreateUpgradeProposalFromCDS(chainID, cds, creator, policyMarhsalled, []byte(escc), []byte(vscc))
    78  	if err != nil {
    79  		return nil, fmt.Errorf("Error creating proposal %s: %s", chainFuncName, err)
    80  	}
    81  	logger.Debugf("Get upgrade proposal for chaincode <%v>", spec.ChaincodeId)
    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  	logger.Debugf("endorse upgrade proposal, get response <%v>", proposalResponse.Response)
    94  
    95  	if proposalResponse != nil {
    96  		// assemble a signed transaction (it's an Envelope message)
    97  		env, err := utils.CreateSignedTx(prop, cf.Signer, proposalResponse)
    98  		if err != nil {
    99  			return nil, fmt.Errorf("Could not assemble transaction, err %s", err)
   100  		}
   101  		logger.Debug("Get Signed envelope")
   102  		return env, nil
   103  	}
   104  
   105  	return nil, nil
   106  }
   107  
   108  // chaincodeUpgrade upgrades the chaincode. On success, the new chaincode
   109  // version is printed to STDOUT
   110  func chaincodeUpgrade(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  
   120  	env, err := upgrade(cmd, cf)
   121  	if err != nil {
   122  		return err
   123  	}
   124  
   125  	if env != nil {
   126  		logger.Debug("Send signed envelope to orderer")
   127  		err = cf.BroadcastClient.Send(env)
   128  		return err
   129  	}
   130  
   131  	return nil
   132  }