> For the complete documentation index, see [llms.txt](https://ret2basic.gitbook.io/ctfnote/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ret2basic.gitbook.io/ctfnote/web3-security-research/foundry/invariant-testing-part-1.md).

# Invariant Testing - Part 1

{% embed url="<https://youtu.be/JtzBi67hgLI>" %}
Invariant Testing - Part 1
{% endembed %}

## Setup

Test file:

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

import "forge-std/Test.sol";
import "forge-std/console.sol";

// Topics
// - Invariant
// - Difference between fuzz and invariant
// - Failing invariant
// - Passing invariant
// - Stats - runs, calls, reverts

contract InvariantIntro {
    bool public flag;

    function func_1() external {}
    function func_2() external {}
    function func_3() external {}
    function func_4() external {}

    function func_5() external {
        flag = true;
    }
}

contract IntroInvariantTest is Test {
    InvariantIntro private target;

    function setUp() public {
        target = new InvariantIntro();
    }

    function invariant_flag_is_always_false() public {
        assertEq(target.flag(), false);
    }
}
```

## Fuzzing vs. Invariant Testing

In fuzzing, Foundry randomly generates input data based on the constraits you set and feeds them into a function.

In invariant testing, Foundry calls a sequence of functions in random order and checks if an "invariant" is always satisfied.

What is an "invariant"?

{% embed url="<https://book.getfoundry.sh/forge/invariant-testing>" %}

> <mark style="color:red;">**Invariants are conditions expressions that should always hold true over the course of a fuzzing campaign.**</mark> A good invariant testing suite should have as many invariants as possible, and can have different testing suites for different protocol states.

<figure><img src="https://3988450783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MWVjG_njKgBtvmnKaJh%2Fuploads%2Frc9MpZvHxUBS1Tr4CNRV%2Fimage.png?alt=media&amp;token=ba61c04b-09b7-4d2b-b3de-cdbc519167cb" alt=""><figcaption></figcaption></figure>

## Writing an invariant test

<mark style="color:red;">**To transform a regular test case to invariant test, we must name it with the prefix "invariant".**</mark> For example:

```solidity
    function invariant_flag_is_always_false() public {
        assertEq(target.flag(), false);
    }
```

Foundry is smart enough to call `func_1()` through `func_5()` in random sequences with 20% probability for each function. Eventually it is going to hit `func_5()` so the invariant fails.

## Writing invariant test for WETH

Target contract:

```solidity
pragma solidity 0.8.18;

contract WETH {
    string public name = "Wrapped Ether";
    string public symbol = "WETH";
    uint8 public decimals = 18;

    event Approval(address indexed src, address indexed guy, uint256 wad);
    event Transfer(address indexed src, address indexed dst, uint256 wad);
    event Deposit(address indexed dst, uint256 wad);
    event Withdrawal(address indexed src, uint256 wad);

    mapping(address => uint256) public balanceOf;
    mapping(address => mapping(address => uint256)) public allowance;

    receive() external payable {
        deposit();
    }

    function deposit() public payable {
        balanceOf[msg.sender] += msg.value;
        emit Deposit(msg.sender, msg.value);
    }

    function withdraw(uint256 wad) public {
        balanceOf[msg.sender] -= wad;
        payable(msg.sender).transfer(wad);
        emit Withdrawal(msg.sender, wad);
    }

    function totalSupply() public view returns (uint256) {
        return address(this).balance;
    }

    function approve(address guy, uint256 wad) public returns (bool) {
        allowance[msg.sender][guy] = wad;
        emit Approval(msg.sender, guy, wad);
        return true;
    }

    function transfer(address dst, uint256 wad) public returns (bool) {
        return transferFrom(msg.sender, dst, wad);
    }

    function transferFrom(address src, address dst, uint256 wad)
        public
        returns (bool)
    {
        if (
            src != msg.sender && allowance[src][msg.sender] != type(uint256).max
        ) {
            allowance[src][msg.sender] -= wad;
        }

        balanceOf[src] -= wad;
        balanceOf[dst] += wad;

        emit Transfer(src, dst, wad);

        return true;
    }
}
```

Test file:

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

import "forge-std/Test.sol";
import "forge-std/console.sol";
import {WETH} from "../../src/WETH.sol";

// https://book.getfoundry.sh/forge/invariant-testing?highlight=targetSelector#invariant-targets
// https://mirror.xyz/horsefacts.eth/Jex2YVaO65dda6zEyfM_-DXlXhOWCAoSpOx5PLocYgw

// NOTE: open testing - randomly call all public functions
contract WETH_Open_Invariant_Tests is Test {
    WETH public weth;

    function setUp() public {
        weth = new WETH();
    }

    receive() external payable {}

    // NOTE: - calls = runs x depth, (runs, calls, reverts)
    function invariant_totalSupply_is_always_zero() public {
        assertEq(0, weth.totalSupply());
    }
}
```

Many calls failed because `msg.sender` does not have enough balance in WETH. In Part 2 we are going to improve this invariant test.
