github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/gossip/service/join_test.go (about) 1 /* 2 Copyright hechain. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package service 8 9 import ( 10 "sync" 11 "testing" 12 "time" 13 14 "github.com/hechain20/hechain/common/channelconfig" 15 "github.com/hechain20/hechain/gossip/api" 16 "github.com/hechain20/hechain/gossip/comm" 17 "github.com/hechain20/hechain/gossip/common" 18 "github.com/hechain20/hechain/gossip/discovery" 19 "github.com/hechain20/hechain/gossip/filter" 20 "github.com/hechain20/hechain/gossip/gossip" 21 "github.com/hechain20/hechain/gossip/protoext" 22 "github.com/hechain20/hechain/gossip/util" 23 "github.com/hechain20/hechain/msp" 24 proto "github.com/hyperledger/fabric-protos-go/gossip" 25 "github.com/hyperledger/fabric-protos-go/peer" 26 "github.com/stretchr/testify/mock" 27 "github.com/stretchr/testify/require" 28 ) 29 30 type secAdvMock struct{} 31 32 func init() { 33 util.SetupTestLogging() 34 } 35 36 func (s *secAdvMock) OrgByPeerIdentity(identity api.PeerIdentityType) api.OrgIdentityType { 37 return api.OrgIdentityType(identity) 38 } 39 40 type gossipMock struct { 41 mock.Mock 42 } 43 44 func (g *gossipMock) SelfChannelInfo(common.ChannelID) *protoext.SignedGossipMessage { 45 panic("implement me") 46 } 47 48 func (g *gossipMock) SelfMembershipInfo() discovery.NetworkMember { 49 panic("implement me") 50 } 51 52 func (*gossipMock) PeerFilter(channel common.ChannelID, messagePredicate api.SubChannelSelectionCriteria) (filter.RoutingFilter, error) { 53 panic("implement me") 54 } 55 56 func (*gossipMock) SuspectPeers(s api.PeerSuspector) { 57 panic("implement me") 58 } 59 60 func (*gossipMock) Send(msg *proto.GossipMessage, peers ...*comm.RemotePeer) { 61 panic("implement me") 62 } 63 64 func (*gossipMock) Peers() []discovery.NetworkMember { 65 panic("implement me") 66 } 67 68 func (*gossipMock) PeersOfChannel(common.ChannelID) []discovery.NetworkMember { 69 panic("implement me") 70 } 71 72 func (*gossipMock) UpdateMetadata(metadata []byte) { 73 panic("implement me") 74 } 75 76 // UpdateLedgerHeight updates the ledger height the peer 77 // publishes to other peers in the channel 78 func (*gossipMock) UpdateLedgerHeight(height uint64, channelID common.ChannelID) { 79 panic("implement me") 80 } 81 82 // UpdateChaincodes updates the chaincodes the peer publishes 83 // to other peers in the channel 84 func (*gossipMock) UpdateChaincodes(chaincode []*proto.Chaincode, channelID common.ChannelID) { 85 panic("implement me") 86 } 87 88 func (*gossipMock) Gossip(msg *proto.GossipMessage) { 89 panic("implement me") 90 } 91 92 func (*gossipMock) Accept(acceptor common.MessageAcceptor, passThrough bool) (<-chan *proto.GossipMessage, <-chan protoext.ReceivedMessage) { 93 panic("implement me") 94 } 95 96 func (g *gossipMock) JoinChan(joinMsg api.JoinChannelMessage, channelID common.ChannelID) { 97 g.Called(joinMsg, channelID) 98 } 99 100 func (g *gossipMock) LeaveChan(channelID common.ChannelID) { 101 panic("implement me") 102 } 103 104 func (g *gossipMock) IdentityInfo() api.PeerIdentitySet { 105 panic("implement me") 106 } 107 108 func (*gossipMock) IsInMyOrg(member discovery.NetworkMember) bool { 109 panic("implement me") 110 } 111 112 func (*gossipMock) Stop() { 113 panic("implement me") 114 } 115 116 func (*gossipMock) SendByCriteria(*protoext.SignedGossipMessage, gossip.SendCriteria) error { 117 panic("implement me") 118 } 119 120 type appOrgMock struct { 121 id string 122 } 123 124 func (*appOrgMock) Name() string { 125 panic("implement me") 126 } 127 128 func (*appOrgMock) MSP() msp.MSP { 129 panic("generate this fake instead") 130 } 131 132 func (ao *appOrgMock) MSPID() string { 133 return ao.id 134 } 135 136 func (ao *appOrgMock) AnchorPeers() []*peer.AnchorPeer { 137 return []*peer.AnchorPeer{} 138 } 139 140 func TestJoinChannelConfig(t *testing.T) { 141 // Scenarios: The channel we're joining has a single org - Org0 142 // but our org ID is actually Org0MSP in the negative path 143 // and Org0 in the positive path 144 145 failChan := make(chan struct{}, 1) 146 g1SvcMock := &gossipMock{} 147 g1SvcMock.On("JoinChan", mock.Anything, mock.Anything).Run(func(_ mock.Arguments) { 148 failChan <- struct{}{} 149 }) 150 anchorPeerTracker := &anchorPeerTracker{allEndpoints: map[string]map[string]struct{}{}} 151 g1 := &GossipService{secAdv: &secAdvMock{}, peerIdentity: api.PeerIdentityType("OrgMSP0"), gossipSvc: g1SvcMock, anchorPeerTracker: anchorPeerTracker} 152 g1.updateAnchors(ConfigUpdate{ 153 ChannelID: "A", 154 OrdererAddresses: []string{"localhost:7050"}, 155 Organizations: map[string]channelconfig.ApplicationOrg{ 156 "Org0": &appOrgMock{id: "Org0"}, 157 }, 158 }) 159 select { 160 case <-time.After(time.Second): 161 case <-failChan: 162 require.Fail(t, "Joined a badly configured channel") 163 } 164 165 succChan := make(chan struct{}, 1) 166 g2SvcMock := &gossipMock{} 167 g2SvcMock.On("JoinChan", mock.Anything, mock.Anything).Run(func(_ mock.Arguments) { 168 succChan <- struct{}{} 169 }) 170 g2 := &GossipService{secAdv: &secAdvMock{}, peerIdentity: api.PeerIdentityType("Org0"), gossipSvc: g2SvcMock, anchorPeerTracker: anchorPeerTracker} 171 g2.updateAnchors(ConfigUpdate{ 172 ChannelID: "A", 173 OrdererAddresses: []string{"localhost:7050"}, 174 Organizations: map[string]channelconfig.ApplicationOrg{ 175 "Org0": &appOrgMock{id: "Org0"}, 176 }, 177 }) 178 select { 179 case <-time.After(time.Second): 180 require.Fail(t, "Didn't join a channel (should have done so within the time period)") 181 case <-succChan: 182 } 183 } 184 185 func TestJoinChannelNoAnchorPeers(t *testing.T) { 186 // Scenario: The channel we're joining has 2 orgs but no anchor peers 187 // The test ensures that JoinChan is called with a JoinChannelMessage with Members 188 // that consist of the organizations of the configuration given. 189 190 var joinChanCalled sync.WaitGroup 191 joinChanCalled.Add(1) 192 gMock := &gossipMock{} 193 gMock.On("JoinChan", mock.Anything, mock.Anything).Run(func(args mock.Arguments) { 194 defer joinChanCalled.Done() 195 jcm := args.Get(0).(api.JoinChannelMessage) 196 channel := args.Get(1).(common.ChannelID) 197 require.Len(t, jcm.Members(), 2) 198 require.Contains(t, jcm.Members(), api.OrgIdentityType("Org0")) 199 require.Contains(t, jcm.Members(), api.OrgIdentityType("Org1")) 200 require.Equal(t, "A", string(channel)) 201 }) 202 203 anchorPeerTracker := &anchorPeerTracker{allEndpoints: map[string]map[string]struct{}{}} 204 g := &GossipService{secAdv: &secAdvMock{}, peerIdentity: api.PeerIdentityType("Org0"), gossipSvc: gMock, anchorPeerTracker: anchorPeerTracker} 205 206 appOrg0 := &appOrgMock{id: "Org0"} 207 appOrg1 := &appOrgMock{id: "Org1"} 208 209 // Make sure the ApplicationOrgs really have no anchor peers 210 require.Empty(t, appOrg0.AnchorPeers()) 211 require.Empty(t, appOrg1.AnchorPeers()) 212 213 g.updateAnchors(ConfigUpdate{ 214 ChannelID: "A", 215 OrdererAddresses: []string{"localhost:7050"}, 216 Organizations: map[string]channelconfig.ApplicationOrg{ 217 "Org0": appOrg0, 218 "Org1": appOrg1, 219 }, 220 }) 221 joinChanCalled.Wait() 222 }