@@ -17,15 +17,14 @@ limitations under the License.
17
17
package channel
18
18
19
19
import (
20
+ "errors"
20
21
"fmt"
21
22
"strconv"
22
23
"sync"
23
24
"sync/atomic"
24
25
"testing"
25
26
"time"
26
27
27
- "errors"
28
-
29
28
"github.com/hyperledger/fabric/gossip/api"
30
29
"github.com/hyperledger/fabric/gossip/comm"
31
30
"github.com/hyperledger/fabric/gossip/common"
@@ -57,11 +56,12 @@ func init() {
57
56
var (
58
57
// Organizations: {ORG1, ORG2}
59
58
// Channel A: {ORG1}
60
- channelA = common .ChainID ("A" )
61
- orgInChannelA = api .OrgIdentityType ("ORG1" )
62
- orgNotInChannelA = api .OrgIdentityType ("ORG2" )
63
- pkiIDInOrg1 = common .PKIidType ("pkiIDInOrg1" )
64
- pkiIDinOrg2 = common .PKIidType ("pkiIDinOrg2" )
59
+ channelA = common .ChainID ("A" )
60
+ orgInChannelA = api .OrgIdentityType ("ORG1" )
61
+ orgNotInChannelA = api .OrgIdentityType ("ORG2" )
62
+ pkiIDInOrg1 = common .PKIidType ("pkiIDInOrg1" )
63
+ pkiIDInOrg1ButNotEligible = common .PKIidType ("pkiIDInOrg1ButNotEligible" )
64
+ pkiIDinOrg2 = common .PKIidType ("pkiIDinOrg2" )
65
65
)
66
66
67
67
type joinChanMsg struct {
@@ -88,15 +88,20 @@ func (jcm *joinChanMsg) AnchorPeers() []api.AnchorPeer {
88
88
}
89
89
90
90
type cryptoService struct {
91
+ mocked bool
91
92
mock.Mock
92
93
}
93
94
94
95
func (cs * cryptoService ) GetPKIidOfCert (peerIdentity api.PeerIdentityType ) common.PKIidType {
95
96
panic ("Should not be called in this test" )
96
97
}
97
98
98
- func (cs * cryptoService ) VerifyByChannel (_ common.ChainID , _ api.PeerIdentityType , _ , _ []byte ) error {
99
- panic ("Should not be called in this test" )
99
+ func (cs * cryptoService ) VerifyByChannel (channel common.ChainID , identity api.PeerIdentityType , _ , _ []byte ) error {
100
+ if ! cs .mocked {
101
+ return nil
102
+ }
103
+ args := cs .Called (identity )
104
+ return args .Get (0 ).(error )
100
105
}
101
106
102
107
func (cs * cryptoService ) VerifyBlock (chainID common.ChainID , signedBlock api.SignedBlock ) error {
@@ -199,10 +204,15 @@ func (ga *gossipAdapterMock) GetOrgOfPeer(PKIIID common.PKIidType) api.OrgIdenti
199
204
return args .Get (0 ).(api.OrgIdentityType )
200
205
}
201
206
207
+ func (ga * gossipAdapterMock ) GetIdentityByPKIID (pkiID common.PKIidType ) api.PeerIdentityType {
208
+ return api .PeerIdentityType (pkiID )
209
+ }
210
+
202
211
func configureAdapter (adapter * gossipAdapterMock , members ... discovery.NetworkMember ) {
203
212
adapter .On ("GetConf" ).Return (conf )
204
213
adapter .On ("GetMembership" ).Return (members )
205
214
adapter .On ("GetOrgOfPeer" , pkiIDInOrg1 ).Return (orgInChannelA )
215
+ adapter .On ("GetOrgOfPeer" , pkiIDInOrg1ButNotEligible ).Return (orgInChannelA )
206
216
adapter .On ("GetOrgOfPeer" , pkiIDinOrg2 ).Return (orgNotInChannelA )
207
217
adapter .On ("GetOrgOfPeer" , mock .Anything ).Return (api .OrgIdentityType (nil ))
208
218
}
@@ -312,6 +322,8 @@ func TestChannelPeerNotInChannel(t *testing.T) {
312
322
gossipMessagesSentFromChannel <- msg
313
323
}
314
324
// First, ensure it does that for pull messages from peers that are in the channel
325
+ // Let the peer first publish it is in the channel
326
+ gc .HandleMessage (& receivedMsg {msg : createStateInfoMsg (10 , pkiIDInOrg1 , channelA ), PKIID : pkiIDInOrg1 })
315
327
helloMsg := createHelloMsg (pkiIDInOrg1 )
316
328
helloMsg .On ("Respond" , mock .Anything ).Run (messageRelayer )
317
329
gc .HandleMessage (helloMsg )
@@ -330,6 +342,23 @@ func TestChannelPeerNotInChannel(t *testing.T) {
330
342
case <- time .After (time .Second * 1 ):
331
343
}
332
344
345
+ // Now for a more advanced scenario- the peer claims to be in the right org, and also claims to be in the channel
346
+ // but the MSP declares it is not eligible for the channel
347
+ // pkiIDInOrg1ButNotEligible
348
+ gc .HandleMessage (& receivedMsg {msg : createStateInfoMsg (10 , pkiIDInOrg1ButNotEligible , channelA ), PKIID : pkiIDInOrg1ButNotEligible })
349
+ cs .On ("VerifyByChannel" , mock .Anything ).Return (errors .New ("Not eligible" ))
350
+ cs .mocked = true
351
+ helloMsg = createHelloMsg (pkiIDInOrg1ButNotEligible )
352
+ helloMsg .On ("Respond" , mock .Anything ).Run (messageRelayer )
353
+ gc .HandleMessage (helloMsg )
354
+ select {
355
+ case <- gossipMessagesSentFromChannel :
356
+ t .Fatal ("Responded with digest, but shouldn't have since peer is not eligible for the channel" )
357
+ case <- time .After (time .Second * 1 ):
358
+ }
359
+
360
+ cs .Mock = mock.Mock {}
361
+
333
362
// Ensure we respond to a valid StateInfoRequest
334
363
req := gc .(* gossipChannel ).createStateInfoRequest ()
335
364
validReceivedMsg := & receivedMsg {
@@ -404,7 +433,7 @@ func TestChannelIsSubscribed(t *testing.T) {
404
433
adapter .On ("Send" , mock .Anything , mock .Anything )
405
434
adapter .On ("DeMultiplex" , mock .Anything )
406
435
gc .HandleMessage (& receivedMsg {msg : createStateInfoMsg (10 , pkiIDInOrg1 , channelA ), PKIID : pkiIDInOrg1 })
407
- assert .True (t , gc .IsSubscribed (discovery.NetworkMember {PKIid : pkiIDInOrg1 }))
436
+ assert .True (t , gc .EligibleForChannel (discovery.NetworkMember {PKIid : pkiIDInOrg1 }))
408
437
}
409
438
410
439
func TestChannelAddToMessageStore (t * testing.T ) {
@@ -456,7 +485,7 @@ func TestChannelAddToMessageStore(t *testing.T) {
456
485
}
457
486
458
487
gc .HandleMessage (& receivedMsg {msg : createStateInfoMsg (10 , pkiIDInOrg1 , channelA ), PKIID : pkiIDInOrg1 })
459
- assert .True (t , gc .IsSubscribed (discovery.NetworkMember {PKIid : pkiIDInOrg1 }))
488
+ assert .True (t , gc .EligibleForChannel (discovery.NetworkMember {PKIid : pkiIDInOrg1 }))
460
489
}
461
490
462
491
func TestChannelBadBlocks (t * testing.T ) {
@@ -782,7 +811,38 @@ func TestChannelReconfigureChannel(t *testing.T) {
782
811
t .Fatal ("Responded with digest, but shouldn't have since peer is in ORG2 and its not in the channel" )
783
812
case <- time .After (time .Second * 1 ):
784
813
}
814
+ }
815
+
816
+ func TestChannelGetPeers (t * testing.T ) {
817
+ t .Parallel ()
785
818
819
+ // Scenario: We have a peer in an org, and the peer is notified that several peers
820
+ // exist, and some of them:
821
+ // (1) Join its channel, and are eligible for receiving blocks.
822
+ // (2) Join its channel, but are not eligible for receiving blocks (MSP doesn't allow this).
823
+ // (3) Say they join its channel, but are actually from an org that is not in the channel.
824
+ // The GetPeers query should only return peers that belong to the first group.
825
+ cs := & cryptoService {}
826
+ adapter := new (gossipAdapterMock )
827
+ adapter .On ("Gossip" , mock .Anything )
828
+ adapter .On ("Send" , mock .Anything , mock .Anything )
829
+ adapter .On ("DeMultiplex" , mock .Anything )
830
+ members := []discovery.NetworkMember {
831
+ {PKIid : pkiIDInOrg1 },
832
+ {PKIid : pkiIDInOrg1ButNotEligible },
833
+ {PKIid : pkiIDinOrg2 },
834
+ }
835
+ configureAdapter (adapter , members ... )
836
+ gc := NewGossipChannel (cs , channelA , adapter , & joinChanMsg {})
837
+ gc .HandleMessage (& receivedMsg {PKIID : pkiIDInOrg1 , msg : createStateInfoMsg (1 , pkiIDInOrg1 , channelA )})
838
+ gc .HandleMessage (& receivedMsg {PKIID : pkiIDInOrg1 , msg : createStateInfoMsg (1 , pkiIDinOrg2 , channelA )})
839
+ assert .Len (t , gc .GetPeers (), 1 )
840
+ assert .Equal (t , pkiIDInOrg1 , gc .GetPeers ()[0 ].PKIid )
841
+
842
+ gc .HandleMessage (& receivedMsg {msg : createStateInfoMsg (10 , pkiIDInOrg1ButNotEligible , channelA ), PKIID : pkiIDInOrg1ButNotEligible })
843
+ cs .On ("VerifyByChannel" , mock .Anything ).Return (errors .New ("Not eligible" ))
844
+ cs .mocked = true
845
+ assert .Len (t , gc .GetPeers (), 0 )
786
846
}
787
847
788
848
func createDataUpdateMsg (nonce uint64 ) * proto.SignedGossipMessage {
0 commit comments