diff --git a/channel/service.go b/channel/service.go index 051f876..f609e37 100644 --- a/channel/service.go +++ b/channel/service.go @@ -174,6 +174,11 @@ func (gc *gossipChannel) Initialize(chainID string, members []common.PKIidType, } func (gc *gossipChannel) AddMember(member common.PKIidType) (*protos.ChainState, error) { + // can't add self to the members + if bytes.Equal(member, gc.pkiID) { + return nil, errors.New("Can't add leader to the channel members") + } + gc.Lock() defer gc.Unlock() @@ -182,7 +187,7 @@ func (gc *gossipChannel) AddMember(member common.PKIidType) (*protos.ChainState, return nil, err } - for _, m := range stateInfo.Properties.Members { + for _, m := range gc.members { if bytes.Equal(member, common.PKIidType(m)) { return gc.chainStateMsg, nil } @@ -631,7 +636,10 @@ func (gc *gossipChannel) requestStateInfo() { return } - endpoints := filter.SelectPeers(gc.GetChannelConfig().PullPeerNum, gc.GetMembership(), gc.IsMemberInChan) + filters := filter.CombineRoutingFilters(gc.IsMemberInChan, func(member common.NetworkMember) bool { + return gc.pkiID.IsNotSameFilter(member.PKIID) + }) + endpoints := filter.SelectPeers(gc.GetChannelConfig().PullPeerNum, gc.GetMembership(), filters) gc.Send(req, endpoints...) } diff --git a/gossip/channel_test.go b/gossip/channel_test.go index a3dfcf9..cfde66c 100644 --- a/gossip/channel_test.go +++ b/gossip/channel_test.go @@ -23,6 +23,20 @@ import ( "google.golang.org/grpc" ) +func TestAddSelfToChainMembers(t *testing.T) { + gossipSvc1, err := CreateGossipServer([]string{"localhost:12053"}, "localhost:12053", 0) + require.NoError(t, err) + defer gossipSvc1.Stop() + + mac := channel.GenerateMAC(gossipSvc1.SelfPKIid(), "testchannel") + _, err = gossipSvc1.CreateChain(mac, "testchannel", []*common.FileSyncInfo{}) + assert.NoError(t, err) + + _, err = gossipSvc1.AddMemberToChain(mac, gossipSvc1.SelfPKIid()) + assert.Error(t, err) + assert.Contains(t, err.Error(), "Can't add leader to the channel members") +} + func TestChannelInit(t *testing.T) { gossipSvc1, err := CreateGossipServer([]string{"localhost:9053"}, "localhost:9053", 0) require.NoError(t, err) diff --git a/gossip/service.go b/gossip/service.go index 8906d32..0c181e2 100644 --- a/gossip/service.go +++ b/gossip/service.go @@ -498,6 +498,7 @@ func (g *gossipService) handleMessage(m protos.ReceivedMessage) { } if gc != nil { + gc.InitializeWithChainState(chainState) gc.HandleMessage(m) } }