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.

1473 lines
42 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
10 years ago
new pubsub package comment out failing consensus tests for now rewrite rpc httpclient to use new pubsub package import pubsub as tmpubsub, query as tmquery make event IDs constants EventKey -> EventTypeKey rename EventsPubsub to PubSub mempool does not use pubsub rename eventsSub to pubsub new subscribe API fix channel size issues and consensus tests bugs refactor rpc client add missing discardFromChan method add mutex rename pubsub to eventBus remove IsRunning from WSRPCConnection interface (not needed) add a comment in broadcastNewRoundStepsAndVotes rename registerEventCallbacks to broadcastNewRoundStepsAndVotes See https://dave.cheney.net/2014/03/19/channel-axioms stop eventBuses after reactor tests remove unnecessary Unsubscribe return subscribe helper function move discardFromChan to where it is used subscribe now returns an err this gives us ability to refuse to subscribe if pubsub is at its max capacity. use context for control overflow cache queries handle err when subscribing in replay_test rename testClientID to testSubscriber extract var set channel buffer capacity to 1 in replay_file fix byzantine_test unsubscribe from single event, not all events refactor httpclient to return events to appropriate channels return failing testReplayCrashBeforeWriteVote test fix TestValidatorSetChanges refactor code a bit fix testReplayCrashBeforeWriteVote add comment fix TestValidatorSetChanges fixes from Bucky's review update comment [ci skip] test TxEventBuffer update changelog fix TestValidatorSetChanges (2nd attempt) only do wg.Done when no errors benchmark event bus create pubsub server inside NewEventBus only expose config params (later if needed) set buffer capacity to 0 so we are not testing cache new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ} This should allow to subscribe to all transactions! or a specific one using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'" use TimeoutCommit instead of afterPublishEventNewBlockTimeout TimeoutCommit is the time a node waits after committing a block, before it goes into the next height. So it will finish everything from the last block, but then wait a bit. The idea is this gives it time to hear more votes from other validators, to strengthen the commit it includes in the next block. But it also gives it time to hear about new transactions. waitForBlockWithUpdatedVals rewrite WAL crash tests Task: test that we can recover from any WAL crash. Solution: the old tests were relying on event hub being run in the same thread (we were injecting the private validator's last signature). when considering a rewrite, we considered two possible solutions: write a "fuzzy" testing system where WAL is crashing upon receiving a new message, or inject failures and trigger them in tests using something like https://github.com/coreos/gofail. remove sleep no cs.Lock around wal.Save test different cases (empty block, non-empty block, ...) comments add comments test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks fixes as per Bucky's last review reset subscriptions on UnsubscribeAll use a simple counter to track message for which we panicked also, set a smaller part size for all test cases
7 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
add support for block pruning via ABCI Commit response (#4588) * Added BlockStore.DeleteBlock() * Added initial block pruner prototype * wip * Added BlockStore.PruneBlocks() * Added consensus setting for block pruning * Added BlockStore base * Error on replay if base does not have blocks * Handle missing blocks when sending VoteSetMaj23Message * Error message tweak * Properly update blockstore state * Error message fix again * blockchain: ignore peer missing blocks * Added FIXME * Added test for block replay with truncated history * Handle peer base in blockchain reactor * Improved replay error handling * Added tests for Store.PruneBlocks() * Fix non-RPC handling of truncated block history * Panic on missing block meta in needProofBlock() * Updated changelog * Handle truncated block history in RPC layer * Added info about earliest block in /status RPC * Reorder height and base in blockchain reactor messages * Updated changelog * Fix tests * Appease linter * Minor review fixes * Non-empty BlockStores should always have base > 0 * Update code to assume base > 0 invariant * Added blockstore tests for pruning to 0 * Make sure we don't prune below the current base * Added BlockStore.Size() * config: added retain_blocks recommendations * Update v1 blockchain reactor to handle blockstore base * Added state database pruning * Propagate errors on missing validator sets * Comment tweaks * Improved error message Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * use ABCI field ResponseCommit.retain_height instead of retain-blocks config option * remove State.RetainHeight, return value instead * fix minor issues * rename pruneHeights() to pruneBlocks() * noop to fix GitHub borkage Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
4 years ago
fix data race Closes #1442 ``` WARNING: DATA RACE Write at 0x00c4209de7c8 by goroutine 23: github.com/tendermint/tendermint/types.(*Block).fillHeader() /home/vagrant/go/src/github.com/tendermint/tendermint/types/block.go:88 +0x157 github.com/tendermint/tendermint/types.(*Block).Hash() /home/vagrant/go/src/github.com/tendermint/tendermint/types/block.go:104 +0x121 github.com/tendermint/tendermint/types.(*Block).HashesTo() /home/vagrant/go/src/github.com/tendermint/tendermint/types/block.go:135 +0x4f github.com/tendermint/tendermint/consensus.(*ConsensusState).enterPrecommit() /home/vagrant/go/src/github.com/tendermint/tendermint/consensus/state.go:1037 +0x182d github.com/tendermint/tendermint/consensus.(*ConsensusState).addVote() /home/vagrant/go/src/github.com/tendermint/tendermint/consensus/state.go:1425 +0x1a6c github.com/tendermint/tendermint/consensus.(*ConsensusState).tryAddVote() /home/vagrant/go/src/github.com/tendermint/tendermint/consensus/state.go:1318 +0x77 github.com/tendermint/tendermint/consensus.(*ConsensusState).handleMsg() /home/vagrant/go/src/github.com/tendermint/tendermint/consensus/state.go:581 +0x7a9 github.com/tendermint/tendermint/consensus.(*ConsensusState).receiveRoutine() /home/vagrant/go/src/github.com/tendermint/tendermint/consensus/state.go:539 +0x6c3 Previous read at 0x00c4209de7c8 by goroutine 47: github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common.(*HexBytes).MarshalJSON() <autogenerated>:1 +0x52 github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.invokeMarshalJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:433 +0x88 github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec)._encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:82 +0x8d2 github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:50 +0x10e github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSONStruct() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:348 +0x539 github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec)._encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:119 +0x83f github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:50 +0x10e github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSONStruct() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:348 +0x539 github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec)._encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:119 +0x83f github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:50 +0x10e github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSONStruct() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:348 +0x539 github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec)._encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:119 +0x83f github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:50 +0x10e github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSONStruct() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:348 +0x539 github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec)._encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:119 +0x83f github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:50 +0x10e github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).MarshalJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/amino.go:296 +0x182 github.com/tendermint/tendermint/rpc/lib/types.NewRPCSuccessResponse() /home/vagrant/go/src/github.com/tendermint/tendermint/rpc/lib/types/types.go:100 +0x12c github.com/tendermint/tendermint/rpc/lib/server.makeJSONRPCHandler.func1() /home/vagrant/go/src/github.com/tendermint/tendermint/rpc/lib/server/handlers.go:152 +0xab7 net/http.HandlerFunc.ServeHTTP() /usr/lib/go-1.9/src/net/http/server.go:1918 +0x51 net/http.(*ServeMux).ServeHTTP() /usr/lib/go-1.9/src/net/http/server.go:2254 +0xa2 github.com/tendermint/tendermint/rpc/lib/server.RecoverAndLogHandler.func1() /home/vagrant/go/src/github.com/tendermint/tendermint/rpc/lib/server/http_server.go:138 +0x4fa net/http.HandlerFunc.ServeHTTP() /usr/lib/go-1.9/src/net/http/server.go:1918 +0x51 net/http.serverHandler.ServeHTTP() /usr/lib/go-1.9/src/net/http/server.go:2619 +0xbc net/http.(*conn).serve() /usr/lib/go-1.9/src/net/http/server.go:1801 +0x83b Goroutine 23 (running) created at: github.com/tendermint/tendermint/consensus.(*ConsensusState).OnStart() /home/vagrant/go/src/github.com/tendermint/tendermint/consensus/state.go:250 +0x35b github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common.(*BaseService).Start() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common/service.go:130 +0x5fc github.com/tendermint/tendermint/consensus.(*ConsensusReactor).OnStart() /home/vagrant/go/src/github.com/tendermint/tendermint/consensus/reactor.go:69 +0x1b4 github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common.(*BaseService).Start() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common/service.go:130 +0x5fc github.com/tendermint/tendermint/consensus.(*ConsensusReactor).Start() <autogenerated>:1 +0x43 github.com/tendermint/tendermint/p2p.(*Switch).OnStart() /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch.go:177 +0x124 github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common.(*BaseService).Start() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common/service.go:130 +0x5fc github.com/tendermint/tendermint/node.(*Node).OnStart() /home/vagrant/go/src/github.com/tendermint/tendermint/node/node.go:416 +0xa1b github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common.(*BaseService).Start() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common/service.go:130 +0x5fc github.com/tendermint/tendermint/rpc/test.StartTendermint() /home/vagrant/go/src/github.com/tendermint/tendermint/rpc/test/helpers.go:100 +0x5b github.com/tendermint/tendermint/rpc/client_test.TestMain() /home/vagrant/go/src/github.com/tendermint/tendermint/rpc/client/main_test.go:17 +0x4c main.main() github.com/tendermint/tendermint/rpc/client/_test/_testmain.go:76 +0x1cd Goroutine 47 (running) created at: net/http.(*Server).Serve() /usr/lib/go-1.9/src/net/http/server.go:2720 +0x37c net/http.Serve() /usr/lib/go-1.9/src/net/http/server.go:2323 +0xe2 github.com/tendermint/tendermint/rpc/lib/server.StartHTTPServer.func1() /home/vagrant/go/src/github.com/tendermint/tendermint/rpc/lib/server/http_server.go:35 +0xb3 ```
6 years ago
protect Record* peerStateStats functions by mutex Fixes #1414 DATA RACE: ``` Read at 0x00c4214ee940 by goroutine 146: github.com/tendermint/tendermint/consensus.(*peerStateStats).String() <autogenerated>:1 +0x57 fmt.(*pp).handleMethods() /usr/local/go/src/fmt/print.go:596 +0x3f4 fmt.(*pp).printArg() /usr/local/go/src/fmt/print.go:679 +0x11f fmt.(*pp).doPrintf() /usr/local/go/src/fmt/print.go:996 +0x319 fmt.Sprintf() /usr/local/go/src/fmt/print.go:196 +0x73 github.com/tendermint/tendermint/consensus.(*PeerState).StringIndented() github.com/tendermint/tendermint/consensus/_test/_obj_test/reactor.go:1426 +0x573 github.com/tendermint/tendermint/consensus.(*PeerState).String() github.com/tendermint/tendermint/consensus/_test/_obj_test/reactor.go:1419 +0x66 github.com/go-logfmt/logfmt.safeString() /home/ubuntu/go/src/github.com/go-logfmt/logfmt/encode.go:299 +0x9d github.com/go-logfmt/logfmt.writeValue() /home/ubuntu/go/src/github.com/go-logfmt/logfmt/encode.go:217 +0x5a0 github.com/go-logfmt/logfmt.(*Encoder).EncodeKeyval() /home/ubuntu/go/src/github.com/go-logfmt/logfmt/encode.go:61 +0x1dd github.com/tendermint/tmlibs/log.tmfmtLogger.Log() /home/ubuntu/go/src/github.com/tendermint/tmlibs/log/tmfmt_logger.go:107 +0x1001 github.com/tendermint/tmlibs/log.(*tmfmtLogger).Log() <autogenerated>:1 +0x93 github.com/go-kit/kit/log.(*context).Log() /home/ubuntu/go/src/github.com/go-kit/kit/log/log.go:124 +0x248 github.com/tendermint/tmlibs/log.(*tmLogger).Debug() /home/ubuntu/go/src/github.com/tendermint/tmlibs/log/tm_logger.go:64 +0x1d0 github.com/tendermint/tendermint/consensus.(*PeerState).PickSendVote() github.com/tendermint/tendermint/consensus/_test/_obj_test/reactor.go:1059 +0x242 github.com/tendermint/tendermint/consensus.(*ConsensusReactor).gossipVotesForHeight() github.com/tendermint/tendermint/consensus/_test/_obj_test/reactor.go:789 +0x6ef github.com/tendermint/tendermint/consensus.(*ConsensusReactor).gossipVotesRoutine() github.com/tendermint/tendermint/consensus/_test/_obj_test/reactor.go:723 +0x1039 Previous write at 0x00c4214ee940 by goroutine 21: github.com/tendermint/tendermint/consensus.(*PeerState).RecordVote() github.com/tendermint/tendermint/consensus/_test/_obj_test/reactor.go:1242 +0x15a github.com/tendermint/tendermint/consensus.(*ConsensusReactor).Receive() github.com/tendermint/tendermint/consensus/_test/_obj_test/reactor.go:309 +0x32e6 github.com/tendermint/tendermint/p2p.createMConnection.func1() /home/ubuntu/go/src/github.com/tendermint/tendermint/p2p/peer.go:365 +0xea github.com/tendermint/tendermint/p2p/conn.(*MConnection).recvRoutine() /home/ubuntu/go/src/github.com/tendermint/tendermint/p2p/conn/connection.go:531 +0x779 ```
6 years ago
10 years ago
10 years ago
10 years ago
  1. package consensus
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "runtime/debug"
  7. "sync"
  8. "time"
  9. cstypes "github.com/tendermint/tendermint/internal/consensus/types"
  10. "github.com/tendermint/tendermint/internal/eventbus"
  11. "github.com/tendermint/tendermint/internal/p2p"
  12. sm "github.com/tendermint/tendermint/internal/state"
  13. "github.com/tendermint/tendermint/libs/bits"
  14. tmevents "github.com/tendermint/tendermint/libs/events"
  15. "github.com/tendermint/tendermint/libs/log"
  16. "github.com/tendermint/tendermint/libs/service"
  17. tmtime "github.com/tendermint/tendermint/libs/time"
  18. tmcons "github.com/tendermint/tendermint/proto/tendermint/consensus"
  19. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  20. "github.com/tendermint/tendermint/types"
  21. )
  22. var (
  23. _ service.Service = (*Reactor)(nil)
  24. _ p2p.Wrapper = (*tmcons.Message)(nil)
  25. )
  26. // GetChannelDescriptor produces an instance of a descriptor for this
  27. // package's required channels.
  28. func getChannelDescriptors() map[p2p.ChannelID]*p2p.ChannelDescriptor {
  29. return map[p2p.ChannelID]*p2p.ChannelDescriptor{
  30. StateChannel: {
  31. ID: StateChannel,
  32. MessageType: new(tmcons.Message),
  33. Priority: 8,
  34. SendQueueCapacity: 64,
  35. RecvMessageCapacity: maxMsgSize,
  36. RecvBufferCapacity: 128,
  37. },
  38. DataChannel: {
  39. // TODO: Consider a split between gossiping current block and catchup
  40. // stuff. Once we gossip the whole block there is nothing left to send
  41. // until next height or round.
  42. ID: DataChannel,
  43. MessageType: new(tmcons.Message),
  44. Priority: 12,
  45. SendQueueCapacity: 64,
  46. RecvBufferCapacity: 512,
  47. RecvMessageCapacity: maxMsgSize,
  48. },
  49. VoteChannel: {
  50. ID: VoteChannel,
  51. MessageType: new(tmcons.Message),
  52. Priority: 10,
  53. SendQueueCapacity: 64,
  54. RecvBufferCapacity: 128,
  55. RecvMessageCapacity: maxMsgSize,
  56. },
  57. VoteSetBitsChannel: {
  58. ID: VoteSetBitsChannel,
  59. MessageType: new(tmcons.Message),
  60. Priority: 5,
  61. SendQueueCapacity: 8,
  62. RecvBufferCapacity: 128,
  63. RecvMessageCapacity: maxMsgSize,
  64. },
  65. }
  66. }
  67. const (
  68. StateChannel = p2p.ChannelID(0x20)
  69. DataChannel = p2p.ChannelID(0x21)
  70. VoteChannel = p2p.ChannelID(0x22)
  71. VoteSetBitsChannel = p2p.ChannelID(0x23)
  72. maxMsgSize = 1048576 // 1MB; NOTE: keep in sync with types.PartSet sizes.
  73. blocksToContributeToBecomeGoodPeer = 10000
  74. votesToContributeToBecomeGoodPeer = 10000
  75. listenerIDConsensus = "consensus-reactor"
  76. )
  77. // NOTE: Temporary interface for switching to block sync, we should get rid of v0.
  78. // See: https://github.com/tendermint/tendermint/issues/4595
  79. type BlockSyncReactor interface {
  80. SwitchToBlockSync(context.Context, sm.State) error
  81. GetMaxPeerBlockHeight() int64
  82. // GetTotalSyncedTime returns the time duration since the blocksync starting.
  83. GetTotalSyncedTime() time.Duration
  84. // GetRemainingSyncTime returns the estimating time the node will be fully synced,
  85. // if will return 0 if the blocksync does not perform or the number of block synced is
  86. // too small (less than 100).
  87. GetRemainingSyncTime() time.Duration
  88. }
  89. //go:generate ../../scripts/mockery_generate.sh ConsSyncReactor
  90. // ConsSyncReactor defines an interface used for testing abilities of node.startStateSync.
  91. type ConsSyncReactor interface {
  92. SwitchToConsensus(sm.State, bool)
  93. SetStateSyncingMetrics(float64)
  94. SetBlockSyncingMetrics(float64)
  95. }
  96. // Reactor defines a reactor for the consensus service.
  97. type Reactor struct {
  98. service.BaseService
  99. logger log.Logger
  100. state *State
  101. eventBus *eventbus.EventBus
  102. Metrics *Metrics
  103. mtx sync.RWMutex
  104. peers map[types.NodeID]*PeerState
  105. waitSync bool
  106. readySignal chan struct{} // closed when the node is ready to start consensus
  107. stateCh *p2p.Channel
  108. dataCh *p2p.Channel
  109. voteCh *p2p.Channel
  110. voteSetBitsCh *p2p.Channel
  111. peerUpdates *p2p.PeerUpdates
  112. }
  113. // NewReactor returns a reference to a new consensus reactor, which implements
  114. // the service.Service interface. It accepts a logger, consensus state, references
  115. // to relevant p2p Channels and a channel to listen for peer updates on. The
  116. // reactor will close all p2p Channels when stopping.
  117. func NewReactor(
  118. ctx context.Context,
  119. logger log.Logger,
  120. cs *State,
  121. channelCreator p2p.ChannelCreator,
  122. peerUpdates *p2p.PeerUpdates,
  123. waitSync bool,
  124. metrics *Metrics,
  125. ) (*Reactor, error) {
  126. chans := getChannelDescriptors()
  127. stateCh, err := channelCreator(ctx, chans[StateChannel])
  128. if err != nil {
  129. return nil, err
  130. }
  131. dataCh, err := channelCreator(ctx, chans[DataChannel])
  132. if err != nil {
  133. return nil, err
  134. }
  135. voteCh, err := channelCreator(ctx, chans[VoteChannel])
  136. if err != nil {
  137. return nil, err
  138. }
  139. voteSetBitsCh, err := channelCreator(ctx, chans[VoteSetBitsChannel])
  140. if err != nil {
  141. return nil, err
  142. }
  143. r := &Reactor{
  144. logger: logger,
  145. state: cs,
  146. waitSync: waitSync,
  147. peers: make(map[types.NodeID]*PeerState),
  148. Metrics: metrics,
  149. stateCh: stateCh,
  150. dataCh: dataCh,
  151. voteCh: voteCh,
  152. voteSetBitsCh: voteSetBitsCh,
  153. peerUpdates: peerUpdates,
  154. readySignal: make(chan struct{}),
  155. }
  156. r.BaseService = *service.NewBaseService(logger, "Consensus", r)
  157. if !r.waitSync {
  158. close(r.readySignal)
  159. }
  160. return r, nil
  161. }
  162. // OnStart starts separate go routines for each p2p Channel and listens for
  163. // envelopes on each. In addition, it also listens for peer updates and handles
  164. // messages on that p2p channel accordingly. The caller must be sure to execute
  165. // OnStop to ensure the outbound p2p Channels are closed.
  166. func (r *Reactor) OnStart(ctx context.Context) error {
  167. r.logger.Debug("consensus wait sync", "wait_sync", r.WaitSync())
  168. // start routine that computes peer statistics for evaluating peer quality
  169. //
  170. // TODO: Evaluate if we need this to be synchronized via WaitGroup as to not
  171. // leak the goroutine when stopping the reactor.
  172. go r.peerStatsRoutine(ctx)
  173. r.subscribeToBroadcastEvents()
  174. if !r.WaitSync() {
  175. if err := r.state.Start(ctx); err != nil {
  176. return err
  177. }
  178. }
  179. go r.processStateCh(ctx)
  180. go r.processDataCh(ctx)
  181. go r.processVoteCh(ctx)
  182. go r.processVoteSetBitsCh(ctx)
  183. go r.processPeerUpdates(ctx)
  184. return nil
  185. }
  186. // OnStop stops the reactor by signaling to all spawned goroutines to exit and
  187. // blocking until they all exit, as well as unsubscribing from events and stopping
  188. // state.
  189. func (r *Reactor) OnStop() {
  190. r.unsubscribeFromBroadcastEvents()
  191. r.state.Stop()
  192. if !r.WaitSync() {
  193. r.state.Wait()
  194. }
  195. }
  196. // SetEventBus sets the reactor's event bus.
  197. func (r *Reactor) SetEventBus(b *eventbus.EventBus) {
  198. r.eventBus = b
  199. r.state.SetEventBus(b)
  200. }
  201. // WaitSync returns whether the consensus reactor is waiting for state/block sync.
  202. func (r *Reactor) WaitSync() bool {
  203. r.mtx.RLock()
  204. defer r.mtx.RUnlock()
  205. return r.waitSync
  206. }
  207. // SwitchToConsensus switches from block-sync mode to consensus mode. It resets
  208. // the state, turns off block-sync, and starts the consensus state-machine.
  209. func (r *Reactor) SwitchToConsensus(ctx context.Context, state sm.State, skipWAL bool) {
  210. r.logger.Info("switching to consensus")
  211. // we have no votes, so reconstruct LastCommit from SeenCommit
  212. if state.LastBlockHeight > 0 {
  213. r.state.reconstructLastCommit(state)
  214. }
  215. // NOTE: The line below causes broadcastNewRoundStepRoutine() to broadcast a
  216. // NewRoundStepMessage.
  217. r.state.updateToState(ctx, state)
  218. r.mtx.Lock()
  219. r.waitSync = false
  220. close(r.readySignal)
  221. r.mtx.Unlock()
  222. r.Metrics.BlockSyncing.Set(0)
  223. r.Metrics.StateSyncing.Set(0)
  224. if skipWAL {
  225. r.state.doWALCatchup = false
  226. }
  227. if err := r.state.Start(ctx); err != nil {
  228. panic(fmt.Sprintf(`failed to start consensus state: %v
  229. conS:
  230. %+v
  231. conR:
  232. %+v`, err, r.state, r))
  233. }
  234. d := types.EventDataBlockSyncStatus{Complete: true, Height: state.LastBlockHeight}
  235. if err := r.eventBus.PublishEventBlockSyncStatus(ctx, d); err != nil {
  236. r.logger.Error("failed to emit the blocksync complete event", "err", err)
  237. }
  238. }
  239. // String returns a string representation of the Reactor.
  240. //
  241. // NOTE: For now, it is just a hard-coded string to avoid accessing unprotected
  242. // shared variables.
  243. //
  244. // TODO: improve!
  245. func (r *Reactor) String() string {
  246. return "ConsensusReactor"
  247. }
  248. // StringIndented returns an indented string representation of the Reactor.
  249. func (r *Reactor) StringIndented(indent string) string {
  250. r.mtx.RLock()
  251. defer r.mtx.RUnlock()
  252. s := "ConsensusReactor{\n"
  253. s += indent + " " + r.state.StringIndented(indent+" ") + "\n"
  254. for _, ps := range r.peers {
  255. s += indent + " " + ps.StringIndented(indent+" ") + "\n"
  256. }
  257. s += indent + "}"
  258. return s
  259. }
  260. // GetPeerState returns PeerState for a given NodeID.
  261. func (r *Reactor) GetPeerState(peerID types.NodeID) (*PeerState, bool) {
  262. r.mtx.RLock()
  263. defer r.mtx.RUnlock()
  264. ps, ok := r.peers[peerID]
  265. return ps, ok
  266. }
  267. func (r *Reactor) broadcastNewRoundStepMessage(ctx context.Context, rs *cstypes.RoundState) error {
  268. return r.stateCh.Send(ctx, p2p.Envelope{
  269. Broadcast: true,
  270. Message: makeRoundStepMessage(rs),
  271. })
  272. }
  273. func (r *Reactor) broadcastNewValidBlockMessage(ctx context.Context, rs *cstypes.RoundState) error {
  274. psHeader := rs.ProposalBlockParts.Header()
  275. return r.stateCh.Send(ctx, p2p.Envelope{
  276. Broadcast: true,
  277. Message: &tmcons.NewValidBlock{
  278. Height: rs.Height,
  279. Round: rs.Round,
  280. BlockPartSetHeader: psHeader.ToProto(),
  281. BlockParts: rs.ProposalBlockParts.BitArray().ToProto(),
  282. IsCommit: rs.Step == cstypes.RoundStepCommit,
  283. },
  284. })
  285. }
  286. func (r *Reactor) broadcastHasVoteMessage(ctx context.Context, vote *types.Vote) error {
  287. return r.stateCh.Send(ctx, p2p.Envelope{
  288. Broadcast: true,
  289. Message: &tmcons.HasVote{
  290. Height: vote.Height,
  291. Round: vote.Round,
  292. Type: vote.Type,
  293. Index: vote.ValidatorIndex,
  294. },
  295. })
  296. }
  297. // subscribeToBroadcastEvents subscribes for new round steps and votes using the
  298. // internal pubsub defined in the consensus state to broadcast them to peers
  299. // upon receiving.
  300. func (r *Reactor) subscribeToBroadcastEvents() {
  301. err := r.state.evsw.AddListenerForEvent(
  302. listenerIDConsensus,
  303. types.EventNewRoundStepValue,
  304. func(ctx context.Context, data tmevents.EventData) error {
  305. if err := r.broadcastNewRoundStepMessage(ctx, data.(*cstypes.RoundState)); err != nil {
  306. return err
  307. }
  308. select {
  309. case r.state.onStopCh <- data.(*cstypes.RoundState):
  310. return nil
  311. case <-ctx.Done():
  312. return ctx.Err()
  313. default:
  314. return nil
  315. }
  316. },
  317. )
  318. if err != nil {
  319. r.logger.Error("failed to add listener for events", "err", err)
  320. }
  321. err = r.state.evsw.AddListenerForEvent(
  322. listenerIDConsensus,
  323. types.EventValidBlockValue,
  324. func(ctx context.Context, data tmevents.EventData) error {
  325. return r.broadcastNewValidBlockMessage(ctx, data.(*cstypes.RoundState))
  326. },
  327. )
  328. if err != nil {
  329. r.logger.Error("failed to add listener for events", "err", err)
  330. }
  331. err = r.state.evsw.AddListenerForEvent(
  332. listenerIDConsensus,
  333. types.EventVoteValue,
  334. func(ctx context.Context, data tmevents.EventData) error {
  335. return r.broadcastHasVoteMessage(ctx, data.(*types.Vote))
  336. },
  337. )
  338. if err != nil {
  339. r.logger.Error("failed to add listener for events", "err", err)
  340. }
  341. }
  342. func (r *Reactor) unsubscribeFromBroadcastEvents() {
  343. r.state.evsw.RemoveListener(listenerIDConsensus)
  344. }
  345. func makeRoundStepMessage(rs *cstypes.RoundState) *tmcons.NewRoundStep {
  346. return &tmcons.NewRoundStep{
  347. Height: rs.Height,
  348. Round: rs.Round,
  349. Step: uint32(rs.Step),
  350. SecondsSinceStartTime: int64(time.Since(rs.StartTime).Seconds()),
  351. LastCommitRound: rs.LastCommit.GetRound(),
  352. }
  353. }
  354. func (r *Reactor) sendNewRoundStepMessage(ctx context.Context, peerID types.NodeID) error {
  355. return r.stateCh.Send(ctx, p2p.Envelope{
  356. To: peerID,
  357. Message: makeRoundStepMessage(r.state.GetRoundState()),
  358. })
  359. }
  360. func (r *Reactor) gossipDataForCatchup(ctx context.Context, rs *cstypes.RoundState, prs *cstypes.PeerRoundState, ps *PeerState) {
  361. logger := r.logger.With("height", prs.Height).With("peer", ps.peerID)
  362. if index, ok := prs.ProposalBlockParts.Not().PickRandom(); ok {
  363. // ensure that the peer's PartSetHeader is correct
  364. blockMeta := r.state.blockStore.LoadBlockMeta(prs.Height)
  365. if blockMeta == nil {
  366. logger.Error(
  367. "failed to load block meta",
  368. "our_height", rs.Height,
  369. "blockstore_base", r.state.blockStore.Base(),
  370. "blockstore_height", r.state.blockStore.Height(),
  371. )
  372. time.Sleep(r.state.config.PeerGossipSleepDuration)
  373. return
  374. } else if !blockMeta.BlockID.PartSetHeader.Equals(prs.ProposalBlockPartSetHeader) {
  375. logger.Info(
  376. "peer ProposalBlockPartSetHeader mismatch; sleeping",
  377. "block_part_set_header", blockMeta.BlockID.PartSetHeader,
  378. "peer_block_part_set_header", prs.ProposalBlockPartSetHeader,
  379. )
  380. time.Sleep(r.state.config.PeerGossipSleepDuration)
  381. return
  382. }
  383. part := r.state.blockStore.LoadBlockPart(prs.Height, index)
  384. if part == nil {
  385. logger.Error(
  386. "failed to load block part",
  387. "index", index,
  388. "block_part_set_header", blockMeta.BlockID.PartSetHeader,
  389. "peer_block_part_set_header", prs.ProposalBlockPartSetHeader,
  390. )
  391. time.Sleep(r.state.config.PeerGossipSleepDuration)
  392. return
  393. }
  394. partProto, err := part.ToProto()
  395. if err != nil {
  396. logger.Error("failed to convert block part to proto", "err", err)
  397. time.Sleep(r.state.config.PeerGossipSleepDuration)
  398. return
  399. }
  400. logger.Debug("sending block part for catchup", "round", prs.Round, "index", index)
  401. _ = r.dataCh.Send(ctx, p2p.Envelope{
  402. To: ps.peerID,
  403. Message: &tmcons.BlockPart{
  404. Height: prs.Height, // not our height, so it does not matter.
  405. Round: prs.Round, // not our height, so it does not matter
  406. Part: *partProto,
  407. },
  408. })
  409. return
  410. }
  411. time.Sleep(r.state.config.PeerGossipSleepDuration)
  412. }
  413. func (r *Reactor) gossipDataRoutine(ctx context.Context, ps *PeerState) {
  414. logger := r.logger.With("peer", ps.peerID)
  415. timer := time.NewTimer(0)
  416. defer timer.Stop()
  417. OUTER_LOOP:
  418. for {
  419. if !r.IsRunning() {
  420. return
  421. }
  422. select {
  423. case <-ctx.Done():
  424. return
  425. default:
  426. }
  427. rs := r.state.GetRoundState()
  428. prs := ps.GetRoundState()
  429. // Send proposal Block parts?
  430. if rs.ProposalBlockParts.HasHeader(prs.ProposalBlockPartSetHeader) {
  431. if index, ok := rs.ProposalBlockParts.BitArray().Sub(prs.ProposalBlockParts.Copy()).PickRandom(); ok {
  432. part := rs.ProposalBlockParts.GetPart(index)
  433. partProto, err := part.ToProto()
  434. if err != nil {
  435. logger.Error("failed to convert block part to proto", "err", err)
  436. return
  437. }
  438. logger.Debug("sending block part", "height", prs.Height, "round", prs.Round)
  439. if err := r.dataCh.Send(ctx, p2p.Envelope{
  440. To: ps.peerID,
  441. Message: &tmcons.BlockPart{
  442. Height: rs.Height, // this tells peer that this part applies to us
  443. Round: rs.Round, // this tells peer that this part applies to us
  444. Part: *partProto,
  445. },
  446. }); err != nil {
  447. return
  448. }
  449. ps.SetHasProposalBlockPart(prs.Height, prs.Round, index)
  450. continue OUTER_LOOP
  451. }
  452. }
  453. // if the peer is on a previous height that we have, help catch up
  454. blockStoreBase := r.state.blockStore.Base()
  455. if blockStoreBase > 0 && 0 < prs.Height && prs.Height < rs.Height && prs.Height >= blockStoreBase {
  456. heightLogger := logger.With("height", prs.Height)
  457. // If we never received the commit message from the peer, the block parts
  458. // will not be initialized.
  459. if prs.ProposalBlockParts == nil {
  460. blockMeta := r.state.blockStore.LoadBlockMeta(prs.Height)
  461. if blockMeta == nil {
  462. heightLogger.Error(
  463. "failed to load block meta",
  464. "blockstoreBase", blockStoreBase,
  465. "blockstoreHeight", r.state.blockStore.Height(),
  466. )
  467. timer.Reset(r.state.config.PeerGossipSleepDuration)
  468. select {
  469. case <-timer.C:
  470. case <-ctx.Done():
  471. return
  472. }
  473. } else {
  474. ps.InitProposalBlockParts(blockMeta.BlockID.PartSetHeader)
  475. }
  476. // Continue the loop since prs is a copy and not effected by this
  477. // initialization.
  478. continue OUTER_LOOP
  479. }
  480. r.gossipDataForCatchup(ctx, rs, prs, ps)
  481. continue OUTER_LOOP
  482. }
  483. // if height and round don't match, sleep
  484. if (rs.Height != prs.Height) || (rs.Round != prs.Round) {
  485. timer.Reset(r.state.config.PeerGossipSleepDuration)
  486. select {
  487. case <-timer.C:
  488. case <-ctx.Done():
  489. return
  490. }
  491. continue OUTER_LOOP
  492. }
  493. // By here, height and round match.
  494. // Proposal block parts were already matched and sent if any were wanted.
  495. // (These can match on hash so the round doesn't matter)
  496. // Now consider sending other things, like the Proposal itself.
  497. // Send Proposal && ProposalPOL BitArray?
  498. if rs.Proposal != nil && !prs.Proposal {
  499. // Proposal: share the proposal metadata with peer.
  500. {
  501. propProto := rs.Proposal.ToProto()
  502. logger.Debug("sending proposal", "height", prs.Height, "round", prs.Round)
  503. if err := r.dataCh.Send(ctx, p2p.Envelope{
  504. To: ps.peerID,
  505. Message: &tmcons.Proposal{
  506. Proposal: *propProto,
  507. },
  508. }); err != nil {
  509. return
  510. }
  511. // NOTE: A peer might have received a different proposal message, so
  512. // this Proposal msg will be rejected!
  513. ps.SetHasProposal(rs.Proposal)
  514. }
  515. // ProposalPOL: lets peer know which POL votes we have so far. The peer
  516. // must receive ProposalMessage first. Note, rs.Proposal was validated,
  517. // so rs.Proposal.POLRound <= rs.Round, so we definitely have
  518. // rs.Votes.Prevotes(rs.Proposal.POLRound).
  519. if 0 <= rs.Proposal.POLRound {
  520. pPol := rs.Votes.Prevotes(rs.Proposal.POLRound).BitArray()
  521. pPolProto := pPol.ToProto()
  522. logger.Debug("sending POL", "height", prs.Height, "round", prs.Round)
  523. if err := r.dataCh.Send(ctx, p2p.Envelope{
  524. To: ps.peerID,
  525. Message: &tmcons.ProposalPOL{
  526. Height: rs.Height,
  527. ProposalPolRound: rs.Proposal.POLRound,
  528. ProposalPol: *pPolProto,
  529. },
  530. }); err != nil {
  531. return
  532. }
  533. }
  534. continue OUTER_LOOP
  535. }
  536. // nothing to do -- sleep
  537. timer.Reset(r.state.config.PeerGossipSleepDuration)
  538. select {
  539. case <-timer.C:
  540. case <-ctx.Done():
  541. return
  542. }
  543. continue OUTER_LOOP
  544. }
  545. }
  546. // pickSendVote picks a vote and sends it to the peer. It will return true if
  547. // there is a vote to send and false otherwise.
  548. func (r *Reactor) pickSendVote(ctx context.Context, ps *PeerState, votes types.VoteSetReader) (bool, error) {
  549. vote, ok := ps.PickVoteToSend(votes)
  550. if !ok {
  551. return false, nil
  552. }
  553. r.logger.Debug("sending vote message", "ps", ps, "vote", vote)
  554. if err := r.voteCh.Send(ctx, p2p.Envelope{
  555. To: ps.peerID,
  556. Message: &tmcons.Vote{
  557. Vote: vote.ToProto(),
  558. },
  559. }); err != nil {
  560. return false, err
  561. }
  562. ps.SetHasVote(vote)
  563. return true, nil
  564. }
  565. func (r *Reactor) gossipVotesForHeight(
  566. ctx context.Context,
  567. rs *cstypes.RoundState,
  568. prs *cstypes.PeerRoundState,
  569. ps *PeerState,
  570. ) (bool, error) {
  571. logger := r.logger.With("height", prs.Height).With("peer", ps.peerID)
  572. // if there are lastCommits to send...
  573. if prs.Step == cstypes.RoundStepNewHeight {
  574. if ok, err := r.pickSendVote(ctx, ps, rs.LastCommit); err != nil {
  575. return false, err
  576. } else if ok {
  577. logger.Debug("picked rs.LastCommit to send")
  578. return true, nil
  579. }
  580. }
  581. // if there are POL prevotes to send...
  582. if prs.Step <= cstypes.RoundStepPropose && prs.Round != -1 && prs.Round <= rs.Round && prs.ProposalPOLRound != -1 {
  583. if polPrevotes := rs.Votes.Prevotes(prs.ProposalPOLRound); polPrevotes != nil {
  584. if ok, err := r.pickSendVote(ctx, ps, polPrevotes); err != nil {
  585. return false, err
  586. } else if ok {
  587. logger.Debug("picked rs.Prevotes(prs.ProposalPOLRound) to send", "round", prs.ProposalPOLRound)
  588. return true, nil
  589. }
  590. }
  591. }
  592. // if there are prevotes to send...
  593. if prs.Step <= cstypes.RoundStepPrevoteWait && prs.Round != -1 && prs.Round <= rs.Round {
  594. if ok, err := r.pickSendVote(ctx, ps, rs.Votes.Prevotes(prs.Round)); err != nil {
  595. return false, err
  596. } else if ok {
  597. logger.Debug("picked rs.Prevotes(prs.Round) to send", "round", prs.Round)
  598. return true, nil
  599. }
  600. }
  601. // if there are precommits to send...
  602. if prs.Step <= cstypes.RoundStepPrecommitWait && prs.Round != -1 && prs.Round <= rs.Round {
  603. if ok, err := r.pickSendVote(ctx, ps, rs.Votes.Precommits(prs.Round)); err != nil {
  604. return false, err
  605. } else if ok {
  606. logger.Debug("picked rs.Precommits(prs.Round) to send", "round", prs.Round)
  607. return true, nil
  608. }
  609. }
  610. // if there are prevotes to send...(which are needed because of validBlock mechanism)
  611. if prs.Round != -1 && prs.Round <= rs.Round {
  612. if ok, err := r.pickSendVote(ctx, ps, rs.Votes.Prevotes(prs.Round)); err != nil {
  613. return false, err
  614. } else if ok {
  615. logger.Debug("picked rs.Prevotes(prs.Round) to send", "round", prs.Round)
  616. return true, nil
  617. }
  618. }
  619. // if there are POLPrevotes to send...
  620. if prs.ProposalPOLRound != -1 {
  621. if polPrevotes := rs.Votes.Prevotes(prs.ProposalPOLRound); polPrevotes != nil {
  622. if ok, err := r.pickSendVote(ctx, ps, polPrevotes); err != nil {
  623. return false, err
  624. } else if ok {
  625. logger.Debug("picked rs.Prevotes(prs.ProposalPOLRound) to send", "round", prs.ProposalPOLRound)
  626. return true, nil
  627. }
  628. }
  629. }
  630. return false, nil
  631. }
  632. func (r *Reactor) gossipVotesRoutine(ctx context.Context, ps *PeerState) {
  633. logger := r.logger.With("peer", ps.peerID)
  634. // XXX: simple hack to throttle logs upon sleep
  635. logThrottle := 0
  636. timer := time.NewTimer(0)
  637. defer timer.Stop()
  638. for {
  639. if !r.IsRunning() {
  640. return
  641. }
  642. select {
  643. case <-ctx.Done():
  644. return
  645. default:
  646. }
  647. rs := r.state.GetRoundState()
  648. prs := ps.GetRoundState()
  649. switch logThrottle {
  650. case 1: // first sleep
  651. logThrottle = 2
  652. case 2: // no more sleep
  653. logThrottle = 0
  654. }
  655. // if height matches, then send LastCommit, Prevotes, and Precommits
  656. if rs.Height == prs.Height {
  657. if ok, err := r.gossipVotesForHeight(ctx, rs, prs, ps); err != nil {
  658. return
  659. } else if ok {
  660. continue
  661. }
  662. }
  663. // special catchup logic -- if peer is lagging by height 1, send LastCommit
  664. if prs.Height != 0 && rs.Height == prs.Height+1 {
  665. if ok, err := r.pickSendVote(ctx, ps, rs.LastCommit); err != nil {
  666. return
  667. } else if ok {
  668. logger.Debug("picked rs.LastCommit to send", "height", prs.Height)
  669. continue
  670. }
  671. }
  672. // catchup logic -- if peer is lagging by more than 1, send Commit
  673. blockStoreBase := r.state.blockStore.Base()
  674. if blockStoreBase > 0 && prs.Height != 0 && rs.Height >= prs.Height+2 && prs.Height >= blockStoreBase {
  675. // Load the block commit for prs.Height, which contains precommit
  676. // signatures for prs.Height.
  677. if commit := r.state.blockStore.LoadBlockCommit(prs.Height); commit != nil {
  678. if ok, err := r.pickSendVote(ctx, ps, commit); err != nil {
  679. return
  680. } else if ok {
  681. logger.Debug("picked Catchup commit to send", "height", prs.Height)
  682. continue
  683. }
  684. }
  685. }
  686. if logThrottle == 0 {
  687. // we sent nothing -- sleep
  688. logThrottle = 1
  689. logger.Debug(
  690. "no votes to send; sleeping",
  691. "rs.Height", rs.Height,
  692. "prs.Height", prs.Height,
  693. "localPV", rs.Votes.Prevotes(rs.Round).BitArray(), "peerPV", prs.Prevotes,
  694. "localPC", rs.Votes.Precommits(rs.Round).BitArray(), "peerPC", prs.Precommits,
  695. )
  696. } else if logThrottle == 2 {
  697. logThrottle = 1
  698. }
  699. timer.Reset(r.state.config.PeerGossipSleepDuration)
  700. select {
  701. case <-ctx.Done():
  702. return
  703. case <-timer.C:
  704. }
  705. }
  706. }
  707. // NOTE: `queryMaj23Routine` has a simple crude design since it only comes
  708. // into play for liveness when there's a signature DDoS attack happening.
  709. func (r *Reactor) queryMaj23Routine(ctx context.Context, ps *PeerState) {
  710. timer := time.NewTimer(0)
  711. defer timer.Stop()
  712. ctx, cancel := context.WithCancel(ctx)
  713. defer cancel()
  714. for {
  715. if !ps.IsRunning() {
  716. return
  717. }
  718. select {
  719. case <-ctx.Done():
  720. return
  721. case <-timer.C:
  722. }
  723. if !ps.IsRunning() {
  724. return
  725. }
  726. rs := r.state.GetRoundState()
  727. prs := ps.GetRoundState()
  728. // TODO create more reliable coppies of these
  729. // structures so the following go routines don't race
  730. wg := &sync.WaitGroup{}
  731. if rs.Height == prs.Height {
  732. wg.Add(1)
  733. go func(rs *cstypes.RoundState, prs *cstypes.PeerRoundState) {
  734. defer wg.Done()
  735. // maybe send Height/Round/Prevotes
  736. if maj23, ok := rs.Votes.Prevotes(prs.Round).TwoThirdsMajority(); ok {
  737. if err := r.stateCh.Send(ctx, p2p.Envelope{
  738. To: ps.peerID,
  739. Message: &tmcons.VoteSetMaj23{
  740. Height: prs.Height,
  741. Round: prs.Round,
  742. Type: tmproto.PrevoteType,
  743. BlockID: maj23.ToProto(),
  744. },
  745. }); err != nil {
  746. cancel()
  747. }
  748. }
  749. }(rs, prs)
  750. if prs.ProposalPOLRound >= 0 {
  751. wg.Add(1)
  752. go func(rs *cstypes.RoundState, prs *cstypes.PeerRoundState) {
  753. defer wg.Done()
  754. // maybe send Height/Round/ProposalPOL
  755. if maj23, ok := rs.Votes.Prevotes(prs.ProposalPOLRound).TwoThirdsMajority(); ok {
  756. if err := r.stateCh.Send(ctx, p2p.Envelope{
  757. To: ps.peerID,
  758. Message: &tmcons.VoteSetMaj23{
  759. Height: prs.Height,
  760. Round: prs.ProposalPOLRound,
  761. Type: tmproto.PrevoteType,
  762. BlockID: maj23.ToProto(),
  763. },
  764. }); err != nil {
  765. cancel()
  766. }
  767. }
  768. }(rs, prs)
  769. }
  770. wg.Add(1)
  771. go func(rs *cstypes.RoundState, prs *cstypes.PeerRoundState) {
  772. defer wg.Done()
  773. // maybe send Height/Round/Precommits
  774. if maj23, ok := rs.Votes.Precommits(prs.Round).TwoThirdsMajority(); ok {
  775. if err := r.stateCh.Send(ctx, p2p.Envelope{
  776. To: ps.peerID,
  777. Message: &tmcons.VoteSetMaj23{
  778. Height: prs.Height,
  779. Round: prs.Round,
  780. Type: tmproto.PrecommitType,
  781. BlockID: maj23.ToProto(),
  782. },
  783. }); err != nil {
  784. cancel()
  785. }
  786. }
  787. }(rs, prs)
  788. }
  789. // Little point sending LastCommitRound/LastCommit, these are fleeting and
  790. // non-blocking.
  791. if prs.CatchupCommitRound != -1 && prs.Height > 0 {
  792. wg.Add(1)
  793. go func(rs *cstypes.RoundState, prs *cstypes.PeerRoundState) {
  794. defer wg.Done()
  795. if prs.Height <= r.state.blockStore.Height() && prs.Height >= r.state.blockStore.Base() {
  796. // maybe send Height/CatchupCommitRound/CatchupCommit
  797. if commit := r.state.LoadCommit(prs.Height); commit != nil {
  798. if err := r.stateCh.Send(ctx, p2p.Envelope{
  799. To: ps.peerID,
  800. Message: &tmcons.VoteSetMaj23{
  801. Height: prs.Height,
  802. Round: commit.Round,
  803. Type: tmproto.PrecommitType,
  804. BlockID: commit.BlockID.ToProto(),
  805. },
  806. }); err != nil {
  807. cancel()
  808. }
  809. }
  810. }
  811. }(rs, prs)
  812. }
  813. waitSignal := make(chan struct{})
  814. go func() { defer close(waitSignal); wg.Wait() }()
  815. select {
  816. case <-waitSignal:
  817. timer.Reset(r.state.config.PeerQueryMaj23SleepDuration)
  818. case <-ctx.Done():
  819. return
  820. }
  821. }
  822. }
  823. // processPeerUpdate process a peer update message. For new or reconnected peers,
  824. // we create a peer state if one does not exist for the peer, which should always
  825. // be the case, and we spawn all the relevant goroutine to broadcast messages to
  826. // the peer. During peer removal, we remove the peer for our set of peers and
  827. // signal to all spawned goroutines to gracefully exit in a non-blocking manner.
  828. func (r *Reactor) processPeerUpdate(ctx context.Context, peerUpdate p2p.PeerUpdate) {
  829. r.logger.Debug("received peer update", "peer", peerUpdate.NodeID, "status", peerUpdate.Status)
  830. r.mtx.Lock()
  831. defer r.mtx.Unlock()
  832. switch peerUpdate.Status {
  833. case p2p.PeerStatusUp:
  834. // Do not allow starting new broadcasting goroutines after reactor shutdown
  835. // has been initiated. This can happen after we've manually closed all
  836. // peer goroutines, but the router still sends in-flight peer updates.
  837. if !r.IsRunning() {
  838. return
  839. }
  840. ps, ok := r.peers[peerUpdate.NodeID]
  841. if !ok {
  842. ps = NewPeerState(r.logger, peerUpdate.NodeID)
  843. r.peers[peerUpdate.NodeID] = ps
  844. }
  845. if !ps.IsRunning() {
  846. // Set the peer state's closer to signal to all spawned goroutines to exit
  847. // when the peer is removed. We also set the running state to ensure we
  848. // do not spawn multiple instances of the same goroutines and finally we
  849. // set the waitgroup counter so we know when all goroutines have exited.
  850. ps.SetRunning(true)
  851. ctx, ps.cancel = context.WithCancel(ctx)
  852. go func() {
  853. select {
  854. case <-ctx.Done():
  855. return
  856. case <-r.readySignal:
  857. }
  858. // do nothing if the peer has
  859. // stopped while we've been waiting.
  860. if !ps.IsRunning() {
  861. return
  862. }
  863. // start goroutines for this peer
  864. go r.gossipDataRoutine(ctx, ps)
  865. go r.gossipVotesRoutine(ctx, ps)
  866. go r.queryMaj23Routine(ctx, ps)
  867. // Send our state to the peer. If we're block-syncing, broadcast a
  868. // RoundStepMessage later upon SwitchToConsensus().
  869. if !r.WaitSync() {
  870. go func() { _ = r.sendNewRoundStepMessage(ctx, ps.peerID) }()
  871. }
  872. }()
  873. }
  874. case p2p.PeerStatusDown:
  875. ps, ok := r.peers[peerUpdate.NodeID]
  876. if ok && ps.IsRunning() {
  877. // signal to all spawned goroutines for the peer to gracefully exit
  878. go func() {
  879. r.mtx.Lock()
  880. delete(r.peers, peerUpdate.NodeID)
  881. r.mtx.Unlock()
  882. ps.SetRunning(false)
  883. ps.cancel()
  884. }()
  885. }
  886. }
  887. }
  888. // handleStateMessage handles envelopes sent from peers on the StateChannel.
  889. // An error is returned if the message is unrecognized or if validation fails.
  890. // If we fail to find the peer state for the envelope sender, we perform a no-op
  891. // and return. This can happen when we process the envelope after the peer is
  892. // removed.
  893. func (r *Reactor) handleStateMessage(ctx context.Context, envelope *p2p.Envelope, msgI Message) error {
  894. ps, ok := r.GetPeerState(envelope.From)
  895. if !ok || ps == nil {
  896. r.logger.Debug("failed to find peer state", "peer", envelope.From, "ch_id", "StateChannel")
  897. return nil
  898. }
  899. switch msg := envelope.Message.(type) {
  900. case *tmcons.NewRoundStep:
  901. r.state.mtx.RLock()
  902. initialHeight := r.state.state.InitialHeight
  903. r.state.mtx.RUnlock()
  904. if err := msgI.(*NewRoundStepMessage).ValidateHeight(initialHeight); err != nil {
  905. r.logger.Error("peer sent us an invalid msg", "msg", msg, "err", err)
  906. return err
  907. }
  908. ps.ApplyNewRoundStepMessage(msgI.(*NewRoundStepMessage))
  909. case *tmcons.NewValidBlock:
  910. ps.ApplyNewValidBlockMessage(msgI.(*NewValidBlockMessage))
  911. case *tmcons.HasVote:
  912. ps.ApplyHasVoteMessage(msgI.(*HasVoteMessage))
  913. case *tmcons.VoteSetMaj23:
  914. r.state.mtx.RLock()
  915. height, votes := r.state.Height, r.state.Votes
  916. r.state.mtx.RUnlock()
  917. if height != msg.Height {
  918. return nil
  919. }
  920. vsmMsg := msgI.(*VoteSetMaj23Message)
  921. // peer claims to have a maj23 for some BlockID at <H,R,S>
  922. err := votes.SetPeerMaj23(msg.Round, msg.Type, ps.peerID, vsmMsg.BlockID)
  923. if err != nil {
  924. return err
  925. }
  926. // Respond with a VoteSetBitsMessage showing which votes we have and
  927. // consequently shows which we don't have.
  928. var ourVotes *bits.BitArray
  929. switch vsmMsg.Type {
  930. case tmproto.PrevoteType:
  931. ourVotes = votes.Prevotes(msg.Round).BitArrayByBlockID(vsmMsg.BlockID)
  932. case tmproto.PrecommitType:
  933. ourVotes = votes.Precommits(msg.Round).BitArrayByBlockID(vsmMsg.BlockID)
  934. default:
  935. panic("bad VoteSetBitsMessage field type; forgot to add a check in ValidateBasic?")
  936. }
  937. eMsg := &tmcons.VoteSetBits{
  938. Height: msg.Height,
  939. Round: msg.Round,
  940. Type: msg.Type,
  941. BlockID: msg.BlockID,
  942. }
  943. if votesProto := ourVotes.ToProto(); votesProto != nil {
  944. eMsg.Votes = *votesProto
  945. }
  946. if err := r.voteSetBitsCh.Send(ctx, p2p.Envelope{
  947. To: envelope.From,
  948. Message: eMsg,
  949. }); err != nil {
  950. return err
  951. }
  952. default:
  953. return fmt.Errorf("received unknown message on StateChannel: %T", msg)
  954. }
  955. return nil
  956. }
  957. // handleDataMessage handles envelopes sent from peers on the DataChannel. If we
  958. // fail to find the peer state for the envelope sender, we perform a no-op and
  959. // return. This can happen when we process the envelope after the peer is
  960. // removed.
  961. func (r *Reactor) handleDataMessage(ctx context.Context, envelope *p2p.Envelope, msgI Message) error {
  962. logger := r.logger.With("peer", envelope.From, "ch_id", "DataChannel")
  963. ps, ok := r.GetPeerState(envelope.From)
  964. if !ok || ps == nil {
  965. r.logger.Debug("failed to find peer state")
  966. return nil
  967. }
  968. if r.WaitSync() {
  969. logger.Info("ignoring message received during sync", "msg", fmt.Sprintf("%T", msgI))
  970. return nil
  971. }
  972. switch msg := envelope.Message.(type) {
  973. case *tmcons.Proposal:
  974. pMsg := msgI.(*ProposalMessage)
  975. ps.SetHasProposal(pMsg.Proposal)
  976. select {
  977. case <-ctx.Done():
  978. return ctx.Err()
  979. case r.state.peerMsgQueue <- msgInfo{pMsg, envelope.From, tmtime.Now()}:
  980. }
  981. case *tmcons.ProposalPOL:
  982. ps.ApplyProposalPOLMessage(msgI.(*ProposalPOLMessage))
  983. case *tmcons.BlockPart:
  984. bpMsg := msgI.(*BlockPartMessage)
  985. ps.SetHasProposalBlockPart(bpMsg.Height, bpMsg.Round, int(bpMsg.Part.Index))
  986. r.Metrics.BlockParts.With("peer_id", string(envelope.From)).Add(1)
  987. select {
  988. case r.state.peerMsgQueue <- msgInfo{bpMsg, envelope.From, tmtime.Now()}:
  989. return nil
  990. case <-ctx.Done():
  991. return ctx.Err()
  992. }
  993. default:
  994. return fmt.Errorf("received unknown message on DataChannel: %T", msg)
  995. }
  996. return nil
  997. }
  998. // handleVoteMessage handles envelopes sent from peers on the VoteChannel. If we
  999. // fail to find the peer state for the envelope sender, we perform a no-op and
  1000. // return. This can happen when we process the envelope after the peer is
  1001. // removed.
  1002. func (r *Reactor) handleVoteMessage(ctx context.Context, envelope *p2p.Envelope, msgI Message) error {
  1003. logger := r.logger.With("peer", envelope.From, "ch_id", "VoteChannel")
  1004. ps, ok := r.GetPeerState(envelope.From)
  1005. if !ok || ps == nil {
  1006. r.logger.Debug("failed to find peer state")
  1007. return nil
  1008. }
  1009. if r.WaitSync() {
  1010. logger.Info("ignoring message received during sync", "msg", msgI)
  1011. return nil
  1012. }
  1013. switch msg := envelope.Message.(type) {
  1014. case *tmcons.Vote:
  1015. r.state.mtx.RLock()
  1016. height, valSize, lastCommitSize := r.state.Height, r.state.Validators.Size(), r.state.LastCommit.Size()
  1017. r.state.mtx.RUnlock()
  1018. vMsg := msgI.(*VoteMessage)
  1019. ps.EnsureVoteBitArrays(height, valSize)
  1020. ps.EnsureVoteBitArrays(height-1, lastCommitSize)
  1021. ps.SetHasVote(vMsg.Vote)
  1022. select {
  1023. case r.state.peerMsgQueue <- msgInfo{vMsg, envelope.From, tmtime.Now()}:
  1024. return nil
  1025. case <-ctx.Done():
  1026. return ctx.Err()
  1027. }
  1028. default:
  1029. return fmt.Errorf("received unknown message on VoteChannel: %T", msg)
  1030. }
  1031. }
  1032. // handleVoteSetBitsMessage handles envelopes sent from peers on the
  1033. // VoteSetBitsChannel. If we fail to find the peer state for the envelope sender,
  1034. // we perform a no-op and return. This can happen when we process the envelope
  1035. // after the peer is removed.
  1036. func (r *Reactor) handleVoteSetBitsMessage(ctx context.Context, envelope *p2p.Envelope, msgI Message) error {
  1037. logger := r.logger.With("peer", envelope.From, "ch_id", "VoteSetBitsChannel")
  1038. ps, ok := r.GetPeerState(envelope.From)
  1039. if !ok || ps == nil {
  1040. r.logger.Debug("failed to find peer state")
  1041. return nil
  1042. }
  1043. if r.WaitSync() {
  1044. logger.Info("ignoring message received during sync", "msg", msgI)
  1045. return nil
  1046. }
  1047. switch msg := envelope.Message.(type) {
  1048. case *tmcons.VoteSetBits:
  1049. r.state.mtx.RLock()
  1050. height, votes := r.state.Height, r.state.Votes
  1051. r.state.mtx.RUnlock()
  1052. vsbMsg := msgI.(*VoteSetBitsMessage)
  1053. if height == msg.Height {
  1054. var ourVotes *bits.BitArray
  1055. switch msg.Type {
  1056. case tmproto.PrevoteType:
  1057. ourVotes = votes.Prevotes(msg.Round).BitArrayByBlockID(vsbMsg.BlockID)
  1058. case tmproto.PrecommitType:
  1059. ourVotes = votes.Precommits(msg.Round).BitArrayByBlockID(vsbMsg.BlockID)
  1060. default:
  1061. panic("bad VoteSetBitsMessage field type; forgot to add a check in ValidateBasic?")
  1062. }
  1063. ps.ApplyVoteSetBitsMessage(vsbMsg, ourVotes)
  1064. } else {
  1065. ps.ApplyVoteSetBitsMessage(vsbMsg, nil)
  1066. }
  1067. default:
  1068. return fmt.Errorf("received unknown message on VoteSetBitsChannel: %T", msg)
  1069. }
  1070. return nil
  1071. }
  1072. // handleMessage handles an Envelope sent from a peer on a specific p2p Channel.
  1073. // It will handle errors and any possible panics gracefully. A caller can handle
  1074. // any error returned by sending a PeerError on the respective channel.
  1075. //
  1076. // NOTE: We process these messages even when we're block syncing. Messages affect
  1077. // either a peer state or the consensus state. Peer state updates can happen in
  1078. // parallel, but processing of proposals, block parts, and votes are ordered by
  1079. // the p2p channel.
  1080. //
  1081. // NOTE: We block on consensus state for proposals, block parts, and votes.
  1082. func (r *Reactor) handleMessage(ctx context.Context, chID p2p.ChannelID, envelope *p2p.Envelope) (err error) {
  1083. defer func() {
  1084. if e := recover(); e != nil {
  1085. err = fmt.Errorf("panic in processing message: %v", e)
  1086. r.logger.Error(
  1087. "recovering from processing message panic",
  1088. "err", err,
  1089. "stack", string(debug.Stack()),
  1090. )
  1091. }
  1092. }()
  1093. // We wrap the envelope's message in a Proto wire type so we can convert back
  1094. // the domain type that individual channel message handlers can work with. We
  1095. // do this here once to avoid having to do it for each individual message type.
  1096. // and because a large part of the core business logic depends on these
  1097. // domain types opposed to simply working with the Proto types.
  1098. protoMsg := new(tmcons.Message)
  1099. if err := protoMsg.Wrap(envelope.Message); err != nil {
  1100. return err
  1101. }
  1102. msgI, err := MsgFromProto(protoMsg)
  1103. if err != nil {
  1104. return err
  1105. }
  1106. r.logger.Debug("received message", "ch_id", chID, "message", msgI, "peer", envelope.From)
  1107. switch chID {
  1108. case StateChannel:
  1109. err = r.handleStateMessage(ctx, envelope, msgI)
  1110. case DataChannel:
  1111. err = r.handleDataMessage(ctx, envelope, msgI)
  1112. case VoteChannel:
  1113. err = r.handleVoteMessage(ctx, envelope, msgI)
  1114. case VoteSetBitsChannel:
  1115. err = r.handleVoteSetBitsMessage(ctx, envelope, msgI)
  1116. default:
  1117. err = fmt.Errorf("unknown channel ID (%d) for envelope (%v)", chID, envelope)
  1118. }
  1119. return err
  1120. }
  1121. // processStateCh initiates a blocking process where we listen for and handle
  1122. // envelopes on the StateChannel. Any error encountered during message
  1123. // execution will result in a PeerError being sent on the StateChannel. When
  1124. // the reactor is stopped, we will catch the signal and close the p2p Channel
  1125. // gracefully.
  1126. func (r *Reactor) processStateCh(ctx context.Context) {
  1127. iter := r.stateCh.Receive(ctx)
  1128. for iter.Next(ctx) {
  1129. envelope := iter.Envelope()
  1130. if err := r.handleMessage(ctx, r.stateCh.ID, envelope); err != nil {
  1131. r.logger.Error("failed to process message", "ch_id", r.stateCh.ID, "envelope", envelope, "err", err)
  1132. if serr := r.stateCh.SendError(ctx, p2p.PeerError{
  1133. NodeID: envelope.From,
  1134. Err: err,
  1135. }); serr != nil {
  1136. return
  1137. }
  1138. }
  1139. }
  1140. }
  1141. // processDataCh initiates a blocking process where we listen for and handle
  1142. // envelopes on the DataChannel. Any error encountered during message
  1143. // execution will result in a PeerError being sent on the DataChannel. When
  1144. // the reactor is stopped, we will catch the signal and close the p2p Channel
  1145. // gracefully.
  1146. func (r *Reactor) processDataCh(ctx context.Context) {
  1147. iter := r.dataCh.Receive(ctx)
  1148. for iter.Next(ctx) {
  1149. envelope := iter.Envelope()
  1150. if err := r.handleMessage(ctx, r.dataCh.ID, envelope); err != nil {
  1151. r.logger.Error("failed to process message", "ch_id", r.dataCh.ID, "envelope", envelope, "err", err)
  1152. if serr := r.dataCh.SendError(ctx, p2p.PeerError{
  1153. NodeID: envelope.From,
  1154. Err: err,
  1155. }); serr != nil {
  1156. return
  1157. }
  1158. }
  1159. }
  1160. }
  1161. // processVoteCh initiates a blocking process where we listen for and handle
  1162. // envelopes on the VoteChannel. Any error encountered during message
  1163. // execution will result in a PeerError being sent on the VoteChannel. When
  1164. // the reactor is stopped, we will catch the signal and close the p2p Channel
  1165. // gracefully.
  1166. func (r *Reactor) processVoteCh(ctx context.Context) {
  1167. iter := r.voteCh.Receive(ctx)
  1168. for iter.Next(ctx) {
  1169. envelope := iter.Envelope()
  1170. if err := r.handleMessage(ctx, r.voteCh.ID, envelope); err != nil {
  1171. r.logger.Error("failed to process message", "ch_id", r.voteCh.ID, "envelope", envelope, "err", err)
  1172. if serr := r.voteCh.SendError(ctx, p2p.PeerError{
  1173. NodeID: envelope.From,
  1174. Err: err,
  1175. }); serr != nil {
  1176. return
  1177. }
  1178. }
  1179. }
  1180. }
  1181. // processVoteCh initiates a blocking process where we listen for and handle
  1182. // envelopes on the VoteSetBitsChannel. Any error encountered during message
  1183. // execution will result in a PeerError being sent on the VoteSetBitsChannel.
  1184. // When the reactor is stopped, we will catch the signal and close the p2p
  1185. // Channel gracefully.
  1186. func (r *Reactor) processVoteSetBitsCh(ctx context.Context) {
  1187. iter := r.voteSetBitsCh.Receive(ctx)
  1188. for iter.Next(ctx) {
  1189. envelope := iter.Envelope()
  1190. if err := r.handleMessage(ctx, r.voteSetBitsCh.ID, envelope); err != nil {
  1191. if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
  1192. return
  1193. }
  1194. r.logger.Error("failed to process message", "ch_id", r.voteSetBitsCh.ID, "envelope", envelope, "err", err)
  1195. if serr := r.voteSetBitsCh.SendError(ctx, p2p.PeerError{
  1196. NodeID: envelope.From,
  1197. Err: err,
  1198. }); serr != nil {
  1199. return
  1200. }
  1201. }
  1202. }
  1203. }
  1204. // processPeerUpdates initiates a blocking process where we listen for and handle
  1205. // PeerUpdate messages. When the reactor is stopped, we will catch the signal and
  1206. // close the p2p PeerUpdatesCh gracefully.
  1207. func (r *Reactor) processPeerUpdates(ctx context.Context) {
  1208. for {
  1209. select {
  1210. case <-ctx.Done():
  1211. return
  1212. case peerUpdate := <-r.peerUpdates.Updates():
  1213. r.processPeerUpdate(ctx, peerUpdate)
  1214. }
  1215. }
  1216. }
  1217. func (r *Reactor) peerStatsRoutine(ctx context.Context) {
  1218. for {
  1219. if !r.IsRunning() {
  1220. r.logger.Info("stopping peerStatsRoutine")
  1221. return
  1222. }
  1223. select {
  1224. case msg := <-r.state.statsMsgQueue:
  1225. ps, ok := r.GetPeerState(msg.PeerID)
  1226. if !ok || ps == nil {
  1227. r.logger.Debug("attempt to update stats for non-existent peer", "peer", msg.PeerID)
  1228. continue
  1229. }
  1230. switch msg.Msg.(type) {
  1231. case *VoteMessage:
  1232. if numVotes := ps.RecordVote(); numVotes%votesToContributeToBecomeGoodPeer == 0 {
  1233. r.peerUpdates.SendUpdate(ctx, p2p.PeerUpdate{
  1234. NodeID: msg.PeerID,
  1235. Status: p2p.PeerStatusGood,
  1236. })
  1237. }
  1238. case *BlockPartMessage:
  1239. if numParts := ps.RecordBlockPart(); numParts%blocksToContributeToBecomeGoodPeer == 0 {
  1240. r.peerUpdates.SendUpdate(ctx, p2p.PeerUpdate{
  1241. NodeID: msg.PeerID,
  1242. Status: p2p.PeerStatusGood,
  1243. })
  1244. }
  1245. }
  1246. case <-ctx.Done():
  1247. return
  1248. }
  1249. }
  1250. }
  1251. func (r *Reactor) GetConsensusState() *State {
  1252. return r.state
  1253. }
  1254. func (r *Reactor) SetStateSyncingMetrics(v float64) {
  1255. r.Metrics.StateSyncing.Set(v)
  1256. }
  1257. func (r *Reactor) SetBlockSyncingMetrics(v float64) {
  1258. r.Metrics.BlockSyncing.Set(v)
  1259. }