github.com/osdi23p228/fabric@v0.0.0-20221218062954-77808885f5db/internal/peer/lifecycle/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  	"time"
    11  
    12  	"github.com/osdi23p228/fabric/bccsp"
    13  	"github.com/osdi23p228/fabric/common/flogging"
    14  	"github.com/osdi23p228/fabric/internal/peer/common"
    15  	"github.com/spf13/cobra"
    16  	"github.com/spf13/pflag"
    17  )
    18  
    19  const (
    20  	lifecycleName                = "_lifecycle"
    21  	approveFuncName              = "ApproveChaincodeDefinitionForMyOrg"
    22  	commitFuncName               = "CommitChaincodeDefinition"
    23  	checkCommitReadinessFuncName = "CheckCommitReadiness"
    24  )
    25  
    26  var logger = flogging.MustGetLogger("cli.lifecycle.chaincode")
    27  
    28  func addFlags(cmd *cobra.Command) {
    29  	common.AddOrdererFlags(cmd)
    30  }
    31  
    32  // Cmd returns the cobra command for Chaincode
    33  func Cmd(cryptoProvider bccsp.BCCSP) *cobra.Command {
    34  	addFlags(chaincodeCmd)
    35  
    36  	chaincodeCmd.AddCommand(PackageCmd(nil))
    37  	chaincodeCmd.AddCommand(InstallCmd(nil, cryptoProvider))
    38  	chaincodeCmd.AddCommand(QueryInstalledCmd(nil, cryptoProvider))
    39  	chaincodeCmd.AddCommand(GetInstalledPackageCmd(nil, cryptoProvider))
    40  	chaincodeCmd.AddCommand(ApproveForMyOrgCmd(nil, cryptoProvider))
    41  	chaincodeCmd.AddCommand(QueryApprovedCmd(nil, cryptoProvider))
    42  	chaincodeCmd.AddCommand(CheckCommitReadinessCmd(nil, cryptoProvider))
    43  	chaincodeCmd.AddCommand(CommitCmd(nil, cryptoProvider))
    44  	chaincodeCmd.AddCommand(QueryCommittedCmd(nil, cryptoProvider))
    45  
    46  	return chaincodeCmd
    47  }
    48  
    49  // Chaincode-related variables.
    50  var (
    51  	chaincodeLang         string
    52  	chaincodePath         string
    53  	chaincodeName         string
    54  	channelID             string
    55  	chaincodeVersion      string
    56  	packageLabel          string
    57  	signaturePolicy       string
    58  	channelConfigPolicy   string
    59  	endorsementPlugin     string
    60  	validationPlugin      string
    61  	collectionsConfigFile string
    62  	peerAddresses         []string
    63  	tlsRootCertFiles      []string
    64  	connectionProfilePath string
    65  	waitForEvent          bool
    66  	waitForEventTimeout   time.Duration
    67  	packageID             string
    68  	sequence              int
    69  	initRequired          bool
    70  	output                string
    71  	outputDirectory       string
    72  )
    73  
    74  var chaincodeCmd = &cobra.Command{
    75  	Use:   "chaincode",
    76  	Short: "Perform chaincode operations: package|install|queryinstalled|getinstalledpackage|approveformyorg|queryapproved|checkcommitreadiness|commit|querycommitted",
    77  	Long:  "Perform chaincode operations: package|install|queryinstalled|getinstalledpackage|approveformyorg|queryapproved|checkcommitreadiness|commit|querycommitted",
    78  	PersistentPreRun: func(cmd *cobra.Command, args []string) {
    79  		common.InitCmd(cmd, args)
    80  		common.SetOrdererEnv(cmd, args)
    81  	},
    82  }
    83  
    84  var flags *pflag.FlagSet
    85  
    86  func init() {
    87  	ResetFlags()
    88  }
    89  
    90  // ResetFlags resets the values of these flags to facilitate tests
    91  func ResetFlags() {
    92  	flags = &pflag.FlagSet{}
    93  
    94  	flags.StringVarP(&chaincodeLang, "lang", "l", "golang", "Language the chaincode is written in")
    95  	flags.StringVarP(&chaincodePath, "path", "p", "", "Path to the chaincode")
    96  	flags.StringVarP(&chaincodeName, "name", "n", "", "Name of the chaincode")
    97  	flags.StringVarP(&chaincodeVersion, "version", "v", "", "Version of the chaincode")
    98  	flags.StringVarP(&packageLabel, "label", "", "", "The package label contains a human-readable description of the package")
    99  	flags.StringVarP(&channelID, "channelID", "C", "", "The channel on which this command should be executed")
   100  	flags.StringVarP(&signaturePolicy, "signature-policy", "", "", "The endorsement policy associated to this chaincode specified as a signature policy")
   101  	flags.StringVarP(&channelConfigPolicy, "channel-config-policy", "", "", "The endorsement policy associated to this chaincode specified as a channel config policy reference")
   102  	flags.StringVarP(&endorsementPlugin, "endorsement-plugin", "E", "", "The name of the endorsement plugin to be used for this chaincode")
   103  	flags.StringVarP(&validationPlugin, "validation-plugin", "V", "", "The name of the validation plugin to be used for this chaincode")
   104  	flags.StringVar(&collectionsConfigFile, "collections-config", "", "The fully qualified path to the collection JSON file including the file name")
   105  	flags.StringArrayVarP(&peerAddresses, "peerAddresses", "", []string{""}, "The addresses of the peers to connect to")
   106  	flags.StringArrayVarP(&tlsRootCertFiles, "tlsRootCertFiles", "", []string{""},
   107  		"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")
   108  	flags.StringVarP(&connectionProfilePath, "connectionProfile", "", "",
   109  		"The fully qualified path to the connection profile that provides the necessary connection information for the network. Note: currently only supported for providing peer connection information")
   110  	flags.BoolVar(&waitForEvent, "waitForEvent", true,
   111  		"Whether to wait for the event from each peer's deliver filtered service signifying that the transaction has been committed successfully")
   112  	flags.DurationVar(&waitForEventTimeout, "waitForEventTimeout", 30*time.Second,
   113  		"Time to wait for the event from each peer's deliver filtered service signifying that the 'invoke' transaction has been committed successfully")
   114  	flags.StringVarP(&packageID, "package-id", "", "", "The identifier of the chaincode install package")
   115  	flags.IntVarP(&sequence, "sequence", "", 0, "The sequence number of the chaincode definition for the channel")
   116  	flags.BoolVarP(&initRequired, "init-required", "", false, "Whether the chaincode requires invoking 'init'")
   117  	flags.StringVarP(&output, "output", "O", "", "The output format for query results. Default is human-readable plain-text. json is currently the only supported format.")
   118  	flags.StringVarP(&outputDirectory, "output-directory", "", "", "The output directory to use when writing a chaincode install package to disk. Default is the current working directory.")
   119  }
   120  
   121  func attachFlags(cmd *cobra.Command, names []string) {
   122  	cmdFlags := cmd.Flags()
   123  	for _, name := range names {
   124  		if flag := flags.Lookup(name); flag != nil {
   125  			cmdFlags.AddFlag(flag)
   126  		} else {
   127  			logger.Fatalf("Could not find flag '%s' to attach to command '%s'", name, cmd.Name())
   128  		}
   129  	}
   130  }