Skip to content

Commit 0aa2581

Browse files
authored
Merge pull request #255 from crytic/dev-state-network-forking
State network fuzzing tutorial
2 parents 52a49d7 + 7aa4097 commit 0aa2581

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

program-analysis/echidna/advanced/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99
- [How to Use Hevm Cheats to Test Permit](./hevm-cheats-to-test-permit.md): Find out how to test code that relies on ecrecover signatures by using hevm cheat codes.
1010
- [How to Seed Echidna with Unit Tests](./end-to-end-testing.md): Discover how to use existing unit tests to seed Echidna.
1111
- [Understanding and Using `multi-abi`](./using-multi-abi.md): Learn what `multi-abi` testing is and how to utilize it effectively.
12-
- [Interacting with off-chain data via FFI cheatcode](./interacting-with-offchain-data-via-ffi.md): Using the `ffi` cheatcode as a way of communicating with the operating system.
12+
- [How to do on-chain fuzzing with state forking](./state-network-forking.md): How Echidna can use the state of blockchain during a fuzzing campaign
13+
- [Interacting with off-chain data via FFI cheatcode](./interacting-with-offchain-data-via-ffi.md): Using the `ffi` cheatcode as a way of communicating with the operating system
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# On-chain fuzzing with state forking
2+
3+
**Table of contents:**
4+
5+
- [On-chain fuzzing with state forking](#on-chain-fuzzing-with-state-forking)
6+
- [Introduction](#introduction)
7+
- [Example](#example)
8+
- [Corpus and RPC cache](#corpus-and-rpc-cache)
9+
- [Coverage and Etherscan integration](#coverage-and-etherscan-integration)
10+
11+
## Introduction
12+
13+
Echidna recently added support for state network forking, starting from the 2.1.0 release. In a few words, our fuzzer can run a campaign starting with an existing blockchain state provided by an external RPC service (Infura, Alchemy, local node, etc). This enables users to speed up the fuzzing setup when using already deployed contracts.
14+
15+
## Example
16+
17+
In the following contract, an assertion will fail if the call to [Compound ETH](https://etherscan.io/token/0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5) `mint` function succeeds and the balance of the contract increases.
18+
19+
```solidity
20+
interface IHevm {
21+
function warp(uint256 newTimestamp) external;
22+
23+
function roll(uint256 newNumber) external;
24+
}
25+
26+
interface Compound {
27+
function mint() external payable;
28+
29+
function balanceOf(address) external view returns (uint256);
30+
}
31+
32+
contract TestCompoundEthMint {
33+
address constant HEVM_ADDRESS = 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D;
34+
IHevm hevm = IHevm(HEVM_ADDRESS);
35+
Compound comp = Compound(0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5);
36+
37+
constructor() {
38+
hevm.roll(16771449); // sets the correct block number
39+
hevm.warp(1678131671); // sets the expected timestamp for the block number
40+
}
41+
42+
function assertNoBalance() public payable {
43+
require(comp.balanceOf(address(this)) == 0);
44+
comp.mint{ value: msg.value }();
45+
assert(comp.balanceOf(address(this)) == 0);
46+
}
47+
}
48+
```
49+
50+
In order to use this feature, the user needs to specify the RPC endpoint for Echidna to use before running the fuzzing campaign. This requires using the `ECHIDNA_RPC_URL` and `ECHIDNA_RPC_BLOCK` environment variables:
51+
52+
```
53+
$ ECHIDNA_RPC_URL=http://.. ECHIDNA_RPC_BLOCK=16771449 echidna compound.sol --test-mode assertion --contract TestCompoundEthMint
54+
...
55+
assertNoBalance(): failed!💥
56+
Call sequence, shrinking (885/5000):
57+
assertNoBalance() Value: 0xd0411a5
58+
```
59+
60+
Echidna will query contract code or storage slots as needed from the provided RPC node. You can press the key `f` key to see which contracts/slots are fetched.
61+
62+
Please note that only the state specified in the `ECHIDNA_RPC_BLOCK` will be fetched. If Echidna increases the block number, it is all just simulated locally but its state is still loaded from the initially set RPC block.
63+
64+
## Corpus and RPC cache
65+
66+
If a corpus directory is used (e.g. `--corpus-dir corpus`), Echidna will save the fetched information inside the `cache` directory.
67+
This will speed up subsequent runs, since the data does not need to be fetched from the RPC. It is recommended to use this feature, in particular if the testing is performed as part of the CI tests.
68+
69+
```
70+
$ ls corpus/cache/
71+
block_16771449_fetch_cache_contracts.json block_16771449_fetch_cache_slots.json
72+
```
73+
74+
## Coverage and Etherscan integration
75+
76+
When the fuzzing campaign is over, if the source code mapping of any executed on-chain contract is available on Etherscan, it will be fetched automatically for the coverage report. Optionally, an Etherscan key can be provided using the `ETHERSCAN_API_KEY` environment variable.
77+
78+
```
79+
Fetching Solidity source for contract at address 0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5... Retrying (5 left). Error: Max rate limit reached, please use API Key for higher rate limit
80+
Retrying (4 left). Error: Max rate limit reached, please use API Key for higher rate limit
81+
Retrying (3 left). Error: Max rate limit reached, please use API Key for higher rate limit
82+
Success!
83+
Fetching Solidity source map for contract at address 0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5... Error!
84+
```
85+
86+
While the source code for the [cETH contract is available](https://etherscan.io/address/0x4ddc2d193948926d02f9b1fe9e1daa0718270ed5#code), their source maps are NOT.
87+
In order to generate the coverage report for a fetched contract, **both** source code and source mapping should be available. In that case, there will be a new directory inside the corpus directory to show coverage for each contract that was fetched. In any case, the coverage report will be always available for the user-provided contracts, such as this one:
88+
89+
```
90+
20 | |
91+
21 | *r | function assertNoBalance() public payable {
92+
22 | *r | require(comp.balanceOf(address(this)) == 0);
93+
23 | *r | comp.mint{value: msg.value}();
94+
24 | *r | assert(comp.balanceOf(address(this)) == 0);
95+
25 | | }
96+
```

0 commit comments

Comments
 (0)