github.com/osdi23p228/fabric@v0.0.0-20221218062954-77808885f5db/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/osdi23p228/fabric/bccsp"
    14  	"github.com/osdi23p228/fabric/common/flogging"
    15  	"github.com/osdi23p228/fabric/internal/peer/common"
    16  	"github.com/osdi23p228/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  		"Name of the chaincode")
   110  	flags.StringVarP(&chaincodeVersion, "version", "v", common.UndefinedParamValue,
   111  		"Version of the chaincode specified in install/instantiate/upgrade commands")
   112  	flags.StringVarP(&chaincodeUsr, "username", "u", common.UndefinedParamValue,
   113  		"Username for chaincode operations when security is enabled")
   114  	flags.StringVarP(&channelID, "channelID", "C", "",
   115  		"The channel on which this command should be executed")
   116  	flags.StringVarP(&policy, "policy", "P", common.UndefinedParamValue,
   117  		"The endorsement policy associated to this chaincode")
   118  	flags.StringVarP(&escc, "escc", "E", common.UndefinedParamValue,
   119  		"The name of the endorsement system chaincode to be used for this chaincode")
   120  	flags.StringVarP(&vscc, "vscc", "V", common.UndefinedParamValue,
   121  		"The name of the verification system chaincode to be used for this chaincode")
   122  	flags.BoolVarP(&isInit, "isInit", "I", false,
   123  		"Is this invocation for init (useful for supporting legacy chaincodes in the new lifecycle)")
   124  	flags.BoolVarP(&getInstalledChaincodes, "installed", "", false,
   125  		"Get the installed chaincodes on a peer")
   126  	flags.BoolVarP(&getInstantiatedChaincodes, "instantiated", "", false,
   127  		"Get the instantiated chaincodes on a channel")
   128  	flags.StringVar(&collectionsConfigFile, "collections-config", common.UndefinedParamValue,
   129  		"The fully qualified path to the collection JSON file including the file name")
   130  	flags.StringArrayVarP(&peerAddresses, "peerAddresses", "", []string{common.UndefinedParamValue},
   131  		"The addresses of the peers to connect to")
   132  	flags.StringArrayVarP(&tlsRootCertFiles, "tlsRootCertFiles", "", []string{common.UndefinedParamValue},
   133  		"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")
   134  	flags.StringVarP(&connectionProfile, "connectionProfile", "", common.UndefinedParamValue,
   135  		"Connection profile that provides the necessary connection information for the network. Note: currently only supported for providing peer connection information")
   136  	flags.BoolVar(&waitForEvent, "waitForEvent", false,
   137  		"Whether to wait for the event from each peer's deliver filtered service signifying that the 'invoke' transaction has been committed successfully")
   138  	flags.DurationVar(&waitForEventTimeout, "waitForEventTimeout", 30*time.Second,
   139  		"Time to wait for the event from each peer's deliver filtered service signifying that the 'invoke' transaction has been committed successfully")
   140  	flags.BoolVarP(&createSignedCCDepSpec, "cc-package", "s", false,
   141  		"create CC deployment spec for owner endorsements instead of raw CC deployment spec")
   142  	flags.BoolVarP(&signCCDepSpec, "sign", "S", false,
   143  		"if creating CC deployment spec package for owner endorsements, also sign it with local MSP")
   144  	flags.StringVarP(&instantiationPolicy, "instantiate-policy", "i", "",
   145  		"instantiation policy for the chaincode")
   146  }
   147  
   148  func attachFlags(cmd *cobra.Command, names []string) {
   149  	cmdFlags := cmd.Flags()
   150  	for _, name := range names {
   151  		if flag := flags.Lookup(name); flag != nil {
   152  			cmdFlags.AddFlag(flag)
   153  		} else {
   154  			logger.Fatalf("Could not find flag '%s' to attach to command '%s'", name, cmd.Name())
   155  		}
   156  	}
   157  }