Skip to content

Commit

Permalink
[FAB-2640] Gossip: Support empty set of anchor peers
Browse files Browse the repository at this point in the history
Gossip currently doesn't support an empty set of anchor peers
in the genesis block.
In such a case, it should be assumed that the organization
of the peer is the only member in the channel.

Change-Id: I37e3149076addc6e1e05746488360f700addfee9
Signed-off-by: Yacov Manevich <yacovm@il.ibm.com>
  • Loading branch information
yacovm committed Mar 6, 2017
1 parent dc7d4d4 commit da355f3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
10 changes: 9 additions & 1 deletion gossip/gossip/channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Config struct {
PullPeerNum int
PullInterval time.Duration
RequestStateInfoInterval time.Duration
Identity api.PeerIdentityType
}

// GossipChannel defines an object that deals with all channel-related messages
Expand Down Expand Up @@ -335,15 +336,22 @@ func (gc *gossipChannel) ConfigureChannel(joinMsg api.JoinChannelMessage) {
gc.logger.Warning("Already have a more updated JoinChannel message(", gc.joinMsg.SequenceNumber(), ") than", gc.joinMsg.SequenceNumber())
return
}
orgs := []api.OrgIdentityType{}

var orgs []api.OrgIdentityType
existingOrgInJoinChanMsg := make(map[string]struct{})
// We are in the channel if the joinMsg contains an empty set of anchor peers
selfOrg := gc.OrgByPeerIdentity(gc.GetConf().Identity)
if len(joinMsg.AnchorPeers()) == 0 {
orgs = []api.OrgIdentityType{selfOrg}
}
for _, anchorPeer := range joinMsg.AnchorPeers() {
orgID := anchorPeer.OrgID
if _, exists := existingOrgInJoinChanMsg[string(orgID)]; !exists {
orgs = append(orgs, orgID)
existingOrgInJoinChanMsg[string(orgID)] = struct{}{}
}
}

gc.orgs = orgs
gc.joinMsg = joinMsg
}
Expand Down
36 changes: 34 additions & 2 deletions gossip/gossip/channel/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var conf = Config{
PullPeerNum: 3,
PullInterval: time.Second,
RequestStateInfoInterval: time.Millisecond * 100,
Identity: api.PeerIdentityType("pkiIDInOrg1"),
}

func init() {
Expand Down Expand Up @@ -197,8 +198,7 @@ func (ga *gossipAdapterMock) ValidateStateInfoMessage(msg *proto.SignedGossipMes
}

func (ga *gossipAdapterMock) OrgByPeerIdentity(identity api.PeerIdentityType) api.OrgIdentityType {
args := ga.Called(identity)
return args.Get(0).(api.OrgIdentityType)
return ga.GetOrgOfPeer(common.PKIidType(identity))
}

func (ga *gossipAdapterMock) GetOrgOfPeer(PKIIID common.PKIidType) api.OrgIdentityType {
Expand All @@ -217,6 +217,9 @@ func configureAdapter(adapter *gossipAdapterMock, members ...discovery.NetworkMe
adapter.On("GetOrgOfPeer", pkiIDInOrg1ButNotEligible).Return(orgInChannelA)
adapter.On("GetOrgOfPeer", pkiIDinOrg2).Return(orgNotInChannelA)
adapter.On("GetOrgOfPeer", mock.Anything).Return(api.OrgIdentityType(nil))
adapter.On("OrgByPeerIdentity", mock.Anything).Run(func(args mock.Arguments) {
fmt.Println(args.Get(0))
})
}

func TestChannelPeriodicalPublishStateInfo(t *testing.T) {
Expand Down Expand Up @@ -815,6 +818,35 @@ func TestChannelReconfigureChannel(t *testing.T) {
}
}

func TestChannelNoAnchorPeers(t *testing.T) {
t.Parallel()

// Scenario: We got a join channel message with no anchor peers
// In this case, we should be in the channel

cs := &cryptoService{}
adapter := new(gossipAdapterMock)
configureAdapter(adapter, discovery.NetworkMember{PKIid: pkiIDInOrg1})

adapter.On("GetConf").Return(conf)
adapter.On("GetMembership").Return([]discovery.NetworkMember{})
adapter.On("OrgByPeerIdentity", api.PeerIdentityType(orgInChannelA)).Return(orgInChannelA)
adapter.On("GetOrgOfPeer", pkiIDInOrg1).Return(orgInChannelA)
adapter.On("GetOrgOfPeer", pkiIDinOrg2).Return(orgNotInChannelA)

jcm := &joinChanMsg{
anchorPeers: func() []api.AnchorPeer {
return []api.AnchorPeer{}
},
getTS: func() time.Time {
return time.Now().Add(time.Millisecond * 100)
},
}

gc := NewGossipChannel(cs, channelA, adapter, api.JoinChannelMessage(jcm))
assert.True(t, gc.IsOrgInChannel(orgInChannelA))
}

func TestChannelGetPeers(t *testing.T) {
t.Parallel()

Expand Down
1 change: 1 addition & 0 deletions gossip/gossip/chanstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func (ga *gossipAdapterImpl) GetConf() channel.Config {
PullInterval: ga.conf.PullInterval,
PullPeerNum: ga.conf.PullPeerNum,
RequestStateInfoInterval: ga.conf.RequestStateInfoInterval,
Identity: ga.selfIdentity,
}
}

Expand Down

0 comments on commit da355f3

Please sign in to comment.