|
| 1 | +# How and when to use cheat codes |
| 2 | + |
| 3 | +**Table of contents:** |
| 4 | + |
| 5 | +- [How and when to use cheat codes](#how-and-when-to-use-cheat-codes) |
| 6 | + - [Introduction](#introduction) |
| 7 | + - [Cheat codes available in Echidna](#cheat-codes-available-in-echidna) |
| 8 | + - [Advise on using cheat codes](#advise-on-using-cheat-codes) |
| 9 | + |
| 10 | +## Introduction |
| 11 | + |
| 12 | +When solidity smart contract testing is performed from Solidity itself, usually requires some "help" in order to tackle some EVM/Solidity limitations. |
| 13 | +Cheat code are special functions that allow to change the state of the EVM in ways that are not posible in production. These were introduced by Dapptools in |
| 14 | +hevm and adopted (and expanded) in other projects such as Foundry. |
| 15 | + |
| 16 | +## Cheat codes available in Echidna |
| 17 | + |
| 18 | +Since Echidna uses [hevm](https://github.com/ethereum/hevm), all the supported list of cheat code is documented here: https://hevm.dev/controlling-the-unit-testing-environment.html#cheat-codes. |
| 19 | +If a new cheat code is added in the future, Echidna only needs to update the hevm version and everything should work out of the box. |
| 20 | + |
| 21 | +As an example, this is code "simulates" the use of another sender for the external call using "prank": |
| 22 | + |
| 23 | +```solidity |
| 24 | +interface IHevm { |
| 25 | + function prank(address) external; |
| 26 | +} |
| 27 | +
|
| 28 | +contract TestPrank { |
| 29 | + address constant HEVM_ADDRESS = 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D; |
| 30 | + IHevm hevm = IHevm(HEVM_ADDRESS); |
| 31 | + Contract c = ... |
| 32 | + |
| 33 | + function prankContract() public payable { |
| 34 | + hevm.prank(address(0x42424242); |
| 35 | + c.f(); |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +A specific example on the use of `sign` cheat code is available [here in our documentation](hevm-cheats-to-test-permit.md). |
| 41 | + |
| 42 | +## Advise on how and when using cheat codes |
| 43 | + |
| 44 | +While we provide support for the use of cheat codes, these should be used responsabily. We offer the following advise on the use of cheat codes: |
| 45 | + |
| 46 | +* It should be used only if Echidna will not perform the same action with a native feature. For instance, Echidna automatically increases the timestamp and block number. There are [some reports of the optimizer interfering with (re)computation of the block.number or timestamp](https://github.com/ethereum/solidity/issues/12963#issuecomment-1110162425), which could generate incorrect tests when using cheat codes. |
| 47 | +Using the corresponding built-in Echidna features should never intefer with the optimization level or any other compiler feature (if this happens, then it is a bug). |
| 48 | + |
| 49 | +* It can introduce false positives on the testing. For instance, using `prank` to simulate calls from account that is not EOA (e.g. a contract) can allow transactions that are not possible in the blockchain. |
| 50 | + |
| 51 | +* Using too much cheat codes: |
| 52 | + * can be confusing or error-prone. Certain cheat code like `prank` allow to change caller in the next external call: It can be difficult to follow, in particular if it is used in internal functions or modifiers. |
| 53 | + * will create a dependency of your code with the particular tool or cheat code implementation: It can cause produce migrations to other tools or reusing the test code to be more difficult than expected. |
0 commit comments