Liking cljdoc? Tell your friends :D

urd.consensus.algorithms.bullshark

Narwhal + Bullshark: a certified DAG mempool with a zero-message ordering layer, on urd's core protocol.

Ported from MystenLabs/narwhal at e67f915 (read Aug 2026). The repo was archived in 2022 when the work moved into sui, so where the code and the papers disagree the papers win -- Narwhal/Tusk arXiv:2105.11827, Bullshark arXiv:2201.05677, and the partially synchronous variant at sonnino.com/papers/bullshark-simple.pdf. One such disagreement is load-bearing and is documented at order-anchors below.

This is the PARTIALLY SYNCHRONOUS Bullshark (2 rounds per wave), not Tusk. The distinction is the reason it is in the portfolio: Tusk needs a threshold common coin, and Bullshark-PS does not, so this algorithm adds no new cryptography at all. Where Narwhal aggregates BLS12-381 signatures into one certificate signature plus a roaring bitmap of committee indices, we carry one attestation per signer -- 2f+1 x 64 bytes on the wire rather than 96 plus a bitmap. That is the entire cost of the substitution.

This paragraph used to claim that the state machine's predicate was unchanged, because what it checks is that the SET of signers is decisive, never the bytes. That sentence was the bug. A certificate is a THIRD PARTY's aggregated claim and it is relayed: the peer handing it to us is almost never a signer, so transport authentication says nothing about the signers and the names alone are worth nothing. certified? verifies each attestation and counts stake only over those that verify; init refuses to start without a verifier, because a permissive default is how the missing check went unnoticed through an entire audit round.

Two layers, and only one of them talks

  1. Narwhal builds a certified DAG. A vertex is a CERTIFICATE: a header (author, round, payload, parents) plus signatures from >= 2f+1 stake. The 2f+1 is what makes the DAG safe rather than merely shaped like a DAG. At most one certificate per (author, round) can be certified, so equivocation cannot survive certification; and >= f+1 signers are honest, so the payload is retrievable. A round r+1 header must name >= 2f+1 stake of certificates from EXACTLY round r as parents (primary/src/core.rs:229-245 -- note this check is in the Core, not in Header::verify, and it is the invariant the whole ordering layer silently assumes).

  2. Bullshark orders it. commit-sequence is a pure function of the DAG and emits NOTHING. Every honest node runs it on its own copy and gets the identical total order. This is the first algorithm in urd's portfolio whose decision layer sends no messages, and the first that commits a multi-vertex sequence retroactively.

The ordering rule, exactly

  • Anchors exist only at EVEN rounds (bullshark.rs:49-52). The anchor of round r is anchor-fn(r, validators, stake) -- a deterministic function of the round alone, evaluated identically by everyone.

  • Commit rule: sum the stake of round-(r+1) certificates whose parents include the anchor; commit iff >= f+1 (bullshark.rs:66-82). Only f+1 is needed, and that is the elegant part: the 2f+1 intersection argument was already discharged at DAG-construction time, so the ordering layer gets to be cheap.

  • Retroactive back-fill (utils.rs:11-40): from the newly committed anchor, walk backwards by 2 to the last committed round, including each earlier anchor iff it is REACHABLE from the newer one. One certificate can therefore commit an anchor plus several earlier anchors and all of their sub-DAGs at once.

  • Deterministic flatten (utils.rs:58-100): pre-order DFS from each anchor over parents, skipping vertices already queued or already committed for that author, then a stable sort by round.

    Arrival-order independence comes from the DFS, not from the sort's stability. The DFS visits parents through (sort (cert-parents x)), so its output sequence is already a function of the DAG alone and cannot see the order certificates were inserted in. Stability then fixes the WITHIN-ROUND order to that DFS sequence rather than to an arbitrary permutation of it. Both matter; only the first is what makes two nodes agree. narwhal's own comment is blunter: ordering the output by round is not really necessary, it just makes the commit sequence prettier (utils.rs:96).

  • GC drops rounds more than gc-depth below the last committed round, and each author's certificates below its own last committed round.

Three mechanisms stop the DFS, and all three are needed

  1. already-queued dedups within one flatten;
  2. each author's committed watermark stops the walk at or below it;
  3. GC and live insertion keep certificates below that watermark out of the DAG.

The bootstrap case falls out of (2): last-committed starts at 0 for EVERY validator, and genesis certificates are at round 0, so the DFS stops there and genesis is never emitted. Miss that seeding and the very first commit contains n phantom vertices -- and, worse, agrees with itself across nodes, so nothing catches it.

Portability: why digests are strings

The DFS pushes parents in sorted digest order and the stable sort by round preserves DFS discovery order WITHIN a round, so the digest comparator decides the total order. hasch/uuid gives content-addressed identity on both platforms, but a raw UUID must never be compared: the JVM's UUID/compareTo compares the two 64-bit halves as SIGNED longs, while ClojureScript's UUID compares the printed string. Those orders differ for any pair whose high bit differs -- half of all pairs -- so a JVM node and a browser node would produce different total orders from the identical DAG, silently. Digests are therefore STRINGS everywhere. Pinned in portability-test.

Payload

Opaque to consensus, and supplied through request-value. The intended payload is the map of snapshot ids datahike already hands to coordinated-commit!; Narwhal's own is {batch-digest -> worker-id}, which is the same shape. See the PR for why the certificate DAG is a urd-owned structure rather than a yggdrasil one.

Narwhal + Bullshark: a certified DAG mempool with a zero-message ordering
layer, on urd's core protocol.

Ported from MystenLabs/narwhal at `e67f915` (read Aug 2026). The repo was
archived in 2022 when the work moved into sui, so where the code and the
papers disagree the papers win -- Narwhal/Tusk arXiv:2105.11827, Bullshark
arXiv:2201.05677, and the partially synchronous variant at
sonnino.com/papers/bullshark-simple.pdf. One such disagreement is load-bearing
and is documented at `order-anchors` below.

This is the PARTIALLY SYNCHRONOUS Bullshark (2 rounds per wave), not Tusk.
The distinction is the reason it is in the portfolio: Tusk needs a threshold
common coin, and Bullshark-PS does not, so this algorithm adds **no new
cryptography at all**. Where Narwhal aggregates BLS12-381 signatures into one
certificate signature plus a roaring bitmap of committee indices, we carry one
attestation per signer -- 2f+1 x 64 bytes on the wire rather than 96 plus a
bitmap. That is the entire cost of the substitution.

This paragraph used to claim that the state machine's predicate was
unchanged, because what it checks is that the SET of signers is decisive,
never the bytes. That sentence was the bug. A certificate is a THIRD PARTY's
aggregated claim and it is relayed: the peer handing it to us is almost never
a signer, so transport authentication says nothing about the signers and the
names alone are worth nothing. `certified?` verifies each attestation and
counts stake only over those that verify; `init` refuses to start without a
verifier, because a permissive default is how the missing check went
unnoticed through an entire audit round.

## Two layers, and only one of them talks

1. **Narwhal** builds a certified DAG. A vertex is a CERTIFICATE: a header
   (author, round, payload, parents) plus signatures from >= 2f+1 stake. The
   2f+1 is what makes the DAG safe rather than merely shaped like a DAG. At
   most one certificate per (author, round) can be certified, so equivocation
   cannot survive certification; and >= f+1 signers are honest, so the payload
   is retrievable. A round r+1 header must name >= 2f+1 stake of certificates
   from EXACTLY round r as parents (`primary/src/core.rs:229-245` -- note this
   check is in the Core, not in `Header::verify`, and it is the invariant the
   whole ordering layer silently assumes).

2. **Bullshark** orders it. `commit-sequence` is a pure function of the DAG
   and emits NOTHING. Every honest node runs it on its own copy and gets the
   identical total order. This is the first algorithm in urd's portfolio whose
   decision layer sends no messages, and the first that commits a multi-vertex
   sequence retroactively.

## The ordering rule, exactly

- Anchors exist only at EVEN rounds (`bullshark.rs:49-52`). The anchor of
  round r is `anchor-fn(r, validators, stake)` -- a deterministic function of
  the round alone, evaluated identically by everyone.
- **Commit rule**: sum the stake of round-(r+1) certificates whose parents
  include the anchor; commit iff `>= f+1` (`bullshark.rs:66-82`). Only f+1 is
  needed, and that is the elegant part: the 2f+1 intersection argument was
  already discharged at DAG-construction time, so the ordering layer gets to
  be cheap.
- **Retroactive back-fill** (`utils.rs:11-40`): from the newly committed
  anchor, walk backwards by 2 to the last committed round, including each
  earlier anchor iff it is REACHABLE from the newer one. One certificate can
  therefore commit an anchor plus several earlier anchors and all of their
  sub-DAGs at once.
- **Deterministic flatten** (`utils.rs:58-100`): pre-order DFS from each
  anchor over parents, skipping vertices already queued or already committed
  for that author, then a stable sort by round.

  Arrival-order independence comes from the DFS, not from the sort's
  stability. The DFS visits parents through `(sort (cert-parents x))`, so its
  output sequence is already a function of the DAG alone and cannot see the
  order certificates were inserted in. Stability then fixes the WITHIN-ROUND
  order to that DFS sequence rather than to an arbitrary permutation of it.
  Both matter; only the first is what makes two nodes agree. narwhal's own
  comment is blunter: ordering the output by round is not really necessary,
  it just makes the commit sequence prettier (`utils.rs:96`).
- GC drops rounds more than `gc-depth` below the last committed round, and
  each author's certificates below its own last committed round.

## Three mechanisms stop the DFS, and all three are needed

1. `already-queued` dedups within one flatten;
2. each author's committed watermark stops the walk at or below it;
3. GC and live insertion keep certificates below that watermark out of the DAG.

The bootstrap case falls out of (2): `last-committed` starts at 0 for EVERY
validator, and genesis certificates are at round 0, so the DFS stops there and
genesis is never emitted. Miss that seeding and the very first commit contains
n phantom vertices -- and, worse, agrees with itself across nodes, so nothing
catches it.

## Portability: why digests are strings

The DFS pushes parents in sorted digest order and the stable sort by round
preserves DFS discovery order WITHIN a round, so the digest comparator decides
the total order. `hasch/uuid` gives content-addressed identity on both
platforms, but a raw UUID must never be compared: the JVM's `UUID/compareTo`
compares the two 64-bit halves as SIGNED longs, while ClojureScript's `UUID`
compares the printed string. Those orders differ for any pair whose high bit
differs -- half of all pairs -- so a JVM node and a browser node would produce
different total orders from the identical DAG, silently. Digests are therefore
STRINGS everywhere. Pinned in `portability-test`.

## Payload

Opaque to consensus, and supplied through `request-value`. The intended
payload is the map of snapshot ids datahike already hands to
`coordinated-commit!`; Narwhal's own is `{batch-digest -> worker-id}`, which
is the same shape. See the PR for why the certificate DAG is a urd-owned
structure rather than a yggdrasil one.
raw docstring

anchor-forclj/s

(anchor-for state round)

The anchor certificate at round, or nil if we do not hold it.

The anchor certificate at `round`, or nil if we do not hold it.
sourceraw docstring

certificateclj/s

(certificate header votes)

Build a certificate value with its digest attached.

votes is {signer attestation}, not a set of names. A certificate is a THIRD PARTY's aggregated claim that 2f+1 validators voted, and it is relayed: the peer who hands it to us is almost never one of the signers. So the names alone are worth nothing, and the attestations are the whole content -- see certified?.

:signers is derived and kept because the equivocation and quorum checks read it; it is never the basis for counting stake.

Build a certificate value with its digest attached.

`votes` is `{signer attestation}`, not a set of names. A certificate is a
THIRD PARTY's aggregated claim that 2f+1 validators voted, and it is relayed:
the peer who hands it to us is almost never one of the signers. So the names
alone are worth nothing, and the attestations are the whole content -- see
`certified?`.

`:signers` is derived and kept because the equivocation and quorum checks
read it; it is never the basis for counting stake.
sourceraw docstring

certificate-idclj/s

(certificate-id {:keys [header]})

The digest of a certificate: (header-id, round, author).

Narwhal hashes exactly those three and deliberately EXCLUDES the signature set (types/src/primary.rs, impl Hash for Certificate). The exclusion is not an optimisation. Two honest nodes may aggregate different 2f+1 subsets of the same votes; if the signer set were part of the identity those would be different vertices, the DAG would fork along an honest path, and parent lookups would fail. Its PartialEq agrees, comparing only those fields.

The digest of a certificate: (header-id, round, author).

Narwhal hashes exactly those three and deliberately EXCLUDES the signature
set (`types/src/primary.rs`, `impl Hash for Certificate`). The exclusion is
not an optimisation. Two honest nodes may aggregate different 2f+1 subsets of
the same votes; if the signer set were part of the identity those would be
different vertices, the DAG would fork along an honest path, and parent
lookups would fail. Its `PartialEq` agrees, comparing only those fields.
sourceraw docstring

commit-sequenceclj/s

(commit-sequence state inserted-round anchor-fn)

THE ordering layer. Pure, total, and it sends nothing.

Given a state and the round of a certificate just inserted, returns [state' sequence]. sequence is the ordered certificates to commit -- usually empty, occasionally one anchor's sub-DAG, and after a gap potentially several anchors' sub-DAGs at once.

The anchor considered is at inserted-round - 1, so support is counted among certificates of inserted-round itself -- the ones provably already present.

The returned sequence carries ::anchors metadata: the rounds of the anchors this call ELECTED as commit points, oldest first. Nothing in the library reads it. It exists because electing an anchor and ordering a vertex are different events that produce the same kind of value, and from the outside they are indistinguishable -- a skipped anchor's certificate is still ordered later, as an ordinary member of some subsequent anchor's causal history, at whatever position that anchor puts it. A test that tried to recover the election by matching author against anchor-for therefore read those back-filled certificates as anchors committed out of order, and reported a violation against a run that was correct. Reconstructing the decision from the output was the mistake; this reports it.

THE ordering layer. Pure, total, and it sends nothing.

Given a state and the round of a certificate just inserted, returns
`[state' sequence]`. `sequence` is the ordered certificates to commit --
usually empty, occasionally one anchor's sub-DAG, and after a gap potentially
several anchors' sub-DAGs at once.

The anchor considered is at `inserted-round - 1`, so support is counted among
certificates of `inserted-round` itself -- the ones provably already present.

The returned sequence carries `::anchors` metadata: the rounds of the anchors
this call ELECTED as commit points, oldest first. Nothing in the library reads
it. It exists because electing an anchor and ordering a vertex are different
events that produce the same kind of value, and from the outside they are
indistinguishable -- a skipped anchor's certificate is still ordered later, as
an ordinary member of some subsequent anchor's causal history, at whatever
position that anchor puts it. A test that tried to recover the election by
matching author against `anchor-for` therefore read those back-filled
certificates as anchors committed out of order, and reported a violation
against a run that was correct. Reconstructing the decision from the output
was the mistake; this reports it.
sourceraw docstring

createclj/s

(create)

Create a Bullshark node.

Config: :stake {author stake}, :gc-depth n, :anchor-fn (fn [round validators stake] -> author), :anchor-wait-ticks n, :gossip-ticks n, :archive-catchup? true to serve certificates beyond :store-depth from retained durable history.

Create a Bullshark node.

Config: `:stake` {author stake}, `:gc-depth` n, `:anchor-fn`
`(fn [round validators stake] -> author)`, `:anchor-wait-ticks` n,
`:gossip-ticks` n, `:archive-catchup?` true to serve certificates beyond
`:store-depth` from retained durable history.
sourceraw docstring

genesis-certificatesclj/s

(genesis-certificates validators)

One empty round-0 certificate per validator (Certificate::genesis).

Uncertified, and correctly so: every node derives the same set from the committee, so there is nothing to agree about. (Narwhal leaves their header.id as 32 zero bytes and then short-circuits Certificate::verify on genesis membership, which means any round-0 certificate with a zeroed header id from a committee member verifies unconditionally. We give them real digests instead.)

One empty round-0 certificate per validator (`Certificate::genesis`).

Uncertified, and correctly so: every node derives the same set from the
committee, so there is nothing to agree about. (Narwhal leaves their
`header.id` as 32 zero bytes and then short-circuits `Certificate::verify` on
genesis membership, which means any round-0 certificate with a zeroed header
id from a committee member verifies unconditionally. We give them real
digests instead.)
sourceraw docstring

header-idclj/s

(header-id {:keys [author round payload parents]})

The digest of a header. Excludes the author's signature, as Narwhal's does.

The digest of a header. Excludes the author's signature, as Narwhal's does.
sourceraw docstring

quorum-thresholdclj/s

(quorum-threshold total)

2f+1: 2 * total / 3 + 1, integer division.

2f+1: `2 * total / 3 + 1`, integer division.
sourceraw docstring

replay-orderclj/s

(replay-order state)

Re-derive the whole committed sequence from a restored DAG.

Runs commit-sequence over every round in ascending order, exactly as a live node would have. Because the ordering layer is pure this reproduces what the node emitted before it crashed: a re-derivation, not a log replay.

Re-derive the whole committed sequence from a restored DAG.

Runs `commit-sequence` over every round in ascending order, exactly as a live
node would have. Because the ordering layer is pure this reproduces what the
node emitted before it crashed: a re-derivation, not a log replay.
sourceraw docstring

round-robin-anchorclj/s

(round-robin-anchor round validators _stake)

The round-th validator in sorted order. Stake-blind; useful in tests.

The round-th validator in sorted order. Stake-blind; useful in tests.
sourceraw docstring

stake-weighted-anchorclj/s

(stake-weighted-anchor round validators stake)

Draw an anchor proportional to stake, deterministically in the round.

A seeded draw rather than round mod n, so a validator with three times the stake anchors three times as often. The PRNG is urd's portable one, whose sequences are pinned identical on both platforms -- an anchor rule that disagreed between a browser and the JVM would split the network on the total order without a single message being lost.

Seeded through from-key, not make

This drew from (rng/make round) -- the round number straight into the generator as its state. xorshift32 is GF(2)-linear and one round of it does not decorrelate consecutive seeds, so the draw tracked the seed almost exactly. At four validators with uniform stake the anchors for rounds 2..15 came out :c :d :a :b repeating: literally round mod n, the rule this function exists to replace.

Worse than it sounds, because ANCHORS ONLY EXIST AT EVEN ROUNDS. Sampling that cycle every second round selects from half of it, so for rounds 4..14 only :a and :c ever anchored -- two of the four validators could not be chosen at all across six consecutive waves. Frequency counts over many rounds still look uniform, which is why the existing stake-weighting test passed; the bias is in the ORDER, not the totals. If one of the two reachable anchors is slow, commits stall while the DAG is perfectly healthy.

rng/from-key applies the murmur3 avalanche finalizer before handing the state to xorshift, so every input bit affects every output bit. Raft's randomized-election-timeout carries the same note, for the same reason, found the same way -- the lesson was recorded there and never applied here.

Draw an anchor proportional to stake, deterministically in the round.

A seeded draw rather than `round mod n`, so a validator with three times the
stake anchors three times as often. The PRNG is urd's portable one, whose
sequences are pinned identical on both platforms -- an anchor rule that
disagreed between a browser and the JVM would split the network on the total
order without a single message being lost.

## Seeded through `from-key`, not `make`

This drew from `(rng/make round)` -- the round number straight into the
generator as its state. xorshift32 is GF(2)-linear and one round of it does
not decorrelate consecutive seeds, so the draw tracked the seed almost
exactly. At four validators with uniform stake the anchors for rounds 2..15
came out `:c :d :a :b` repeating: literally `round mod n`, the rule this
function exists to replace.

Worse than it sounds, because ANCHORS ONLY EXIST AT EVEN ROUNDS. Sampling
that cycle every second round selects from half of it, so for rounds 4..14
only `:a` and `:c` ever anchored -- two of the four validators could not be
chosen at all across six consecutive waves. Frequency counts over many
rounds still look uniform, which is why the existing stake-weighting test
passed; the bias is in the ORDER, not the totals. If one of the two
reachable anchors is slow, commits stall while the DAG is perfectly healthy.

`rng/from-key` applies the murmur3 avalanche finalizer before handing the
state to xorshift, so every input bit affects every output bit. Raft's
`randomized-election-timeout` carries the same note, for the same reason,
found the same way -- the lesson was recorded there and never applied here.
sourceraw docstring

store-depthclj/s

(store-depth state)

How far back the certificate store retains, in rounds.

Deeper than gc-depth by construction -- the DAG is the working set, the store is what a lagging peer is served from. Ten times, floored at 100, which at Narwhal's round rate is minutes of history rather than a fixed count: the bound exists to stop unbounded growth, not to make catch-up marginal.

Configurable via :store-depth, and that is not only for tests: the right retention depends on how far behind a peer may fall before it is cheaper to state-sync than to replay, which is a deployment question. It also makes the property testable at all -- with the 100-round floor, a run short enough to be a unit test never reaches the frontier, so the store keeps everything and a test cannot tell a correct horizon from a wrong one.

How far back the certificate store retains, in rounds.

Deeper than `gc-depth` by construction -- the DAG is the working set, the
store is what a lagging peer is served from. Ten times, floored at 100, which
at Narwhal's round rate is minutes of history rather than a fixed count: the
bound exists to stop unbounded growth, not to make catch-up marginal.

Configurable via `:store-depth`, and that is not only for tests: the right
retention depends on how far behind a peer may fall before it is cheaper to
state-sync than to replay, which is a deployment question. It also makes the
property testable at all -- with the 100-round floor, a run short enough to be
a unit test never reaches the frontier, so the store keeps everything and a
test cannot tell a correct horizon from a wrong one.
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-signer string, so anyone who can read this namespace can mint one for any signer. What it does provide is the SHAPE: an attestation binds a signer to a header id, verification is a pure predicate, and an attestation that was not produced this way fails -- which is what a test needs to drive certified? down both branches.

A deployment supplies real signatures here. init refuses to start without them precisely so that 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-signer string, so anyone who can read
this namespace can mint one for any signer. What it does provide is the
SHAPE: an attestation binds a signer to a header id, verification is a pure
predicate, and an attestation that was not produced this way fails -- which
is what a test needs to drive `certified?` down both branches.

A deployment supplies real signatures here. `init` refuses to start without
them precisely so that this stub cannot be reached by accident.
sourceraw docstring

valid-certificate?clj/s

(valid-certificate? state c)

Well formed AND structurally valid against a dag that holds its parents.

Well formed AND structurally valid against a dag that holds its parents.
sourceraw docstring

validity-thresholdclj/s

(validity-threshold total)

f+1: (total + 2) / 3, integer division -- i.e. ceil(total/3).

f+1: `(total + 2) / 3`, integer division -- i.e. ceil(total/3).
sourceraw docstring

vertexclj/s

(vertex c)
source

well-formed?clj/s

(well-formed? state c)

The checks that need nothing but the certificate itself and the committee.

Separated from quorum-of-parents? deliberately. That check resolves parent digests in OUR dag, so it answers no both for a malformed certificate and for a perfectly good one whose history has not arrived yet. Running the two together -- which is what the first version of this port did -- turns every out-of-order arrival into a permanent rejection, and the algorithm still looks like it works, because gossip eventually redelivers everything in an order that happens to succeed.

The checks that need nothing but the certificate itself and the committee.

Separated from `quorum-of-parents?` deliberately. That check resolves parent
digests in OUR dag, so it answers `no` both for a malformed certificate and
for a perfectly good one whose history has not arrived yet. Running the two
together -- which is what the first version of this port did -- turns every
out-of-order arrival into a permanent rejection, and the algorithm still
looks like it works, because gossip eventually redelivers everything in an
order that happens to succeed.
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