You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.2 KiB

lint: Enable Golint (#4212) * 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
5 years ago
  1. package behaviour
  2. import (
  3. "errors"
  4. "sync"
  5. "github.com/tendermint/tendermint/p2p"
  6. )
  7. // Reporter provides an interface for reactors to report the behaviour
  8. // of peers synchronously to other components.
  9. type Reporter interface {
  10. Report(behaviour PeerBehaviour) error
  11. }
  12. // SwitchReporter reports peer behaviour to an internal Switch.
  13. type SwitchReporter struct {
  14. sw *p2p.Switch
  15. }
  16. // NewSwitchReporter return a new SwitchReporter instance which wraps the Switch.
  17. func NewSwitcReporter(sw *p2p.Switch) *SwitchReporter {
  18. return &SwitchReporter{
  19. sw: sw,
  20. }
  21. }
  22. // Report reports the behaviour of a peer to the Switch.
  23. func (spbr *SwitchReporter) Report(behaviour PeerBehaviour) error {
  24. peer := spbr.sw.Peers().Get(behaviour.peerID)
  25. if peer == nil {
  26. return errors.New("peer not found")
  27. }
  28. switch reason := behaviour.reason.(type) {
  29. case consensusVote, blockPart:
  30. spbr.sw.MarkPeerAsGood(peer)
  31. case badMessage:
  32. spbr.sw.StopPeerForError(peer, reason.explanation)
  33. case messageOutOfOrder:
  34. spbr.sw.StopPeerForError(peer, reason.explanation)
  35. default:
  36. return errors.New("unknown reason reported")
  37. }
  38. return nil
  39. }
  40. // MockReporter is a concrete implementation of the Reporter
  41. // interface used in reactor tests to ensure reactors report the correct
  42. // behaviour in manufactured scenarios.
  43. type MockReporter struct {
  44. mtx sync.RWMutex
  45. pb map[p2p.ID][]PeerBehaviour
  46. }
  47. // NewMockReporter returns a Reporter which records all reported
  48. // behaviours in memory.
  49. func NewMockReporter() *MockReporter {
  50. return &MockReporter{
  51. pb: map[p2p.ID][]PeerBehaviour{},
  52. }
  53. }
  54. // Report stores the PeerBehaviour produced by the peer identified by peerID.
  55. func (mpbr *MockReporter) Report(behaviour PeerBehaviour) error {
  56. mpbr.mtx.Lock()
  57. defer mpbr.mtx.Unlock()
  58. mpbr.pb[behaviour.peerID] = append(mpbr.pb[behaviour.peerID], behaviour)
  59. return nil
  60. }
  61. // GetBehaviours returns all behaviours reported on the peer identified by peerID.
  62. func (mpbr *MockReporter) GetBehaviours(peerID p2p.ID) []PeerBehaviour {
  63. mpbr.mtx.RLock()
  64. defer mpbr.mtx.RUnlock()
  65. if items, ok := mpbr.pb[peerID]; ok {
  66. result := make([]PeerBehaviour, len(items))
  67. copy(result, items)
  68. return result
  69. }
  70. return []PeerBehaviour{}
  71. }