Consensus engine
The consensus engine drives agreement on the next block at each blockchain height. It moves through ordered rounds, gathers a proposal and validator votes, waits when progress is slow, and commits only after more than two-thirds of voting power supports the same block. It also records enough ordered input to resume safely after a crash and connects the local state machine to peers.
Consensus state
The consensus state is the shared view of the current height, round, and step. It also tracks the proposal, block data, validator votes, the block lock, and the latest block that has enough prevotes to guide a later round.
State machine
The state machine processes peer messages, local validator actions, and timer events through one ordered path. This keeps proposals, block parts, votes, and timeouts from changing the round state in conflicting orders.
The normal path is:
flowchart LR
NewHeight[New height] --> NewRound[New round]
NewRound --> Propose[Propose]
Propose --> Prevote[Prevote]
Prevote --> PrevoteWait[Prevote wait]
PrevoteWait --> Precommit[Precommit]
Precommit --> PrecommitWait[Precommit wait]
PrecommitWait --> Commit[Commit]
Commit --> NewHeight
Propose -. timeout .-> Prevote
PrevoteWait -. timeout .-> Precommit
PrecommitWait -. timeout .-> NextRound[Next round]
NextRound --> Propose
NewHeight waits for the height start deadline. That deadline comes from the commit timeout or an application pacing rule. NewRound selects the proposer for the round. The three main steps then test a proposal, collect prevotes, and collect precommits. A block becomes committed only when the commit step has the required precommits and the complete block data.
The state installs the active height, round, and proposer in the propagation layer as one update. This matters after WAL replay because replay can resume at a later round with a different proposer. A cached proposal is checked only after this complete context is installed, and it is handed to consensus at most once. If the local validator is the active proposer, startup can also reseed the propagation layer with a replayed proposal and the exact parts named by its proposal identity.
The state contract requires height, round, and step to move forward. Accepted signed data must remain verifiable. Every height change must have a stored block with a two-thirds precommit proof.
References: consensus/…/round_state.go• L13-31 consensus/state.go• L463-478 consensus/state.go• L719-753 consensus/state.go• L972-1055 consensus/state.go• L1199-1277 consensus/state.go• L344-356 consensus/…/reactor.go• L344-356 consensus/state_test.go• L577-620 consensus/state_fuzz_test.go• L631-699
Round state
The round state carries the information needed to preserve safety while a height moves through several rounds. A locked block is the block a validator must continue to support until a later proof permits a safe change. A valid block is a block with more than two-thirds of prevotes, even when the application has not yet accepted it for voting.
Votes are grouped by height, round, and type. The vote set can detect a majority for one block or for any value. That distinction lets the state machine either commit a block or move forward after learning that enough validators have participated without agreeing on one block.
References: consensus/…/round_state.go• L64-103 consensus/…/height_vote_set.go• L27-50 consensus/…/height_vote_set.go• L131-182 consensus/state.go• L2632-2746
Proposal and vote flow
Consensus turns a proposer’s block into a commit through two kinds of signed vote. The proposal supplies the candidate block and may point to an earlier round with enough prevotes. The prevote tests the candidate. The precommit records whether the validator supports a block that has the required prevote proof.
Proposal flow
At each round, one validator is the proposer. The proposer builds a block from the current state and available transactions, signs the proposal, and passes it to the propagation layer. Other nodes accept the proposal only for the active height and round and only when it identifies the expected proposer and a valid block-part set.
The state waits for the proposal and the complete block parts before it can prevote the block. If the proposal names a previous proof of lock change, the related prevotes must also arrive before the proposal is complete. The application can accept or reject the proposed block before the local validator prevotes it, so an application rejection leads to a nil prevote. Block validation and application execution are part of the wider state execution flow.
When a proposer re-proposes a valid block from an earlier round, it reuses the original block parts whenever rebuilding the block would produce a different part-set header. This preserves the block identity that locked validators already support. Replayed or otherwise stored parts can lack transaction positions, so the propagation layer sends no transaction metadata for those parts and peers fetch them instead of rebuilding them from their mempool.
The proposal and its parts identify one block through both the block hash and the part-set header. The engine does not promote, lock, or commit a body when either part of that identity differs. If a node learns a valid block or commit for a body it does not have, it drops any mismatched body and waits for the matching parts before voting or finalizing.
When the active protocol version requires canonical block encoding, a complete part set must also match the block’s standard encoding. The node keeps a non-canonical block so it can finish a commit that already has enough precommits, but it does not prevote, mark it valid, or lock it during normal voting. This rule starts at the height after the application version becomes active, so older protocol versions keep their prior voting behavior.
The consensus block limit also applies to the proposal identity and the received body. The active height installs its Block.MaxBytes value in the propagation layer, which rejects oversized compact proposals and complete part sets. A change in that limit takes effect when consensus enters the next height.
References: consensus/state.go• L1331-1484 consensus/state.go• L1544-1651 consensus/state.go• L1615-1665 consensus/state.go• L1707-1735 consensus/state.go• L2240-2312 consensus/state.go• L2315-2441 consensus/state.go• L1865-1977 consensus/state.go• L2493-2515 consensus/state.go• L2525-2627 consensus/state.go• L2632-2654 consensus/state.go• L2691-2704 consensus/state.go• L992-995 consensus/state_test.go• L307-395 consensus/state_test.go• L397-499 consensus/state_test.go• L555-673 consensus/state_test.go• L684-843 consensus/state_repropose_test.go• L18-175 consensus/state_block_max_bytes_test.go• L35-54 consensus/state_partset_identity_test.go• L47-85 consensus/…/commitment.go• L180-266 consensus/…/catchup.go• L151-287
Vote flow
A validator prevotes its locked block when it has one. Otherwise it prevotes the complete proposal after consensus and application checks. It prevotes nil when the proposal is missing, incomplete, or not accepted. When a round has more than two-thirds of prevotes for one block, the validator can lock that block and precommit it. In every other case it precommits nil.
The vote set drives progress as votes arrive. A majority for any value can move a node into a wait step or skip it to a later round. A majority for one block can update the valid block, create a commit, or unlock a prior choice when the protocol permits it. The block match includes the part-set header, so a majority for a block hash does not promote or lock a locally held body with different parts. Conflicting signed votes are reported as evidence rather than treated as normal progress.
The state journals a local signed vote before it acts on that vote. This ordering matters when an earlier prevote quorum would otherwise start the precommit path before the local prevote reaches the state queue. If signing fails, the local vote is not queued and the state can still handle the quorum without treating an unrecorded signature as durable.
The application may add vote extensions at heights where they are enabled. The validator creates an extension when it makes a non-nil precommit, and the node checks received extensions before it uses those precommits. The next proposer receives the accepted extensions from the last commit as part of its proposal input. For the related message formats, validation rules, and validator signing boundary, see Consensus messages and validator signing.
References: consensus/state.go• L1570-1651 consensus/state.go• L1690-1830 consensus/state.go• L1769-1812 consensus/state.go• L2488-2839 consensus/state.go• L2842-2950 consensus/state.go• L2940-2993 consensus/state.go• L3001-3079 consensus/state.go• L3050-3179 consensus/…/height_vote_set.go• L131-182 consensus/state_test.go• L1500-1612 consensus/state_test.go• L1694-1824 consensus/state_partset_identity_test.go• L87-272 consensus/state_fuzz_test.go• L631-727
Timeout progression
Timeouts provide liveness when a proposal or enough votes do not arrive. Each timer carries a height, round, and step, so an old timeout cannot move a node backward after it has learned more recent consensus state.
Timeout steps
The timeout path follows the same ordered state machine as message-driven progress:
- The new-height timeout starts round zero after the commit wait or the application’s height pacing deadline.
- The propose timeout causes a prevote, usually nil when no usable proposal is ready.
- The prevote-wait timeout causes a precommit after the node has seen enough participation.
- The precommit-wait timeout causes a precommit if needed and advances to the next round.
The propose, prevote, and precommit waits use a base duration plus a round-dependent increase. This gives later proposers more time after an earlier round fails. The application can also set a dynamic height interval that delays round zero after a commit. A delayed-precommit rule can hold the first precommit until the height’s required time, except while the node catches up. Legacy pacing settings can replace the application pacing rule for chains that still use them.
If the configured policy allows it, a node can skip the new-height wait after it receives all precommits for the previous height. Dynamic application pacing disables that skip because its deadline is the height’s liveness floor.
References: consensus/ticker.go• L14-30 consensus/ticker.go• L99-143 consensus/state.go• L719-753 consensus/state.go• L972-1055 consensus/state.go• L646-701 consensus/state.go• L1112-1187 consensus/state.go• L1327-1375 consensus/state.go• L1397-1507 consensus/state.go• L1690-1725 consensus/state.go• L1942-1978 consensus/state.go• L2217-2235 consensus/state.go• L2508-2527 consensus/state.go• L2952-2957 consensus/state.go• L3165-3170 consensus/state_test.go• L2076-2195 consensus/state_fuzz_test.go• L139-157
Height transition
When more than two-thirds of precommits support a block, the engine enters commit. It waits for the committed block and its complete matching part set if they are not local, then saves the block and applies it to the application state. The next height starts in NewHeight with the new validator set and the commit as its last commit.
If a round fails to produce a block, the engine advances the round and changes the proposer. If a later round reaches a commit for a block already known as valid, the engine can reuse that block and its original parts instead of creating a new one.
The commit path does not finalize on the block hash alone. It drops a locally held body when its part-set header differs from the committed header, requests the committed parts, and waits until those parts are complete before applying the block. A commit for an earlier round remains final for the height and does not send the state back to a later precommit wait.
References: consensus/state.go• L1227-1305 consensus/state.go• L1842-2117 consensus/state.go• L2096-2184 consensus/state.go• L2300-2409 consensus/state.go• L3020-3079 consensus/…/catchup.go• L15-140 consensus/state_test.go• L2197-2417 consensus/state_partset_identity_test.go• L274-383
WAL recovery
The consensus write-ahead log (WAL) records state steps, incoming messages, local messages, timeouts, and lock changes before or as the state machine processes them. It gives a recovering node the ordered inputs needed to rebuild an unfinished height and reduces the risk that a validator signs a conflicting proposal or vote after a crash.
WAL replay
On start, the engine finds the last completed height marker and replays the records for the current unfinished height. Replay feeds proposals, block parts, votes, timeouts, and lock records back through the same state transitions used during normal operation. A lock record stores the locked block identity and time. Round -1 records an unlock. Height markers are boundaries because the block store and application handshake handle completed blocks.
Replay restores the round-specific validator context before peer processing starts. A node that resumes at a later round must validate proposals against that round’s proposer. Replayed proposal parts that are not the active lock or valid block remain available to the propagation layer so peers can recover a proposal held at another round. If the replayed proposal belongs to the active local proposer, startup reseeds propagation with the proposal and a part set that matches its recorded BlockID; this avoids changing the identity when a valid block was originally encoded in a different form. Before the node joins consensus, it can inspect recent commits for a signature from its own validator key. A matching past signature stops startup because signing again could create a double-signing risk.
The WAL protects two recovery cases. A crash during consensus replays the unfinished message sequence. A crash while applying a block uses the application’s reported height and app hash to replay stored blocks and bring the application and node state back into agreement before consensus resumes.
References: consensus/state.go• L396-504 consensus/state.go• L940-1072 consensus/state.go• L541-550 consensus/state.go• L1707-1735 consensus/state.go• L2969-2992 consensus/reactor.go• L134-176 consensus/wal.go• L35-65 consensus/wal.go• L239-297 consensus/wal.go• L368-429 consensus/replay.go• L22-166 consensus/replay.go• L242-471 consensus/state_test.go• L2743-2870 consensus/state_repropose_test.go• L140-175
WAL durability
Messages from peers and timeout events are written to the WAL as the ordered loop receives them. Local proposals and votes are synchronised before the state loop processes them because they carry validator signatures. Unsigned block parts use buffered writes because they do not authorize a validator action. Lock records use the same buffered log after the related lock or unlock is decided, while the signed precommit has already been synchronised.
The WAL flushes buffered data on a regular interval and when it stops. Each record has a length and checksum. Replay reads the complete header and payload before it decodes the record, so a truncated, oversized, or altered record is treated as recovery data corruption instead of valid consensus input. Recovery can make one repair attempt after saving a copy of the damaged file. It must not treat damaged input as a valid consensus event.
References: consensus/state.go• L1054-1070 consensus/state.go• L1144-1170 consensus/state.go• L1189-1253 consensus/state.go• L2026-2029 consensus/state.go• L2044-2045 consensus/state.go• L2086-2093 consensus/wal.go• L102-230 consensus/wal.go• L299-346 consensus/wal.go• L368-429 consensus/state_test.go• L2901-2994 consensus/wal_test.go• L181-221 consensus/wal_test.go• L361-408
Locked block recovery
When a node uses an internal-only WAL, peer block parts are not all durable in the consensus log. If a height remains locked long enough, the engine makes a separate copy of the locked block parts so a restart can restore the lock without first asking a peer for the block.
The copy is a recovery aid, not a second consensus record. It is kept only for the current locked height and block identity. A complete copy is accepted only when its lock header, part proofs, decoded block, block hash, and part-set header all match the restored lock. A missing, stale, damaged, or partial copy is ignored and causes normal part recovery through the propagation layer. A committed height removes the pending copy, while a clean stop keeps a completed copy for the next start.
References: consensus/state.go• L421-429 consensus/state.go• L508-519 consensus/state.go• L2651-2654 consensus/state.go• L3555-3577 consensus/wal_blocks.go• L22-31 consensus/wal_blocks.go• L59-158 consensus/wal_blocks.go• L201-273 consensus/wal_blocks_linux.go• L8-10 consensus/wal_blocks_other.go• L1-8 consensus/wal_blocks_test.go• L75-178 consensus/wal_blocks_test.go• L268-326
Consensus reactor
The consensus reactor connects the local state machine to peers. It validates and routes incoming messages, tracks each peer’s height, round, step, proposal parts, and known votes, then sends the information that helps a peer catch up or progress. The detailed message contracts and validator signing boundary are described in Consensus messages and validator signing. The block-data exchange is described in Consensus propagation.
Message routing
Incoming consensus messages enter through the peer-to-peer layer and are decoded before routing. State messages update peer knowledge. Proposal metadata and block parts enter the consensus state queue. Votes enter the vote queue. The state machine then processes proposals, parts, and votes in order while peer bookkeeping can proceed separately.
The reactor uses separate paths for state hints, block data, votes, and vote-set bit hints. These paths let urgent votes move independently from larger block data. A peer that claims a two-thirds majority can receive a bit map that shows which votes are still missing locally.
The propagation reactor validates compact proposals against the proposer context for the active round. It caches proposals for a future height or round and retries them when that context becomes active. Conflicting block identities are rejected before they reach consensus. After replay, the active local proposer can reseed this layer with its own proposal so peers can again verify and request its block parts.
References: consensus/reactor.go• L178-220 consensus/reactor.go• L275-507 consensus/state.go• L1707-1735 consensus/…/reactor.go• L246-299 consensus/…/commitment.go• L180-266 consensus/…/commitment_state.go• L91-114 consensus/state_test.go• L2572-2645
Message dissemination
The state machine emits round, valid-block, and vote events. The reactor turns those events into broadcasts and uses each peer’s known state to choose follow-up data. It sends missing block parts, then sends the proposal and any proof-of-lock-change information. It sends votes for the peer’s current round and uses older commits to help peers that lag by one or more heights.
For a peer at the same height, the reactor sends the last commit at the start of the height, then proposal-lock prevotes, current prevotes, and precommits that the peer does not know. For a peer one height behind, it sends the last commit. For a peer at least two heights behind, it sends stored commit votes only when the peer’s height is in the local block store; it does not run this catch-up path for a peer at a higher height.
The propagation reactor spreads a compact proposal first, then uses have and want messages to find missing parts. Peers exchange only the parts they need. Proof checks bind recovered parts to the proposal or commitment. A complete block is then forwarded to consensus as original parts.
This design combines broadcast for fast awareness with per-peer gossip for repair and catch-up. A node can relay consensus data even when it has no validator key. During block or state sync, the reactor may receive state messages but delays normal consensus data processing until the node switches back to consensus.
For the detailed propagation lifecycle, see Consensus propagation.
References: consensus/reactor.go• L528-673 consensus/reactor.go• L698-885 consensus/reactor.go• L887-1045 consensus/reactor.go• L1053-1187 consensus/…/reactor.go• L246-299 consensus/…/commitment.go• L26-132 consensus/…/have_wants.go• L20-365 consensus/…/catchup.go• L15-287 consensus/…/peer_state.go• L358-410 consensus/reactor_test.go• L1-300
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to round steps or transition rules must stay aligned with round-state values, timeout handling, vote-majority checks, and the proposer context installed in propagation.
- Changes to application timeout or pacing rules must keep height start deadlines, delayed precommit behavior, the legacy pacing switch, and the early round-zero skip aligned.
- Changes to proposal, valid-block, lock, or commit matching must preserve the joint block-hash and part-set-header identity across vote proofs, propagation state, and finalization.
- Changes to valid-block re-proposal must preserve the original part set when re-encoding would change its part-set header, and must keep propagation metadata valid when stored parts have no transaction positions.
- Changes to canonical proposal encoding rules must keep the application-version gate aligned with nil voting, valid-block selection, locking, and commit-time finalization.
- Changes to block size limits must update both compact-proposal checks and completed-part checks, and must keep the active height’s limit aligned with the propagation layer.
- Changes to WAL durability must preserve the ordering between local signing, WAL sync, replay, round-specific proposer setup, lock records, and the completed-height marker.
- Changes to replayed proposal handling must keep the active proposer context and the recorded proposal
BlockIDaligned before the propagation layer is reseeded. - Changes to locked-block recovery must preserve the match between
LockInfo, every part proof, the decoded block, the block hash, and the lock’s fullBlockID. - Changes to timeout or delayed-precommit timing must preserve monotonic height-round-step progress and the catch-up exception.
- Changes to reactor channels or message routing must keep peer state updates, proposal and block-part delivery, vote catch-up, and non-blocking statistics behavior consistent.
- Changes to vote extensions must keep activation height, local extension creation, received extension checks, and next-height proposal input aligned.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A proposal does not arrive or its block is not complete before the propose timeout | The node prevotes nil and may move to another round | Step-specific timeout and proposer rotation |
| A cached proposal is checked against a proposer from a different round | A valid proposal can be rejected or a wrong proposal can be accepted | Install height, round, and proposer together before validation |
| A dynamic height pacing rule is applied as a normal round timeout | Validators can start round zero before the application’s liveness floor | Apply pacing to the height start deadline and disable the early skip |
| A proposer re-encodes a valid block with a different part-set header after an earlier polka | Locked and unlocked validators prevote different block identities, so the height can stall | Re-propose the original parts that received the polka |
| A proposal or local block has the committed hash but a different part-set header | The node cannot safely promote or finalize that body | Match both the block hash and part-set header |
| A complete proposal uses a non-canonical block encoding while the protocol rule is active | The node keeps the block but does not vote for it, mark it valid, or lock it | Check canonical encoding before normal voting and skip only at commit |
A proposal declares too many parts or the received parts exceed Block.MaxBytes | The node accepts data that is outside the active consensus block limit | Check the part count and assembled size against the height’s limit |
| A node sees more than two-thirds prevotes for a block it does not have | It cannot safely commit and must fetch the matching block parts | Commitment tracking and identity-bound part recovery |
| A timeout belongs to an older height, round, or step | The node could move backward in the state machine | Ignore stale timeouts by comparing height, round, and step |
| A precommit quorum for an earlier round arrives after the node has committed | The node could incorrectly re-enter a precommit wait | Treat commit as final for the height |
| A local prevote is signed but its WAL record is not durable before quorum handling | A restart can replay a quorum without the local vote that led to the next step | Synchronise the signed vote before processing it and defer quorum handling while it is queued |
| A WAL record has a partial header, incomplete payload, invalid length, or bad checksum | The unfinished height cannot be replayed with confidence | Framed record checks, corruption detection, repair, and a saved copy |
| A WAL write or sync fails before a local signed message is processed | Recovery may lose the input needed to prevent a conflicting signature | Synchronous WAL writes for local proposals and votes |
| A locked-block copy is stale, incomplete, or does not match the full block identity | Restart cannot restore the lock bytes and must refetch the block | Verify the lock header, part proofs, decoded block, block hash, and part-set header |
| A recent commit contains a signature from the local validator key | Startup stops instead of risking a second signature | Bounded recent-commit scan before joining consensus |
| A validator needs vote extensions but a precommit has none or an invalid one | The vote cannot complete a successful commit | Enforce activation and verify extension data |
| A peer sends a vote that conflicts with an existing validator vote | The vote may indicate validator equivocation | Report conflicting votes to the evidence pool |
| A peer is ahead of the local height | The reactor may send irrelevant stored commit votes | Restrict commit catch-up to peers at least two heights behind and present in the block store |
Key files
| File | Why to read it |
|---|---|
| consensus/state.go | Primary owner of the ordered consensus loop, round transitions, proposals, votes, commits, timeouts, application pacing, startup checks, block limits, canonical proposal checks, valid-block re-proposals, and WAL recovery. |
| consensus/…/round_state.go | Defines the height, round, step, lock, valid block, proposal, and vote view used by the state machine. |
| consensus/…/height_vote_set.go | Explains how votes are grouped by round and how majority proofs drive progress. |
| consensus/ticker.go | Shows how step-specific timeouts are scheduled and stale timeouts are rejected. |
| consensus/wal.go | Defines the durable consensus log, lock records, record integrity checks, height markers, flushing, and replay reading. |
| consensus/wal_blocks.go | Describes the separate locked-block copy that supports recovery when the internal-only WAL does not retain peer parts. |
| consensus/replay.go | Describes recovery for an unfinished consensus height and for application block progress. |
| consensus/reactor.go | Connects consensus state to peers through message routing, event broadcasts, vote gossip, and height-aware catch-up. |
| consensus/…/reactor.go | Defines the block-data reactor and its connection to consensus peer state. |
| consensus/…/commitment.go | Shows compact proposal validation, commitment identity checks, and block-part dissemination. |
| consensus/…/catchup.go | Shows commitment-driven part requests and cached-proposal replay during catch-up. |
| consensus/state_repropose_test.go | Demonstrates that valid-block re-proposals and restart reseeding preserve the original block-part identity. |
| docs/consensus-messages-and-signing.md | Child page for consensus message types, validation, and validator signing. |
| docs/consensus-propagation.md | Child page for compact proposals, block-part exchange, and peer catch-up. |
| docs/state-execution.md | Related page for block validation, application processing, and commit execution around consensus. |