@@ -66,19 +66,61 @@ type CDSPackage struct {
66
66
buf []byte
67
67
depSpec * pb.ChaincodeDeploymentSpec
68
68
data * CDSData
69
+ datab []byte
70
+ id []byte
71
+ }
72
+
73
+ // resets data
74
+ func (ccpack * CDSPackage ) reset () {
75
+ * ccpack = CDSPackage {}
76
+ }
77
+
78
+ // GetId gets the fingerprint of the chaincode based on package computation
79
+ func (ccpack * CDSPackage ) GetId () []byte {
80
+ //this has to be after creating a package and initializing it
81
+ //If those steps fail, GetId() should never be called
82
+ if ccpack .id == nil {
83
+ panic ("GetId called on uninitialized package" )
84
+ }
85
+ return ccpack .id
69
86
}
70
87
71
88
// GetDepSpec gets the ChaincodeDeploymentSpec from the package
72
89
func (ccpack * CDSPackage ) GetDepSpec () * pb.ChaincodeDeploymentSpec {
90
+ //this has to be after creating a package and initializing it
91
+ //If those steps fail, GetDepSpec() should never be called
92
+ if ccpack .depSpec == nil {
93
+ panic ("GetDepSpec called on uninitialized package" )
94
+ }
73
95
return ccpack .depSpec
74
96
}
75
97
98
+ // GetDepSpecBytes gets the serialized ChaincodeDeploymentSpec from the package
99
+ func (ccpack * CDSPackage ) GetDepSpecBytes () []byte {
100
+ //this has to be after creating a package and initializing it
101
+ //If those steps fail, GetDepSpecBytes() should never be called
102
+ if ccpack .buf == nil {
103
+ panic ("GetDepSpecBytes called on uninitialized package" )
104
+ }
105
+ return ccpack .buf
106
+ }
107
+
76
108
// GetPackageObject gets the ChaincodeDeploymentSpec as proto.Message
77
109
func (ccpack * CDSPackage ) GetPackageObject () proto.Message {
78
110
return ccpack .depSpec
79
111
}
80
112
81
- func (ccpack * CDSPackage ) getCDSData (cds * pb.ChaincodeDeploymentSpec ) ([]byte , * CDSData , error ) {
113
+ // GetChaincodeData gets the ChaincodeData
114
+ func (ccpack * CDSPackage ) GetChaincodeData () * ChaincodeData {
115
+ //this has to be after creating a package and initializing it
116
+ //If those steps fail, GetChaincodeData() should never be called
117
+ if ccpack .depSpec == nil || ccpack .datab == nil || ccpack .id == nil {
118
+ panic ("GetChaincodeData called on uninitialized package" )
119
+ }
120
+ return & ChaincodeData {Name : ccpack .depSpec .ChaincodeSpec .ChaincodeId .Name , Version : ccpack .depSpec .ChaincodeSpec .ChaincodeId .Version , Data : ccpack .datab , Id : ccpack .id }
121
+ }
122
+
123
+ func (ccpack * CDSPackage ) getCDSData (cds * pb.ChaincodeDeploymentSpec ) ([]byte , []byte , * CDSData , error ) {
82
124
// check for nil argument. It is an assertion that getCDSData
83
125
// is never called on a package that did not go through/succeed
84
126
// package initialization.
@@ -88,17 +130,17 @@ func (ccpack *CDSPackage) getCDSData(cds *pb.ChaincodeDeploymentSpec) ([]byte, *
88
130
89
131
b , err := proto .Marshal (cds )
90
132
if err != nil {
91
- return nil , nil , err
133
+ return nil , nil , nil , err
92
134
}
93
135
94
136
if err = factory .InitFactories (nil ); err != nil {
95
- return nil , nil , fmt .Errorf ("Internal error, BCCSP could not be initialized : %s" , err )
137
+ return nil , nil , nil , fmt .Errorf ("Internal error, BCCSP could not be initialized : %s" , err )
96
138
}
97
139
98
140
//compute hashes now
99
141
hash , err := factory .GetDefault ().GetHash (& bccsp.SHAOpts {})
100
142
if err != nil {
101
- return nil , nil , err
143
+ return nil , nil , nil , err
102
144
}
103
145
104
146
cdsdata := & CDSData {}
@@ -116,93 +158,88 @@ func (ccpack *CDSPackage) getCDSData(cds *pb.ChaincodeDeploymentSpec) ([]byte, *
116
158
117
159
b , err = proto .Marshal (cdsdata )
118
160
if err != nil {
119
- return nil , nil , err
161
+ return nil , nil , nil , err
120
162
}
121
163
122
- return b , cdsdata , nil
164
+ hash .Reset ()
165
+
166
+ //compute the id
167
+ hash .Write (cdsdata .CodeHash )
168
+ hash .Write (cdsdata .MetaDataHash )
169
+
170
+ id := hash .Sum (nil )
171
+
172
+ return b , id , cdsdata , nil
123
173
}
124
174
125
175
// ValidateCC returns error if the chaincode is not found or if its not a
126
176
// ChaincodeDeploymentSpec
127
- func (ccpack * CDSPackage ) ValidateCC (ccdata * ChaincodeData ) ( * pb. ChaincodeDeploymentSpec , error ) {
177
+ func (ccpack * CDSPackage ) ValidateCC (ccdata * ChaincodeData ) error {
128
178
if ccpack .depSpec == nil {
129
- return nil , fmt .Errorf ("uninitialized package" )
179
+ return fmt .Errorf ("uninitialized package" )
130
180
}
131
181
132
182
if ccpack .data == nil {
133
- return nil , fmt .Errorf ("nil data" )
183
+ return fmt .Errorf ("nil data" )
134
184
}
135
185
136
186
if ccdata .Name != ccpack .depSpec .ChaincodeSpec .ChaincodeId .Name || ccdata .Version != ccpack .depSpec .ChaincodeSpec .ChaincodeId .Version {
137
- return nil , fmt .Errorf ("invalid chaincode data %v (%v)" , ccdata , ccpack .depSpec .ChaincodeSpec .ChaincodeId )
187
+ return fmt .Errorf ("invalid chaincode data %v (%v)" , ccdata , ccpack .depSpec .ChaincodeSpec .ChaincodeId )
138
188
}
139
189
140
190
otherdata := & CDSData {}
141
191
err := proto .Unmarshal (ccdata .Data , otherdata )
142
192
if err != nil {
143
- return nil , err
193
+ return err
144
194
}
145
195
146
196
if ! ccpack .data .Equals (otherdata ) {
147
- return nil , fmt .Errorf ("data mismatch" )
197
+ return fmt .Errorf ("data mismatch" )
148
198
}
149
199
150
- return ccpack . depSpec , nil
200
+ return nil
151
201
}
152
202
153
203
//InitFromBuffer sets the buffer if valid and returns ChaincodeData
154
204
func (ccpack * CDSPackage ) InitFromBuffer (buf []byte ) (* ChaincodeData , error ) {
155
205
//incase ccpack is reused
156
- ccpack .buf = nil
157
- ccpack .depSpec = nil
158
- ccpack .data = nil
206
+ ccpack .reset ()
159
207
160
208
depSpec := & pb.ChaincodeDeploymentSpec {}
161
209
err := proto .Unmarshal (buf , depSpec )
162
210
if err != nil {
163
211
return nil , fmt .Errorf ("failed to unmarshal deployment spec from bytes" )
164
212
}
165
213
166
- databytes , data , err := ccpack .getCDSData (depSpec )
214
+ databytes , id , data , err := ccpack .getCDSData (depSpec )
167
215
if err != nil {
168
216
return nil , err
169
217
}
170
218
171
219
ccpack .buf = buf
172
220
ccpack .depSpec = depSpec
173
221
ccpack .data = data
222
+ ccpack .datab = databytes
223
+ ccpack .id = id
174
224
175
- return & ChaincodeData { Name : depSpec . ChaincodeSpec . ChaincodeId . Name , Version : depSpec . ChaincodeSpec . ChaincodeId . Version , Data : databytes } , nil
225
+ return ccpack . GetChaincodeData () , nil
176
226
}
177
227
178
228
//InitFromFS returns the chaincode and its package from the file system
179
229
func (ccpack * CDSPackage ) InitFromFS (ccname string , ccversion string ) ([]byte , * pb.ChaincodeDeploymentSpec , error ) {
180
230
//incase ccpack is reused
181
- ccpack .buf = nil
182
- ccpack .depSpec = nil
183
- ccpack .data = nil
231
+ ccpack .reset ()
184
232
185
233
buf , err := GetChaincodePackage (ccname , ccversion )
186
234
if err != nil {
187
235
return nil , nil , err
188
236
}
189
237
190
- depSpec := & pb.ChaincodeDeploymentSpec {}
191
- err = proto .Unmarshal (buf , depSpec )
192
- if err != nil {
193
- return nil , nil , fmt .Errorf ("failed to unmarshal fs deployment spec for %s, %s" , ccname , ccversion )
194
- }
195
-
196
- _ , data , err := ccpack .getCDSData (depSpec )
197
- if err != nil {
238
+ if _ , err = ccpack .InitFromBuffer (buf ); err != nil {
198
239
return nil , nil , err
199
240
}
200
241
201
- ccpack .buf = buf
202
- ccpack .depSpec = depSpec
203
- ccpack .data = data
204
-
205
- return buf , depSpec , nil
242
+ return ccpack .buf , ccpack .depSpec , nil
206
243
}
207
244
208
245
//PutChaincodeToFS - serializes chaincode to a package on the file system
@@ -211,6 +248,10 @@ func (ccpack *CDSPackage) PutChaincodeToFS() error {
211
248
return fmt .Errorf ("uninitialized package" )
212
249
}
213
250
251
+ if ccpack .id == nil {
252
+ return fmt .Errorf ("id cannot be nil if buf is not nil" )
253
+ }
254
+
214
255
if ccpack .depSpec == nil {
215
256
return fmt .Errorf ("depspec cannot be nil if buf is not nil" )
216
257
}
@@ -219,6 +260,10 @@ func (ccpack *CDSPackage) PutChaincodeToFS() error {
219
260
return fmt .Errorf ("nil data" )
220
261
}
221
262
263
+ if ccpack .datab == nil {
264
+ return fmt .Errorf ("nil data bytes" )
265
+ }
266
+
222
267
ccname := ccpack .depSpec .ChaincodeSpec .ChaincodeId .Name
223
268
ccversion := ccpack .depSpec .ChaincodeSpec .ChaincodeId .Version
224
269
0 commit comments