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