forked from foundry-rs/foundry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGasMetering.t.sol
More file actions
84 lines (56 loc) · 1.95 KB
/
Copy pathGasMetering.t.sol
File metadata and controls
84 lines (56 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.18;
import "ds-test/test.sol";
import "cheats/Vm.sol";
contract B {
function a() public returns (uint256) {
return 100;
}
}
contract GasMeteringTest is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);
function testGasMetering() public {
uint256 gas_start = gasleft();
consumeGas();
uint256 gas_end_normal = gas_start - gasleft();
vm.pauseGasMetering();
uint256 gas_start_not_metered = gasleft();
consumeGas();
uint256 gas_end_not_metered = gas_start_not_metered - gasleft();
vm.resumeGasMetering();
uint256 gas_start_metered = gasleft();
consumeGas();
uint256 gas_end_resume_metered = gas_start_metered - gasleft();
assertEq(gas_end_normal, gas_end_resume_metered);
assertEq(gas_end_not_metered, 0);
}
function testGasMeteringExternal() public {
B b = new B();
uint256 gas_start = gasleft();
b.a();
uint256 gas_end_normal = gas_start - gasleft();
vm.pauseGasMetering();
uint256 gas_start_not_metered = gasleft();
b.a();
uint256 gas_end_not_metered = gas_start_not_metered - gasleft();
vm.resumeGasMetering();
uint256 gas_start_metered = gasleft();
b.a();
uint256 gas_end_resume_metered = gas_start_metered - gasleft();
assertEq(gas_end_normal, gas_end_resume_metered);
assertEq(gas_end_not_metered, 0);
}
function testGasMeteringContractCreate() public {
vm.pauseGasMetering();
uint256 gas_start_not_metered = gasleft();
B b = new B();
uint256 gas_end_not_metered = gas_start_not_metered - gasleft();
vm.resumeGasMetering();
assertEq(gas_end_not_metered, 0);
}
function consumeGas() internal returns (uint256 x) {
for (uint256 i; i < 10000; i++) {
x += i;
}
}
}