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

blockchain: add v2 reactor (#4361) The work includes the reactor which ties together all the seperate routines involved in the design of the blockchain v2 refactor. This PR replaces #4067 which got far too large and messy after a failed attempt to rebase. ## Commits: * Blockchainv 2 reactor: + I cleaner copy of the work done in #4067 which fell too far behind and was a nightmare to rebase. + The work includes the reactor which ties together all the seperate routines involved in the design of the blockchain v2 refactor. * fixes after merge * reorder iIO interface methodset * change iO -> IO * panic before send nil block * rename switchToConsensus -> trySwitchToConsensus * rename tdState -> tmState * Update blockchain/v2/reactor.go Co-Authored-By: Bot from GolangCI <42910462+golangcibot@users.noreply.github.com> * remove peer when it sends a block unsolicited * check for not ready in markReceived * fix error * fix the pcFinished event * typo fix * add documentation for processor fields * simplify time.Since * try and make the linter happy * some doc updates * fix channel diagram * Update adr-043-blockchain-riri-org.md * panic on nil switch * liting fixes * account for nil block in bBlockResponseMessage * panic on duplicate block enqueued by processor * linting * goimport reactor_test.go Co-authored-by: Bot from GolangCI <42910462+golangcibot@users.noreply.github.com> Co-authored-by: Anca Zamfir <ancazamfir@users.noreply.github.com> Co-authored-by: Marko <marbar3778@yahoo.com> Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
5 years ago
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 behavior
  2. import (
  3. "errors"
  4. tmsync "github.com/tendermint/tendermint/libs/sync"
  5. "github.com/tendermint/tendermint/p2p"
  6. )
  7. // Reporter provides an interface for reactors to report the behavior
  8. // of peers synchronously to other components.
  9. type Reporter interface {
  10. Report(behavior PeerBehavior) error
  11. }
  12. // SwitchReporter reports peer behavior 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 NewSwitchReporter(sw *p2p.Switch) *SwitchReporter {
  18. return &SwitchReporter{
  19. sw: sw,
  20. }
  21. }
  22. // Report reports the behavior of a peer to the Switch.
  23. func (spbr *SwitchReporter) Report(behavior PeerBehavior) error {
  24. peer := spbr.sw.Peers().Get(behavior.peerID)
  25. if peer == nil {
  26. return errors.New("peer not found")
  27. }
  28. switch reason := behavior.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. // behavior in manufactured scenarios.
  43. type MockReporter struct {
  44. mtx tmsync.RWMutex
  45. pb map[p2p.NodeID][]PeerBehavior
  46. }
  47. // NewMockReporter returns a Reporter which records all reported
  48. // behaviors in memory.
  49. func NewMockReporter() *MockReporter {
  50. return &MockReporter{
  51. pb: map[p2p.NodeID][]PeerBehavior{},
  52. }
  53. }
  54. // Report stores the PeerBehavior produced by the peer identified by peerID.
  55. func (mpbr *MockReporter) Report(behavior PeerBehavior) error {
  56. mpbr.mtx.Lock()
  57. defer mpbr.mtx.Unlock()
  58. mpbr.pb[behavior.peerID] = append(mpbr.pb[behavior.peerID], behavior)
  59. return nil
  60. }
  61. // GetBehaviors returns all behaviors reported on the peer identified by peerID.
  62. func (mpbr *MockReporter) GetBehaviors(peerID p2p.NodeID) []PeerBehavior {
  63. mpbr.mtx.RLock()
  64. defer mpbr.mtx.RUnlock()
  65. if items, ok := mpbr.pb[peerID]; ok {
  66. result := make([]PeerBehavior, len(items))
  67. copy(result, items)
  68. return result
  69. }
  70. return []PeerBehavior{}
  71. }