mirror of
https://github.com/tendermint/tendermint.git
synced 2026-02-07 04:20:44 +00:00
* Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
87 lines
2.2 KiB
Go
87 lines
2.2 KiB
Go
package behaviour
|
|
|
|
import (
|
|
"errors"
|
|
"sync"
|
|
|
|
"github.com/tendermint/tendermint/p2p"
|
|
)
|
|
|
|
// Reporter provides an interface for reactors to report the behaviour
|
|
// of peers synchronously to other components.
|
|
type Reporter interface {
|
|
Report(behaviour PeerBehaviour) error
|
|
}
|
|
|
|
// SwitchReporter reports peer behaviour to an internal Switch.
|
|
type SwitchReporter struct {
|
|
sw *p2p.Switch
|
|
}
|
|
|
|
// NewSwitchReporter return a new SwitchReporter instance which wraps the Switch.
|
|
func NewSwitcReporter(sw *p2p.Switch) *SwitchReporter {
|
|
return &SwitchReporter{
|
|
sw: sw,
|
|
}
|
|
}
|
|
|
|
// Report reports the behaviour of a peer to the Switch.
|
|
func (spbr *SwitchReporter) Report(behaviour PeerBehaviour) error {
|
|
peer := spbr.sw.Peers().Get(behaviour.peerID)
|
|
if peer == nil {
|
|
return errors.New("peer not found")
|
|
}
|
|
|
|
switch reason := behaviour.reason.(type) {
|
|
case consensusVote, blockPart:
|
|
spbr.sw.MarkPeerAsGood(peer)
|
|
case badMessage:
|
|
spbr.sw.StopPeerForError(peer, reason.explanation)
|
|
case messageOutOfOrder:
|
|
spbr.sw.StopPeerForError(peer, reason.explanation)
|
|
default:
|
|
return errors.New("unknown reason reported")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// MockReporter is a concrete implementation of the Reporter
|
|
// interface used in reactor tests to ensure reactors report the correct
|
|
// behaviour in manufactured scenarios.
|
|
type MockReporter struct {
|
|
mtx sync.RWMutex
|
|
pb map[p2p.ID][]PeerBehaviour
|
|
}
|
|
|
|
// NewMockReporter returns a Reporter which records all reported
|
|
// behaviours in memory.
|
|
func NewMockReporter() *MockReporter {
|
|
return &MockReporter{
|
|
pb: map[p2p.ID][]PeerBehaviour{},
|
|
}
|
|
}
|
|
|
|
// Report stores the PeerBehaviour produced by the peer identified by peerID.
|
|
func (mpbr *MockReporter) Report(behaviour PeerBehaviour) error {
|
|
mpbr.mtx.Lock()
|
|
defer mpbr.mtx.Unlock()
|
|
mpbr.pb[behaviour.peerID] = append(mpbr.pb[behaviour.peerID], behaviour)
|
|
|
|
return nil
|
|
}
|
|
|
|
// GetBehaviours returns all behaviours reported on the peer identified by peerID.
|
|
func (mpbr *MockReporter) GetBehaviours(peerID p2p.ID) []PeerBehaviour {
|
|
mpbr.mtx.RLock()
|
|
defer mpbr.mtx.RUnlock()
|
|
if items, ok := mpbr.pb[peerID]; ok {
|
|
result := make([]PeerBehaviour, len(items))
|
|
copy(result, items)
|
|
|
|
return result
|
|
}
|
|
|
|
return []PeerBehaviour{}
|
|
}
|