Liking cljdoc? Tell your friends :D

urd.consensus.algorithms.round-robin

Round-robin consensus: a rotating single proposer with majority voting, on urd's core protocol.

The simplest thing that is still consensus, and it is in the portfolio for two reasons. It is the reference the datahike writer path was first built against, and it is the CRASH-fault-tolerant corner: fixed proposer per height, no locking, no equivocation handling.

Proposer selection rotates by HEIGHT, not by round:

height 0 -> validator 0 proposes height 1 -> validator 1 ...

Each height: the proposer broadcasts, validators vote if the proposal is valid, a majority finalizes, and a timeout retransmits the outstanding request.

Ported from the legacy ConsensusAlgorithm protocol

This used to sit on urd.consensus.state-machine, an earlier version of node -- same idea, its docstring even says so: transitions are pure functions from (state, event) to state plus effects. Two protocols for one concept is one too many, so this moved and state-machine went.

Four things changed in the move, and each was a defect rather than a translation:

  1. The transitions were not pure. handle-proposal-received and handle-proposal-ready called println -- I/O inside step, which rule 1 of the core protocol forbids, and which is why the legacy runtime's test output was full of interleaved debug lines from concurrent validators. Those are log effects now, which is what the effect exists for.

  2. Timeouts were in MILLISECONDS. The legacy runtime interpreted set-timer with :duration-ms against a wall clock. Logical ticks are what make a failing schedule reproducible, and half the repository did not have them; the flakiness of the legacy tests under load traces to exactly this.

  3. :add-own-vote was an effect, so a node's own vote was written to a log by the runtime rather than held in the node's own state. The algorithm now records it directly, which is both simpler and the only version that can be reasoned about locally -- a vote that lives outside the state machine is a vote the state machine cannot count.

  4. The effect vocabulary was this algorithm's, generalised by accident. request-missing-proposal, request-state-sync, request-votes, broadcast-finalization and trigger-next-round were all interpreted by a runtime that called itself generic. They are ordinary messages and a timer, and they are spelled that way now.

Round-robin consensus: a rotating single proposer with majority voting, on
urd's core protocol.

The simplest thing that is still consensus, and it is in the portfolio for two
reasons. It is the reference the datahike writer path was first built against,
and it is the CRASH-fault-tolerant corner: fixed proposer per height, no
locking, no equivocation handling.

Proposer selection rotates by HEIGHT, not by round:

  height 0 -> validator 0 proposes
  height 1 -> validator 1
  ...

Each height: the proposer broadcasts, validators vote if the proposal is
valid, a majority finalizes, and a timeout retransmits the outstanding request.

## Ported from the legacy `ConsensusAlgorithm` protocol

This used to sit on `urd.consensus.state-machine`, an earlier version of
`node` -- same idea, its docstring even says so: transitions are pure
functions from (state, event) to state plus effects. Two protocols for one
concept is one too many, so this moved and `state-machine` went.

Four things changed in the move, and each was a defect rather than a
translation:

1. **The transitions were not pure.** `handle-proposal-received` and
   `handle-proposal-ready` called `println` -- I/O inside `step`, which rule 1
   of the core protocol forbids, and which is why the legacy runtime's test
   output was full of interleaved debug lines from concurrent validators.
   Those are `log` effects now, which is what the effect exists for.

2. **Timeouts were in MILLISECONDS.** The legacy runtime interpreted
   `set-timer` with `:duration-ms` against a wall clock. Logical ticks are
   what make a failing schedule reproducible, and half the repository did not
   have them; the flakiness of the legacy tests under load traces to exactly
   this.

3. **`:add-own-vote` was an effect**, so a node's own vote was written to a log
   by the runtime rather than held in the node's own state. The algorithm now
   records it directly, which is both simpler and the only version that can
   be reasoned about locally -- a vote that lives outside the state machine is
   a vote the state machine cannot count.

4. **The effect vocabulary was this algorithm's, generalised by accident.**
   `request-missing-proposal`, `request-state-sync`, `request-votes`,
   `broadcast-finalization` and `trigger-next-round` were all interpreted by a
   runtime that called itself generic. They are ordinary messages and a timer,
   and they are spelled that way now.
raw docstring

am-proposer?clj/s

(am-proposer? state)
source

answer-state-syncclj/s

(answer-state-sync state to from)

A peer is behind. Send the history it asked for, and say what we still hold.

from is the requester's height, which the request has always carried and this function used to ignore -- it replied with (:finalized state) wholesale. That map is bounded to the last ten heights, so a peer at 0 asking a peer at 500 was answered with heights 490-499: nothing it could use, and nothing that told it so.

:oldest is the earliest height we can still serve. A requester below it cannot be caught up by us, and needs to hear that rather than infer it from an answer that looks fine and helps not at all.

A peer is behind. Send the history it asked for, and say what we still hold.

`from` is the requester's height, which the request has always carried and
this function used to ignore -- it replied with `(:finalized state)`
wholesale. That map is bounded to the last ten heights, so a peer at 0 asking
a peer at 500 was answered with heights 490-499: nothing it could use, and
nothing that told it so.

`:oldest` is the earliest height we can still serve. A requester below it
cannot be caught up by us, and needs to hear that rather than infer it from
an answer that looks fine and helps not at all.
sourceraw docstring

answer-want-proposalclj/s

(answer-want-proposal state height _round to)

A peer timed out waiting for a proposal. If we hold one for that height, send it with every vote we have -- proposal and votes together, since a peer that is behind needs both and one round trip is enough.

The peer's ROUND is not checked, for the same reason valid-proposal? does not check it: one proposer per height means one proposal per height, so a peer asking about our height is asking about the only proposal there is.

Answering a peer that is BEHIND, which used to get silence

A request for a height we have already passed used to fall through to (n/result state) -- no reply at all. That is the stranding described in handle-finalization, seen from the other side, and it is permanent.

The catch-up path fires only inside handle-finalization, so a node learns it is behind only by RECEIVING a finalization. Finalizations are broadcast once, at the moment of finalization. Miss that one-shot event -- subscribe a few milliseconds late, drop the packet -- and there is no second trigger ever: the node refuses every later proposal on the exact-height check, its want-proposal is answered by nobody, and :sync-requested-at stays nil.

Measured over kabel: two of five validators missed the first finalization, received 105 :want-proposal messages and NOTHING else for twenty seconds, and sat at height 0 with no votes and no sync request. The network then halted outright -- once a stranded node's turn to propose came round it produced nothing, and the remaining nodes spent the run asking for a proposal that was never going to arrive. One height committed in twenty seconds where a healthy run does two hundred.

So a peer asking about a height below ours is told where we are. It is the one moment we are certain it is behind, and it costs a single directed message -- the requests are already rate-limited by the asker's proposal timeout, and they stop as soon as it catches up.

A peer timed out waiting for a proposal. If we hold one for that height, send
it with every vote we have -- proposal and votes together, since a peer that is
behind needs both and one round trip is enough.

The peer's ROUND is not checked, for the same reason `valid-proposal?` does
not check it: one proposer per height means one proposal per height, so a peer
asking about our height is asking about the only proposal there is.

## Answering a peer that is BEHIND, which used to get silence

A request for a height we have already passed used to fall through to
`(n/result state)` -- no reply at all. That is the stranding described in
`handle-finalization`, seen from the other side, and it is permanent.

The catch-up path fires only inside `handle-finalization`, so a node learns
it is behind only by RECEIVING a finalization. Finalizations are broadcast
once, at the moment of finalization. Miss that one-shot event -- subscribe a
few milliseconds late, drop the packet -- and there is no second trigger
ever: the node refuses every later proposal on the exact-height check, its
`want-proposal` is answered by nobody, and `:sync-requested-at` stays nil.

Measured over kabel: two of five validators missed the first finalization,
received 105 `:want-proposal` messages and NOTHING else for twenty seconds,
and sat at height 0 with no votes and no sync request. The network then
halted outright -- once a stranded node's turn to propose came round it
produced nothing, and the remaining nodes spent the run asking for a proposal
that was never going to arrive. One height committed in twenty seconds where
a healthy run does two hundred.

So a peer asking about a height below ours is told where we are. It is the
one moment we are certain it is behind, and it costs a single directed
message -- the requests are already rate-limited by the asker's proposal
timeout, and they stop as soon as it catches up.
sourceraw docstring

answer-want-votesclj/s

(answer-want-votes state proposal-id to)

A peer reached the quorum timeout. Send it what we have for that proposal.

A peer reached the quorum timeout. Send it what we have for that proposal.
sourceraw docstring

cleanup-old-dataclj/s

(cleanup-old-data state)

Bound :finalized, which would otherwise grow for the life of the process.

Bound `:finalized`, which would otherwise grow for the life of the process.
sourceraw docstring

createclj/s

(create)
(create validator-set)

A round-robin node.

validator-set is {peer-id -> public-key}; when omitted it is derived from :validators in the config, since this algorithm only ever uses the KEYS.

A round-robin node.

`validator-set` is {peer-id -> public-key}; when omitted it is derived from
`:validators` in the config, since this algorithm only ever uses the KEYS.
sourceraw docstring

default-configclj/s

Timeouts in TICKS. The legacy runtime used milliseconds against a wall clock; see the namespace docstring.

Timeouts in TICKS. The legacy runtime used milliseconds against a wall clock;
see the namespace docstring.
sourceraw docstring

default-finalized-retentionclj/s

How many finalized heights to keep, and therefore how far behind a peer may fall and still be caught up from memory.

Ten was hard-coded, and ten is very few. :finalized is the ONLY history this algorithm can serve -- answer-state-sync reads it and nothing else -- so a node that misses eleven heights can never be caught up by anyone, no matter how many peers are healthy or how much history the store holds. For a validator that restarts, or one whose link drops for a few seconds, that is not an edge case.

Raising it trades memory for reachable catch-up, and the useful range is wide: a finalized height is one proposal, so thousands cost little. It stays a bound rather than becoming unbounded because the alternative is a map that grows for the life of the process.

The floor is height - retention inclusive. The upper bound is the last finalized height, height - 1, so this keeps N heights. The exact number matters because answer-state-sync reports the floor as :oldest and a requester decides whether it is stranded by comparing against it.

It does not FIX the limitation, it sizes it. A peer further behind than this still cannot be caught up from a peer's memory and still needs a snapshot transfer, which is Tier 5 work: serving arbitrary history means reading the durable store, and step is pure by construction.

How many finalized heights to keep, and therefore how far behind a peer may
fall and still be caught up from memory.

Ten was hard-coded, and ten is very few. `:finalized` is the ONLY history
this algorithm can serve -- `answer-state-sync` reads it and nothing else --
so a node that misses eleven heights can never be caught up by anyone, no
matter how many peers are healthy or how much history the store holds. For a
validator that restarts, or one whose link drops for a few seconds, that is
not an edge case.

Raising it trades memory for reachable catch-up, and the useful range is
wide: a finalized height is one proposal, so thousands cost little. It stays
a bound rather than becoming unbounded because the alternative is a map that
grows for the life of the process.

The floor is `height - retention` inclusive. The upper bound is the last
finalized height, `height - 1`, so this keeps N heights. The
exact number matters because `answer-state-sync` reports the floor as
`:oldest` and a requester decides whether it is stranded by comparing against
it.

It does not FIX the limitation, it sizes it. A peer further behind than this
still cannot be caught up from a peer's memory and still needs a snapshot
transfer, which is Tier 5 work: serving arbitrary history means reading the
durable store, and `step` is pure by construction.
sourceraw docstring

get-proposer-idclj/s

(get-proposer-id height validator-set)

Which validator proposes at height.

Rotates by height, not round. Ids are sorted so every node computes the same answer without coordinating.

Which validator proposes at `height`.

Rotates by height, not round. Ids are sorted so every node computes the same
answer without coordinating.
sourceraw docstring

handle-finalizationclj/s

(handle-finalization state proposal height)
(handle-finalization state proposal height proof)

A peer told us a height finalized, or our own commit did.

A peer told us a height finalized, or our own commit did.
sourceraw docstring

handle-proposal-catchupclj/s

(handle-proposal-catchup state proposal height round votes)

A peer sent us a proposal together with the votes it has for it.

It does NOT replace a proposal we already hold. This function used to assoc :current-proposal unconditionally, which is the same mistake handle-value-ready and handle-proposal-received each already guard against, on the one path that had no guard -- and the most dangerous path to leave open, because a catchup is precisely a proposal arriving from someone who is not its author.

A node that has voted for P and is then handed Q for the same height would adopt Q and vote for that too. Having voted for both, it counts toward BOTH tallies, and with four validators both can reach three. That is two nodes committing different values at one position, from one relayed message.

The rule is the same one stated at handle-value-ready: the first proposal for a height is THE proposal for that height. It has to hold on every path that can set one, and this was the third.

Known gap, recorded rather than fixed here: a proposal carries no signature from its proposer, so a relayed one cannot be checked against valid-proposer? -- the sender is the relayer. Votes are attested now; proposals are not. Until they are, this guard is what keeps a relayer from introducing a second proposal for a height we have already committed to.

A peer sent us a proposal together with the votes it has for it.

It does NOT replace a proposal we already hold. This function used to
`assoc` `:current-proposal` unconditionally, which is the same mistake
`handle-value-ready` and `handle-proposal-received` each already guard
against, on the one path that had no guard -- and the most dangerous path to
leave open, because a catchup is precisely a proposal arriving from someone
who is not its author.

A node that has voted for P and is then handed Q for the same height would
adopt Q and vote for that too. Having voted for both, it counts toward BOTH
tallies, and with four validators both can reach three. That is two nodes
committing different values at one position, from one relayed message.

The rule is the same one stated at `handle-value-ready`: the first proposal
for a height is THE proposal for that height. It has to hold on every path
that can set one, and this was the third.

Known gap, recorded rather than fixed here: a proposal carries no signature
from its proposer, so a relayed one cannot be checked against
`valid-proposer?` -- the sender is the relayer. Votes are attested now;
proposals are not. Until they are, this guard is what keeps a relayer from
introducing a second proposal for a height we have already committed to.
sourceraw docstring

handle-proposal-receivedclj/s

(handle-proposal-received state proposal height round sender)
source

handle-start-roundclj/s

(handle-start-round state)
source

handle-state-sync-responseclj/s

(handle-state-sync-response state finalized-heights current-height oldest)
(handle-state-sync-response state
                            finalized-heights
                            current-height
                            oldest
                            sender
                            proofs)

Adopt a peer's finalized history -- and COMMIT what we learn from it.

The commits are the point. This used to jump the height and merge the finalized map while emitting nothing, so a node that synced skipped straight past decisions it had never reported: its committed sequence acquired a hole, and two nodes then disagreed at those positions. sim/agreement? catches it as a safety violation, which is what it is.

It went unseen because handle-finalization only asked for a sync when it was TWO or more heights behind, and being exactly one behind -- the common case -- fell into a branch that did nothing. Fixing that off-by-one made syncs ordinary and the hole appeared at once. One defect was hiding the other.

Only the CONTIGUOUS run starting at our own height is committed. A peer may send a history with gaps, and a commit sequence with a gap is precisely what is being fixed here; the rest arrives on the next sync.

A sync that teaches us nothing must change nothing

This used to reset :round, :current-proposal and :votes UNCONDITIONALLY, including when new-height came out equal to the height we were already at -- which is the common case, because a state-sync-response is an answer to a question we asked a while ago and the answer is routinely stale by the time it lands.

For an ordinary node that is merely wasteful. For the PROPOSER it is a safety violation, and it is the one that had round-robin diverging on roughly one conformance run in eight:

  1. :a is the proposer for height 8. It has asked the application for a value, got one, broadcast it and voted for it. Peers are voting too.
  2. A stale state-sync-response arrives that teaches it nothing.
  3. It wipes its own proposal and the votes cast for it, and returns to :proposing at round 0.
  4. :next-round fires. It is still the proposer for height 8, so it asks the application AGAIN -- and an application is entitled to answer with a different value, because request-value never promised to ask once.
  5. Two proposals now exist for one height. Nodes holding the first keep it (handle-value-ready guards that); nodes holding nothing take the second. Votes accumulate on BOTH tallies, and with four validators both can reach three. Two nodes commit different values at the same position.

handle-value-ready's "the first value for a height is the proposal for that height" was the right rule and could not hold, because the state it is written on was being erased underneath it. The whole safety argument of this algorithm -- one proposer per height, therefore one proposal, therefore only one value can reach a majority -- fails at the second step.

So the reset is now conditional on actually having moved.

We advance only as far as we have COMMITTED

new-height used to include the peer's current-height in its max, so a node at 0 syncing from a peer at 500 arrived at 500 having committed nothing at all. Its committed sequence then began at 500 with heights 0-499 missing, and against the datahike ledger that transacts height 500 into a database that has never seen the preceding 499 -- a hole, presented as a catch-up.

The height now advances only over the contiguous run we actually learned and emitted commits for. A node that is far behind therefore takes several rounds, which is correct and merely slow.

oldest is what the responder can still serve. :finalized is bounded to ten heights, so a peer far enough behind cannot be caught up by this peer at all -- and that is a real limitation of having no archival history (see the compaction finding), not something to paper over. It is reported at :warn, because the alternative is what this function used to do: jump, and look caught up.

Adopt a peer's finalized history -- and COMMIT what we learn from it.

The commits are the point. This used to jump the height and merge the
finalized map while emitting nothing, so a node that synced skipped straight
past decisions it had never reported: its committed sequence acquired a hole,
and two nodes then disagreed at those positions. `sim/agreement?` catches it
as a safety violation, which is what it is.

It went unseen because `handle-finalization` only asked for a sync when it was
TWO or more heights behind, and being exactly one behind -- the common case --
fell into a branch that did nothing. Fixing that off-by-one made syncs
ordinary and the hole appeared at once. One defect was hiding the other.

Only the CONTIGUOUS run starting at our own height is committed. A peer may
send a history with gaps, and a commit sequence with a gap is precisely what
is being fixed here; the rest arrives on the next sync.

## A sync that teaches us nothing must change nothing

This used to reset `:round`, `:current-proposal` and `:votes`
UNCONDITIONALLY, including when `new-height` came out equal to the height we
were already at -- which is the common case, because a `state-sync-response`
is an answer to a question we asked a while ago and the answer is routinely
stale by the time it lands.

For an ordinary node that is merely wasteful. For the PROPOSER it is a safety
violation, and it is the one that had round-robin diverging on roughly one
conformance run in eight:

  1. `:a` is the proposer for height 8. It has asked the application for a
     value, got one, broadcast it and voted for it. Peers are voting too.
  2. A stale `state-sync-response` arrives that teaches it nothing.
  3. It wipes its own proposal and the votes cast for it, and returns to
     `:proposing` at round 0.
  4. `:next-round` fires. It is still the proposer for height 8, so it asks
     the application AGAIN -- and an application is entitled to answer with a
     different value, because `request-value` never promised to ask once.
  5. Two proposals now exist for one height. Nodes holding the first keep it
     (`handle-value-ready` guards that); nodes holding nothing take the
     second. Votes accumulate on BOTH tallies, and with four validators both
     can reach three. Two nodes commit different values at the same position.

`handle-value-ready`'s "the first value for a height is the proposal for that
height" was the right rule and could not hold, because the state it is
written on was being erased underneath it. The whole safety argument of this
algorithm -- one proposer per height, therefore one proposal, therefore only
one value can reach a majority -- fails at the second step.

So the reset is now conditional on actually having moved.

## We advance only as far as we have COMMITTED

`new-height` used to include the peer's `current-height` in its `max`, so a
node at 0 syncing from a peer at 500 arrived at 500 having committed nothing
at all. Its committed sequence then began at 500 with heights 0-499 missing,
and against the datahike ledger that transacts height 500 into a database
that has never seen the preceding 499 -- a hole, presented as a catch-up.

The height now advances only over the contiguous run we actually learned and
emitted commits for. A node that is far behind therefore takes several
rounds, which is correct and merely slow.

`oldest` is what the responder can still serve. `:finalized` is bounded to
ten heights, so a peer far enough behind cannot be caught up by this peer at
all -- and that is a real limitation of having no archival history (see the
compaction finding), not something to paper over. It is reported at `:warn`,
because the alternative is what this function used to do: jump, and look
caught up.
sourceraw docstring

handle-timeoutclj/s

(handle-timeout state timer-id)
source

handle-value-readyclj/s

(handle-value-ready state proposal)

The application supplied a proposal. Broadcast it and count our own vote -- IN STATE, not as an effect for a runtime to remember for us.

One proposal per height, enforced here rather than assumed

valid-proposal? rests the entire safety argument of this algorithm on there being at most one proposal per height: one proposer per height, therefore one proposal, therefore two values cannot both reach a majority because only one was ever offered. That is true of the PROPOSER and was not true of the PROPOSAL. This function overwrote :current-proposal unconditionally, so an application that answered a second request-value for the same height with a different value replaced a proposal that had already been broadcast and voted on -- and then two values had been offered for one height after all.

Nothing in the protocol forbids that answer. request-value asks the application for a payload; it does not promise to ask once, and it cannot require the application to be a pure function of height. So the algorithm pins it: the first value for a height is the proposal for that height, and a later one is ignored.

Measured on the real runtime, four nodes, all honest: with a fresh value per request the committed sequences diverged at position 148 -- the proposer had committed its own second value where its peers had committed the first. With a value that was a pure function of height, 298 committed positions and no divergence. The application was doing nothing wrong; the algorithm was trusting something it had not established.

The application supplied a proposal. Broadcast it and count our own vote --
IN STATE, not as an effect for a runtime to remember for us.

## One proposal per height, enforced here rather than assumed

`valid-proposal?` rests the entire safety argument of this algorithm on there
being at most one proposal per height: one proposer per height, therefore one
proposal, therefore two values cannot both reach a majority because only one
was ever offered. That is true of the PROPOSER and was not true of the
PROPOSAL. This function overwrote `:current-proposal` unconditionally, so an
application that answered a second `request-value` for the same height with a
different value replaced a proposal that had already been broadcast and voted
on -- and then two values had been offered for one height after all.

Nothing in the protocol forbids that answer. `request-value` asks the
application for a payload; it does not promise to ask once, and it cannot
require the application to be a pure function of height. So the algorithm
pins it: the first value for a height is the proposal for that height, and a
later one is ignored.

Measured on the real runtime, four nodes, all honest: with a fresh value per
request the committed sequences diverged at position 148 -- the proposer had
committed its own second value where its peers had committed the first. With
a value that was a pure function of height, 298 committed positions and no
divergence. The application was doing nothing wrong; the algorithm was
trusting something it had not established.
sourceraw docstring

handle-vote-bundleclj/s

(handle-vote-bundle state proposal-id votes)
source

handle-vote-receivedclj/s

(handle-vote-received state proposal-id voter att)
source

has-quorum?clj/s

(has-quorum? state proposal-id)
source

makeclj/s

(make)
source

max-vote-bundleclj/s

Ceiling on a relayed vote collection before the message is refused outright.

handle-vote-bundle already dedups by voter and truncates to the validator count, so this is not what bounds the WORK. It bounds what has to be held to do the truncating: a 50,000-element vector still gets allocated, walked and reduced before the bound applies. Refusing it at the edge costs one count.

Ceiling on a relayed vote collection before the message is refused outright.

`handle-vote-bundle` already dedups by voter and truncates to the validator
count, so this is not what bounds the WORK. It bounds what has to be held to
do the truncating: a 50,000-element vector still gets allocated, walked and
reduced before the bound applies. Refusing it at the edge costs one `count`.
sourceraw docstring

proposal-idclj/s

(proposal-id proposal)

A proposal's identity, or nil if it has none.

Separated out because ABSENT is a value here and the code used to treat it as an ordinary one. Every id came from (:snapshot-id proposal), and a proposal without that key produced nil -- which then flowed through :votes as a perfectly good map key, matched :current-proposal-id (also nil) so the "is this vote for our current proposal" guard passed, accumulated a quorum, and committed (:current-proposal state), which was nil as well. A node emitted nil into its committed sequence twice in a 120-tick run, and two peers that had also done it at different moments then disagreed.

A proposal's identity, or nil if it has none.

Separated out because ABSENT is a value here and the code used to treat it as
an ordinary one. Every id came from `(:snapshot-id proposal)`, and a proposal
without that key produced `nil` -- which then flowed through `:votes` as a
perfectly good map key, matched `:current-proposal-id` (also nil) so the
"is this vote for our current proposal" guard passed, accumulated a quorum,
and committed `(:current-proposal state)`, which was nil as well. A node
emitted `nil` into its committed sequence twice in a 120-tick run, and two
peers that had also done it at different moments then disagreed.
sourceraw docstring

quorum-sizeclj/s

(quorum-size validator-set)

CRASH-fault-tolerant quorum: a simple majority, floor(n/2) + 1.

This is deliberately NOT the Byzantine threshold, and the difference is not an oversight -- read before 'fixing' it to match the certificate threshold.

Round-robin is a CFT reference algorithm. It has a fixed proposer per height, no locking, and no equivocation handling, so it is not Byzantine-safe at ANY threshold: a faulty proposer can equivocate and nothing here detects it. Raising the threshold would cost liveness while buying no safety.

n | CFT majority | BFT floor(2n/3)+1 | f = floor((n-1)/3)
3 |      2       |         3         |        0
4 |      3       |         3         |        1
5 |      3       |         4         |        1
7 |      4       |         5         |        2

Note n=3 tolerates ZERO Byzantine faults, so its Byzantine quorum is unanimity -- which is why a 3-validator cluster is a CFT configuration only.

urd.consensus.attest/byzantine-quorum-size uses the Byzantine threshold because it validates CERTIFICATES, which are meant to be checkable by third parties under an adversarial assumption. The two are different threat models and are supposed to differ. quorum-thresholds-are-intentionally-different in the generative tests pins that.

CRASH-fault-tolerant quorum: a simple majority, floor(n/2) + 1.

This is deliberately NOT the Byzantine threshold, and the difference is not an
oversight -- read before 'fixing' it to match the certificate threshold.

Round-robin is a CFT reference algorithm. It has a fixed proposer per height,
no locking, and no equivocation handling, so it is not Byzantine-safe at ANY
threshold: a faulty proposer can equivocate and nothing here detects it.
Raising the threshold would cost liveness while buying no safety.

    n | CFT majority | BFT floor(2n/3)+1 | f = floor((n-1)/3)
    3 |      2       |         3         |        0
    4 |      3       |         3         |        1
    5 |      3       |         4         |        1
    7 |      4       |         5         |        2

Note n=3 tolerates ZERO Byzantine faults, so its Byzantine quorum is
unanimity -- which is why a 3-validator cluster is a CFT configuration only.

`urd.consensus.attest/byzantine-quorum-size` uses the Byzantine threshold
because it validates CERTIFICATES, which are meant to be checkable by third
parties under an adversarial assumption. The two are different threat models
and are supposed to differ. `quorum-thresholds-are-intentionally-different` in the generative
tests pins that.
sourceraw docstring

test-attestationsclj/s

(test-attestations)

A toy vote-attestation scheme, for tests and simulations only.

NOT CRYPTOGRAPHY. It is a hash of a per-voter string, so anyone who can read this namespace can mint one for any voter. What it provides is the SHAPE: an attestation binds a voter to a vote-payload, verification is a pure predicate, and anything not produced this way fails -- which is what a test needs to drive the guard down both branches.

urd.consensus.attest/attestations is the real one and is a drop-in for this. init refuses to start without a scheme precisely so this stub cannot be reached by accident.

A toy vote-attestation scheme, for tests and simulations only.

NOT CRYPTOGRAPHY. It is a hash of a per-voter string, so anyone who can read
this namespace can mint one for any voter. What it provides is the SHAPE: an
attestation binds a voter to a `vote-payload`, verification is a pure
predicate, and anything not produced this way fails -- which is what a test
needs to drive the guard down both branches.

`urd.consensus.attest/attestations` is the real one and is a drop-in for
this. `init` refuses to start without a scheme precisely so this stub cannot
be reached by accident.
sourceraw docstring

valid-height-round?clj/s

(valid-height-round? state height round)

Current, neither stale nor implausibly far ahead. The upper bound is memory exhaustion protection: anyone may send anything.

Current, neither stale nor implausibly far ahead. The upper bound is memory
exhaustion protection: anyone may send anything.
sourceraw docstring

valid-proposal?clj/s

(valid-proposal? state proposal height _round)

For our current HEIGHT, and identifiable.

Deliberately not (= round (:round state)), and the reason is the whole safety argument of this algorithm. Exactly one validator proposes per height -- get-proposer-id takes the height alone -- so there is at most one proposal per height ever, and that is what makes a majority quorum safe here without any locking: two different values cannot both reach a majority when only one was ever offered.

Requiring the ROUND to match as well bought nothing and cost liveness outright. A validator that missed the proposal broadcast bumped its round on timeout, and from then on refused the very proposal it was waiting for, while the proposer -- which only proposes from handle-start-round, driven by the post-finalization timer -- never sent another. Measured: 5 nodes at 30% message loss committed NOTHING in 300 ticks, with the proposer sitting on its proposal at round 2 and every other node at round 1 holding none.

Rotating the proposer by round instead, which is what a view change normally does, would fix the liveness and BREAK the safety: two proposers for one height means two proposals, and with no locking a node may vote for both, so two values could each collect a majority across rounds. The round bump is vestigial here by design -- see handle-timeout.

:valid-fn is the APPLICATION's predicate, and it defaults to (constantly true) so nothing changes for a caller that does not supply one. Tendermint has had this seam since it was ported; round-robin had no way for an application to refuse a proposal on its CONTENT at all, which meant the app-hash convention (urd.consensus.apphash) was enforceable in exactly one of the six algorithms. Structural checks stay above it: a proposal with no id is refused before the application is consulted, because there is nothing coherent to consult it about.

For our current HEIGHT, and identifiable.

Deliberately not `(= round (:round state))`, and the reason is the whole
safety argument of this algorithm. Exactly one validator proposes per height
-- `get-proposer-id` takes the height alone -- so there is at most one
proposal per height ever, and that is what makes a majority quorum safe here
without any locking: two different values cannot both reach a majority when
only one was ever offered.

Requiring the ROUND to match as well bought nothing and cost liveness
outright. A validator that missed the proposal broadcast bumped its round on
timeout, and from then on refused the very proposal it was waiting for, while
the proposer -- which only proposes from `handle-start-round`, driven by the
post-finalization timer -- never sent another. Measured: 5 nodes at 30%
message loss committed NOTHING in 300 ticks, with the proposer sitting on its
proposal at round 2 and every other node at round 1 holding none.

Rotating the proposer by round instead, which is what a view change normally
does, would fix the liveness and BREAK the safety: two proposers for one
height means two proposals, and with no locking a node may vote for both, so
two values could each collect a majority across rounds. The round bump is
vestigial here by design -- see `handle-timeout`.

`:valid-fn` is the APPLICATION's predicate, and it defaults to
`(constantly true)` so nothing changes for a caller that does not supply one.
Tendermint has had this seam since it was ported; round-robin had no way for
an application to refuse a proposal on its CONTENT at all, which meant the
app-hash convention (`urd.consensus.apphash`) was enforceable in exactly one
of the six algorithms. Structural checks stay above it: a proposal with no
id is refused before the application is consulted, because there is nothing
coherent to consult it about.
sourceraw docstring

valid-proposer?clj/s

(valid-proposer? sender height validator-set)
source

valid-voter?clj/s

(valid-voter? voter validator-set)
source

vote-payloadclj/s

(vote-payload state proposal-id)

What an attestation by a voter covers: [height proposal-id].

The HEIGHT is not decoration. This used to sign a bare proposal-id, which is only safe while a proposal id never repeats -- and a proposal id may be a snapshot-id is a "UUID or content-hash". Under the content-hash reading, proposing the same value at two heights produces the SAME id, so a vote cast at the first height is a valid vote at the second, and an attacker who kept a single old attestation could replay it into a later quorum. Tendermint's vote-payload has made this argument since it was ported; round-robin was the one that had not.

(:height state) is the right anchor at every call site because valid-proposal? requires (= height (:height state)) -- a proposal is only ever accepted at the current height, so there is no moment at which the proposal's height and the state's height differ.

What an attestation by a voter covers: `[height proposal-id]`.

The HEIGHT is not decoration. This used to sign a bare `proposal-id`, which is
only safe while a proposal id never repeats -- and a proposal id may be a
`snapshot-id` is a "UUID or content-hash". Under the content-hash reading,
proposing the same value at two heights produces the SAME id, so a vote cast
at the first height is a valid vote at the second, and an attacker who kept a
single old attestation could replay it into a later quorum. Tendermint's
`vote-payload` has made this argument since it was ported; round-robin was
the one that had not.

`(:height state)` is the right anchor at every call site because
`valid-proposal?` requires `(= height (:height state))` -- a proposal is only
ever accepted at the current height, so there is no moment at which the
proposal's height and the state's height differ.
sourceraw docstring

well-formed?clj/s

(well-formed? msg)

Shape checks that need nothing but the message itself.

Authentication says WHO sent a message. It says nothing about whether the fields inside it mean anything, and a Byzantine peer is authenticated too -- so every field here is one an authenticated peer may choose freely.

The TYPE checks are the load-bearing half. get-proposer-id computes (mod height (count validators)) and nths the result, so a height that is a string throws inside step. The runtime's containment turns that into a dropped message and a counter, which is survivable -- but containment is the wrong layer to lean on for something int? answers, and a contained throw costs a stack unwind per message where a predicate costs nothing.

The NON-NEGATIVE half is defence in depth, and is documented as such because the obvious justification for it is false. (mod -1 3) is 2 in Clojure -- mod is floored, not truncated -- so a negative height selects a valid proposer rather than indexing backwards or throwing, and it is dropped by the height comparison before that anyway. answer-state-sync's (>= h from) with a negative from selects everything it holds, which retention already bounds. No reachable harm was found; the check stays because a negative height is meaningless and cheap to refuse, not because it is known to stop anything. Mutation-verified as NOT load-bearing today, which is the honest status rather than an assumed one.

NOT bounded relative to our own height, deliberately. A node at 0 hearing a finalization for height 500 is the catch-up path working, not an attack, and a bound tight enough to matter here would refuse exactly the messages a lagging node needs. What an absurd height actually costs is one throttled sync request -- :sync-requested-at -- which is already bounded.

Shape checks that need nothing but the message itself.

Authentication says WHO sent a message. It says nothing about whether the
fields inside it mean anything, and a Byzantine peer is authenticated too --
so every field here is one an authenticated peer may choose freely.

The TYPE checks are the load-bearing half. `get-proposer-id` computes
`(mod height (count validators))` and `nth`s the result, so a height that is
a string throws inside `step`. The runtime's containment turns that into a
dropped message and a counter, which is survivable -- but containment is the
wrong layer to lean on for something `int?` answers, and a contained throw
costs a stack unwind per message where a predicate costs nothing.

The NON-NEGATIVE half is defence in depth, and is documented as such because
the obvious justification for it is false. `(mod -1 3)` is 2 in Clojure --
`mod` is floored, not truncated -- so a negative height selects a valid
proposer rather than indexing backwards or throwing, and it is dropped by the
height comparison before that anyway. `answer-state-sync`'s `(>= h from)`
with a negative `from` selects everything it holds, which retention already
bounds. No reachable harm was found; the check stays because a negative
height is meaningless and cheap to refuse, not because it is known to stop
anything. Mutation-verified as NOT load-bearing today, which is the honest
status rather than an assumed one.

NOT bounded relative to our own height, deliberately. A node at 0 hearing a
finalization for height 500 is the catch-up path working, not an attack, and
a bound tight enough to matter here would refuse exactly the messages a
lagging node needs. What an absurd height actually costs is one throttled
sync request -- `:sync-requested-at` -- which is already bounded.
sourceraw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close