What is invariant testing in smart contracts?

Invariant testing is fuzzing across many calls: the tool sends random sequences of transactions to your contracts and, after each call, checks properties that must hold for the whole system, such as solvency or total supply. It finds bugs that only appear after a particular series of deposits, withdrawals and price moves, which a single-call fuzz test can't reach.

Last updated . 2 minute read. By the QuillAudits Team.

How does Foundry run an invariant test?

Forge runs random sequences of function calls and checks every invariant_* function after each call (Foundry). By default the targets are the contracts deployed in setUp (Foundry Book). These settings shape a campaign (Foundry config):

Setting What it controls Default
runs Call sequences generated per invariant test 256
depth Calls in each sequence 500
fail_on_revert Whether a reverting call fails the test false

A bank and a solvency invariant test (illustrative, not audited code) BankInvariant.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;

contract Bank {
    mapping(address => uint256) public balanceOf;
    uint256 public totalDeposits;

    function deposit() external payable {
        balanceOf[msg.sender] += msg.value;
        totalDeposits += msg.value;
    }

    function withdraw(uint256 amount) external {
        balanceOf[msg.sender] -= amount;
        totalDeposits -= amount;
        payable(msg.sender).transfer(amount);
    }
}

contract BankInvariantTest {
    Bank internal bank;

    function setUp() public {
        bank = new Bank(); // deployed here, so it becomes a target
    }

    // Checked after every call in every sequence.
    function invariant_bankIsSolvent() public view {
        assert(address(bank).balance >= bank.totalDeposits());
    }
}

When an invariant breaks, Forge shrinks the failing sequence, trying up to 5,000 times by default (shrink_run_limit), then saves it under cache/invariant and replays it first on later runs until the bug is fixed (Foundry config).

What makes a good invariant test?

  • Properties come from the spec, not from reading the code: solvency, supply equal to the sum of balances, and no shares minted without payment.
  • Handlers bound amounts and pick existing users, so most calls do something instead of reverting.
  • Ghost variables in the handler track what the protocol should hold, to compare with what it does hold.
  • Sequences are deep enough to reach the states that matter, such as a liquidation after several price moves.

Setting call_override makes Forge inject reentrant calls into the targeted contracts during the run, so the same solvency invariant also tests for reentrancy (Foundry config).

Handlers and ghost variables are the terms Foundry uses for these two ideas (Foundry). The invariant entry has example properties by protocol type.

QuillShield is building the security layer between AI and money, so you can keep shipping between audits.

Sign-up is free, and new accounts get $50 in credit, good for up to half of an order.

  1. 1Sign up and import a repo
  2. 2See the exact price before anything is charged
  3. 3Approve it, and the audit starts