You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: program-analysis/echidna/advanced/on-using-cheat-codes.md
+7-8Lines changed: 7 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,20 +5,19 @@
5
5
-[How and when to use cheat codes](#how-and-when-to-use-cheat-codes)
6
6
-[Introduction](#introduction)
7
7
-[Cheat codes available in Echidna](#cheat-codes-available-in-echidna)
8
-
-[Advise on using cheat codes](#advise-on-using-cheat-codes)
8
+
-[Advice on using cheat codes](#advice-on-using-cheat-codes)
9
9
10
10
## Introduction
11
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.
12
+
When testing smart contracts in Solidity itself, it can be helpful to use cheat codes in order to overcome some of the limitations of the EVM/Solidity.
13
+
Cheat codes 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 hevm and adopted (and expanded) in other projects such as Foundry.
15
14
16
15
## Cheat codes available in Echidna
17
16
18
17
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
18
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
19
21
-
As an example, this is code "simulates" the use of another sender for the external call using "prank":
20
+
As an example, the `prank` cheat code is able to set the `msg.sender` address in the context of the next external call:
22
21
23
22
```solidity
24
23
interface IHevm {
@@ -32,16 +31,16 @@ contract TestPrank {
32
31
33
32
function prankContract() public payable {
34
33
hevm.prank(address(0x42424242);
35
-
c.f();
34
+
c.f(); // `c` will be called with `msg.sender = 0x42424242`
36
35
}
37
36
}
38
37
```
39
38
40
39
A specific example on the use of `sign` cheat code is available [here in our documentation](hevm-cheats-to-test-permit.md).
41
40
42
-
## Advise on how and when using cheat codes
41
+
## Advice on how and when using cheat codes
43
42
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:
43
+
While we provide support for the use of cheat codes, these should be used responsibly. We offer the following advice on the use of cheat codes:
45
44
46
45
* 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
46
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).
0 commit comments