-
Notifications
You must be signed in to change notification settings - Fork 28
/
test_evm.py
316 lines (247 loc) · 8.55 KB
/
test_evm.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import json
import os
from pyrevm import EVM, AccountInfo, BlockEnv, Env, TxEnv
import pytest
from tests.utils import encode_address, encode_uint, load_contract_bin
address = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045" # vitalik.eth
address2 = "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
# use your own key during development to avoid rate limiting the CI job
fork_url = (
os.getenv("FORK_URL")
or "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27"
)
KWARG_CASES = [
{"fork_url": fork_url},
{"fork_url": fork_url, "tracing": False, "fork_block": "latest"},
{},
]
def test_revm_fork():
# set up an evm
evm = EVM(
# can fork from a remote node
fork_url=fork_url,
# can set tracing to true/false
tracing=True,
# can configure the environment
env=Env(block=BlockEnv(timestamp=100, prevrandao=bytes([0] * 32))),
)
assert evm.env.block.timestamp == 100
vb_before = evm.basic(address)
assert vb_before is not None
# Execute the tx
evm.message_call(
caller=address,
to=address2,
value=10000,
# data
)
info = evm.basic(address2)
assert info is not None
assert vb_before != evm.basic(address)
assert info.balance == 10000
def test_fork_storage():
weth = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
evm = EVM(fork_url=fork_url, fork_block="latest")
value = evm.storage(weth, 0)
assert value > 0
def test_deploy():
evm = EVM()
account_before = evm.basic(address)
assert account_before is not None
assert account_before.nonce == 0
assert account_before.code == b"\0"
# Deploy the contract
code = load_contract_bin("blueprint.bin")
deployed_at = evm.deploy(address, code)
assert deployed_at == "0x3e4ea2156166390f880071d94458efb098473311"
deployed_code = evm.get_code(deployed_at)
assert deployed_code.hex().rstrip("0") in code.hex()
assert evm.basic(deployed_at).code.hex() == deployed_code.hex()
result = evm.message_call(
address,
deployed_at,
calldata=b"\xc2\x98Ux", # ==method_id('foo()')
)
assert int(result.hex(), 16) == 123
def test_balances():
evm = EVM()
vb_before = evm.basic(address)
assert vb_before is not None
assert vb_before.balance == 0
# Give ether
amount = 10000
evm.set_balance(address, amount)
assert evm.get_balance(address) == amount
assert evm.basic(address).balance == amount
def test_balances_fork():
evm = EVM(
fork_url=fork_url,
fork_block="0x3b01f793ed1923cd82df5fe345b3e12211aedd514c8546e69efd6386dc0c9a97",
)
vb_before = evm.basic(address)
assert vb_before.balance == 955628344913799071315
amount = 10000
evm.set_balance(address, amount)
assert evm.get_balance(address) == amount
assert evm.basic(address).balance == amount
@pytest.mark.parametrize("kwargs", KWARG_CASES)
def test_message_call(kwargs):
evm = EVM(**kwargs)
info = AccountInfo(code=load_contract_bin("full_math.bin"))
evm.insert_account_info(address, info)
assert evm.basic(address).code == info.code
# mulDiv() -> 64 * 8 / 2
result = evm.message_call(
caller=address2,
to=address,
calldata=bytes.fromhex(
f"aa9a0912{encode_uint(64)}{encode_uint(8)}{encode_uint(2)}"
),
)
assert int.from_bytes(result, "big") == 256
@pytest.mark.parametrize("kwargs", KWARG_CASES)
def test_call_committing(kwargs):
evm = EVM(**kwargs)
evm.insert_account_info(
address, AccountInfo(code=load_contract_bin("full_math.bin"))
)
# mulDivRoundingUp() -> 64 * 8 / 3
result = evm.message_call(
caller=address2,
to=address,
calldata=bytes.fromhex(
f"0af8b27f{encode_uint(64)}{encode_uint(8)}{encode_uint(3)}"
),
)
assert int.from_bytes(result, "big") == 171
def test_call_revert():
evm = EVM()
amount = 10000
evm.set_balance(address2, amount)
checkpoint = evm.snapshot()
evm.message_call(
caller=address2,
to=address,
value=amount,
)
assert evm.get_balance(address) == amount
evm.revert(checkpoint)
assert evm.get_balance(address) == 0
assert evm.get_balance(address2) == amount
@pytest.mark.parametrize("kwargs", KWARG_CASES)
def test_call_empty_result(kwargs):
evm = EVM(**kwargs)
evm.insert_account_info(address, AccountInfo(code=load_contract_bin("weth_9.bin")))
evm.set_balance(address2, 10000)
deposit = evm.message_call(
caller=address2,
to=address,
value=10000,
calldata=bytes.fromhex("d0e30db0"),
)
assert deposit == b""
balance = evm.message_call(
caller=address2,
to=address,
calldata=bytes.fromhex("70a08231" + encode_address(address2)),
)
assert int.from_bytes(balance, "big") == 10000
assert not evm.tracing
def test_tracing(capsys):
evm = EVM(tracing=True)
evm.insert_account_info(address, AccountInfo(code=load_contract_bin("weth_9.bin")))
evm.set_balance(address2, 10000)
evm.message_call(
caller=address2,
to=address,
value=10000,
calldata=bytes.fromhex("d0e30db0"),
)
assert evm.tracing
captured = capsys.readouterr()
traces = [json.loads(i) for i in captured.out.split("\n") if i]
expected = {
"fork": "Latest",
"gasUsed": "0xafee",
"output": "0x",
"pass": True,
"stateRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
}
assert expected == traces[-1]
assert len(traces) == 128
def test_blueprint():
evm = EVM()
# bytecode based on vyper `@external def foo() -> uint256: return 123`
bytecode = load_contract_bin("blueprint.bin")
bytecode = b"\xfe\x71\x00" + bytecode
bytecode_len = len(bytecode)
bytecode_len_hex = hex(bytecode_len)[2:].rjust(4, "0")
# prepend a quick deploy preamble
deploy_preamble = bytes.fromhex("61" + bytecode_len_hex + "3d81600a3d39f3")
deploy_bytecode = deploy_preamble + bytecode
deployer_address = evm.deploy(address, deploy_bytecode)
assert evm.basic(deployer_address).code.hex().rstrip("0") in deploy_bytecode.hex()
def test_block_setters():
evm = EVM()
block_env = evm.env.block
block_env.number = 20
block_env.timestamp = 100
block_env.excess_blob_gas = 200
assert block_env.number == 20
assert block_env.timestamp == 100
assert block_env.excess_blob_gas == 200
evm.set_block_env(block_env)
assert evm.env.block.number == 20
def test_tx_setters():
evm = EVM()
tx_env = evm.env.tx
tx_env.blob_hashes = [b"1" * 32]
tx_env.max_fee_per_blob_gas = 100
assert tx_env.blob_hashes == [b"1" * 32]
assert tx_env.max_fee_per_blob_gas == 100
evm.set_tx_env(tx_env)
assert evm.env.tx.blob_hashes == [b"1" * 32]
@pytest.mark.parametrize(
"excess_blob_gas,expected_fee",
[(0, 1), (10**3, 1), (2**24, 152), (2**26, 537070730)],
)
def test_get_blobbasefee(excess_blob_gas, expected_fee):
evm = EVM()
evm.set_block_env(BlockEnv(excess_blob_gas=excess_blob_gas))
bytecode = load_contract_bin("blob_base_fee.bin")
deployer_address = evm.deploy(address, bytecode)
blobbasefee = evm.message_call(
caller=address,
to=deployer_address,
calldata=bytes.fromhex("5fb0146d"), # method_id("get_blobbasefee()")
)
assert int.from_bytes(blobbasefee, "big") == expected_fee
@pytest.mark.parametrize("blob_hashes", [[b"1" * 32] * 2, [b"2" * 32] * 6])
def test_get_blobhashes(blob_hashes):
evm = EVM()
evm.set_tx_env(TxEnv(blob_hashes=blob_hashes))
bytecode = load_contract_bin("blob_hash.bin")
deployer_address = evm.deploy(address, bytecode)
evm.message_call(
caller=address,
to=deployer_address,
calldata=bytes.fromhex("cc883ac4"), # method_id("log_blobhashes()")
)
logged = [log.data[0][1] for log in evm.result.logs]
# the contract logs 6 blob hashes, so pad with 0s
assert logged == blob_hashes + [b"\0" * 32] * (6 - len(blob_hashes))
@pytest.mark.parametrize("kwargs", KWARG_CASES)
def test_call_reverting(kwargs):
evm = EVM()
code = load_contract_bin("min.bin")
deploy_address = evm.deploy(address, code)
with pytest.raises(RuntimeError) as err:
evm.message_call(
caller=address,
to=deploy_address,
value=10,
)
assert evm.get_code(
deploy_address
), "The code should still be deployed after revert"
assert str(err.value).startswith("Transaction(LackOfFundForMaxFee")