github.com/anjalikarhana/fabric@v2.1.1+incompatible/internal/peer/chaincode/chaincode.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  	"fmt"
    11  	"time"
    12  
    13  	"github.com/hyperledger/fabric/bccsp"
    14  	"github.com/hyperledger/fabric/common/flogging"
    15  	"github.com/hyperledger/fabric/internal/peer/common"
    16  	"github.com/hyperledger/fabric/internal/peer/packaging"
    17  	"github.com/spf13/cobra"
    18  	"github.com/spf13/pflag"
    19  )
    20  
    21  const (
    22  	chainFuncName = "chaincode"
    23  	chainCmdDes   = "Operate a chaincode: install|instantiate|invoke|package|query|signpackage|upgrade|list."
    24  )
    25  
    26  var logger = flogging.MustGetLogger("chaincodeCmd")
    27  
    28  // XXX This is a terrible singleton hack, however
    29  // it simply making a latent dependency explicit.
    30  // It should be removed along with the other package
    31  // scoped variables
    32  var platformRegistry = packaging.NewRegistry(packaging.SupportedPlatforms...)
    33  
    34  func addFlags(cmd *cobra.Command) {
    35  	common.AddOrdererFlags(cmd)
    36  	flags := cmd.PersistentFlags()
    37  	flags.StringVarP(&transient, "transient", "", "", "Transient map of arguments in JSON encoding")
    38  }
    39  
    40  // Cmd returns the cobra command for Chaincode
    41  func Cmd(cf *ChaincodeCmdFactory, cryptoProvider bccsp.BCCSP) *cobra.Command {
    42  	addFlags(chaincodeCmd)
    43  
    44  	chaincodeCmd.AddCommand(installCmd(cf, nil, cryptoProvider))
    45  	chaincodeCmd.AddCommand(instantiateCmd(cf, cryptoProvider))
    46  	chaincodeCmd.AddCommand(invokeCmd(cf, cryptoProvider))
    47  	chaincodeCmd.AddCommand(packageCmd(cf, nil, nil, cryptoProvider))
    48  	chaincodeCmd.AddCommand(queryCmd(cf, cryptoProvider))
    49  	chaincodeCmd.AddCommand(signpackageCmd(cf, cryptoProvider))
    50  	chaincodeCmd.AddCommand(upgradeCmd(cf, cryptoProvider))
    51  	chaincodeCmd.AddCommand(listCmd(cf, cryptoProvider))
    52  
    53  	return chaincodeCmd
    54  }
    55  
    56  // Chaincode-related variables.
    57  var (
    58  	chaincodeLang         string
    59  	chaincodeCtorJSON     string
    60  	chaincodePath         string
    61  	chaincodeName         string
    62  	chaincodeUsr          string // Not used
    63  	chaincodeQueryRaw     bool
    64  	chaincodeQueryHex     bool
    65  	channelID             string
    66  	chaincodeVersion      string
    67  	policy                string
    68  	escc                  string
    69  	vscc                  string
    70  	policyMarshalled      []byte
    71  	transient             string
    72  	isInit                bool
    73  	collectionsConfigFile string
    74  	collectionConfigBytes []byte
    75  	peerAddresses         []string
    76  	tlsRootCertFiles      []string
    77  	connectionProfile     string
    78  	waitForEvent          bool
    79  	waitForEventTimeout   time.Duration
    80  )
    81  
    82  var chaincodeCmd = &cobra.Command{
    83  	Use:   chainFuncName,
    84  	Short: fmt.Sprint(chainCmdDes),
    85  	Long:  fmt.Sprint(chainCmdDes),
    86  	PersistentPreRun: func(cmd *cobra.Command, args []string) {
    87  		common.InitCmd(cmd, args)
    88  		common.SetOrdererEnv(cmd, args)
    89  	},
    90  }
    91  
    92  var flags *pflag.FlagSet
    93  
    94  func init() {
    95  	resetFlags()
    96  }
    97  
    98  // Explicitly define a method to facilitate tests
    99  func resetFlags() {
   100  	flags = &pflag.FlagSet{}
   101  
   102  	flags.StringVarP(&chaincodeLang, "lang", "l", "golang",
   103  		fmt.Sprintf("Language the %s is written in", chainFuncName))
   104  	flags.StringVarP(&chaincodeCtorJSON, "ctor", "c", "{}",
   105  		fmt.Sprintf("Constructor message for the %s in JSON format", chainFuncName))
   106  	flags.StringVarP(&chaincodePath, "path", "p", common.UndefinedParamValue,
   107  		fmt.Sprintf("Path to %s", chainFuncName))
   108  	flags.StringVarP(&chaincodeName, "name", "n", common.UndefinedParamValue,
   109  		fmt.Sprint("Name of the chaincode"))
   110  	flags.StringVarP(&chaincodeVersion, "version", "v", common.UndefinedParamValue,
   111  		fmt.Sprint("Version of the chaincode specified in install/instantiate/upgrade commands"))
   112  	flags.StringVarP(&chaincodeUsr, "username", "u", common.UndefinedParamValue,
   113  		fmt.Sprint("Username for chaincode operations when security is enabled"))
   114  	flags.StringVarP(&channelID, "channelID", "C", "",
   115  		fmt.Sprint("The channel on which this command should be executed"))
   116  	flags.StringVarP(&policy, "policy", "P", common.UndefinedParamValue,
   117  		fmt.Sprint("The endorsement policy associated to this chaincode"))
   118  	flags.StringVarP(&escc, "escc", "E", common.UndefinedParamValue,
   119  		fmt.Sprint("The name of the endorsement system chaincode to be used for this chaincode"))
   120  	flags.StringVarP(&vscc, "vscc", "V", common.UndefinedParamValue,
   121  		fmt.Sprint("The name of the verification system chaincode to be used for this chaincode"))
   122  	flags.BoolVarP(&isInit, "isInit", "I", false, "Is this invocation for init (useful for supporting legacy chaincodes in the new lifecycle)")
   123  	flags.BoolVarP(&getInstalledChaincodes, "installed", "", false,
   124  		"Get the installed chaincodes on a peer")
   125  	flags.BoolVarP(&getInstantiatedChaincodes, "instantiated", "", false,
   126  		"Get the instantiated chaincodes on a channel")
   127  	flags.StringVar(&collectionsConfigFile, "collections-config", common.UndefinedParamValue,
   128  		fmt.Sprint("The fully qualified path to the collection JSON file including the file name"))
   129  	flags.StringArrayVarP(&peerAddresses, "peerAddresses", "", []string{common.UndefinedParamValue},
   130  		fmt.Sprint("The addresses of the peers to connect to"))
   131  	flags.StringArrayVarP(&tlsRootCertFiles, "tlsRootCertFiles", "", []string{common.UndefinedParamValue},
   132  		fmt.Sprint("If TLS is enabled, the paths to the TLS root cert files of the peers to connect to. The order and number of certs specified should match the --peerAddresses flag"))
   133  	flags.StringVarP(&connectionProfile, "connectionProfile", "", common.UndefinedParamValue,
   134  		fmt.Sprint("Connection profile that provides the necessary connection information for the network. Note: currently only supported for providing peer connection information"))
   135  	flags.BoolVar(&waitForEvent, "waitForEvent", false,
   136  		fmt.Sprint("Whether to wait for the event from each peer's deliver filtered service signifying that the 'invoke' transaction has been committed successfully"))
   137  	flags.DurationVar(&waitForEventTimeout, "waitForEventTimeout", 30*time.Second,
   138  		fmt.Sprint("Time to wait for the event from each peer's deliver filtered service signifying that the 'invoke' transaction has been committed successfully"))
   139  	flags.BoolVarP(&createSignedCCDepSpec, "cc-package", "s", false, "create CC deployment spec for owner endorsements instead of raw CC deployment spec")
   140  	flags.BoolVarP(&signCCDepSpec, "sign", "S", false, "if creating CC deployment spec package for owner endorsements, also sign it with local MSP")
   141  	flags.StringVarP(&instantiationPolicy, "instantiate-policy", "i", "", "instantiation policy for the chaincode")
   142  }
   143  
   144  func attachFlags(cmd *cobra.Command, names []string) {
   145  	cmdFlags := cmd.Flags()
   146  	for _, name := range names {
   147  		if flag := flags.Lookup(name); flag != nil {
   148  			cmdFlags.AddFlag(flag)
   149  		} else {
   150  			logger.Fatalf("Could not find flag '%s' to attach to command '%s'", name, cmd.Name())
   151  		}
   152  	}
   153  }