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/basic/working-with-eth.md
+41-40Lines changed: 41 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,51 +10,51 @@
10
10
11
11
## Introduction
12
12
13
-
We will see how to use ETH during the fuzzing fuzzed. The following smart contract will be used as example:
13
+
We will see how to use ETH during the fuzzing fuzzed. The following smart contract will be used as example:
14
14
15
15
```solidity
16
16
contract C {
17
-
function pay() payable public {
18
-
require(msg.value == 12000);
19
-
}
17
+
function pay() public payable {
18
+
require(msg.value == 12000);
19
+
}
20
20
21
-
function echidna_has_some_value() public returns (bool) {
22
-
return (address(this).balance != 12000);
23
-
}
21
+
function echidna_has_some_value() public returns (bool) {
22
+
return (address(this).balance != 12000);
23
+
}
24
24
}
25
25
```
26
26
27
-
This code forces Echidna to send a particular amount of eth as value in the `pay` function.
27
+
This code forces Echidna to send a particular amount of eth as value in the `pay` function.
28
28
Echidna will do this for each payable function in the target function (or any contract if `allContracts` is enabled):
29
29
30
30
```
31
-
$ echidna balanceSender.sol
31
+
$ echidna balanceSender.sol
32
32
...
33
-
echidna_has_some_value: failed!💥
33
+
echidna_has_some_value: failed!💥
34
34
Call sequence:
35
35
pay() Value: 0x2ee0
36
36
```
37
37
38
-
Echidna will show the value amount in hexadecimal.
38
+
Echidna will show the value amount in hexadecimal.
39
39
40
40
## Controlling the amount of ether in payable functions
41
41
42
42
The amount of ether to send in each payable function will be randomly selected, but with a maximum value determined by the `maxValue` value
43
43
with a default of 100 eth per transaction:
44
44
45
45
```yaml
46
-
maxValue: 100000000000000000000
46
+
maxValue: 100000000000000000000
47
47
```
48
48
49
-
This means that each transaction will contain, at most, 100 eth in value. However, there is no maximum that will be used in total.
50
-
The maximum amount to receive will be determined by the number of transactions. If you are using 100 transactions (`--seq-len 100`),
51
-
then the maximum amount of ether in transactions will be 100 * 100 eth.
49
+
This means that each transaction will contain, at most, 100 eth in value. However, there is no maximum that will be used in total.
50
+
The maximum amount to receive will be determined by the number of transactions. If you are using 100 transactions (`--seq-len 100`),
51
+
then the maximum amount of ether in transactions will be 100 \* 100 eth.
52
52
53
-
Keep in mind that the balance of the senders (e.g. `msg.sender.balance`) is a fixed value that will NOT change between transactions.
53
+
Keep in mind that the balance of the senders (e.g. `msg.sender.balance`) is a fixed value that will NOT change between transactions.
54
54
This value is determined by the following config option:
55
55
56
56
```yaml
57
-
balanceAddr: 0xffffffff
57
+
balanceAddr: 0xffffffff
58
58
```
59
59
60
60
## Controlling the amount of ether in contracts
@@ -63,26 +63,27 @@ Another approach to handle ether will be allow the testing contract to receive c
63
63
64
64
```solidity
65
65
contract A {
66
-
C internal c;
67
-
constructor() payable public {
68
-
require(msg.value == 12000);
69
-
c = new C();
70
-
}
71
-
72
-
function payToContract(uint256 toPay) public {
73
-
toPay = toPay % (address(this).balance + 1);
74
-
c.pay{value: toPay}();
75
-
}
76
-
77
-
function echidna_C_has_some_value() public returns (bool) {
78
-
return (address(c).balance != 12000);
79
-
}
66
+
C internal c;
67
+
68
+
constructor() public payable {
69
+
require(msg.value == 12000);
70
+
c = new C();
71
+
}
72
+
73
+
function payToContract(uint256 toPay) public {
74
+
toPay = toPay % (address(this).balance + 1);
75
+
c.pay{ value: toPay }();
76
+
}
77
+
78
+
function echidna_C_has_some_value() public returns (bool) {
79
+
return (address(c).balance != 12000);
80
+
}
80
81
}
81
82
82
83
contract C {
83
-
function pay() payable public {
84
-
require(msg.value == 12000);
85
-
}
84
+
function pay() public payable {
85
+
require(msg.value == 12000);
86
+
}
86
87
}
87
88
```
88
89
@@ -94,7 +95,7 @@ $ echidna balanceContract.sol
94
95
echidna: Deploying the contract 0x00a329c0648769A73afAc7F9381E08FB43dBEA72 failed (revert, out-of-gas, sending ether to an non-payable constructor, etc.):
95
96
```
96
97
97
-
We need to define the amount to send during the contract creation:
98
+
We need to define the amount to send during the contract creation:
98
99
99
100
```yaml
100
101
balanceContract: 12000
@@ -103,16 +104,16 @@ balanceContract: 12000
103
104
We can re-run echidna, using that config file, to obtain the expected result:
0 commit comments