In this track: Build, test and reason
advanced · 4 MIN READ

Test storage invariants

Check logical state properties across every modifying path.

LET’S PICTURE IT

A cabinet can have valid drawer positions while its records still contradict each other.

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.

Separate layout from accounting

Compiler output identifies positions and types. An invariant concerns meaning, such as total recorded credits remaining bounded by accounted assets.

Exercise all relevant transitions

Deposits, withdrawals, fees and emergency actions can change the property. A fixture should include the paths that the stated invariant covers.

Check after failures too

Rejected actions should leave the relevant state consistent. Catching a child failure can require explicit parent-side handling rather than a blanket rollback assumption.

Keep assumptions visible

Supported token behavior, direct donations and rounding can change the correct property. A test oracle must match those assumptions.

Define the test

before action: assert invariant
perform allowed or rejected action
after action: assert invariant
repeat across multiple actors and sequences

Test plans are framework-neutral unless labeled as complete Solidity or JavaScript. Use a local test environment and no real funds.

QUICK CHECK · NO PRESSURE

Does correct field packing prove a financial accounting invariant?

Your progress stays in this browser.
Keep exploring the original referenceethereum.org ↗

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