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