github.com/lzy4123/fabric@v2.1.1+incompatible/discovery/client/selection_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package discovery 8 9 import ( 10 "testing" 11 12 "github.com/golang/protobuf/proto" 13 "github.com/hyperledger/fabric-protos-go/gossip" 14 "github.com/hyperledger/fabric/gossip/protoext" 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func TestShuffle(t *testing.T) { 19 endorsers := make(Endorsers, 1000) 20 for i := 0; i < len(endorsers); i++ { 21 endorsers[i] = &Peer{ 22 StateInfoMessage: stateInfoWithHeight(uint64(i)), 23 } 24 } 25 26 isHeightAscending := func(endorsers Endorsers) bool { 27 for i := 0; i < len(endorsers)-1; i++ { 28 currHeight := endorsers[i].StateInfoMessage.GetStateInfo().Properties.LedgerHeight 29 nextHeight := endorsers[i+1].StateInfoMessage.GetStateInfo().Properties.LedgerHeight 30 if currHeight > nextHeight { 31 return false 32 } 33 } 34 return true 35 } 36 37 assert.True(t, isHeightAscending(endorsers)) 38 assert.False(t, isHeightAscending(endorsers.Shuffle())) 39 } 40 41 func TestExclusionAndPriority(t *testing.T) { 42 newPeer := func(i int) *Peer { 43 si := stateInfoWithHeight(uint64(i)) 44 am, _ := protoext.EnvelopeToGossipMessage(aliveMessage(i)) 45 return &Peer{ 46 StateInfoMessage: si, 47 AliveMessage: am, 48 } 49 } 50 51 excludeFirst := selectionFunc(func(p Peer) bool { 52 return p.AliveMessage.GetAliveMsg().Timestamp.SeqNum == uint64(1) 53 }) 54 55 givenPeers := Endorsers{newPeer(3), newPeer(5), newPeer(1), newPeer(4), newPeer(2), newPeer(3)} 56 assert.Equal(t, []int{5, 4, 3, 3, 2}, heights(givenPeers.Filter(excludeFirst).Sort(PrioritiesByHeight))) 57 } 58 59 func TestExcludeEndpoints(t *testing.T) { 60 secretEndpoint := &gossip.Secret{ 61 Content: &gossip.Secret_InternalEndpoint{ 62 InternalEndpoint: "s2", 63 }, 64 } 65 secret, _ := proto.Marshal(secretEndpoint) 66 am1 := aliveMessage(1) 67 am2 := aliveMessage(2) 68 am2.SecretEnvelope = &gossip.SecretEnvelope{ 69 Payload: secret, 70 } 71 am3 := aliveMessage(3) 72 g1, _ := protoext.EnvelopeToGossipMessage(am1) 73 g2, _ := protoext.EnvelopeToGossipMessage(am2) 74 g3, _ := protoext.EnvelopeToGossipMessage(am3) 75 p1 := Peer{ 76 AliveMessage: g1, 77 } 78 p2 := Peer{ 79 AliveMessage: g2, 80 } 81 p3 := Peer{ 82 AliveMessage: g3, 83 } 84 85 s := ExcludeHosts("p1", "s2") 86 assert.True(t, s.Exclude(p1)) 87 assert.True(t, s.Exclude(p2)) 88 assert.False(t, s.Exclude(p3)) 89 90 s = NoExclusion 91 assert.False(t, s.Exclude(p1)) 92 assert.False(t, s.Exclude(p2)) 93 assert.False(t, s.Exclude(p3)) 94 } 95 96 func TestNoPriorities(t *testing.T) { 97 s1 := stateInfoWithHeight(100) 98 s2 := stateInfoWithHeight(200) 99 p1 := Peer{ 100 StateInfoMessage: s1, 101 } 102 p2 := Peer{ 103 StateInfoMessage: s2, 104 } 105 assert.Equal(t, Priority(0), NoPriorities.Compare(p1, p2)) 106 } 107 108 func TestPrioritiesByHeight(t *testing.T) { 109 tests := []struct { 110 name string 111 expected Priority 112 leftHeight uint64 113 rightHeight uint64 114 }{ 115 { 116 name: "Same heights", 117 expected: 0, 118 leftHeight: 100, 119 rightHeight: 100, 120 }, 121 { 122 name: "Right height bigger", 123 expected: -1, 124 leftHeight: 100, 125 rightHeight: 200, 126 }, 127 { 128 name: "Left height bigger", 129 expected: 1, 130 leftHeight: 200, 131 rightHeight: 100, 132 }, 133 } 134 135 for _, test := range tests { 136 test := test 137 t.Run(test.name, func(t *testing.T) { 138 s1 := stateInfoWithHeight(test.leftHeight) 139 s2 := stateInfoWithHeight(test.rightHeight) 140 p1 := Peer{ 141 StateInfoMessage: s1, 142 } 143 p2 := Peer{ 144 StateInfoMessage: s2, 145 } 146 p := PrioritiesByHeight.Compare(p1, p2) 147 assert.Equal(t, test.expected, p) 148 }) 149 } 150 151 } 152 153 func stateInfoWithHeight(h uint64) *protoext.SignedGossipMessage { 154 g := &gossip.GossipMessage{ 155 Content: &gossip.GossipMessage_StateInfo{ 156 StateInfo: &gossip.StateInfo{ 157 Properties: &gossip.Properties{ 158 LedgerHeight: h, 159 }, 160 Timestamp: &gossip.PeerTime{}, 161 }, 162 }, 163 } 164 sMsg, _ := protoext.NoopSign(g) 165 return sMsg 166 } 167 168 func heights(endorsers Endorsers) []int { 169 var res []int 170 for _, e := range endorsers { 171 res = append(res, int(e.StateInfoMessage.GetStateInfo().Properties.LedgerHeight)) 172 } 173 return res 174 }