mirror of
https://github.com/fluencelabs/tendermint
synced 2025-04-25 14:52:17 +00:00
* p2p: initial implementation of peer behaviour * [p2p] re-use newMockPeer * p2p: add inline docs for peer_behaviour interface * [p2p] Align PeerBehaviour interface (#3558) * [p2p] make switchedPeerHebaviour private * [p2p] make storePeerHebaviour private * [p2p] Add CHANGELOG_PENDING entry for PeerBehaviour * [p2p] Adjustment naming for PeerBehaviour * [p2p] Add coarse lock around storedPeerBehaviour * [p2p]: Fix non-pointer methods in storedPeerBehaviour + Structs with embeded locks must specify all methods with pointer receivers to avoid creating a copy of the embeded lock. * [p2p] Thorough refactoring based on comments in #3552 + Decouple PeerBehaviour interface from Peer by parametrizing methods with `p2p.ID` instead of `p2p.Peer` + Setter methods wrapped in a write lock + Getter methods wrapped in a read lock + Getter methods on storedPeerBehaviour now take a `p2p.ID` + Getter methods return a copy of underlying stored behaviours + Add doc strings to public types and methods * [p2p] make structs public * [p2p] Test empty StoredPeerBehaviour * [p2p] typo fix * [p2p] add TestStoredPeerBehaviourConcurrency + Add a test which uses StoredPeerBehaviour in multiple goroutines to ensure thread-safety. * Update p2p/peer_behaviour.go Co-Authored-By: brapse <brapse@gmail.com> * Update p2p/peer_behaviour.go Co-Authored-By: brapse <brapse@gmail.com> * Update p2p/peer_behaviour.go Co-Authored-By: brapse <brapse@gmail.com> * Update p2p/peer_behaviour.go Co-Authored-By: brapse <brapse@gmail.com> * Update p2p/peer_behaviour.go Co-Authored-By: brapse <brapse@gmail.com> * Update p2p/peer_behaviour_test.go Co-Authored-By: brapse <brapse@gmail.com> * Update p2p/peer_behaviour.go Co-Authored-By: brapse <brapse@gmail.com> * Update p2p/peer_behaviour.go Co-Authored-By: brapse <brapse@gmail.com> * Update p2p/peer_behaviour.go Co-Authored-By: brapse <brapse@gmail.com> * Update p2p/peer_behaviour.go Co-Authored-By: brapse <brapse@gmail.com> * [p2p] field ordering convention * p2p: peer behaviour refactor + Change naming of reporting behaviour to `Report` + Remove the responsibility of distinguishing between the categories of good and bad behaviour and instead focus on reporting behaviour. * p2p: rename PeerReporter -> PeerBehaviourReporter
116 lines
2.9 KiB
Go
116 lines
2.9 KiB
Go
package p2p
|
|
|
|
import (
|
|
"net"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
// TestMockPeerBehaviour tests the MockPeerBehaviour' ability to store reported
|
|
// peer behaviour in memory indexed by the peerID
|
|
func TestMockPeerBehaviourReporter(t *testing.T) {
|
|
peer := newMockPeer(net.IP{127, 0, 0, 1})
|
|
pr := NewMockPeerBehaviourReporter()
|
|
|
|
behaviours := pr.GetBehaviours(peer.ID())
|
|
if len(behaviours) != 0 {
|
|
t.Errorf("Expected to have no behaviours reported")
|
|
}
|
|
|
|
pr.Report(peer.ID(), PeerBehaviourBadMessage)
|
|
behaviours = pr.GetBehaviours(peer.ID())
|
|
if len(behaviours) != 1 {
|
|
t.Errorf("Expected the peer have one reported behaviour")
|
|
}
|
|
|
|
if behaviours[0] != PeerBehaviourBadMessage {
|
|
t.Errorf("Expected PeerBehaviourBadMessage to have been reported")
|
|
}
|
|
}
|
|
|
|
type scriptedBehaviours struct {
|
|
PeerID ID
|
|
Behaviours []PeerBehaviour
|
|
}
|
|
|
|
type scriptItem struct {
|
|
PeerID ID
|
|
Behaviour PeerBehaviour
|
|
}
|
|
|
|
func equalBehaviours(a []PeerBehaviour, b []PeerBehaviour) bool {
|
|
if len(a) != len(b) {
|
|
return false
|
|
}
|
|
same := make([]PeerBehaviour, len(a))
|
|
|
|
for i, aBehaviour := range a {
|
|
for _, bBehaviour := range b {
|
|
if aBehaviour == bBehaviour {
|
|
same[i] = aBehaviour
|
|
}
|
|
}
|
|
}
|
|
|
|
return len(same) == len(a)
|
|
}
|
|
|
|
// TestPeerBehaviourConcurrency constructs a scenario in which
|
|
// multiple goroutines are using the same MockPeerBehaviourReporter instance.
|
|
// This test reproduces the conditions in which MockPeerBehaviourReporter will
|
|
// be used within a Reactor Receive method tests to ensure thread safety.
|
|
func TestMockPeerBehaviourReporterConcurrency(t *testing.T) {
|
|
behaviourScript := []scriptedBehaviours{
|
|
{"1", []PeerBehaviour{PeerBehaviourVote}},
|
|
{"2", []PeerBehaviour{PeerBehaviourVote, PeerBehaviourVote, PeerBehaviourVote, PeerBehaviourVote}},
|
|
{"3", []PeerBehaviour{PeerBehaviourBlockPart, PeerBehaviourVote, PeerBehaviourBlockPart, PeerBehaviourVote}},
|
|
{"4", []PeerBehaviour{PeerBehaviourVote, PeerBehaviourVote, PeerBehaviourVote, PeerBehaviourVote}},
|
|
{"5", []PeerBehaviour{PeerBehaviourBlockPart, PeerBehaviourVote, PeerBehaviourBlockPart, PeerBehaviourVote}},
|
|
}
|
|
|
|
var receiveWg sync.WaitGroup
|
|
pr := NewMockPeerBehaviourReporter()
|
|
scriptItems := make(chan scriptItem)
|
|
done := make(chan int)
|
|
numConsumers := 3
|
|
for i := 0; i < numConsumers; i++ {
|
|
receiveWg.Add(1)
|
|
go func() {
|
|
defer receiveWg.Done()
|
|
for {
|
|
select {
|
|
case pb := <-scriptItems:
|
|
pr.Report(pb.PeerID, pb.Behaviour)
|
|
case <-done:
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
var sendingWg sync.WaitGroup
|
|
sendingWg.Add(1)
|
|
go func() {
|
|
defer sendingWg.Done()
|
|
for _, item := range behaviourScript {
|
|
for _, reason := range item.Behaviours {
|
|
scriptItems <- scriptItem{item.PeerID, reason}
|
|
}
|
|
}
|
|
}()
|
|
|
|
sendingWg.Wait()
|
|
|
|
for i := 0; i < numConsumers; i++ {
|
|
done <- 1
|
|
}
|
|
|
|
receiveWg.Wait()
|
|
|
|
for _, items := range behaviourScript {
|
|
if !equalBehaviours(pr.GetBehaviours(items.PeerID), items.Behaviours) {
|
|
t.Errorf("Expected peer %s to have behaved \n", items.PeerID)
|
|
}
|
|
}
|
|
}
|