In this track: Advanced security reasoning
advanced · 4 MIN READ

Accounting invariants

State the quantity that must remain conserved or bounded.

LET’S PICTURE IT

A ledger should explain every coin entering, leaving or remaining owed.

SEE IT FOR YOURSELF
Pack small values into Solidity storage slotsStep 1 / 4
Pack small values into Solidity storage slotsFirstuint128 A16 bytesSeconduint128 B16 bytesThirduint256 C32 bytesSimple elementary types only; not a complete compiler layoutSlot 0 · 32 bytesUnused · 16 BA · 16 BHigh-order bytes ← → low-order bytesSlot 0 · fullB · 16 BA · 16 BDeclaration order and sizes determine this packingSlot 0BASlot 1C · 32 bytesTry different sequences in the storage-layout tool

In this simple default-layout example, two uint128 values come before one uint256. Each slot holds thirty-two bytes.

Animated local teaching model. No wallet, RPC or live blockchain data. Some labels and hashes are abbreviated for readability.

Choose a precise property

An example is total recorded credits never exceeding accounted assets under the supported asset model. State whether direct transfers, fees and rounding are included.

Include every transition

Deposits, withdrawals, transfers, fees, upgrades and emergency actions can affect the property. Testing only the main deposit path misses other state changes.

Model external asset behavior

Rebases, transfer fees or unsolicited balances can break simplistic equality assumptions. Define supported behavior and the expected accounting treatment.

Test action sequences

Invariant testing should generate sequences and multiple actors. A property that holds after isolated calls can still fail after a particular combination.

Work through the boundary

accounted assets >= outstanding redeemable obligations

Define rounding, fees, direct donations and supported assets before treating this as a test oracle.

A simplified local review scenario. The example establishes a question to test, not a guarantee of production safety.

QUICK CHECK · NO PRESSURE

Should an invariant state its asset-model assumptions?

Your progress stays in this browser.
Keep exploring the original referenceSolidity documentation ↗

Original explanations by EtherTools. Examples are educational and source-linked; this site is independent of the Ethereum Foundation. Our approach.