Chapter 06 · ~14 min
Commonware: the parts we stand on
Library, not framework — what we delegate, what we keep, and why the same stack runs in two universes.
What you’ll walk away with
- What “library, not framework” buys: we compose consensus primitives and keep everything we didn’t delegate — block format, execution, storage, RPC
- The whole integration surface: two small adapters, two traits, and one seam (
ExecutionEnv) with four core calls - Why the same production stack runs under tokio and under a seeded deterministic scheduler — and why that makes testing an architecture property, not a QA habit
Three doors
Picture the meeting where this started. The task: add BFT consensus to a production sequencer — a live system with its own block format, its own VM, its own storage and RPC and operational habits. There are three doors out of that meeting.
Door one: write the protocol yourself. You just read chapter 3. Thirty years of very smart people shipping subtle liveness bugs is not a dare — and even a correct implementation buys you a decade of maintaining your own view-change lore.
Door two: adopt a framework. A chain-in-a-box gives you consensus on day one — and its opinions forever after. Its block format, its state model, its storage engine, its RPC surface. Our node already has all of those, in production. Re-platforming a working system into a framework’s shape is a rewrite wearing a costume.
Door three: compose a library. Take the parts that took thirty years to get right — the vote state machine, authenticated networking, durable journals and archives — as primitives with seams, and keep absolute ownership of everything else. That’s commonware: a toolkit of composable blockchain components, each usable without the others.
The rest of this chapter is what door three looks like in our repository — and the punchline is how little of it there is.
The parts, by name
Twelve crates, one version, pinned exactly. Below is the map: click any crate to see
what we actually take from it and where it lives in our tree. Start with
consensus — the big one — then runtime, which is the quiet hero of this whole
chapter. Notice two crates are dashed: our code never imports them directly; they’re
pinned only so the family moves in lockstep.
Read the footer line again, because it’s the chapter’s first aha in one sentence: the list of things not on that map — block format, transaction types, execution, state storage, RPC — is exactly the list of things a framework would have taken. Everything our users touch stayed ours.
One validator, assembled
So what do the parts make? One file in the repo answers that literally — lib/consensus/core/src/stack.rs assembles a validator’s entire consensus stack, and opens with an ASCII diagram of what it built. The widget below is that diagram, made clickable. Click through the layers top to bottom — the right edge tells you who owns each one. Then press the swap button and watch carefully which boxes change. Missions:
- Click the simplex engine — it speaks only 32-byte digests. Full blocks travel on a separate gossip channel and meet the engine’s world one layer down.
- Click Inline — by the time our code is asked anything, the block’s structure (parent linkage, heights, epoch rules) is already checked. We only judge content.
- Click the two gold layers — our adapters and the seam. This is the entire surface we maintain toward the library.
- Now swap universes, and find every box that did not change.
Put into one sentence what you just clicked through:
Our commonware-facing code is two thin adapters implementing two traits —
Application(build a block, verify a block) andReporter(a finalized block arrived) — and everything consensus asks of the node fits in one trait,ExecutionEnv, with four core calls:genesis_block,build,verify,commit.
That’s the whole treaty between thirty years of consensus research and our node. The
trait is about a hundred readable lines —
lib/consensus/core/src/execution.rs — and its doc comment
states the design rule this chapter orbits: everything consensus needs from the node
goes through it, and nothing else. (The same Reporter trait, implemented a second
time, is how the node’s observability taps the full activity stream — votes,
certificates, misbehavior evidence. That story belongs to the operations chapter.)
For the rigor-minded: what “generic” means here, precisely
The engine is generic over a signing scheme (what a vote physically is — chapter 5’s multisig and threshold designs are both schemes under this abstraction) and a leader elector (chapter 4 used round-robin; unpredictable rotations slot into the same seam). It is also generic over the runtime and the network, which is the next section. The concrete choices our committee runs with are configuration, not architecture — the reference docs and lib/consensus/core/src/types.rs are the source of truth for what’s instantiated today, and this guide deliberately doesn’t hard-code it.
Same code, two universes
Here is the design decision that quietly pays for everything chapter 4 made you feel. Nothing in the consensus stack ever calls the clock, spawns a task, opens a socket, or touches a disk directly. All of it goes through runtime traits — a clock, a spawner, storage, metrics — plus a network interface. Which means the universe the stack runs in is a parameter:
- Production: the tokio runner. Real milliseconds, real sockets, real disks.
- Test: the deterministic runner. A seeded scheduler decides every task interleaving; time is virtual (a timeout costs nothing to wait out); storage is in-memory but survives simulated crashes — so journals behave like a disk that made it through the power cut. And the runtime keeps an audit fingerprint — a rolling hash over every scheduling decision and random draw — so two runs of the same seed can be compared bit-for-bit. Every simulation scenario in the repo runs twice per seed, and the fingerprints must match; nondeterminism doesn’t make tests flaky here, it makes them fail.
Consensus bugs live in interleavings — a crash between a vote and its broadcast, a partition healing one message too late. Wall-clock tests sample interleavings at random and pray; a seeded scheduler enumerates them on demand, and replays any of them exactly. The testing doc says it best, and it’s worth quoting verbatim:
The deterministic swap is not a mock of consensus — it is the production consensus stack on different hardware.
If this sounds familiar, it should: every simulator in this guide works the same way — seeded randomness, virtual time, “reset” replaying the identical run. The guide didn’t invent that style; it inherited the repo’s.
The toy blockchain you already have
The plan for this chapter once included “build a toy blockchain to play with”.
It turns out the repo ships one, and it’s load-bearing:
lib/consensus/sim/ is a complete miniature chain — the
production consensus stack over the deterministic runtime, with an ExecutionEnv of
content-free blocks (and a second one running the real VM over in-memory state,
for scenarios where state itself is the assertion). Its test directory is a scenario
corpus organized by capability — partitions, byzantine peers of both flavors,
crash-restart, epochs, late join, migration — and it runs in CI. A guided reading
order, roughly forty-five minutes:
- lib/consensus/core/src/execution.rs — the seam trait itself. Read the doc comments; you now have the vocabulary for all of them.
- lib/consensus/sim/src/execution.rs — the mock environment. Notice how little a “blockchain” needs to be.
- lib/consensus/sim/src/cluster.rs —
SimCluster: N validators, scriptable links (partitions, loss, latency), crash and restart. - One scenario file from lib/consensus/sim/tests/ —
partitions.rsreads like chapter 4’s mission list written in Rust, which is not a coincidence.
Try to break it
Time to attack the claim this chapter stands on. Below are two complete simplex committees — chapter 4’s protocol, animation removed — each running twelve virtual seconds over a lossy network, each producing an event trace and a rolling fingerprint (same shape as the runtime’s audit fingerprint). Your mission is to catch determinism lying:
- Both runs start from the same seed. Compare the fingerprints — then look at the last events of each trace. Identical, event for event.
- Press harsher network: 20% packet loss, doubled latency. Surely chaos breaks it? Chaos is drawn from the seed — the same messengers fall in the same ditches. Fingerprints still match.
- Now try to make the same seed produce two different histories. Take your time with the controls. There is no combination that does it — and that missing button is precisely what the repo’s whole testing strategy stands on.
- Concede, and press new fate for B: a different seed, a genuinely different universe — and the widget pins the exact first event where the histories fork. When a nightly seed sweep fails, that fork point is what the harness hands the engineer: not “flaky”, but seed 1999, event 41, here’s the replay.
The four things to keep
- Library, not framework: consensus arrived as primitives we compose. The block format, VM, storage, and RPC never left our codebase — chapter 7 is possible because of this.
- The integration surface is deliberately tiny: two adapter structs, two library traits (
Application,Reporter), one seam trait (ExecutionEnv) with four core calls. - The engine speaks digests; blocks travel by gossip; marshal and its
Inlinewrapper bridge the two worlds and hand us only structurally-valid blocks. - The runtime is a parameter: tokio in production, a seeded deterministic scheduler in tests — same consensus code, fingerprint-checked determinism, perfect reproducers. Testing as architecture, not habit.
Where this lives in our stack
- The reference docs’ tour of these parts is docs/src/consensus/intro.md (“What commonware provides”) — five paragraphs you’ll now read as a checklist of things you’ve clicked. The deep map of the production side — every trait, channel, and knob the seam widget showed you — is docs/src/consensus/integration.md.
- The testing philosophy in full — the five enforced principles, the test pyramid, and where each layer’s authority ends — is docs/src/consensus/testing.md. This chapter’s fingerprint game is that document, miniaturized.
- The assembly itself: lib/consensus/core/src/stack.rs (the diagram you clicked is its module doc), the seam in lib/consensus/core/src/execution.rs, the adapters in lib/consensus/core/src/application.rs and committer.rs beside it.
- The pin policy is a comment in the workspace Cargo.toml (search for “commonware”): one exact version for the family, upgrades as a deliberate chore. Wire-format immutability — why upgrades can’t quietly change bytes — is enforced by the golden tests in lib/wire/, a thread the operations chapter picks up.
Next: you now know the seam by name — four calls, one trait. Chapter 7 is what
lives behind it on the real node: how build borrows the sequencer’s
machinery, how verify means re-executing a leader’s block before vouching
for it, how speculative state floats in overlays until finality collapses it, and
where certificates go to live forever.