-
Notifications
You must be signed in to change notification settings - Fork 374
State network fuzzing tutorial #255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6ef8492
Create state-network-forking.md
gustavo-grieco d1abd67
Update README.md
gustavo-grieco ea2b242
Update state-network-forking.md
gustavo-grieco a9d0e48
Update README.md
gustavo-grieco 2886f39
Update state-network-forking.md
gustavo-grieco 3b628c2
Merge remote-tracking branch 'origin/master' into dev-state-network-f…
gustavo-grieco 336db36
run format
gustavo-grieco 5cfa1ba
Update state-network-forking.md
gustavo-grieco b13d4b8
Merge remote-tracking branch 'origin/master' into dev-state-network-f…
gustavo-grieco 3b47e68
npm run format
gustavo-grieco 7aa4097
Merge branch 'master' into dev-state-network-forking
montyly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
program-analysis/echidna/advanced/state-network-forking.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| # On-chain fuzzing with state forking | ||
|
|
||
| **Table of contents:** | ||
|
|
||
| - [On-chain fuzzing with state forking](#on-chain-fuzzing-with-state-forking) | ||
| - [Introduction](#introduction) | ||
| - [Example](#example) | ||
| - [Corpus and RPC cache](#corpus-and-rpc-cache) | ||
| - [Coverage and Etherscan integration](#coverage-and-etherscan-integration) | ||
|
|
||
| ## Introduction | ||
|
|
||
| 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. | ||
|
|
||
| ## Example | ||
|
|
||
| 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. | ||
|
|
||
| ```solidity | ||
| interface IHevm { | ||
| function warp(uint256 newTimestamp) external; | ||
|
|
||
| function roll(uint256 newNumber) external; | ||
| } | ||
|
|
||
| interface Compound { | ||
| function mint() external payable; | ||
|
|
||
| function balanceOf(address) external view returns (uint256); | ||
| } | ||
|
|
||
| contract TestCompoundEthMint { | ||
| address constant HEVM_ADDRESS = 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D; | ||
| IHevm hevm = IHevm(HEVM_ADDRESS); | ||
| Compound comp = Compound(0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5); | ||
|
|
||
| constructor() { | ||
| hevm.roll(16771449); // sets the correct block number | ||
| hevm.warp(1678131671); // sets the expected timestamp for the block number | ||
| } | ||
|
|
||
| function assertNoBalance() public payable { | ||
| require(comp.balanceOf(address(this)) == 0); | ||
| comp.mint{ value: msg.value }(); | ||
| assert(comp.balanceOf(address(this)) == 0); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| 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: | ||
|
|
||
| ``` | ||
| $ ECHIDNA_RPC_URL=http://.. ECHIDNA_RPC_BLOCK=16771449 echidna compound.sol --test-mode assertion --contract TestCompoundEthMint | ||
| ... | ||
| assertNoBalance(): failed!💥 | ||
| Call sequence, shrinking (885/5000): | ||
| assertNoBalance() Value: 0xd0411a5 | ||
| ``` | ||
|
|
||
| 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. | ||
|
|
||
| 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. | ||
|
|
||
| ## Corpus and RPC cache | ||
|
|
||
| If a corpus directory is used (e.g. `--corpus-dir corpus`), Echidna will save the fetched information inside the `cache` directory. | ||
| 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. | ||
|
|
||
| ``` | ||
| $ ls corpus/cache/ | ||
| block_16771449_fetch_cache_contracts.json block_16771449_fetch_cache_slots.json | ||
| ``` | ||
|
|
||
| ## Coverage and Etherscan integration | ||
|
|
||
| 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. | ||
|
|
||
| ``` | ||
| Fetching Solidity source for contract at address 0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5... Retrying (5 left). Error: Max rate limit reached, please use API Key for higher rate limit | ||
| Retrying (4 left). Error: Max rate limit reached, please use API Key for higher rate limit | ||
| Retrying (3 left). Error: Max rate limit reached, please use API Key for higher rate limit | ||
| Success! | ||
| Fetching Solidity source map for contract at address 0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5... Error! | ||
| ``` | ||
|
|
||
| While the source code for the [cETH contract is available](https://etherscan.io/address/0x4ddc2d193948926d02f9b1fe9e1daa0718270ed5#code), their source maps are NOT. | ||
| 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: | ||
|
|
||
| ``` | ||
| 20 | | | ||
| 21 | *r | function assertNoBalance() public payable { | ||
| 22 | *r | require(comp.balanceOf(address(this)) == 0); | ||
| 23 | *r | comp.mint{value: msg.value}(); | ||
| 24 | *r | assert(comp.balanceOf(address(this)) == 0); | ||
| 25 | | } | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mhh, not related to this article, but why not relying on crytic-compile for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK, crytic-compile cannot produce the same bytecode as the one deployed, so it is unclear if that will be useful, but @arcz has the detail.