@@ -18,11 +18,15 @@ package chaincode
18
18
19
19
import (
20
20
"fmt"
21
+ "io/ioutil"
21
22
23
+ "github.com/golang/protobuf/proto"
22
24
"golang.org/x/net/context"
23
25
26
+ "github.com/hyperledger/fabric/core/common/ccpackage"
24
27
"github.com/hyperledger/fabric/core/common/ccprovider"
25
28
"github.com/hyperledger/fabric/peer/common"
29
+ pcommon "github.com/hyperledger/fabric/protos/common"
26
30
pb "github.com/hyperledger/fabric/protos/peer"
27
31
"github.com/hyperledger/fabric/protos/utils"
28
32
@@ -43,21 +47,25 @@ func installCmd(cf *ChaincodeCmdFactory) *cobra.Command {
43
47
Long : fmt .Sprintf (install_desc ),
44
48
ValidArgs : []string {"1" },
45
49
RunE : func (cmd * cobra.Command , args []string ) error {
46
- return chaincodeInstall (cmd , args , cf )
50
+ var ccpackfile string
51
+ if len (args ) > 0 {
52
+ ccpackfile = args [0 ]
53
+ }
54
+ return chaincodeInstall (cmd , ccpackfile , cf )
47
55
},
48
56
}
49
57
50
58
return chaincodeInstallCmd
51
59
}
52
60
53
61
//install the depspec to "peer.address"
54
- func install (chaincodeName string , chaincodeVersion string , cds * pb. ChaincodeDeploymentSpec , cf * ChaincodeCmdFactory ) error {
62
+ func install (msg proto. Message , cf * ChaincodeCmdFactory ) error {
55
63
creator , err := cf .Signer .Serialize ()
56
64
if err != nil {
57
65
return fmt .Errorf ("Error serializing identity for %s: %s" , cf .Signer .GetIdentifier (), err )
58
66
}
59
67
60
- prop , _ , err := utils .CreateInstallProposalFromCDS (cds , creator )
68
+ prop , _ , err := utils .CreateInstallProposalFromCDS (msg , creator )
61
69
if err != nil {
62
70
return fmt .Errorf ("Error creating proposal %s: %s" , chainFuncName , err )
63
71
}
@@ -80,12 +88,72 @@ func install(chaincodeName string, chaincodeVersion string, cds *pb.ChaincodeDep
80
88
return nil
81
89
}
82
90
83
- // chaincodeInstall installs the chaincode. If remoteinstall, does it via a lccc call
84
- func chaincodeInstall (cmd * cobra.Command , args []string , cf * ChaincodeCmdFactory ) error {
85
- if chaincodePath == common .UndefinedParamValue || chaincodeVersion == common .UndefinedParamValue {
86
- return fmt .Errorf ("Must supply value for %s path and version parameters." , chainFuncName )
91
+ //generateChaincode creates ChaincodeDeploymentSpec as the package to install
92
+ func generateChaincode (cmd * cobra.Command , chaincodeName , chaincodeVersion string ) (* pb.ChaincodeDeploymentSpec , error ) {
93
+ tmppkg , _ := ccprovider .GetChaincodePackage (chaincodeName , chaincodeVersion )
94
+ if tmppkg != nil {
95
+ return nil , fmt .Errorf ("chaincode %s:%s exists" , chaincodeName , chaincodeVersion )
96
+ }
97
+
98
+ spec , err := getChaincodeSpecification (cmd )
99
+ if err != nil {
100
+ return nil , err
87
101
}
88
102
103
+ cds , err := getChaincodeBytes (spec , true )
104
+ if err != nil {
105
+ return nil , fmt .Errorf ("Error getting chaincode code %s: %s" , chainFuncName , err )
106
+ }
107
+
108
+ return cds , nil
109
+ }
110
+
111
+ //getPackageFromFile get the chaincode package from file and the extracted ChaincodeDeploymentSpec
112
+ func getPackageFromFile (ccpackfile string ) (proto.Message , * pb.ChaincodeDeploymentSpec , error ) {
113
+ b , err := ioutil .ReadFile (ccpackfile )
114
+ if err != nil {
115
+ return nil , nil , err
116
+ }
117
+
118
+ //the bytes should be a valid package (CDS or SigedCDS)
119
+ ccpack , err := ccprovider .GetCCPackage (b )
120
+ if err != nil {
121
+ return nil , nil , err
122
+ }
123
+
124
+ //either CDS or Envelope
125
+ o , err := ccpack .GetPackageObject (), nil
126
+ if err != nil {
127
+ return nil , nil , err
128
+ }
129
+
130
+ //try CDS first
131
+ cds , ok := o .(* pb.ChaincodeDeploymentSpec )
132
+ if ! ok || cds == nil {
133
+ //try Envelope next
134
+ env , ok := o .(* pcommon.Envelope )
135
+ if ! ok || env == nil {
136
+ return nil , nil , fmt .Errorf ("error extracting valid chaincode package" )
137
+ }
138
+
139
+ //this will check for a valid package Envelope
140
+ _ , sCDS , err := ccpackage .ExtractSignedCCDepSpec (env )
141
+ if err != nil {
142
+ return nil , nil , fmt .Errorf ("error extracting valid signed chaincode package(%s)" , err )
143
+ }
144
+
145
+ //...and get the CDS at last
146
+ cds , err = utils .GetChaincodeDeploymentSpec (sCDS .ChaincodeDeploymentSpec )
147
+ if err != nil {
148
+ return nil , nil , fmt .Errorf ("error extracting chaincode deployment spec(%s)" , err )
149
+ }
150
+ }
151
+
152
+ return o , cds , nil
153
+ }
154
+
155
+ // chaincodeInstall installs the chaincode. If remoteinstall, does it via a lccc call
156
+ func chaincodeInstall (cmd * cobra.Command , ccpackfile string , cf * ChaincodeCmdFactory ) error {
89
157
var err error
90
158
if cf == nil {
91
159
cf , err = InitCmdFactory (true , false )
@@ -94,22 +162,42 @@ func chaincodeInstall(cmd *cobra.Command, args []string, cf *ChaincodeCmdFactory
94
162
}
95
163
}
96
164
97
- tmppkg , _ := ccprovider .GetChaincodePackage (chaincodeName , chaincodeVersion )
98
- if tmppkg != nil {
99
- return fmt .Errorf ("chaincode %s:%s exists" , chaincodeName , chaincodeVersion )
100
- }
165
+ var ccpackmsg proto.Message
166
+ if ccpackfile == "" {
167
+ if chaincodePath == common .UndefinedParamValue || chaincodeVersion == common .UndefinedParamValue {
168
+ return fmt .Errorf ("Must supply value for %s path and version parameters." , chainFuncName )
169
+ }
170
+ //generate a raw ChaincodeDeploymentSpec
171
+ ccpackmsg , err = generateChaincode (cmd , chaincodeName , chaincodeVersion )
172
+ if err != nil {
173
+ return err
174
+ }
175
+ } else {
176
+ //read in a package generated by the "package" sub-command (and perhaps signed
177
+ //by multiple owners with the "signpackage" sub-command)
178
+ var cds * pb.ChaincodeDeploymentSpec
179
+ ccpackmsg , cds , err = getPackageFromFile (ccpackfile )
101
180
102
- spec , err := getChaincodeSpecification (cmd )
103
- if err != nil {
104
- return err
105
- }
181
+ if err != nil {
182
+ return err
183
+ }
106
184
107
- cds , err := getChaincodeBytes (spec , true )
108
- if err != nil {
109
- return fmt .Errorf ("Error getting chaincode code %s: %s" , chainFuncName , err )
185
+ //get the chaincode details from cds
186
+ cName := cds .ChaincodeSpec .ChaincodeId .Name
187
+ cVersion := cds .ChaincodeSpec .ChaincodeId .Version
188
+
189
+ //if user provided chaincodeName, use it for validation
190
+ if chaincodeName != "" && chaincodeName != cName {
191
+ return fmt .Errorf ("chaincode name %s does not match name %s in package" , chaincodeName , cName )
192
+ }
193
+
194
+ //if user provided chaincodeVersion, use it for validation
195
+ if chaincodeVersion != "" && chaincodeVersion != cVersion {
196
+ return fmt .Errorf ("chaincode version %s does not match version %s in packages" , chaincodeVersion , cVersion )
197
+ }
110
198
}
111
199
112
- err = install (chaincodeName , chaincodeVersion , cds , cf )
200
+ err = install (ccpackmsg , cf )
113
201
114
202
return err
115
203
}
0 commit comments