github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/internal/peer/lifecycle/chaincode/chaincode.go (about)

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