Tendermint BFT consensus as a pure state machine on urd.consensus.node.
The first genuinely BYZANTINE algorithm in the library: Raft tolerates crashes, Snowman is probabilistic, this one keeps agreement while f of 3f+1 validators lie.
The normative algorithm is the pseudocode of arXiv:1807.04938, The latest gossip on BFT consensus, transcribed in the CometBFT tree at
spec/consensus/consensus-paper/consensus.tex:28-174. Line numbers in the
comments below (L22, L28, ...) refer to it.
Two sources that look authoritative and are NOT used:
spec/consensus/consensus.md is STALE -- it documents the pre-2018
PoLC/unlock design, and spec/consensus/readme.md:26 says so.consensus/state.go:2362) where the paper uses f+1, and the L28 unlock is
not a prevote rule there at all but happens asynchronously inside addVote
(consensus/state.go:2312-2328). We implement the paper.The structure follows spec/ivy-proofs/tendermint.ivy, a mechanically
checked transliteration in which the paper's for the first time latches are
explicit relations -- done_l34(R), done_l36(R,V), done_l47(R)
(tendermint.ivy:86-88). Those three latches are the trickiest bookkeeping
in the protocol and having them named is worth the whole file.
Each of these is subtle enough that it has a dedicated test:
lockedRound <= vr -- less-than-or-EQUAL. And the 2f+1
prevotes it needs are at round vr, not at the current round.step >= prevote, so it also fires when we are already
in precommit; in that case ONLY validValue/validRound move.validRound := round_p, the current round, NOT the proposal's
vr.Signature verification happens outside this namespace, by construction:
ClojureScript's Web Crypto is promise-based so geheimnis' Ed25519 verify
returns a channel and cannot be called from a pure function. Events arrive
pre-authenticated with :verified-by, which is exactly the seam CometBFT's
own layering already has -- addVerifiedVote is documented "assumes
signature is valid" (types/vote_set.go:259-329).
A consequence worth stating: (vote -> signature) is NOT a function.
Signatures from a remote signer can be non-deterministic; CometBFT carries
ErrVoteNonDeterministicSignature (types/vote_set.go:215) precisely for
this. So two votes from the same validator for the same value with DIFFERENT
signature bytes are a duplicate, not equivocation. Only a differing value is
equivocation, and it is reported through report-equivocation rather than
dropped.
CometBFT fsyncs exactly the messages this node SIGNS -- its own proposal,
prevote, precommit -- plus the end-height marker, in one function with a
default: panic so that a new self-message type cannot silently skip the
barrier (consensus/state.go:881-905). signed-message-step below is that
function, throw and all.
The ordering is STRICTER than Raft's: sign -> fsync -> apply -> broadcast,
with the fsync happening before this state machine even sees its own message.
So a signed message is put in :pending-signed, the broadcast rides in the
:then of a persist-then, and the vote is added to our own tally only when
the persisted event comes back. A node that crashes before its own fsync
has not voted, which is the entire point.
.cljc, no host interop, no clock, no RNG. Timeouts are logical ticks and
grow per round: timeout(r) = init + r*delta, reset at each height.
Tendermint BFT consensus as a pure state machine on `urd.consensus.node`. The first genuinely BYZANTINE algorithm in the library: Raft tolerates crashes, Snowman is probabilistic, this one keeps agreement while f of 3f+1 validators lie. ## Source of truth The normative algorithm is the pseudocode of arXiv:1807.04938, `The latest gossip on BFT consensus`, transcribed in the CometBFT tree at `spec/consensus/consensus-paper/consensus.tex:28-174`. Line numbers in the comments below (`L22`, `L28`, ...) refer to it. Two sources that look authoritative and are NOT used: - `spec/consensus/consensus.md` is STALE -- it documents the pre-2018 PoLC/unlock design, and `spec/consensus/readme.md:26` says so. - CometBFT's own implementation DIVERGES from the paper in at least two places we deliberately did not copy: round-skip triggers on 2f+1 (`consensus/state.go:2362`) where the paper uses f+1, and the L28 unlock is not a prevote rule there at all but happens asynchronously inside `addVote` (`consensus/state.go:2312-2328`). We implement the paper. The structure follows `spec/ivy-proofs/tendermint.ivy`, a mechanically checked transliteration in which the paper's `for the first time` latches are explicit relations -- `done_l34(R)`, `done_l36(R,V)`, `done_l47(R)` (`tendermint.ivy:86-88`). Those three latches are the trickiest bookkeeping in the protocol and having them named is worth the whole file. ## The rules, and the four traps Each of these is subtle enough that it has a dedicated test: - **L28's unlock is `lockedRound <= vr`** -- less-than-or-EQUAL. And the 2f+1 prevotes it needs are at round `vr`, not at the current round. - **L36 fires when `step >= prevote`**, so it also fires when we are already in precommit; in that case ONLY validValue/validRound move. - **L36 sets `validRound := round_p`**, the current round, NOT the proposal's `vr`. - **L47 has no step guard at all**, and **L49 decides on ANY round r**, not only the current one. ## Where the crypto is not Signature verification happens outside this namespace, by construction: ClojureScript's Web Crypto is promise-based so geheimnis' Ed25519 verify returns a channel and cannot be called from a pure function. Events arrive pre-authenticated with `:verified-by`, which is exactly the seam CometBFT's own layering already has -- `addVerifiedVote` is documented "assumes signature is valid" (`types/vote_set.go:259-329`). A consequence worth stating: `(vote -> signature)` is NOT a function. Signatures from a remote signer can be non-deterministic; CometBFT carries `ErrVoteNonDeterministicSignature` (`types/vote_set.go:215`) precisely for this. So two votes from the same validator for the same value with DIFFERENT signature bytes are a duplicate, not equivocation. Only a differing value is equivocation, and it is reported through `report-equivocation` rather than dropped. ## Durability CometBFT fsyncs exactly the messages this node SIGNS -- its own proposal, prevote, precommit -- plus the end-height marker, in one function with a `default: panic` so that a new self-message type cannot silently skip the barrier (`consensus/state.go:881-905`). `signed-message-step` below is that function, throw and all. The ordering is STRICTER than Raft's: sign -> fsync -> apply -> broadcast, with the fsync happening before this state machine even sees its own message. So a signed message is put in `:pending-signed`, the broadcast rides in the `:then` of a `persist-then`, and the vote is added to our own tally only when the `persisted` event comes back. A node that crashes before its own fsync has not voted, which is the entire point. ## Portability `.cljc`, no host interop, no clock, no RNG. Timeouts are logical ticks and grow per round: `timeout(r) = init + r*delta`, reset at each height.
Asking where everyone is, SEPARATELY from re-sending our own votes.
These rode the same timer, and that made the gossip interval the catch-up
rate: answer-status replies with exactly one height, so a lagging node
gained one height per gossip interval and no faster.
Measured on four validators over websockets, crash and restart, twenty seconds:
:gossip-interval 12 (default) gap 20 -> 40 never converges
:gossip-interval 3 gap 7 -> 0 fully caught up
Turning the gossip interval down fixes the lag and costs the whole vote
retransmission at the same rate -- the payload of a gossip tick is every
message we signed this round plus the last commit, while the payload of this
one is {:type :status :height h}, about forty bytes.
So they are two timers. CometBFT splits them for exactly this reason:
peer-state broadcast (reactor.go:516/:593) is continuous and separate
from gossipVotesRoutine.
Armed ONLY while behind, so a healthy cluster pays nothing at all for it.
Asking where everyone is, SEPARATELY from re-sending our own votes.
These rode the same timer, and that made the gossip interval the catch-up
rate: `answer-status` replies with exactly one height, so a lagging node
gained one height per gossip interval and no faster.
Measured on four validators over websockets, crash and restart, twenty
seconds:
:gossip-interval 12 (default) gap 20 -> 40 never converges
:gossip-interval 3 gap 7 -> 0 fully caught up
Turning the gossip interval down fixes the lag and costs the whole vote
retransmission at the same rate -- the payload of a `gossip` tick is every
message we signed this round plus the last commit, while the payload of this
one is `{:type :status :height h}`, about forty bytes.
So they are two timers. CometBFT splits them for exactly this reason:
peer-state broadcast (`reactor.go:516`/`:593`) is continuous and separate
from `gossipVotesRoutine`.
Armed ONLY while behind, so a healthy cluster pays nothing at all for it.(commit-certificate st r vid)The 2f+1 precommits that decided vid at [h r], as a relayable value.
This is what corroborated? exists to work around. Our ingest authenticates
the SENDER of a message and nothing else, so a forwarded precommit is a claim
by the forwarder -- which is why catch-up needs f+1 DISTINCT peers to say the
same thing independently before it can be believed. A certificate carries the
voters' own attestations, so one peer's answer is evidence rather than
hearsay, and the reply is verifiable by anyone holding the validator set.
Returns nil when no signing scheme is configured, which keeps the whole feature opt-in and leaves the f+1 path exactly as it was.
The 2f+1 precommits that decided `vid` at `[h r]`, as a relayable value. This is what `corroborated?` exists to work around. Our ingest authenticates the SENDER of a message and nothing else, so a forwarded precommit is a claim by the forwarder -- which is why catch-up needs f+1 DISTINCT peers to say the same thing independently before it can be believed. A certificate carries the voters' own attestations, so one peer's answer is evidence rather than hearsay, and the reply is verifiable by anyone holding the validator set. Returns nil when no signing scheme is configured, which keeps the whole feature opt-in and leaves the f+1 path exactly as it was.
(content-id proposal)Lowercase hexadecimal SHA-256 of a proposal map's canonical CBOR payload.
Build a proposal with (assoc payload :id (content-id payload)). Every top-level field except :id participates, including :validator-updates.
Lowercase hexadecimal SHA-256 of a proposal map's canonical CBOR payload. Build a proposal with (assoc payload :id (content-id payload)). Every top-level field except :id participates, including :validator-updates.
(decided st height)The value this node decided at height, or nil.
The value this node decided at `height`, or nil.
(fault-tolerance total)f, the number of Byzantine faults a set of total voting power tolerates.
Note f = 0 below 4: three equal validators tolerate NO Byzantine fault.
f, the number of Byzantine faults a set of `total` voting power tolerates. Note f = 0 below 4: three equal validators tolerate NO Byzantine fault.
Deliberately not carrying (height, round): retransmission is a property of the node, not of a round.
Deliberately not carrying (height, round): retransmission is a property of the node, not of a round.
(make)A Tendermint node. Per-node configuration -- :powers, :valid-fn, the
timeouts -- is supplied through the simulator's :node-config or merged into
init's config by a runtime.
A Tendermint node. Per-node configuration -- `:powers`, `:valid-fn`, the timeouts -- is supplied through the simulator's `:node-config` or merged into `init`'s config by a runtime.
(signed-message-step msg)Which signing step is this self-message, if any?
This is writeInternalMsgToWAL (consensus/state.go:881-905) transliterated,
default: panic included. The panic is the load-bearing part: it is what
stops someone adding a fourth self-message type in six months and having it
silently skip the fsync. An unsigned message must be listed explicitly as
unsigned, not merely omitted.
Which signing step is this self-message, if any? This is `writeInternalMsgToWAL` (`consensus/state.go:881-905`) transliterated, `default: panic` included. The panic is the load-bearing part: it is what stops someone adding a fourth self-message type in six months and having it silently skip the fsync. An unsigned message must be listed explicitly as unsigned, not merely omitted.
(test-attestations)A toy vote-attestation scheme, for tests and simulations only.
NOT CRYPTOGRAPHY -- a hash of the voter and the payload, so anyone reading
this namespace can mint one for any voter. What it supplies is the SHAPE: an
attestation binds a voter to a specific [height round value-id],
verification is a pure predicate, and anything not produced this way fails.
That is what a test needs to drive the certificate guard down both branches.
Unlike round-robin, init does NOT refuse to start without a scheme here --
certificates are opt-in and their absence falls back to f+1 corroboration --
so this stub is reachable by a caller who configures it. A deployment
supplies real signatures.
A toy vote-attestation scheme, for tests and simulations only. NOT CRYPTOGRAPHY -- a hash of the voter and the payload, so anyone reading this namespace can mint one for any voter. What it supplies is the SHAPE: an attestation binds a voter to a specific `[height round value-id]`, verification is a pure predicate, and anything not produced this way fails. That is what a test needs to drive the certificate guard down both branches. Unlike round-robin, `init` does NOT refuse to start without a scheme here -- certificates are opt-in and their absence falls back to f+1 corroboration -- so this stub is reachable by a caller who configures it. A deployment supplies real signatures.
(valid-certificate? st cert h vid)Does cert prove that 2f+1 of OUR validators precommitted vid at [h r]?
TWO checks carry the weight, and both were mutation-verified:
vote-payload, which binds the height,
the round and the value id. This is the whole security of the thing.membership the live
path uses, not by counting entries. distinct-voters means one validator
repeated cannot clear a threshold by itself.The rest is defence in depth, and is labelled as such because mutation says
so rather than because it feels weaker. Skipping the (= h (:height cert))
and (= vid (:value-id cert)) comparisons changes NO test outcome, and the
reason is the payload binding above: a certificate for another height or
another value carries signatures over that height and that value, so
verification fails on its own. They stay because a cheap explicit comparison
documents the intent and would keep holding if the payload ever narrowed --
not because they are currently stopping anything.
The (contains? vset voter) filter is the same: weight returns nothing for
a peer outside :powers, so padding a certificate with invented voters
already fails the threshold.
Returns false rather than throwing on anything malformed: this arrives from the network and a malformed certificate is a packet, not an error.
Does `cert` prove that 2f+1 of OUR validators precommitted `vid` at `[h r]`?
TWO checks carry the weight, and both were mutation-verified:
- every attestation VERIFIES over `vote-payload`, which binds the height,
the round and the value id. This is the whole security of the thing.
- the voters are counted by WEIGHT through the same `membership` the live
path uses, not by counting entries. `distinct-voters` means one validator
repeated cannot clear a threshold by itself.
The rest is defence in depth, and is labelled as such because mutation says
so rather than because it feels weaker. Skipping the `(= h (:height cert))`
and `(= vid (:value-id cert))` comparisons changes NO test outcome, and the
reason is the payload binding above: a certificate for another height or
another value carries signatures over that height and that value, so
verification fails on its own. They stay because a cheap explicit comparison
documents the intent and would keep holding if the payload ever narrowed --
not because they are currently stopping anything.
The `(contains? vset voter)` filter is the same: `weight` returns nothing for
a peer outside `:powers`, so padding a certificate with invented voters
already fails the threshold.
Returns false rather than throwing on anything malformed: this arrives from
the network and a malformed certificate is a packet, not an error.(valid-content-id? proposal)A :valid-fn for deployments using content-id. Compose with the application's schema, transaction, and reconfiguration checks, e.g. (every-pred valid-content-id? application-valid?). All validators must agree on the accepted payload representation.
A :valid-fn for deployments using content-id. Compose with the application's schema, transaction, and reconfiguration checks, e.g. (every-pred valid-content-id? application-valid?). All validators must agree on the accepted payload representation.
(value-id v)id(v). A map carrying :id supplies its own; anything else is its own id.
An explicit id is part of the existing vote format. For structured values, deployments must supply a deterministic :valid-fn that recomputes the canonical identifier from the entire consensus payload, including validator updates. All accepted values sharing an id must be identical.
`id(v)`. A map carrying `:id` supplies its own; anything else is its own id. An explicit id is part of the existing vote format. For structured values, deployments must supply a deterministic :valid-fn that recomputes the canonical identifier from the entire consensus payload, including validator updates. All accepted values sharing an id must be identical.
(vote-payload h r vid)What a precommit attestation covers: height, round and value id.
All three, and none is optional. Without the HEIGHT a signature replays at
every later height; without the ROUND it replays across rounds of the same
height, where the same validator may legitimately precommit a different
value; without the VALUE it attests to nothing at all. This is the same
argument auth/digest makes about topic and recipient, one layer down.
What a precommit attestation covers: height, round and value id. All three, and none is optional. Without the HEIGHT a signature replays at every later height; without the ROUND it replays across rounds of the same height, where the same validator may legitimately precommit a different value; without the VALUE it attests to nothing at all. This is the same argument `auth/digest` makes about topic and recipient, one layer down.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |