@@ -40,18 +40,12 @@ var pkiID2MSPID = map[string]string{
40
40
"p10" : "Org10MSP" ,
41
41
"p11" : "Org11MSP" ,
42
42
"p12" : "Org12MSP" ,
43
+ "p13" : "Org13MSP" ,
44
+ "p14" : "Org14MSP" ,
45
+ "p15" : "Org15MSP" ,
43
46
}
44
47
45
48
func TestPeersForEndorsement (t * testing.T ) {
46
- peerRole := func (pkiID string ) * msp.MSPPrincipal {
47
- return & msp.MSPPrincipal {
48
- PrincipalClassification : msp .MSPPrincipal_ROLE ,
49
- Principal : utils .MarshalOrPanic (& msp.MSPRole {
50
- MspIdentifier : pkiID2MSPID [pkiID ],
51
- Role : msp .MSPRole_PEER ,
52
- }),
53
- }
54
- }
55
49
extractPeers := func (desc * discoveryprotos.EndorsementDescriptor ) map [string ]struct {} {
56
50
res := make (map [string ]struct {})
57
51
for _ , endorsers := range desc .EndorsersByGroups {
@@ -183,7 +177,7 @@ func TestPeersForEndorsement(t *testing.T) {
183
177
analyzer := NewEndorsementAnalyzer (g , pf , & principalEvaluatorMock {}, mf )
184
178
desc , err := analyzer .PeersForEndorsement (channel , & discoveryprotos.ChaincodeInterest {Chaincodes : []* discoveryprotos.ChaincodeCall {{Name : cc }}})
185
179
assert .Nil (t , desc )
186
- assert .Equal (t , err . Error (), "chaincode isn't installed on sufficient organizations required by the endorsement policy" )
180
+ assert .Equal (t , "cannot satisfy any principal combination" , err . Error () )
187
181
188
182
// Scenario VI: Policy is found, there are enough peers to satisfy policy combinations,
189
183
// but some peers have the wrong chaincode version, and some don't even have it installed.
@@ -203,22 +197,22 @@ func TestPeersForEndorsement(t *testing.T) {
203
197
}).Once ()
204
198
desc , err = analyzer .PeersForEndorsement (channel , & discoveryprotos.ChaincodeInterest {Chaincodes : []* discoveryprotos.ChaincodeCall {{Name : cc }}})
205
199
assert .Nil (t , desc )
206
- assert .Equal (t , err . Error (), "chaincode isn't installed on sufficient organizations required by the endorsement policy" )
200
+ assert .Equal (t , "cannot satisfy any principal combination" , err . Error () )
207
201
})
208
202
209
203
t .Run ("NoChaincodeMetadataFromLedger" , func (t * testing.T ) {
210
204
// Scenario VII: Policy is found, there are enough peers to satisfy the policy,
211
205
// but the chaincode metadata cannot be fetched from the ledger.
212
- //g.On("PeersOfChannel").Return(chanPeers.toMembers()).Once()
213
206
pb := principalBuilder {}
214
207
policy := pb .newSet ().addPrincipal (peerRole ("p0" )).addPrincipal (peerRole ("p6" )).
215
208
newSet ().addPrincipal (peerRole ("p12" )).buildPolicy ()
209
+ g .On ("PeersOfChannel" ).Return (chanPeers .toMembers ()).Once ()
216
210
pf .On ("PolicyByChaincode" , cc ).Return (policy ).Once ()
217
211
mf .On ("Metadata" ).Return (nil ).Once ()
218
212
analyzer := NewEndorsementAnalyzer (g , pf , & principalEvaluatorMock {}, mf )
219
213
desc , err := analyzer .PeersForEndorsement (channel , & discoveryprotos.ChaincodeInterest {Chaincodes : []* discoveryprotos.ChaincodeCall {{Name : cc }}})
220
214
assert .Nil (t , desc )
221
- assert .Equal (t , err . Error (), "No metadata was found for chaincode chaincode in channel test" )
215
+ assert .Equal (t , "No metadata was found for chaincode chaincode in channel test" , err . Error () )
222
216
})
223
217
224
218
t .Run ("Collections" , func (t * testing.T ) {
@@ -328,6 +322,113 @@ func TestPeersForEndorsement(t *testing.T) {
328
322
})
329
323
}
330
324
325
+ func TestPeersAuthorizedByCriteria (t * testing.T ) {
326
+ cc1 := "cc1"
327
+ cc2 := "cc2"
328
+ members := peerSet {
329
+ newPeer (0 ).withChaincode (cc1 , "1.0" ),
330
+ newPeer (3 ).withChaincode (cc1 , "1.0" ),
331
+ newPeer (6 ).withChaincode (cc1 , "1.0" ),
332
+ newPeer (9 ).withChaincode (cc1 , "1.0" ),
333
+ newPeer (12 ).withChaincode (cc1 , "1.0" ),
334
+ }.toMembers ()
335
+
336
+ members2 := append (discovery.Members {}, members ... )
337
+ members2 = append (members2 , peerSet {newPeer (13 ).withChaincode (cc1 , "1.1" ).withChaincode (cc2 , "1.0" )}.toMembers ()... )
338
+ members2 = append (members2 , peerSet {newPeer (14 ).withChaincode (cc1 , "1.1" )}.toMembers ()... )
339
+ members2 = append (members2 , peerSet {newPeer (15 ).withChaincode (cc2 , "1.0" )}.toMembers ()... )
340
+
341
+ alivePeers := peerSet {
342
+ newPeer (0 ),
343
+ newPeer (2 ),
344
+ newPeer (4 ),
345
+ newPeer (6 ),
346
+ newPeer (8 ),
347
+ newPeer (10 ),
348
+ newPeer (11 ),
349
+ newPeer (12 ),
350
+ newPeer (13 ),
351
+ newPeer (14 ),
352
+ newPeer (15 ),
353
+ }.toMembers ()
354
+
355
+ identities := identitySet (pkiID2MSPID )
356
+
357
+ for _ , tst := range []struct {
358
+ name string
359
+ arguments * discoveryprotos.ChaincodeInterest
360
+ totalExistingMembers discovery.Members
361
+ metadata []* chaincode.Metadata
362
+ expected discovery.Members
363
+ }{
364
+ {
365
+ name : "Nil interest" ,
366
+ arguments : nil ,
367
+ totalExistingMembers : members ,
368
+ expected : members ,
369
+ },
370
+ {
371
+ name : "Empty interest invocation chain" ,
372
+ arguments : & discoveryprotos.ChaincodeInterest {},
373
+ totalExistingMembers : members ,
374
+ expected : members ,
375
+ },
376
+ {
377
+ name : "Chaincodes only installed on some peers" ,
378
+ arguments : & discoveryprotos.ChaincodeInterest {
379
+ Chaincodes : []* discoveryprotos.ChaincodeCall {
380
+ {Name : cc1 }, {Name : cc2 },
381
+ },
382
+ },
383
+ totalExistingMembers : members2 ,
384
+ metadata : []* chaincode.Metadata {{
385
+ Name : "cc1" , Version : "1.1" ,
386
+ }, {
387
+ Name : "cc2" , Version : "1.0" ,
388
+ }},
389
+ expected : peerSet {newPeer (13 ).withChaincode (cc1 , "1.1" ).withChaincode (cc2 , "1.0" )}.toMembers (),
390
+ },
391
+ {
392
+ name : "Only some peers authorized by collection" ,
393
+ arguments : & discoveryprotos.ChaincodeInterest {
394
+ Chaincodes : []* discoveryprotos.ChaincodeCall {
395
+ {Name : cc1 , CollectionNames : []string {"collection" }},
396
+ },
397
+ },
398
+ totalExistingMembers : members ,
399
+ metadata : []* chaincode.Metadata {{
400
+ Name : cc1 , Version : "1.0" ,
401
+ CollectionsConfig : buildCollectionConfig (map [string ][]* msp.MSPPrincipal {
402
+ "collection" : {
403
+ peerRole ("p0" ),
404
+ peerRole ("p12" ),
405
+ },
406
+ }),
407
+ }},
408
+ expected : peerSet {
409
+ newPeer (0 ).withChaincode (cc1 , "1.0" ),
410
+ newPeer (12 ).withChaincode (cc1 , "1.0" )}.toMembers (),
411
+ },
412
+ } {
413
+ t .Run (tst .name , func (t * testing.T ) {
414
+ g := & gossipMock {}
415
+ pf := & policyFetcherMock {}
416
+ mf := & metadataFetcher {}
417
+ g .On ("Peers" ).Return (alivePeers )
418
+ g .On ("IdentityInfo" ).Return (identities )
419
+ g .On ("PeersOfChannel" ).Return (tst .totalExistingMembers ).Once ()
420
+ for _ , md := range tst .metadata {
421
+ mf .On ("Metadata" ).Return (md ).Once ()
422
+ }
423
+
424
+ analyzer := NewEndorsementAnalyzer (g , pf , & principalEvaluatorMock {}, mf )
425
+ actualMembers , err := analyzer .PeersAuthorizedByCriteria (common .ChainID ("mychannel" ), tst .arguments )
426
+ assert .NoError (t , err )
427
+ assert .Equal (t , tst .expected , actualMembers )
428
+ })
429
+ }
430
+ }
431
+
331
432
func TestPop (t * testing.T ) {
332
433
slice := []inquire.ComparablePrincipalSets {{}, {}}
333
434
assert .Len (t , slice , 2 )
@@ -354,10 +455,7 @@ func TestComputePrincipalSetsNoPolicies(t *testing.T) {
354
455
Chaincodes : []* discoveryprotos.ChaincodeCall {},
355
456
}
356
457
ea := & endorsementAnalyzer {}
357
- acceptAll := func (policies.PrincipalSet ) bool {
358
- return true
359
- }
360
- _ , err := ea .computePrincipalSets (common .ChainID ("mychannel" ), interest , acceptAll )
458
+ _ , err := ea .computePrincipalSets (common .ChainID ("mychannel" ), interest )
361
459
assert .Error (t , err )
362
460
assert .Contains (t , err .Error (), "no principal sets remained after filtering" )
363
461
}
@@ -482,6 +580,16 @@ func newPeer(i int) *peerInfo {
482
580
}
483
581
}
484
582
583
+ func peerRole (pkiID string ) * msp.MSPPrincipal {
584
+ return & msp.MSPPrincipal {
585
+ PrincipalClassification : msp .MSPPrincipal_ROLE ,
586
+ Principal : utils .MarshalOrPanic (& msp.MSPRole {
587
+ MspIdentifier : pkiID2MSPID [pkiID ],
588
+ Role : msp .MSPRole_PEER ,
589
+ }),
590
+ }
591
+ }
592
+
485
593
func (pi * peerInfo ) withChaincode (name , version string ) * peerInfo {
486
594
if pi .Properties == nil {
487
595
pi .Properties = & gossip.Properties {}
@@ -553,12 +661,6 @@ func (ip inquireablePolicy) SatisfiedBy() []policies.PrincipalSet {
553
661
type principalEvaluatorMock struct {
554
662
}
555
663
556
- func (pe * principalEvaluatorMock ) MSPOfPrincipal (principal * msp.MSPPrincipal ) string {
557
- role := & msp.MSPRole {}
558
- proto .Unmarshal (principal .Principal , role )
559
- return role .MspIdentifier
560
- }
561
-
562
664
func (pe * principalEvaluatorMock ) SatisfiesPrincipal (channel string , identity []byte , principal * msp.MSPPrincipal ) error {
563
665
peerRole := & msp.MSPRole {}
564
666
if err := proto .Unmarshal (principal .Principal , peerRole ); err != nil {
0 commit comments