Liking cljdoc? Tell your friends :D

Conservation laws

Five properties of the stake state that must never be false, checked at the end of every block. When one is false, the node halts.

Cosmos calls these module invariants and registers four for staking — delegator-shares, nonnegative-power, positive-delegation, module-accounts — in a crisis module whose job is exactly this.

Checks, not contracts

skuld's invariants validate and never compute. That makes them a good fit for a guard on an attribute and a poor one for staking's state transitions, which are shared-accumulator arithmetic: F1's cumulative reward-per-token cannot be produced by a delegator and checked, because two delegators in one block each compute against a pre-state the other has already changed.

So the computation stays in Clojure, where it is tested, and what moves into data is the properties. That is what Cosmos and Solana both do, and it is the useful half rather than a compromise.

Why halting, and not refusing

These run after a block has been applied. If share conservation is broken, the damage has already happened — refusing the current transaction undoes nothing, and every subsequent block builds on the wrong state.

A node that detects it must stop producing rather than continue.

That makes this a deliberate node-killer, so the bar for adding a check here is high: a property that is merely usually true would halt the chain on a legitimate edge case.

The five

Each corresponds to a bug this codebase actually shipped.

checkstatesthe bug
delegator-sharesevery validator's share total equals the sum of its delegations'undelegate took its share count on trust, so unbonding more shares than were held minted tokens
nonnegative-powerno validator holds negative tokens or sharesa negative remainder matched neither the zero? nor the pos? branch and left the delegation intact
positive-delegationno delegation holds negative shares—
no-zombie-sharesno validator holds shares against zero tokensthe redemption rate is tokens/shares, so any delegation into it divides by zero
nonnegative-balancesno account holds a negative balancea stale balance read let the same tokens delegate twice

delegator-shares is the one that makes shares meaningful at all: a slash moves tokens and leaves share counts alone, so the two numbers are only ever reconciled through this identity. If it drifts, every delegator's redemption value is wrong and nothing else would say so.

They run cheapest first; delegator-shares is last because it aggregates over every delegation row.

One port deliberately not made

Cosmos's PositiveDelegationInvariant forbids zero shares. Here a zero-share row is a legitimate tombstone left by a complete undelegation, so porting it as written would halt the chain on the first one.

That is the whole argument for reading a reference implementation rather than copying it.

Turning them off

:check-conservation? false skips them. The trade is explicit: a node that skips them is faster and will build silently on wrong state — which is the same trade Cosmos makes by putting its equivalents behind a crisis module rather than deleting them.

A note on writing checks like these

The share-sum query needs :with ?e. Without it, Datalog's set semantics collapse two delegations that happen to hold equal share counts, and the sum comes out short — which would halt a healthy chain.

See also

Staking for the state these are stated over

Can you improve this documentation?Edit on GitHub

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