forked from ethereum/py-evm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_london.py
More file actions
350 lines (305 loc) · 11.2 KB
/
Copy pathtest_london.py
File metadata and controls
350 lines (305 loc) · 11.2 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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import pytest
from eth_typing import Address
from eth_utils import decode_hex
from eth import constants
from eth.consensus.noproof import NoProofConsensus
from eth.chains.base import MiningChain
from eth.chains.mainnet import (
MAINNET_VMS,
)
from eth.exceptions import InvalidInstruction
from eth.vm.forks import BerlinVM
from eth.tools.factories.transaction import (
new_dynamic_fee_transaction, new_transaction,
)
FOUR_TXN_GAS_LIMIT = 21000 * 4
EIP_3541_CREATE_AND_CREATE2_REVERT_TEST_CASES = (
# negative test cases from https://eips.ethereum.org/EIPS/eip-3541#test-cases
# CREATE opcode tests:
(
decode_hex("0x6000356000523660006000f0151560165760006000fd5b"),
decode_hex("0x60ef60005360016000f3"),
),
(
decode_hex("0x6000356000523660006000f0151560165760006000fd5b"),
decode_hex("0x60ef60005360026000f3"),
),
(
decode_hex("0x6000356000523660006000f0151560165760006000fd5b"),
decode_hex("0x60ef60005360036000f3"),
),
(
decode_hex("0x6000356000523660006000f0151560165760006000fd5b"),
decode_hex("0x60ef60005360206000f3"),
),
# CREATE2 opcode tests:
(
decode_hex("0x60003560005260003660006000f5151560185760006000fd5b"),
decode_hex("0x60ef60005360016000f3"),
),
(
decode_hex("0x60003560005260003660006000f5151560185760006000fd5b"),
decode_hex("0x60ef60005360026000f3"),
),
(
decode_hex("0x60003560005260003660006000f5151560185760006000fd5b"),
decode_hex("0x60ef60005360036000f3"),
),
(
decode_hex("0x60003560005260003660006000f5151560185760006000fd5b"),
decode_hex("0x60ef60005360206000f3"),
),
)
def _configure_mining_chain(name, genesis_vm, vm_under_test):
return MiningChain.configure(
__name__=name,
vm_configuration=(
(
constants.GENESIS_BLOCK_NUMBER,
genesis_vm.configure(consensus_class=NoProofConsensus),
),
(
constants.GENESIS_BLOCK_NUMBER + 1,
vm_under_test.configure(consensus_class=NoProofConsensus),
),
),
chain_id=1337,
)
# VMs starting at London
@pytest.fixture(params=MAINNET_VMS[9:])
def london_plus_miner(request, base_db, genesis_state):
vm_under_test = request.param
klass = _configure_mining_chain('LondonAt1', BerlinVM, vm_under_test)
header_fields = dict(
difficulty=1,
gas_limit=21000 * 2, # block limit is hit with two transactions
)
# On the first London+ block, it will double the block limit so that it
# can precisely hold 4 base transactions.
return klass.from_genesis(base_db, header_fields, genesis_state)
# VMs up to, but not including, London
@pytest.fixture(params=MAINNET_VMS[0:9])
def pre_london_miner(request, base_db, genesis_state):
vm_under_test = request.param
klass = _configure_mining_chain('EndsBeforeLondon', MAINNET_VMS[0], vm_under_test)
header_fields = dict(
difficulty=1,
gas_limit=100000, # arbitrary, just enough for testing
)
return klass.from_genesis(base_db, header_fields, genesis_state)
@pytest.mark.parametrize(
'num_txns, expected_base_fee',
(
(0, 875000000),
(1, 937500000),
# base fee should stay stable at 1 gwei when block is exactly half full
(2, 1000000000),
(3, 1062500000),
(4, 1125000000),
),
)
def test_base_fee_evolution(
london_plus_miner, funded_address, funded_address_private_key, num_txns, expected_base_fee):
chain = london_plus_miner
assert chain.header.gas_limit == FOUR_TXN_GAS_LIMIT
vm = chain.get_vm()
txns = [
new_transaction(
vm,
funded_address,
b'\x00' * 20,
private_key=funded_address_private_key,
gas=21000,
nonce=nonce,
)
for nonce in range(num_txns)
]
block_import, _, _ = chain.mine_all(txns, gas_limit=FOUR_TXN_GAS_LIMIT)
mined_header = block_import.imported_block.header
assert mined_header.gas_limit == FOUR_TXN_GAS_LIMIT
assert mined_header.gas_used == 21000 * num_txns
assert mined_header.base_fee_per_gas == 10 ** 9 # Initialize at 1 gwei
block_import, _, _ = chain.mine_all([], gas_limit=FOUR_TXN_GAS_LIMIT)
mined_header = block_import.imported_block.header
assert mined_header.gas_limit == FOUR_TXN_GAS_LIMIT
assert mined_header.gas_used == 0
# Check that the base fee evolved correctly, depending on how much gas was used in the parent
assert mined_header.base_fee_per_gas == expected_base_fee
@pytest.mark.parametrize(
"code, data",
EIP_3541_CREATE_AND_CREATE2_REVERT_TEST_CASES
)
def test_revert_on_reserved_0xEF_byte_for_CREATE_and_CREATE2_post_london(
london_plus_miner, funded_address, code, data,
):
chain = london_plus_miner
vm = chain.get_vm()
# test positive case from https://eips.ethereum.org/EIPS/eip-3541#test-cases
successful_create_computation = vm.execute_bytecode(
origin=funded_address,
to=funded_address,
sender=funded_address,
value=0,
code=code,
data=decode_hex("0x60fe60005360016000f3"),
gas=400000,
gas_price=1,
)
assert successful_create_computation.is_success
# assert only the appropriate gas is consumed, not all the gas. This falls within a range
assert 32261 <= successful_create_computation.get_gas_used() <= 32270
# test parameterized negative cases
revert_create_computation = vm.execute_bytecode(
origin=funded_address,
to=funded_address,
sender=funded_address,
value=0,
code=code,
data=data,
gas=40000,
gas_price=1,
)
assert revert_create_computation.is_error
assert 35000 < revert_create_computation.get_gas_used() < 40000 # assert gas is still consumed
assert revert_create_computation.get_gas_refund() == 0
@pytest.mark.parametrize(
"data",
(
# negative test cases from https://eips.ethereum.org/EIPS/eip-3541#test-cases
decode_hex("0x60ef60005360016000f3"),
decode_hex("0x60ef60005360026000f3"),
decode_hex("0x60ef60005360036000f3"),
decode_hex("0x60ef60005360206000f3"),
)
)
def test_state_revert_on_reserved_0xEF_byte_for_create_transaction_post_london(
london_plus_miner, funded_address, funded_address_private_key, data
):
chain = london_plus_miner
vm = chain.get_vm()
initial_block_header = chain.get_block().header
initial_balance = vm.state.get_balance(funded_address)
assert initial_balance > 1000000 # arbitrary number, enough for all our transactions
# positive test case from https://eips.ethereum.org/EIPS/eip-3541#test-cases
create_successful_contract_transaction = new_dynamic_fee_transaction(
vm=vm,
from_=funded_address,
to=Address(b''),
amount=0,
private_key=funded_address_private_key,
gas=53354,
max_priority_fee_per_gas=100,
max_fee_per_gas=100000000000,
nonce=0,
data=decode_hex("0x60fe60005360016000f3"),
)
block_import, _, computations = chain.mine_all(
[create_successful_contract_transaction],
gas_limit=84081,
)
successful_create_computation = computations[0]
successful_create_computation_state = successful_create_computation.state
mined_header = block_import.imported_block.header
gas_used = mined_header.gas_used
mined_txn = block_import.imported_block.transactions[0]
new_balance = successful_create_computation_state.get_balance(funded_address)
assert successful_create_computation.is_success
assert successful_create_computation_state.get_nonce(funded_address) == 1
assert gas_used == 53354
fees_consumed = (
(mined_txn.max_priority_fee_per_gas * gas_used)
+ (initial_block_header.base_fee_per_gas * gas_used)
)
# successful txn consumes gas and fees:
assert new_balance == initial_balance - fees_consumed
# test the parametrized negative cases
create_contract_txn_reserved_byte = new_dynamic_fee_transaction(
vm=vm,
from_=funded_address,
to=Address(b''),
amount=0,
private_key=funded_address_private_key,
gas=60000,
max_priority_fee_per_gas=100,
max_fee_per_gas=100000000000,
nonce=1,
data=data,
)
block_import, _, computations = chain.mine_all(
[create_contract_txn_reserved_byte],
gas_limit=84082
)
reverted_computation = computations[0]
mined_header = block_import.imported_block.header
assert reverted_computation.is_error
assert "0xef" in repr(reverted_computation.error).lower()
# reverted txn consumes the gas:
assert mined_header.gas_used == 60000
@pytest.mark.parametrize(
"code, data",
EIP_3541_CREATE_AND_CREATE2_REVERT_TEST_CASES
)
def test_state_does_not_revert_on_reserved_0xEF_byte_for_CREATE_and_CREATE2_pre_london(
pre_london_miner, funded_address, code, data,
):
chain = pre_london_miner
vm = chain.get_vm()
computation = vm.execute_bytecode(
origin=funded_address,
to=funded_address,
sender=funded_address,
value=0,
code=code,
data=data,
gas=40000,
gas_price=1,
)
if computation.is_error:
assert isinstance(computation.error, InvalidInstruction)
# pre-CREATE2 vms will not recognize the CREATE2 opcode:
assert "0xf5" in repr(computation.error).lower()
assert vm.fork in (_.fork for _ in MAINNET_VMS[:5])
else:
assert computation.is_success
# assert only the appropriate gas is consumed, not all the gas. This falls within a range
assert 32261 <= computation.get_gas_used() <= 38470
assert computation.get_gas_refund() == 0
@pytest.mark.parametrize(
"data",
(
# negative test cases from https://eips.ethereum.org/EIPS/eip-3541#test-cases
decode_hex("0x60ef60005360016000f3"),
decode_hex("0x60ef60005360026000f3"),
decode_hex("0x60ef60005360036000f3"),
decode_hex("0x60ef60005360206000f3"),
)
)
def test_state_does_not_revert_on_reserved_0xEF_byte_for_create_transaction_pre_london(
pre_london_miner, funded_address, funded_address_private_key, data
):
chain = pre_london_miner
vm = chain.get_vm()
initial_balance = vm.state.get_balance(funded_address)
# test the parametrized negative cases
create_contract_txn_0xef_byte = new_transaction(
vm=vm,
from_=funded_address,
to=Address(b''),
amount=0,
private_key=funded_address_private_key,
gas=60000,
nonce=0,
data=data,
)
block_import, _, computations = chain.mine_all(
[create_contract_txn_0xef_byte],
gas_limit=99904
)
computation = computations[0]
mined_header = block_import.imported_block.header
txn = block_import.imported_block.transactions[0]
end_balance = computation.state.get_balance(funded_address)
assert computation.is_success
assert computation.state.get_nonce(funded_address) == 1
# txn consumes gas and fees
assert end_balance == initial_balance - (txn.gas_price * mined_header.gas_used)