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.

1409 lines
39 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
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
abci: localClient improvements & bugfixes & pubsub Unsubscribe issues (#2748) * use READ lock/unlock in ConsensusState#GetLastHeight Refs #2721 * do not use defers when there's no need * fix peer formatting (output its address instead of the pointer) ``` [54310]: E[11-02|11:59:39.851] Connection failed @ sendRoutine module=p2p peer=0xb78f00 conn=MConn{74.207.236.148:26656} err="pong timeout" ``` https://github.com/tendermint/tendermint/issues/2721#issuecomment-435326581 * panic if peer has no state https://github.com/tendermint/tendermint/issues/2721#issuecomment-435347165 It's confusing that sometimes we check if peer has a state, but most of the times we expect it to be there 1. https://github.com/tendermint/tendermint/blob/add79700b5fe84417538202b6c927c8cc5383672/mempool/reactor.go#L138 2. https://github.com/tendermint/tendermint/blob/add79700b5fe84417538202b6c927c8cc5383672/rpc/core/consensus.go#L196 (edited) I will change everything to always assume peer has a state and panic otherwise that should help identify issues earlier * abci/localclient: extend lock on app callback App callback should be protected by lock as well (note this was already done for InitChainAsync, why not for others???). Otherwise, when we execute the block, tx might come in and call the callback in the same time we're updating it in execBlockOnProxyApp => DATA RACE Fixes #2721 Consensus state is locked ``` goroutine 113333 [semacquire, 309 minutes]: sync.runtime_SemacquireMutex(0xc00180009c, 0xc0000c7e00) /usr/local/go/src/runtime/sema.go:71 +0x3d sync.(*RWMutex).RLock(0xc001800090) /usr/local/go/src/sync/rwmutex.go:50 +0x4e github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).GetRoundState(0xc001800000, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:218 +0x46 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusReactor).queryMaj23Routine(0xc0017def80, 0x11104a0, 0xc0072488f0, 0xc007248 9c0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/reactor.go:735 +0x16d created by github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusReactor).AddPeer /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/reactor.go:172 +0x236 ``` because localClient is locked ``` goroutine 1899 [semacquire, 309 minutes]: sync.runtime_SemacquireMutex(0xc00003363c, 0xc0000cb500) /usr/local/go/src/runtime/sema.go:71 +0x3d sync.(*Mutex).Lock(0xc000033638) /usr/local/go/src/sync/mutex.go:134 +0xff github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client.(*localClient).SetResponseCallback(0xc0001fb560, 0xc007868540) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client/local_client.go:32 +0x33 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy.(*appConnConsensus).SetResponseCallback(0xc00002f750, 0xc007868540) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy/app_conn.go:57 +0x40 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state.execBlockOnProxyApp(0x1104e20, 0xc002ca0ba0, 0x11092a0, 0xc00002f750, 0xc0001fe960, 0xc000bfc660, 0x110cfe0, 0xc000090330, 0xc9d12, 0xc000d9d5a0, ...) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state/execution.go:230 +0x1fd github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state.(*BlockExecutor).ApplyBlock(0xc002c2a230, 0x7, 0x0, 0xc000eae880, 0x6, 0xc002e52c60, 0x16, 0x1f927, 0xc9d12, 0xc000d9d5a0, ...) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state/execution.go:96 +0x142 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).finalizeCommit(0xc001800000, 0x1f928) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1339 +0xa3e github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).tryFinalizeCommit(0xc001800000, 0x1f928) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1270 +0x451 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).enterCommit.func1(0xc001800000, 0x0, 0x1f928) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1218 +0x90 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).enterCommit(0xc001800000, 0x1f928, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1247 +0x6b8 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).addVote(0xc001800000, 0xc003d8dea0, 0xc000cf4cc0, 0x28, 0xf1, 0xc003bc7ad0, 0xc003bc7b10) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1659 +0xbad github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).tryAddVote(0xc001800000, 0xc003d8dea0, 0xc000cf4cc0, 0x28, 0xf1, 0xf1, 0xf1) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1517 +0x59 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).handleMsg(0xc001800000, 0xd98200, 0xc0070dbed0, 0xc000cf4cc0, 0x28) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:660 +0x64b github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).receiveRoutine(0xc001800000, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:617 +0x670 created by github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).OnStart /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:311 +0x132 ``` tx comes in and CheckTx is executed right when we execute the block ``` goroutine 111044 [semacquire, 309 minutes]: sync.runtime_SemacquireMutex(0xc00003363c, 0x0) /usr/local/go/src/runtime/sema.go:71 +0x3d sync.(*Mutex).Lock(0xc000033638) /usr/local/go/src/sync/mutex.go:134 +0xff github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client.(*localClient).CheckTxAsync(0xc0001fb0e0, 0xc002d94500, 0x13f, 0x280, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client/local_client.go:85 +0x47 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy.(*appConnMempool).CheckTxAsync(0xc00002f720, 0xc002d94500, 0x13f, 0x280, 0x1) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy/app_conn.go:114 +0x51 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/mempool.(*Mempool).CheckTx(0xc002d3a320, 0xc002d94500, 0x13f, 0x280, 0xc0072355f0, 0x0, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/mempool/mempool.go:316 +0x17b github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/core.BroadcastTxSync(0xc002d94500, 0x13f, 0x280, 0x0, 0x0, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/core/mempool.go:93 +0xb8 reflect.Value.call(0xd85560, 0x10326c0, 0x13, 0xec7b8b, 0x4, 0xc00663f180, 0x1, 0x1, 0xc00663f180, 0xc00663f188, ...) /usr/local/go/src/reflect/value.go:447 +0x449 reflect.Value.Call(0xd85560, 0x10326c0, 0x13, 0xc00663f180, 0x1, 0x1, 0x0, 0x0, 0xc005cc9344) /usr/local/go/src/reflect/value.go:308 +0xa4 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.makeHTTPHandler.func2(0x1102060, 0xc00663f100, 0xc0082d7900) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/handlers.go:269 +0x188 net/http.HandlerFunc.ServeHTTP(0xc002c81f20, 0x1102060, 0xc00663f100, 0xc0082d7900) /usr/local/go/src/net/http/server.go:1964 +0x44 net/http.(*ServeMux).ServeHTTP(0xc002c81b60, 0x1102060, 0xc00663f100, 0xc0082d7900) /usr/local/go/src/net/http/server.go:2361 +0x127 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.maxBytesHandler.ServeHTTP(0x10f8a40, 0xc002c81b60, 0xf4240, 0x1102060, 0xc00663f100, 0xc0082d7900) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/http_server.go:219 +0xcf github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.RecoverAndLogHandler.func1(0x1103220, 0xc00121e620, 0xc0082d7900) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/http_server.go:192 +0x394 net/http.HandlerFunc.ServeHTTP(0xc002c06ea0, 0x1103220, 0xc00121e620, 0xc0082d7900) /usr/local/go/src/net/http/server.go:1964 +0x44 net/http.serverHandler.ServeHTTP(0xc001a1aa90, 0x1103220, 0xc00121e620, 0xc0082d7900) /usr/local/go/src/net/http/server.go:2741 +0xab net/http.(*conn).serve(0xc00785a3c0, 0x11041a0, 0xc000f844c0) /usr/local/go/src/net/http/server.go:1847 +0x646 created by net/http.(*Server).Serve /usr/local/go/src/net/http/server.go:2851 +0x2f5 ``` * consensus: use read lock in Receive#VoteMessage * use defer to unlock mutex because application might panic * use defer in every method of the localClient * add a changelog entry * drain channels before Unsubscribe(All) Read https://github.com/tendermint/tendermint/blob/55362ed76630f3e1ebec159a598f6a9fb5892cb1/libs/pubsub/pubsub.go#L13 for the detailed explanation of the issue. We'll need to fix it someday. Make sure to keep an eye on https://github.com/tendermint/tendermint/blob/master/docs/architecture/adr-033-pubsub.md * retry instead of panic when peer has no state in reactors other than consensus in /dump_consensus_state RPC endpoint, skip a peer with no state * rpc/core/mempool: simplify error messages * rpc/core/mempool: use time.After instead of timer also, do not log DeliverTx result (to be consistent with other memthods) * unlock before calling the callback in reqRes#SetCallback
6 years ago
7 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
10 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
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. "fmt"
  4. "time"
  5. cstypes "github.com/tendermint/tendermint/internal/consensus/types"
  6. tmsync "github.com/tendermint/tendermint/internal/libs/sync"
  7. "github.com/tendermint/tendermint/internal/p2p"
  8. "github.com/tendermint/tendermint/libs/bits"
  9. tmevents "github.com/tendermint/tendermint/libs/events"
  10. "github.com/tendermint/tendermint/libs/log"
  11. "github.com/tendermint/tendermint/libs/service"
  12. tmcons "github.com/tendermint/tendermint/proto/tendermint/consensus"
  13. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  14. sm "github.com/tendermint/tendermint/state"
  15. "github.com/tendermint/tendermint/types"
  16. )
  17. var (
  18. _ service.Service = (*Reactor)(nil)
  19. _ p2p.Wrapper = (*tmcons.Message)(nil)
  20. // ChannelShims contains a map of ChannelDescriptorShim objects, where each
  21. // object wraps a reference to a legacy p2p ChannelDescriptor and the corresponding
  22. // p2p proto.Message the new p2p Channel is responsible for handling.
  23. //
  24. //
  25. // TODO: Remove once p2p refactor is complete.
  26. // ref: https://github.com/tendermint/tendermint/issues/5670
  27. ChannelShims = map[p2p.ChannelID]*p2p.ChannelDescriptorShim{
  28. StateChannel: {
  29. MsgType: new(tmcons.Message),
  30. Descriptor: &p2p.ChannelDescriptor{
  31. ID: byte(StateChannel),
  32. Priority: 6,
  33. SendQueueCapacity: 100,
  34. RecvMessageCapacity: maxMsgSize,
  35. MaxSendBytes: 12000,
  36. },
  37. },
  38. DataChannel: {
  39. MsgType: new(tmcons.Message),
  40. Descriptor: &p2p.ChannelDescriptor{
  41. // TODO: Consider a split between gossiping current block and catchup
  42. // stuff. Once we gossip the whole block there is nothing left to send
  43. // until next height or round.
  44. ID: byte(DataChannel),
  45. Priority: 10,
  46. SendQueueCapacity: 100,
  47. RecvBufferCapacity: 50 * 4096,
  48. RecvMessageCapacity: maxMsgSize,
  49. MaxSendBytes: 40000,
  50. },
  51. },
  52. VoteChannel: {
  53. MsgType: new(tmcons.Message),
  54. Descriptor: &p2p.ChannelDescriptor{
  55. ID: byte(VoteChannel),
  56. Priority: 7,
  57. SendQueueCapacity: 100,
  58. RecvBufferCapacity: 100 * 100,
  59. RecvMessageCapacity: maxMsgSize,
  60. MaxSendBytes: 150,
  61. },
  62. },
  63. VoteSetBitsChannel: {
  64. MsgType: new(tmcons.Message),
  65. Descriptor: &p2p.ChannelDescriptor{
  66. ID: byte(VoteSetBitsChannel),
  67. Priority: 1,
  68. SendQueueCapacity: 2,
  69. RecvBufferCapacity: 1024,
  70. RecvMessageCapacity: maxMsgSize,
  71. MaxSendBytes: 50,
  72. },
  73. },
  74. }
  75. )
  76. const (
  77. StateChannel = p2p.ChannelID(0x20)
  78. DataChannel = p2p.ChannelID(0x21)
  79. VoteChannel = p2p.ChannelID(0x22)
  80. VoteSetBitsChannel = p2p.ChannelID(0x23)
  81. maxMsgSize = 1048576 // 1MB; NOTE: keep in sync with types.PartSet sizes.
  82. blocksToContributeToBecomeGoodPeer = 10000
  83. votesToContributeToBecomeGoodPeer = 10000
  84. listenerIDConsensus = "consensus-reactor"
  85. )
  86. type ReactorOption func(*Reactor)
  87. // Reactor defines a reactor for the consensus service.
  88. type Reactor struct {
  89. service.BaseService
  90. state *State
  91. eventBus *types.EventBus
  92. Metrics *Metrics
  93. mtx tmsync.RWMutex
  94. peers map[types.NodeID]*PeerState
  95. waitSync bool
  96. stateCh *p2p.Channel
  97. dataCh *p2p.Channel
  98. voteCh *p2p.Channel
  99. voteSetBitsCh *p2p.Channel
  100. peerUpdates *p2p.PeerUpdates
  101. // NOTE: We need a dedicated stateCloseCh channel for signaling closure of
  102. // the StateChannel due to the fact that the StateChannel message handler
  103. // performs a send on the VoteSetBitsChannel. This is an antipattern, so having
  104. // this dedicated channel,stateCloseCh, is necessary in order to avoid data races.
  105. stateCloseCh chan struct{}
  106. closeCh chan struct{}
  107. }
  108. // NewReactor returns a reference to a new consensus reactor, which implements
  109. // the service.Service interface. It accepts a logger, consensus state, references
  110. // to relevant p2p Channels and a channel to listen for peer updates on. The
  111. // reactor will close all p2p Channels when stopping.
  112. func NewReactor(
  113. logger log.Logger,
  114. cs *State,
  115. stateCh *p2p.Channel,
  116. dataCh *p2p.Channel,
  117. voteCh *p2p.Channel,
  118. voteSetBitsCh *p2p.Channel,
  119. peerUpdates *p2p.PeerUpdates,
  120. waitSync bool,
  121. options ...ReactorOption,
  122. ) *Reactor {
  123. r := &Reactor{
  124. state: cs,
  125. waitSync: waitSync,
  126. peers: make(map[types.NodeID]*PeerState),
  127. Metrics: NopMetrics(),
  128. stateCh: stateCh,
  129. dataCh: dataCh,
  130. voteCh: voteCh,
  131. voteSetBitsCh: voteSetBitsCh,
  132. peerUpdates: peerUpdates,
  133. stateCloseCh: make(chan struct{}),
  134. closeCh: make(chan struct{}),
  135. }
  136. r.BaseService = *service.NewBaseService(logger, "Consensus", r)
  137. for _, opt := range options {
  138. opt(r)
  139. }
  140. return r
  141. }
  142. // OnStart starts separate go routines for each p2p Channel and listens for
  143. // envelopes on each. In addition, it also listens for peer updates and handles
  144. // messages on that p2p channel accordingly. The caller must be sure to execute
  145. // OnStop to ensure the outbound p2p Channels are closed.
  146. func (r *Reactor) OnStart() error {
  147. r.Logger.Debug("consensus wait sync", "wait_sync", r.WaitSync())
  148. // start routine that computes peer statistics for evaluating peer quality
  149. //
  150. // TODO: Evaluate if we need this to be synchronized via WaitGroup as to not
  151. // leak the goroutine when stopping the reactor.
  152. go r.peerStatsRoutine()
  153. r.subscribeToBroadcastEvents()
  154. if !r.WaitSync() {
  155. if err := r.state.Start(); err != nil {
  156. return err
  157. }
  158. }
  159. go r.processStateCh()
  160. go r.processDataCh()
  161. go r.processVoteCh()
  162. go r.processVoteSetBitsCh()
  163. go r.processPeerUpdates()
  164. return nil
  165. }
  166. // OnStop stops the reactor by signaling to all spawned goroutines to exit and
  167. // blocking until they all exit, as well as unsubscribing from events and stopping
  168. // state.
  169. func (r *Reactor) OnStop() {
  170. r.unsubscribeFromBroadcastEvents()
  171. if err := r.state.Stop(); err != nil {
  172. r.Logger.Error("failed to stop consensus state", "err", err)
  173. }
  174. if !r.WaitSync() {
  175. r.state.Wait()
  176. }
  177. r.mtx.Lock()
  178. peers := r.peers
  179. r.mtx.Unlock()
  180. // wait for all spawned peer goroutines to gracefully exit
  181. for _, ps := range peers {
  182. ps.closer.Close()
  183. }
  184. for _, ps := range peers {
  185. ps.broadcastWG.Wait()
  186. }
  187. // Close the StateChannel goroutine separately since it uses its own channel
  188. // to signal closure.
  189. close(r.stateCloseCh)
  190. <-r.stateCh.Done()
  191. // Close closeCh to signal to all spawned goroutines to gracefully exit. All
  192. // p2p Channels should execute Close().
  193. close(r.closeCh)
  194. // Wait for all p2p Channels to be closed before returning. This ensures we
  195. // can easily reason about synchronization of all p2p Channels and ensure no
  196. // panics will occur.
  197. <-r.voteSetBitsCh.Done()
  198. <-r.dataCh.Done()
  199. <-r.voteCh.Done()
  200. <-r.peerUpdates.Done()
  201. }
  202. // SetEventBus sets the reactor's event bus.
  203. func (r *Reactor) SetEventBus(b *types.EventBus) {
  204. r.eventBus = b
  205. r.state.SetEventBus(b)
  206. }
  207. // WaitSync returns whether the consensus reactor is waiting for state/fast sync.
  208. func (r *Reactor) WaitSync() bool {
  209. r.mtx.RLock()
  210. defer r.mtx.RUnlock()
  211. return r.waitSync
  212. }
  213. // ReactorMetrics sets the reactor's metrics as an option function.
  214. func ReactorMetrics(metrics *Metrics) ReactorOption {
  215. return func(r *Reactor) { r.Metrics = metrics }
  216. }
  217. // SwitchToConsensus switches from fast-sync mode to consensus mode. It resets
  218. // the state, turns off fast-sync, and starts the consensus state-machine.
  219. func (r *Reactor) SwitchToConsensus(state sm.State, skipWAL bool) {
  220. r.Logger.Info("switching to consensus")
  221. // we have no votes, so reconstruct LastCommit from SeenCommit
  222. if state.LastBlockHeight > 0 {
  223. r.state.reconstructLastCommit(state)
  224. }
  225. // NOTE: The line below causes broadcastNewRoundStepRoutine() to broadcast a
  226. // NewRoundStepMessage.
  227. r.state.updateToState(state)
  228. r.mtx.Lock()
  229. r.waitSync = false
  230. r.mtx.Unlock()
  231. r.Metrics.FastSyncing.Set(0)
  232. r.Metrics.StateSyncing.Set(0)
  233. if skipWAL {
  234. r.state.doWALCatchup = false
  235. }
  236. if err := r.state.Start(); err != nil {
  237. panic(fmt.Sprintf(`failed to start consensus state: %v
  238. conS:
  239. %+v
  240. conR:
  241. %+v`, err, r.state, r))
  242. }
  243. }
  244. // String returns a string representation of the Reactor.
  245. //
  246. // NOTE: For now, it is just a hard-coded string to avoid accessing unprotected
  247. // shared variables.
  248. //
  249. // TODO: improve!
  250. func (r *Reactor) String() string {
  251. return "ConsensusReactor"
  252. }
  253. // StringIndented returns an indented string representation of the Reactor.
  254. func (r *Reactor) StringIndented(indent string) string {
  255. r.mtx.RLock()
  256. defer r.mtx.RUnlock()
  257. s := "ConsensusReactor{\n"
  258. s += indent + " " + r.state.StringIndented(indent+" ") + "\n"
  259. for _, ps := range r.peers {
  260. s += indent + " " + ps.StringIndented(indent+" ") + "\n"
  261. }
  262. s += indent + "}"
  263. return s
  264. }
  265. // GetPeerState returns PeerState for a given NodeID.
  266. func (r *Reactor) GetPeerState(peerID types.NodeID) (*PeerState, bool) {
  267. r.mtx.RLock()
  268. defer r.mtx.RUnlock()
  269. ps, ok := r.peers[peerID]
  270. return ps, ok
  271. }
  272. func (r *Reactor) broadcastNewRoundStepMessage(rs *cstypes.RoundState) {
  273. r.stateCh.Out <- p2p.Envelope{
  274. Broadcast: true,
  275. Message: makeRoundStepMessage(rs),
  276. }
  277. }
  278. func (r *Reactor) broadcastNewValidBlockMessage(rs *cstypes.RoundState) {
  279. psHeader := rs.ProposalBlockParts.Header()
  280. r.stateCh.Out <- p2p.Envelope{
  281. Broadcast: true,
  282. Message: &tmcons.NewValidBlock{
  283. Height: rs.Height,
  284. Round: rs.Round,
  285. BlockPartSetHeader: psHeader.ToProto(),
  286. BlockParts: rs.ProposalBlockParts.BitArray().ToProto(),
  287. IsCommit: rs.Step == cstypes.RoundStepCommit,
  288. },
  289. }
  290. }
  291. func (r *Reactor) broadcastHasVoteMessage(vote *types.Vote) {
  292. r.stateCh.Out <- p2p.Envelope{
  293. Broadcast: true,
  294. Message: &tmcons.HasVote{
  295. Height: vote.Height,
  296. Round: vote.Round,
  297. Type: vote.Type,
  298. Index: vote.ValidatorIndex,
  299. },
  300. }
  301. }
  302. // subscribeToBroadcastEvents subscribes for new round steps and votes using the
  303. // internal pubsub defined in the consensus state to broadcast them to peers
  304. // upon receiving.
  305. func (r *Reactor) subscribeToBroadcastEvents() {
  306. err := r.state.evsw.AddListenerForEvent(
  307. listenerIDConsensus,
  308. types.EventNewRoundStep,
  309. func(data tmevents.EventData) {
  310. r.broadcastNewRoundStepMessage(data.(*cstypes.RoundState))
  311. select {
  312. case r.state.onStopCh <- data.(*cstypes.RoundState):
  313. default:
  314. }
  315. },
  316. )
  317. if err != nil {
  318. r.Logger.Error("failed to add listener for events", "err", err)
  319. }
  320. err = r.state.evsw.AddListenerForEvent(
  321. listenerIDConsensus,
  322. types.EventValidBlock,
  323. func(data tmevents.EventData) {
  324. r.broadcastNewValidBlockMessage(data.(*cstypes.RoundState))
  325. },
  326. )
  327. if err != nil {
  328. r.Logger.Error("failed to add listener for events", "err", err)
  329. }
  330. err = r.state.evsw.AddListenerForEvent(
  331. listenerIDConsensus,
  332. types.EventVote,
  333. func(data tmevents.EventData) {
  334. r.broadcastHasVoteMessage(data.(*types.Vote))
  335. },
  336. )
  337. if err != nil {
  338. r.Logger.Error("failed to add listener for events", "err", err)
  339. }
  340. }
  341. func (r *Reactor) unsubscribeFromBroadcastEvents() {
  342. r.state.evsw.RemoveListener(listenerIDConsensus)
  343. }
  344. func makeRoundStepMessage(rs *cstypes.RoundState) *tmcons.NewRoundStep {
  345. return &tmcons.NewRoundStep{
  346. Height: rs.Height,
  347. Round: rs.Round,
  348. Step: uint32(rs.Step),
  349. SecondsSinceStartTime: int64(time.Since(rs.StartTime).Seconds()),
  350. LastCommitRound: rs.LastCommit.GetRound(),
  351. }
  352. }
  353. func (r *Reactor) sendNewRoundStepMessage(peerID types.NodeID) {
  354. rs := r.state.GetRoundState()
  355. msg := makeRoundStepMessage(rs)
  356. r.stateCh.Out <- p2p.Envelope{
  357. To: peerID,
  358. Message: msg,
  359. }
  360. }
  361. func (r *Reactor) gossipDataForCatchup(rs *cstypes.RoundState, prs *cstypes.PeerRoundState, ps *PeerState) {
  362. logger := r.Logger.With("height", prs.Height).With("peer", ps.peerID)
  363. if index, ok := prs.ProposalBlockParts.Not().PickRandom(); ok {
  364. // ensure that the peer's PartSetHeader is correct
  365. blockMeta := r.state.blockStore.LoadBlockMeta(prs.Height)
  366. if blockMeta == nil {
  367. logger.Error(
  368. "failed to load block meta",
  369. "our_height", rs.Height,
  370. "blockstore_base", r.state.blockStore.Base(),
  371. "blockstore_height", r.state.blockStore.Height(),
  372. )
  373. time.Sleep(r.state.config.PeerGossipSleepDuration)
  374. return
  375. } else if !blockMeta.BlockID.PartSetHeader.Equals(prs.ProposalBlockPartSetHeader) {
  376. logger.Info(
  377. "peer ProposalBlockPartSetHeader mismatch; sleeping",
  378. "block_part_set_header", blockMeta.BlockID.PartSetHeader,
  379. "peer_block_part_set_header", prs.ProposalBlockPartSetHeader,
  380. )
  381. time.Sleep(r.state.config.PeerGossipSleepDuration)
  382. return
  383. }
  384. part := r.state.blockStore.LoadBlockPart(prs.Height, index)
  385. if part == nil {
  386. logger.Error(
  387. "failed to load block part",
  388. "index", index,
  389. "block_part_set_header", blockMeta.BlockID.PartSetHeader,
  390. "peer_block_part_set_header", prs.ProposalBlockPartSetHeader,
  391. )
  392. time.Sleep(r.state.config.PeerGossipSleepDuration)
  393. return
  394. }
  395. partProto, err := part.ToProto()
  396. if err != nil {
  397. logger.Error("failed to convert block part to proto", "err", err)
  398. time.Sleep(r.state.config.PeerGossipSleepDuration)
  399. return
  400. }
  401. logger.Debug("sending block part for catchup", "round", prs.Round, "index", index)
  402. r.dataCh.Out <- p2p.Envelope{
  403. To: ps.peerID,
  404. Message: &tmcons.BlockPart{
  405. Height: prs.Height, // not our height, so it does not matter.
  406. Round: prs.Round, // not our height, so it does not matter
  407. Part: *partProto,
  408. },
  409. }
  410. return
  411. }
  412. time.Sleep(r.state.config.PeerGossipSleepDuration)
  413. }
  414. func (r *Reactor) gossipDataRoutine(ps *PeerState) {
  415. logger := r.Logger.With("peer", ps.peerID)
  416. defer ps.broadcastWG.Done()
  417. OUTER_LOOP:
  418. for {
  419. if !r.IsRunning() {
  420. return
  421. }
  422. select {
  423. case <-ps.closer.Done():
  424. // The peer is marked for removal via a PeerUpdate as the doneCh was
  425. // explicitly closed to signal we should exit.
  426. return
  427. default:
  428. }
  429. rs := r.state.GetRoundState()
  430. prs := ps.GetRoundState()
  431. // Send proposal Block parts?
  432. if rs.ProposalBlockParts.HasHeader(prs.ProposalBlockPartSetHeader) {
  433. if index, ok := rs.ProposalBlockParts.BitArray().Sub(prs.ProposalBlockParts.Copy()).PickRandom(); ok {
  434. part := rs.ProposalBlockParts.GetPart(index)
  435. partProto, err := part.ToProto()
  436. if err != nil {
  437. logger.Error("failed to convert block part to proto", "err", err)
  438. return
  439. }
  440. logger.Debug("sending block part", "height", prs.Height, "round", prs.Round)
  441. r.dataCh.Out <- p2p.Envelope{
  442. To: ps.peerID,
  443. Message: &tmcons.BlockPart{
  444. Height: rs.Height, // this tells peer that this part applies to us
  445. Round: rs.Round, // this tells peer that this part applies to us
  446. Part: *partProto,
  447. },
  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. time.Sleep(r.state.config.PeerGossipSleepDuration)
  468. } else {
  469. ps.InitProposalBlockParts(blockMeta.BlockID.PartSetHeader)
  470. }
  471. // Continue the loop since prs is a copy and not effected by this
  472. // initialization.
  473. continue OUTER_LOOP
  474. }
  475. r.gossipDataForCatchup(rs, prs, ps)
  476. continue OUTER_LOOP
  477. }
  478. // if height and round don't match, sleep
  479. if (rs.Height != prs.Height) || (rs.Round != prs.Round) {
  480. time.Sleep(r.state.config.PeerGossipSleepDuration)
  481. continue OUTER_LOOP
  482. }
  483. // By here, height and round match.
  484. // Proposal block parts were already matched and sent if any were wanted.
  485. // (These can match on hash so the round doesn't matter)
  486. // Now consider sending other things, like the Proposal itself.
  487. // Send Proposal && ProposalPOL BitArray?
  488. if rs.Proposal != nil && !prs.Proposal {
  489. // Proposal: share the proposal metadata with peer.
  490. {
  491. propProto := rs.Proposal.ToProto()
  492. logger.Debug("sending proposal", "height", prs.Height, "round", prs.Round)
  493. r.dataCh.Out <- p2p.Envelope{
  494. To: ps.peerID,
  495. Message: &tmcons.Proposal{
  496. Proposal: *propProto,
  497. },
  498. }
  499. // NOTE: A peer might have received a different proposal message, so
  500. // this Proposal msg will be rejected!
  501. ps.SetHasProposal(rs.Proposal)
  502. }
  503. // ProposalPOL: lets peer know which POL votes we have so far. The peer
  504. // must receive ProposalMessage first. Note, rs.Proposal was validated,
  505. // so rs.Proposal.POLRound <= rs.Round, so we definitely have
  506. // rs.Votes.Prevotes(rs.Proposal.POLRound).
  507. if 0 <= rs.Proposal.POLRound {
  508. pPol := rs.Votes.Prevotes(rs.Proposal.POLRound).BitArray()
  509. pPolProto := pPol.ToProto()
  510. logger.Debug("sending POL", "height", prs.Height, "round", prs.Round)
  511. r.dataCh.Out <- p2p.Envelope{
  512. To: ps.peerID,
  513. Message: &tmcons.ProposalPOL{
  514. Height: rs.Height,
  515. ProposalPolRound: rs.Proposal.POLRound,
  516. ProposalPol: *pPolProto,
  517. },
  518. }
  519. }
  520. continue OUTER_LOOP
  521. }
  522. // nothing to do -- sleep
  523. time.Sleep(r.state.config.PeerGossipSleepDuration)
  524. continue OUTER_LOOP
  525. }
  526. }
  527. // pickSendVote picks a vote and sends it to the peer. It will return true if
  528. // there is a vote to send and false otherwise.
  529. func (r *Reactor) pickSendVote(ps *PeerState, votes types.VoteSetReader) bool {
  530. if vote, ok := ps.PickVoteToSend(votes); ok {
  531. r.Logger.Debug("sending vote message", "ps", ps, "vote", vote)
  532. r.voteCh.Out <- p2p.Envelope{
  533. To: ps.peerID,
  534. Message: &tmcons.Vote{
  535. Vote: vote.ToProto(),
  536. },
  537. }
  538. ps.SetHasVote(vote)
  539. return true
  540. }
  541. return false
  542. }
  543. func (r *Reactor) gossipVotesForHeight(rs *cstypes.RoundState, prs *cstypes.PeerRoundState, ps *PeerState) bool {
  544. logger := r.Logger.With("height", prs.Height).With("peer", ps.peerID)
  545. // if there are lastCommits to send...
  546. if prs.Step == cstypes.RoundStepNewHeight {
  547. if r.pickSendVote(ps, rs.LastCommit) {
  548. logger.Debug("picked rs.LastCommit to send")
  549. return true
  550. }
  551. }
  552. // if there are POL prevotes to send...
  553. if prs.Step <= cstypes.RoundStepPropose && prs.Round != -1 && prs.Round <= rs.Round && prs.ProposalPOLRound != -1 {
  554. if polPrevotes := rs.Votes.Prevotes(prs.ProposalPOLRound); polPrevotes != nil {
  555. if r.pickSendVote(ps, polPrevotes) {
  556. logger.Debug("picked rs.Prevotes(prs.ProposalPOLRound) to send", "round", prs.ProposalPOLRound)
  557. return true
  558. }
  559. }
  560. }
  561. // if there are prevotes to send...
  562. if prs.Step <= cstypes.RoundStepPrevoteWait && prs.Round != -1 && prs.Round <= rs.Round {
  563. if r.pickSendVote(ps, rs.Votes.Prevotes(prs.Round)) {
  564. logger.Debug("picked rs.Prevotes(prs.Round) to send", "round", prs.Round)
  565. return true
  566. }
  567. }
  568. // if there are precommits to send...
  569. if prs.Step <= cstypes.RoundStepPrecommitWait && prs.Round != -1 && prs.Round <= rs.Round {
  570. if r.pickSendVote(ps, rs.Votes.Precommits(prs.Round)) {
  571. logger.Debug("picked rs.Precommits(prs.Round) to send", "round", prs.Round)
  572. return true
  573. }
  574. }
  575. // if there are prevotes to send...(which are needed because of validBlock mechanism)
  576. if prs.Round != -1 && prs.Round <= rs.Round {
  577. if r.pickSendVote(ps, rs.Votes.Prevotes(prs.Round)) {
  578. logger.Debug("picked rs.Prevotes(prs.Round) to send", "round", prs.Round)
  579. return true
  580. }
  581. }
  582. // if there are POLPrevotes to send...
  583. if prs.ProposalPOLRound != -1 {
  584. if polPrevotes := rs.Votes.Prevotes(prs.ProposalPOLRound); polPrevotes != nil {
  585. if r.pickSendVote(ps, polPrevotes) {
  586. logger.Debug("picked rs.Prevotes(prs.ProposalPOLRound) to send", "round", prs.ProposalPOLRound)
  587. return true
  588. }
  589. }
  590. }
  591. return false
  592. }
  593. func (r *Reactor) gossipVotesRoutine(ps *PeerState) {
  594. logger := r.Logger.With("peer", ps.peerID)
  595. defer ps.broadcastWG.Done()
  596. // XXX: simple hack to throttle logs upon sleep
  597. logThrottle := 0
  598. OUTER_LOOP:
  599. for {
  600. if !r.IsRunning() {
  601. return
  602. }
  603. select {
  604. case <-ps.closer.Done():
  605. // The peer is marked for removal via a PeerUpdate as the doneCh was
  606. // explicitly closed to signal we should exit.
  607. return
  608. default:
  609. }
  610. rs := r.state.GetRoundState()
  611. prs := ps.GetRoundState()
  612. switch logThrottle {
  613. case 1: // first sleep
  614. logThrottle = 2
  615. case 2: // no more sleep
  616. logThrottle = 0
  617. }
  618. // if height matches, then send LastCommit, Prevotes, and Precommits
  619. if rs.Height == prs.Height {
  620. if r.gossipVotesForHeight(rs, prs, ps) {
  621. continue OUTER_LOOP
  622. }
  623. }
  624. // special catchup logic -- if peer is lagging by height 1, send LastCommit
  625. if prs.Height != 0 && rs.Height == prs.Height+1 {
  626. if r.pickSendVote(ps, rs.LastCommit) {
  627. logger.Debug("picked rs.LastCommit to send", "height", prs.Height)
  628. continue OUTER_LOOP
  629. }
  630. }
  631. // catchup logic -- if peer is lagging by more than 1, send Commit
  632. blockStoreBase := r.state.blockStore.Base()
  633. if blockStoreBase > 0 && prs.Height != 0 && rs.Height >= prs.Height+2 && prs.Height >= blockStoreBase {
  634. // Load the block commit for prs.Height, which contains precommit
  635. // signatures for prs.Height.
  636. if commit := r.state.blockStore.LoadBlockCommit(prs.Height); commit != nil {
  637. if r.pickSendVote(ps, commit) {
  638. logger.Debug("picked Catchup commit to send", "height", prs.Height)
  639. continue OUTER_LOOP
  640. }
  641. }
  642. }
  643. if logThrottle == 0 {
  644. // we sent nothing -- sleep
  645. logThrottle = 1
  646. logger.Debug(
  647. "no votes to send; sleeping",
  648. "rs.Height", rs.Height,
  649. "prs.Height", prs.Height,
  650. "localPV", rs.Votes.Prevotes(rs.Round).BitArray(), "peerPV", prs.Prevotes,
  651. "localPC", rs.Votes.Precommits(rs.Round).BitArray(), "peerPC", prs.Precommits,
  652. )
  653. } else if logThrottle == 2 {
  654. logThrottle = 1
  655. }
  656. time.Sleep(r.state.config.PeerGossipSleepDuration)
  657. continue OUTER_LOOP
  658. }
  659. }
  660. // NOTE: `queryMaj23Routine` has a simple crude design since it only comes
  661. // into play for liveness when there's a signature DDoS attack happening.
  662. func (r *Reactor) queryMaj23Routine(ps *PeerState) {
  663. defer ps.broadcastWG.Done()
  664. OUTER_LOOP:
  665. for {
  666. if !r.IsRunning() {
  667. return
  668. }
  669. select {
  670. case <-ps.closer.Done():
  671. // The peer is marked for removal via a PeerUpdate as the doneCh was
  672. // explicitly closed to signal we should exit.
  673. return
  674. default:
  675. }
  676. // maybe send Height/Round/Prevotes
  677. {
  678. rs := r.state.GetRoundState()
  679. prs := ps.GetRoundState()
  680. if rs.Height == prs.Height {
  681. if maj23, ok := rs.Votes.Prevotes(prs.Round).TwoThirdsMajority(); ok {
  682. r.stateCh.Out <- p2p.Envelope{
  683. To: ps.peerID,
  684. Message: &tmcons.VoteSetMaj23{
  685. Height: prs.Height,
  686. Round: prs.Round,
  687. Type: tmproto.PrevoteType,
  688. BlockID: maj23.ToProto(),
  689. },
  690. }
  691. time.Sleep(r.state.config.PeerQueryMaj23SleepDuration)
  692. }
  693. }
  694. }
  695. // maybe send Height/Round/Precommits
  696. {
  697. rs := r.state.GetRoundState()
  698. prs := ps.GetRoundState()
  699. if rs.Height == prs.Height {
  700. if maj23, ok := rs.Votes.Precommits(prs.Round).TwoThirdsMajority(); ok {
  701. r.stateCh.Out <- p2p.Envelope{
  702. To: ps.peerID,
  703. Message: &tmcons.VoteSetMaj23{
  704. Height: prs.Height,
  705. Round: prs.Round,
  706. Type: tmproto.PrecommitType,
  707. BlockID: maj23.ToProto(),
  708. },
  709. }
  710. time.Sleep(r.state.config.PeerQueryMaj23SleepDuration)
  711. }
  712. }
  713. }
  714. // maybe send Height/Round/ProposalPOL
  715. {
  716. rs := r.state.GetRoundState()
  717. prs := ps.GetRoundState()
  718. if rs.Height == prs.Height && prs.ProposalPOLRound >= 0 {
  719. if maj23, ok := rs.Votes.Prevotes(prs.ProposalPOLRound).TwoThirdsMajority(); ok {
  720. r.stateCh.Out <- p2p.Envelope{
  721. To: ps.peerID,
  722. Message: &tmcons.VoteSetMaj23{
  723. Height: prs.Height,
  724. Round: prs.ProposalPOLRound,
  725. Type: tmproto.PrevoteType,
  726. BlockID: maj23.ToProto(),
  727. },
  728. }
  729. time.Sleep(r.state.config.PeerQueryMaj23SleepDuration)
  730. }
  731. }
  732. }
  733. // Little point sending LastCommitRound/LastCommit, these are fleeting and
  734. // non-blocking.
  735. // maybe send Height/CatchupCommitRound/CatchupCommit
  736. {
  737. prs := ps.GetRoundState()
  738. if prs.CatchupCommitRound != -1 && prs.Height > 0 && prs.Height <= r.state.blockStore.Height() &&
  739. prs.Height >= r.state.blockStore.Base() {
  740. if commit := r.state.LoadCommit(prs.Height); commit != nil {
  741. r.stateCh.Out <- p2p.Envelope{
  742. To: ps.peerID,
  743. Message: &tmcons.VoteSetMaj23{
  744. Height: prs.Height,
  745. Round: commit.Round,
  746. Type: tmproto.PrecommitType,
  747. BlockID: commit.BlockID.ToProto(),
  748. },
  749. }
  750. time.Sleep(r.state.config.PeerQueryMaj23SleepDuration)
  751. }
  752. }
  753. }
  754. time.Sleep(r.state.config.PeerQueryMaj23SleepDuration)
  755. continue OUTER_LOOP
  756. }
  757. }
  758. // processPeerUpdate process a peer update message. For new or reconnected peers,
  759. // we create a peer state if one does not exist for the peer, which should always
  760. // be the case, and we spawn all the relevant goroutine to broadcast messages to
  761. // the peer. During peer removal, we remove the peer for our set of peers and
  762. // signal to all spawned goroutines to gracefully exit in a non-blocking manner.
  763. func (r *Reactor) processPeerUpdate(peerUpdate p2p.PeerUpdate) {
  764. r.Logger.Debug("received peer update", "peer", peerUpdate.NodeID, "status", peerUpdate.Status)
  765. r.mtx.Lock()
  766. defer r.mtx.Unlock()
  767. switch peerUpdate.Status {
  768. case p2p.PeerStatusUp:
  769. // Do not allow starting new broadcasting goroutines after reactor shutdown
  770. // has been initiated. This can happen after we've manually closed all
  771. // peer goroutines and closed r.closeCh, but the router still sends in-flight
  772. // peer updates.
  773. if !r.IsRunning() {
  774. return
  775. }
  776. var (
  777. ps *PeerState
  778. ok bool
  779. )
  780. ps, ok = r.peers[peerUpdate.NodeID]
  781. if !ok {
  782. ps = NewPeerState(r.Logger, peerUpdate.NodeID)
  783. r.peers[peerUpdate.NodeID] = ps
  784. }
  785. if !ps.IsRunning() {
  786. // Set the peer state's closer to signal to all spawned goroutines to exit
  787. // when the peer is removed. We also set the running state to ensure we
  788. // do not spawn multiple instances of the same goroutines and finally we
  789. // set the waitgroup counter so we know when all goroutines have exited.
  790. ps.broadcastWG.Add(3)
  791. ps.SetRunning(true)
  792. // start goroutines for this peer
  793. go r.gossipDataRoutine(ps)
  794. go r.gossipVotesRoutine(ps)
  795. go r.queryMaj23Routine(ps)
  796. // Send our state to the peer. If we're fast-syncing, broadcast a
  797. // RoundStepMessage later upon SwitchToConsensus().
  798. if !r.waitSync {
  799. go r.sendNewRoundStepMessage(ps.peerID)
  800. }
  801. }
  802. case p2p.PeerStatusDown:
  803. ps, ok := r.peers[peerUpdate.NodeID]
  804. if ok && ps.IsRunning() {
  805. // signal to all spawned goroutines for the peer to gracefully exit
  806. ps.closer.Close()
  807. go func() {
  808. // Wait for all spawned broadcast goroutines to exit before marking the
  809. // peer state as no longer running and removal from the peers map.
  810. ps.broadcastWG.Wait()
  811. r.mtx.Lock()
  812. delete(r.peers, peerUpdate.NodeID)
  813. r.mtx.Unlock()
  814. ps.SetRunning(false)
  815. }()
  816. }
  817. }
  818. }
  819. // handleStateMessage handles envelopes sent from peers on the StateChannel.
  820. // An error is returned if the message is unrecognized or if validation fails.
  821. // If we fail to find the peer state for the envelope sender, we perform a no-op
  822. // and return. This can happen when we process the envelope after the peer is
  823. // removed.
  824. func (r *Reactor) handleStateMessage(envelope p2p.Envelope, msgI Message) error {
  825. ps, ok := r.GetPeerState(envelope.From)
  826. if !ok || ps == nil {
  827. r.Logger.Debug("failed to find peer state", "peer", envelope.From, "ch_id", "StateChannel")
  828. return nil
  829. }
  830. switch msg := envelope.Message.(type) {
  831. case *tmcons.NewRoundStep:
  832. r.state.mtx.RLock()
  833. initialHeight := r.state.state.InitialHeight
  834. r.state.mtx.RUnlock()
  835. if err := msgI.(*NewRoundStepMessage).ValidateHeight(initialHeight); err != nil {
  836. r.Logger.Error("peer sent us an invalid msg", "msg", msg, "err", err)
  837. return err
  838. }
  839. ps.ApplyNewRoundStepMessage(msgI.(*NewRoundStepMessage))
  840. case *tmcons.NewValidBlock:
  841. ps.ApplyNewValidBlockMessage(msgI.(*NewValidBlockMessage))
  842. case *tmcons.HasVote:
  843. ps.ApplyHasVoteMessage(msgI.(*HasVoteMessage))
  844. case *tmcons.VoteSetMaj23:
  845. r.state.mtx.RLock()
  846. height, votes := r.state.Height, r.state.Votes
  847. r.state.mtx.RUnlock()
  848. if height != msg.Height {
  849. return nil
  850. }
  851. vsmMsg := msgI.(*VoteSetMaj23Message)
  852. // peer claims to have a maj23 for some BlockID at <H,R,S>
  853. err := votes.SetPeerMaj23(msg.Round, msg.Type, ps.peerID, vsmMsg.BlockID)
  854. if err != nil {
  855. return err
  856. }
  857. // Respond with a VoteSetBitsMessage showing which votes we have and
  858. // consequently shows which we don't have.
  859. var ourVotes *bits.BitArray
  860. switch vsmMsg.Type {
  861. case tmproto.PrevoteType:
  862. ourVotes = votes.Prevotes(msg.Round).BitArrayByBlockID(vsmMsg.BlockID)
  863. case tmproto.PrecommitType:
  864. ourVotes = votes.Precommits(msg.Round).BitArrayByBlockID(vsmMsg.BlockID)
  865. default:
  866. panic("bad VoteSetBitsMessage field type; forgot to add a check in ValidateBasic?")
  867. }
  868. eMsg := &tmcons.VoteSetBits{
  869. Height: msg.Height,
  870. Round: msg.Round,
  871. Type: msg.Type,
  872. BlockID: msg.BlockID,
  873. }
  874. if votesProto := ourVotes.ToProto(); votesProto != nil {
  875. eMsg.Votes = *votesProto
  876. }
  877. r.voteSetBitsCh.Out <- p2p.Envelope{
  878. To: envelope.From,
  879. Message: eMsg,
  880. }
  881. default:
  882. return fmt.Errorf("received unknown message on StateChannel: %T", msg)
  883. }
  884. return nil
  885. }
  886. // handleDataMessage handles envelopes sent from peers on the DataChannel. If we
  887. // fail to find the peer state for the envelope sender, we perform a no-op and
  888. // return. This can happen when we process the envelope after the peer is
  889. // removed.
  890. func (r *Reactor) handleDataMessage(envelope p2p.Envelope, msgI Message) error {
  891. logger := r.Logger.With("peer", envelope.From, "ch_id", "DataChannel")
  892. ps, ok := r.GetPeerState(envelope.From)
  893. if !ok || ps == nil {
  894. r.Logger.Debug("failed to find peer state")
  895. return nil
  896. }
  897. if r.WaitSync() {
  898. logger.Info("ignoring message received during sync", "msg", msgI)
  899. return nil
  900. }
  901. switch msg := envelope.Message.(type) {
  902. case *tmcons.Proposal:
  903. pMsg := msgI.(*ProposalMessage)
  904. ps.SetHasProposal(pMsg.Proposal)
  905. r.state.peerMsgQueue <- msgInfo{pMsg, envelope.From}
  906. case *tmcons.ProposalPOL:
  907. ps.ApplyProposalPOLMessage(msgI.(*ProposalPOLMessage))
  908. case *tmcons.BlockPart:
  909. bpMsg := msgI.(*BlockPartMessage)
  910. ps.SetHasProposalBlockPart(bpMsg.Height, bpMsg.Round, int(bpMsg.Part.Index))
  911. r.Metrics.BlockParts.With("peer_id", string(envelope.From)).Add(1)
  912. r.state.peerMsgQueue <- msgInfo{bpMsg, envelope.From}
  913. default:
  914. return fmt.Errorf("received unknown message on DataChannel: %T", msg)
  915. }
  916. return nil
  917. }
  918. // handleVoteMessage handles envelopes sent from peers on the VoteChannel. If we
  919. // fail to find the peer state for the envelope sender, we perform a no-op and
  920. // return. This can happen when we process the envelope after the peer is
  921. // removed.
  922. func (r *Reactor) handleVoteMessage(envelope p2p.Envelope, msgI Message) error {
  923. logger := r.Logger.With("peer", envelope.From, "ch_id", "VoteChannel")
  924. ps, ok := r.GetPeerState(envelope.From)
  925. if !ok || ps == nil {
  926. r.Logger.Debug("failed to find peer state")
  927. return nil
  928. }
  929. if r.WaitSync() {
  930. logger.Info("ignoring message received during sync", "msg", msgI)
  931. return nil
  932. }
  933. switch msg := envelope.Message.(type) {
  934. case *tmcons.Vote:
  935. r.state.mtx.RLock()
  936. height, valSize, lastCommitSize := r.state.Height, r.state.Validators.Size(), r.state.LastCommit.Size()
  937. r.state.mtx.RUnlock()
  938. vMsg := msgI.(*VoteMessage)
  939. ps.EnsureVoteBitArrays(height, valSize)
  940. ps.EnsureVoteBitArrays(height-1, lastCommitSize)
  941. ps.SetHasVote(vMsg.Vote)
  942. r.state.peerMsgQueue <- msgInfo{vMsg, envelope.From}
  943. default:
  944. return fmt.Errorf("received unknown message on VoteChannel: %T", msg)
  945. }
  946. return nil
  947. }
  948. // handleVoteSetBitsMessage handles envelopes sent from peers on the
  949. // VoteSetBitsChannel. If we fail to find the peer state for the envelope sender,
  950. // we perform a no-op and return. This can happen when we process the envelope
  951. // after the peer is removed.
  952. func (r *Reactor) handleVoteSetBitsMessage(envelope p2p.Envelope, msgI Message) error {
  953. logger := r.Logger.With("peer", envelope.From, "ch_id", "VoteSetBitsChannel")
  954. ps, ok := r.GetPeerState(envelope.From)
  955. if !ok || ps == nil {
  956. r.Logger.Debug("failed to find peer state")
  957. return nil
  958. }
  959. if r.WaitSync() {
  960. logger.Info("ignoring message received during sync", "msg", msgI)
  961. return nil
  962. }
  963. switch msg := envelope.Message.(type) {
  964. case *tmcons.VoteSetBits:
  965. r.state.mtx.RLock()
  966. height, votes := r.state.Height, r.state.Votes
  967. r.state.mtx.RUnlock()
  968. vsbMsg := msgI.(*VoteSetBitsMessage)
  969. if height == msg.Height {
  970. var ourVotes *bits.BitArray
  971. switch msg.Type {
  972. case tmproto.PrevoteType:
  973. ourVotes = votes.Prevotes(msg.Round).BitArrayByBlockID(vsbMsg.BlockID)
  974. case tmproto.PrecommitType:
  975. ourVotes = votes.Precommits(msg.Round).BitArrayByBlockID(vsbMsg.BlockID)
  976. default:
  977. panic("bad VoteSetBitsMessage field type; forgot to add a check in ValidateBasic?")
  978. }
  979. ps.ApplyVoteSetBitsMessage(vsbMsg, ourVotes)
  980. } else {
  981. ps.ApplyVoteSetBitsMessage(vsbMsg, nil)
  982. }
  983. default:
  984. return fmt.Errorf("received unknown message on VoteSetBitsChannel: %T", msg)
  985. }
  986. return nil
  987. }
  988. // handleMessage handles an Envelope sent from a peer on a specific p2p Channel.
  989. // It will handle errors and any possible panics gracefully. A caller can handle
  990. // any error returned by sending a PeerError on the respective channel.
  991. //
  992. // NOTE: We block on consensus state for proposals, block parts, and votes.
  993. func (r *Reactor) handleMessage(chID p2p.ChannelID, envelope p2p.Envelope) (err error) {
  994. defer func() {
  995. if e := recover(); e != nil {
  996. err = fmt.Errorf("panic in processing message: %v", e)
  997. r.Logger.Error("recovering from processing message panic", "err", err)
  998. }
  999. }()
  1000. // Just skip the entire message during syncing so that we can
  1001. // process fewer messages.
  1002. if r.WaitSync() {
  1003. return
  1004. }
  1005. // We wrap the envelope's message in a Proto wire type so we can convert back
  1006. // the domain type that individual channel message handlers can work with. We
  1007. // do this here once to avoid having to do it for each individual message type.
  1008. // and because a large part of the core business logic depends on these
  1009. // domain types opposed to simply working with the Proto types.
  1010. protoMsg := new(tmcons.Message)
  1011. if err := protoMsg.Wrap(envelope.Message); err != nil {
  1012. return err
  1013. }
  1014. msgI, err := MsgFromProto(protoMsg)
  1015. if err != nil {
  1016. return err
  1017. }
  1018. r.Logger.Debug("received message", "ch_id", chID, "message", msgI, "peer", envelope.From)
  1019. switch chID {
  1020. case StateChannel:
  1021. err = r.handleStateMessage(envelope, msgI)
  1022. case DataChannel:
  1023. err = r.handleDataMessage(envelope, msgI)
  1024. case VoteChannel:
  1025. err = r.handleVoteMessage(envelope, msgI)
  1026. case VoteSetBitsChannel:
  1027. err = r.handleVoteSetBitsMessage(envelope, msgI)
  1028. default:
  1029. err = fmt.Errorf("unknown channel ID (%d) for envelope (%v)", chID, envelope)
  1030. }
  1031. return err
  1032. }
  1033. // processStateCh initiates a blocking process where we listen for and handle
  1034. // envelopes on the StateChannel. Any error encountered during message
  1035. // execution will result in a PeerError being sent on the StateChannel. When
  1036. // the reactor is stopped, we will catch the signal and close the p2p Channel
  1037. // gracefully.
  1038. func (r *Reactor) processStateCh() {
  1039. defer r.stateCh.Close()
  1040. for {
  1041. select {
  1042. case envelope := <-r.stateCh.In:
  1043. if err := r.handleMessage(r.stateCh.ID, envelope); err != nil {
  1044. r.Logger.Error("failed to process message", "ch_id", r.stateCh.ID, "envelope", envelope, "err", err)
  1045. r.stateCh.Error <- p2p.PeerError{
  1046. NodeID: envelope.From,
  1047. Err: err,
  1048. }
  1049. }
  1050. case <-r.stateCloseCh:
  1051. r.Logger.Debug("stopped listening on StateChannel; closing...")
  1052. return
  1053. }
  1054. }
  1055. }
  1056. // processDataCh initiates a blocking process where we listen for and handle
  1057. // envelopes on the DataChannel. Any error encountered during message
  1058. // execution will result in a PeerError being sent on the DataChannel. When
  1059. // the reactor is stopped, we will catch the signal and close the p2p Channel
  1060. // gracefully.
  1061. func (r *Reactor) processDataCh() {
  1062. defer r.dataCh.Close()
  1063. for {
  1064. select {
  1065. case envelope := <-r.dataCh.In:
  1066. if err := r.handleMessage(r.dataCh.ID, envelope); err != nil {
  1067. r.Logger.Error("failed to process message", "ch_id", r.dataCh.ID, "envelope", envelope, "err", err)
  1068. r.dataCh.Error <- p2p.PeerError{
  1069. NodeID: envelope.From,
  1070. Err: err,
  1071. }
  1072. }
  1073. case <-r.closeCh:
  1074. r.Logger.Debug("stopped listening on DataChannel; closing...")
  1075. return
  1076. }
  1077. }
  1078. }
  1079. // processVoteCh initiates a blocking process where we listen for and handle
  1080. // envelopes on the VoteChannel. Any error encountered during message
  1081. // execution will result in a PeerError being sent on the VoteChannel. When
  1082. // the reactor is stopped, we will catch the signal and close the p2p Channel
  1083. // gracefully.
  1084. func (r *Reactor) processVoteCh() {
  1085. defer r.voteCh.Close()
  1086. for {
  1087. select {
  1088. case envelope := <-r.voteCh.In:
  1089. if err := r.handleMessage(r.voteCh.ID, envelope); err != nil {
  1090. r.Logger.Error("failed to process message", "ch_id", r.voteCh.ID, "envelope", envelope, "err", err)
  1091. r.voteCh.Error <- p2p.PeerError{
  1092. NodeID: envelope.From,
  1093. Err: err,
  1094. }
  1095. }
  1096. case <-r.closeCh:
  1097. r.Logger.Debug("stopped listening on VoteChannel; closing...")
  1098. return
  1099. }
  1100. }
  1101. }
  1102. // processVoteCh initiates a blocking process where we listen for and handle
  1103. // envelopes on the VoteSetBitsChannel. Any error encountered during message
  1104. // execution will result in a PeerError being sent on the VoteSetBitsChannel.
  1105. // When the reactor is stopped, we will catch the signal and close the p2p
  1106. // Channel gracefully.
  1107. func (r *Reactor) processVoteSetBitsCh() {
  1108. defer r.voteSetBitsCh.Close()
  1109. for {
  1110. select {
  1111. case envelope := <-r.voteSetBitsCh.In:
  1112. if err := r.handleMessage(r.voteSetBitsCh.ID, envelope); err != nil {
  1113. r.Logger.Error("failed to process message", "ch_id", r.voteSetBitsCh.ID, "envelope", envelope, "err", err)
  1114. r.voteSetBitsCh.Error <- p2p.PeerError{
  1115. NodeID: envelope.From,
  1116. Err: err,
  1117. }
  1118. }
  1119. case <-r.closeCh:
  1120. r.Logger.Debug("stopped listening on VoteSetBitsChannel; closing...")
  1121. return
  1122. }
  1123. }
  1124. }
  1125. // processPeerUpdates initiates a blocking process where we listen for and handle
  1126. // PeerUpdate messages. When the reactor is stopped, we will catch the signal and
  1127. // close the p2p PeerUpdatesCh gracefully.
  1128. func (r *Reactor) processPeerUpdates() {
  1129. defer r.peerUpdates.Close()
  1130. for {
  1131. select {
  1132. case peerUpdate := <-r.peerUpdates.Updates():
  1133. r.processPeerUpdate(peerUpdate)
  1134. case <-r.closeCh:
  1135. r.Logger.Debug("stopped listening on peer updates channel; closing...")
  1136. return
  1137. }
  1138. }
  1139. }
  1140. func (r *Reactor) peerStatsRoutine() {
  1141. for {
  1142. if !r.IsRunning() {
  1143. r.Logger.Info("stopping peerStatsRoutine")
  1144. return
  1145. }
  1146. select {
  1147. case msg := <-r.state.statsMsgQueue:
  1148. ps, ok := r.GetPeerState(msg.PeerID)
  1149. if !ok || ps == nil {
  1150. r.Logger.Debug("attempt to update stats for non-existent peer", "peer", msg.PeerID)
  1151. continue
  1152. }
  1153. switch msg.Msg.(type) {
  1154. case *VoteMessage:
  1155. if numVotes := ps.RecordVote(); numVotes%votesToContributeToBecomeGoodPeer == 0 {
  1156. r.peerUpdates.SendUpdate(p2p.PeerUpdate{
  1157. NodeID: msg.PeerID,
  1158. Status: p2p.PeerStatusGood,
  1159. })
  1160. }
  1161. case *BlockPartMessage:
  1162. if numParts := ps.RecordBlockPart(); numParts%blocksToContributeToBecomeGoodPeer == 0 {
  1163. r.peerUpdates.SendUpdate(p2p.PeerUpdate{
  1164. NodeID: msg.PeerID,
  1165. Status: p2p.PeerStatusGood,
  1166. })
  1167. }
  1168. }
  1169. case <-r.closeCh:
  1170. return
  1171. }
  1172. }
  1173. }
  1174. func (r *Reactor) GetConsensusState() *State {
  1175. return r.state
  1176. }