@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
- package peer
17
+ package validation
18
18
19
19
import (
20
20
"fmt"
@@ -192,8 +192,6 @@ func validateChainHeader(cHdr *common.ChainHeader) error {
192
192
193
193
// TODO: validate epoch in cHdr.Epoch
194
194
195
- // TODO: validate type in cHdr.Type
196
-
197
195
// TODO: validate version in cHdr.Version
198
196
199
197
return nil
@@ -218,125 +216,150 @@ func validateCommonHeader(hdr *common.Header) error {
218
216
return nil
219
217
}
220
218
219
+ // validateConfigTransaction validates the payload of a
220
+ // transaction assuming its type is CONFIGURATION_TRANSACTION
221
+ func validateConfigTransaction (data []byte , hdr * common.Header ) error {
222
+ putilsLogger .Infof ("validateConfigTransaction starts for data %p, header %s" , data , hdr )
223
+
224
+ // check for nil argument
225
+ if data == nil || hdr == nil {
226
+ return fmt .Errorf ("Nil arguments" )
227
+ }
228
+
229
+ // if the type is CONFIGURATION_TRANSACTION we unmarshal a ConfigurationEnvelope message
230
+ ce , err := utils .GetConfigurationEnvelope (data )
231
+ if err != nil {
232
+ return fmt .Errorf ("GetConfigurationEnvelope failed, err %s" , err )
233
+ }
234
+
235
+ // check that we have at least one configuration item
236
+ if ce .Items == nil || len (ce .Items ) == 0 {
237
+ return fmt .Errorf ("At least one configuration item is necessary" )
238
+ }
239
+
240
+ for _ , item := range ce .Items {
241
+ if item .ConfigurationItem == nil {
242
+ return fmt .Errorf ("ConfigurationItem cannot be nil" )
243
+ }
244
+ }
245
+
246
+ return nil
247
+ }
248
+
221
249
// validateEndorserTransaction validates the payload of a
222
250
// transaction assuming its type is ENDORSER_TRANSACTION
223
- func validateEndorserTransaction (data []byte , hdr * common.Header ) ([] * pb. TransactionAction , error ) {
251
+ func validateEndorserTransaction (data []byte , hdr * common.Header ) error {
224
252
putilsLogger .Infof ("validateEndorserTransaction starts for data %p, header %s" , data , hdr )
225
253
226
254
// check for nil argument
227
255
if data == nil || hdr == nil {
228
- return nil , fmt .Errorf ("Nil arguments" )
256
+ return fmt .Errorf ("Nil arguments" )
229
257
}
230
258
231
259
// if the type is ENDORSER_TRANSACTION we unmarshal a Transaction message
232
260
tx , err := utils .GetTransaction (data )
233
261
if err != nil {
234
- return nil , err
262
+ return err
235
263
}
236
264
237
265
// check for nil argument
238
266
if tx == nil {
239
- return nil , fmt .Errorf ("Nil transaction" )
267
+ return fmt .Errorf ("Nil transaction" )
240
268
}
241
269
242
270
// TODO: validate tx.Version
243
271
244
272
// TODO: validate ChaincodeHeaderExtension
245
273
246
274
if len (tx .Actions ) == 0 {
247
- return nil , fmt .Errorf ("At least one TransactionAction is required" )
275
+ return fmt .Errorf ("At least one TransactionAction is required" )
248
276
}
249
277
250
278
putilsLogger .Infof ("validateEndorserTransaction info: there are %d actions" , len (tx .Actions ))
251
279
252
280
for _ , act := range tx .Actions {
253
281
// check for nil argument
254
282
if act == nil {
255
- return nil , fmt .Errorf ("Nil action" )
283
+ return fmt .Errorf ("Nil action" )
256
284
}
257
285
258
286
// if the type is ENDORSER_TRANSACTION we unmarshal a SignatureHeader
259
287
sHdr , err := utils .GetSignatureHeader (act .Header )
260
288
if err != nil {
261
- return nil , err
289
+ return err
262
290
}
263
291
264
292
// validate the SignatureHeader - here we actually only
265
293
// care about the nonce since the creator is in the outer header
266
294
err = validateSignatureHeader (sHdr )
267
295
if err != nil {
268
- return nil , err
296
+ return err
269
297
}
270
298
271
299
putilsLogger .Infof ("validateEndorserTransaction info: signature header is valid" )
272
300
273
301
// if the type is ENDORSER_TRANSACTION we unmarshal a ChaincodeActionPayload
274
302
cap , err := utils .GetChaincodeActionPayload (act .Payload )
275
303
if err != nil {
276
- return nil , err
304
+ return err
277
305
}
278
306
279
307
// extract the proposal response payload
280
308
prp , err := utils .GetProposalResponsePayload (cap .Action .ProposalResponsePayload )
281
309
if err != nil {
282
- return nil , err
310
+ return err
283
311
}
284
312
285
313
// build the original header by stitching together
286
314
// the common ChainHeader and the per-action SignatureHeader
287
315
hdrOrig := & common.Header {ChainHeader : hdr .ChainHeader , SignatureHeader : sHdr }
288
316
hdrBytes , err := utils .GetBytesHeader (hdrOrig ) // FIXME: here we hope that hdrBytes will be the same one that the endorser had
289
317
if err != nil {
290
- return nil , err
318
+ return err
291
319
}
292
320
293
321
// compute proposalHash
294
322
pHash , err := utils .GetProposalHash2 (hdrBytes , cap .ChaincodeProposalPayload )
295
323
if err != nil {
296
- return nil , err
324
+ return err
297
325
}
298
326
299
327
// ensure that the proposal hash matches
300
328
if bytes .Compare (pHash , prp .ProposalHash ) != 0 {
301
- return nil , fmt .Errorf ("proposal hash does not match" )
329
+ return fmt .Errorf ("proposal hash does not match" )
302
330
}
303
331
}
304
332
305
- return tx . Actions , nil
333
+ return nil
306
334
}
307
335
308
336
// ValidateTransaction checks that the transaction envelope is properly formed
309
- func ValidateTransaction (e * common.Envelope ) (* common.Payload , [] * pb. TransactionAction , error ) {
337
+ func ValidateTransaction (e * common.Envelope ) (* common.Payload , error ) {
310
338
putilsLogger .Infof ("ValidateTransactionEnvelope starts for envelope %p" , e )
311
339
312
340
// check for nil argument
313
341
if e == nil {
314
- return nil , nil , fmt .Errorf ("Nil Envelope" )
342
+ return nil , fmt .Errorf ("Nil Envelope" )
315
343
}
316
344
317
345
// get the payload from the envelope
318
346
payload , err := utils .GetPayload (e )
319
347
if err != nil {
320
- return nil , nil , fmt .Errorf ("Could not extract payload from envelope, err %s" , err )
348
+ return nil , fmt .Errorf ("Could not extract payload from envelope, err %s" , err )
321
349
}
322
350
323
351
putilsLogger .Infof ("Header is %s" , payload .Header )
324
352
325
- if common .HeaderType (payload .Header .ChainHeader .Type ) == common .HeaderType_CONFIGURATION_TRANSACTION {
326
- putilsLogger .Warningf ("Skipping common.HeaderType_CONFIGURATION_TRANSACTION validation pending JIRA-1639\n " )
327
- return payload , nil , nil
328
- }
329
-
330
353
// validate the header
331
354
err = validateCommonHeader (payload .Header )
332
355
if err != nil {
333
- return nil , nil , err
356
+ return nil , err
334
357
}
335
358
336
359
// validate the signature in the envelope
337
360
err = checkSignatureFromCreator (payload .Header .SignatureHeader .Creator , e .Signature , e .Payload , payload .Header .ChainHeader .ChainID )
338
361
if err != nil {
339
- return nil , nil , err
362
+ return nil , err
340
363
}
341
364
342
365
// TODO: ensure that creator can transact with us (some ACLs?) which set of APIs is supposed to give us this info?
@@ -346,10 +369,14 @@ func ValidateTransaction(e *common.Envelope) (*common.Payload, []*pb.Transaction
346
369
// continue the validation in a way that depends on the type specified in the header
347
370
switch common .HeaderType (payload .Header .ChainHeader .Type ) {
348
371
case common .HeaderType_ENDORSER_TRANSACTION :
349
- rv , err := validateEndorserTransaction (payload .Data , payload .Header )
350
- putilsLogger .Infof ("ValidateTransactionEnvelope returns %p, err %s" , rv , err )
351
- return payload , rv , err
372
+ err = validateEndorserTransaction (payload .Data , payload .Header )
373
+ putilsLogger .Infof ("ValidateTransactionEnvelope returns err %s" , err )
374
+ return payload , err
375
+ case common .HeaderType_CONFIGURATION_TRANSACTION :
376
+ err = validateConfigTransaction (payload .Data , payload .Header )
377
+ putilsLogger .Infof ("ValidateTransactionEnvelope returns err %s" , err )
378
+ return payload , err
352
379
default :
353
- return nil , nil , fmt .Errorf ("Unsupported transaction payload type %d" , common .HeaderType (payload .Header .ChainHeader .Type ))
380
+ return nil , fmt .Errorf ("Unsupported transaction payload type %d" , common .HeaderType (payload .Header .ChainHeader .Type ))
354
381
}
355
382
}
0 commit comments