Properties of the stake state that must never be false, checked every block.
Cosmos calls these MODULE INVARIANTS and registers four for staking --
delegator-shares, nonnegative-power, positive-delegation and
module-accounts -- in a crisis module whose job is to HALT THE CHAIN when
one is proven false. These are the same idea in urd's vocabulary.
urd's invariants (skuld.invariant) evaluate a Datalog query and reject a
transaction when it is false. They validate; they 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 the same block would each compute against a pre-state the other
has already changed.
So the computation stays in skuld.ledger.stake, where it is tested, and what
moves into data is the PROPERTIES. This is what Cosmos and Solana both do,
and it is worth being plain that it is the useful half rather than a
compromise.
These run AFTER a block has been applied. If share conservation is broken, the damage has already happened and refusing the current transaction undoes nothing -- the state is wrong and every subsequent block builds on it. A node that detects it must stop producing rather than continue, which is exactly what Cosmos's crisis module does.
That makes assert! a deliberate node-killer, and the bar for adding a check
here is correspondingly high: a property that is merely usually true would
halt the chain on a legitimate edge case. Every check below is stated over
the projection in skuld.block, was verified to hold on real state before
it was written down, and has a test that breaks it on purpose.
Each corresponds to a bug this repository actually shipped and fixed with a single hand-written test:
delegator-shares undelegate took its share count on trust, so
unbonding more shares than were held minted tokens
positive-delegation a fully-slashed validator kept shares against zero
tokens -- stake/delegate now throws on it
nonnegative-power a negative remainder matched neither the zero? nor the
pos? branch and left the delegation intact
A test catches one case once. A check here catches the class, every block.
Properties of the stake state that must never be false, checked every block.
Cosmos calls these MODULE INVARIANTS and registers four for staking --
`delegator-shares`, `nonnegative-power`, `positive-delegation` and
`module-accounts` -- in a crisis module whose job is to HALT THE CHAIN when
one is proven false. These are the same idea in urd's vocabulary.
## Why they are checks and not contracts
urd's invariants (`skuld.invariant`) evaluate a Datalog query and reject a
transaction when it is false. They validate; they 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 the same block would each compute against a pre-state the other
has already changed.
So the computation stays in `skuld.ledger.stake`, where it is tested, and what
moves into data is the PROPERTIES. This is what Cosmos and Solana both do,
and it is worth being plain that it is the useful half rather than a
compromise.
## Why halting is the right response, and refusing is not
These run AFTER a block has been applied. If share conservation is broken,
the damage has already happened and refusing the current transaction undoes
nothing -- the state is wrong and every subsequent block builds on it. A node
that detects it must stop producing rather than continue, which is exactly
what Cosmos's crisis module does.
That makes `assert!` a deliberate node-killer, and the bar for adding a check
here is correspondingly high: a property that is merely usually true would
halt the chain on a legitimate edge case. Every check below is stated over
the projection in `skuld.block`, was verified to hold on real state before
it was written down, and has a test that breaks it on purpose.
## These are not hypothetical
Each corresponds to a bug this repository actually shipped and fixed with a
single hand-written test:
delegator-shares undelegate took its share count on trust, so
unbonding more shares than were held minted tokens
positive-delegation a fully-slashed validator kept shares against zero
tokens -- `stake/delegate` now throws on it
nonnegative-power a negative remainder matched neither the zero? nor the
pos? branch and left the delegation intact
A test catches one case once. A check here catches the class, every block.(assert! db)Halt unless every check holds.
Throws, and a caller must NOT catch it. Cosmos halts the chain here for the reason given in the namespace docstring: these run after the block, so a violation is damage already done, and continuing to produce blocks on state known to be wrong is worse than stopping.
Returns db so it can sit in a threading form.
Halt unless every check holds. Throws, and a caller must NOT catch it. Cosmos halts the chain here for the reason given in the namespace docstring: these run after the block, so a violation is damage already done, and continuing to produce blocks on state known to be wrong is worse than stopping. Returns `db` so it can sit in a threading form.
Every check, by name. Ordered so the cheapest run first.
Every check, by name. Ordered so the cheapest run first.
(delegator-shares db)Every validator's share total equals the sum of its delegations'.
Cosmos's DelegatorSharesInvariant, and the reason shares exist at all: a
slash reduces a validator's TOKENS and leaves the 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.
Zero-share rows are counted and contribute nothing, which is why a fully
unbonded delegation left in the database by save-stake-tx-data does not
break this.
Every validator's share total equals the sum of its delegations'. Cosmos's `DelegatorSharesInvariant`, and the reason shares exist at all: a slash reduces a validator's TOKENS and leaves the 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. Zero-share rows are counted and contribute nothing, which is why a fully unbonded delegation left in the database by `save-stake-tx-data` does not break this.
(no-zombie-shares db)No validator holds shares against zero tokens.
The redemption rate is tokens/shares, so this state has no defined value for
a delegation and any delegation into it would divide by zero. stake/delegate
throws on it; this is the same property stated over persisted state, which
catches it arriving by any other route.
No validator holds shares against zero tokens. The redemption rate is tokens/shares, so this state has no defined value for a delegation and any delegation into it would divide by zero. `stake/delegate` throws on it; this is the same property stated over persisted state, which catches it arriving by any other route.
(nonnegative-balances db)No account holds a negative balance.
Not one of Cosmos's four -- their equivalent is module-accounts, which
reconciles the bonded and not-bonded pools against the validators, and urd
has no pool accounts to reconcile because delegated tokens simply leave the
account. What it does have is the failure that pooling would have caught: a
debit applied without checking the balance first. The staking intent path
spends balances at the end of the block, and a stale read there let the same
tokens delegate twice.
No account holds a negative balance. Not one of Cosmos's four -- their equivalent is `module-accounts`, which reconciles the bonded and not-bonded pools against the validators, and urd has no pool accounts to reconcile because delegated tokens simply leave the account. What it does have is the failure that pooling would have caught: a debit applied without checking the balance first. The staking intent path spends balances at the end of the block, and a stale read there let the same tokens delegate twice.
(nonnegative-power db)No validator holds negative tokens or negative shares.
Cosmos's NonNegativePowerInvariant. Negative stake is not a small error --
it is a validator whose consensus power the set computation cannot represent,
and it arrived here once already through an unchecked subtraction.
No validator holds negative tokens or negative shares. Cosmos's `NonNegativePowerInvariant`. Negative stake is not a small error -- it is a validator whose consensus power the set computation cannot represent, and it arrived here once already through an unchecked subtraction.
(positive-delegation db)No delegation holds negative shares.
Cosmos's PositiveDelegationInvariant forbids zero as well. Here zero is
legitimate in the DATABASE and absent from the model: save-stake-tx-data
does not yet retract a fully unbonded delegation, and load-stake drops
zero-share rows on the way back in. Forbidding zero would therefore halt the
chain on the first complete undelegation, which is a good illustration of why
a check here needs its own justification rather than a port.
No delegation holds negative shares. Cosmos's `PositiveDelegationInvariant` forbids zero as well. Here zero is legitimate in the DATABASE and absent from the model: `save-stake-tx-data` does not yet retract a fully unbonded delegation, and `load-stake` drops zero-share rows on the way back in. Forbidding zero would therefore halt the chain on the first complete undelegation, which is a good illustration of why a check here needs its own justification rather than a port.
(violations db)Every check that does not hold, as {:check name :detail [...]}.
Data rather than an exception, so a caller can log all of them, test them, or
decide for itself what a violation means. assert! is the one that halts.
Every check that does not hold, as `{:check name :detail [...]}`.
Data rather than an exception, so a caller can log all of them, test them, or
decide for itself what a violation means. `assert!` is the one that halts.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 |