From 613c94c416bf3152f6d88b8825d95dc1b5ccf698 Mon Sep 17 00:00:00 2001 From: Xu Qiaolun Date: Sun, 28 Apr 2019 14:23:52 +0800 Subject: [PATCH 1/2] Can't add leader to the channel members --- channel/service.go | 4 ++++ gossip/channel_test.go | 14 ++++++++++++++ gossip/service.go | 1 + 3 files changed, 19 insertions(+) diff --git a/channel/service.go b/channel/service.go index 051f876..57932d6 100644 --- a/channel/service.go +++ b/channel/service.go @@ -182,6 +182,10 @@ func (gc *gossipChannel) AddMember(member common.PKIidType) (*protos.ChainState, return nil, err } + if bytes.Equal(member, gc.pkiID) { + return nil, errors.New("Can't add leader to the channel members") + } + for _, m := range stateInfo.Properties.Members { if bytes.Equal(member, common.PKIidType(m)) { return gc.chainStateMsg, nil diff --git a/gossip/channel_test.go b/gossip/channel_test.go index a3dfcf9..04e70bf 100644 --- a/gossip/channel_test.go +++ b/gossip/channel_test.go @@ -23,6 +23,20 @@ import ( "google.golang.org/grpc" ) +func TestAddLeaderToChainMembers(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) } } From 51f6747e67a06f330c9d6a4079cd89fb0cce8f5a Mon Sep 17 00:00:00 2001 From: Xu Qiaolun Date: Sun, 28 Apr 2019 15:14:54 +0800 Subject: [PATCH 2/2] Filter self-node when requesting channel state --- channel/service.go | 16 ++++++++++------ gossip/channel_test.go | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/channel/service.go b/channel/service.go index 57932d6..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,11 +187,7 @@ func (gc *gossipChannel) AddMember(member common.PKIidType) (*protos.ChainState, return nil, err } - if bytes.Equal(member, gc.pkiID) { - return nil, errors.New("Can't add leader to the channel members") - } - - for _, m := range stateInfo.Properties.Members { + for _, m := range gc.members { if bytes.Equal(member, common.PKIidType(m)) { return gc.chainStateMsg, nil } @@ -635,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 04e70bf..cfde66c 100644 --- a/gossip/channel_test.go +++ b/gossip/channel_test.go @@ -23,7 +23,7 @@ import ( "google.golang.org/grpc" ) -func TestAddLeaderToChainMembers(t *testing.T) { +func TestAddSelfToChainMembers(t *testing.T) { gossipSvc1, err := CreateGossipServer([]string{"localhost:12053"}, "localhost:12053", 0) require.NoError(t, err) defer gossipSvc1.Stop()