Skip to content

Commit e2423cd

Browse files
committed
refactor recently-added tests to test_london.py
vm.execute_bytecode seems to be a more appropriate way to conduct these tests + added some clarification and appropriate variable names for configure_mining_chain fixture.
1 parent cba70e1 commit e2423cd

File tree

1 file changed

+26
-29
lines changed

1 file changed

+26
-29
lines changed

tests/core/vm/test_london.py

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from eth.tools.factories.transaction import (
1515
new_dynamic_fee_transaction, new_transaction,
1616
)
17-
from eth.vm.message import Message
1817

1918
FOUR_TXN_GAS_LIMIT = 21000 * 4
2019

@@ -57,17 +56,17 @@
5756
)
5857

5958

60-
def _configure_mining_chain(name, request):
59+
def _configure_mining_chain(name, genesis_vm, vm_under_test):
6160
return MiningChain.configure(
6261
__name__=name,
6362
vm_configuration=(
6463
(
6564
constants.GENESIS_BLOCK_NUMBER,
66-
BerlinVM.configure(consensus_class=NoProofConsensus),
65+
genesis_vm.configure(consensus_class=NoProofConsensus),
6766
),
6867
(
6968
constants.GENESIS_BLOCK_NUMBER + 1,
70-
request.param.configure(consensus_class=NoProofConsensus),
69+
vm_under_test.configure(consensus_class=NoProofConsensus),
7170
),
7271
),
7372
chain_id=1337,
@@ -77,7 +76,9 @@ def _configure_mining_chain(name, request):
7776
# VMs starting at London
7877
@pytest.fixture(params=MAINNET_VMS[9:])
7978
def london_plus_miner(request, base_db, genesis_state):
80-
klass = _configure_mining_chain('LondonAt1', request)
79+
vm_under_test = request.param
80+
81+
klass = _configure_mining_chain('LondonAt1', BerlinVM, vm_under_test)
8182
header_fields = dict(
8283
difficulty=1,
8384
gas_limit=21000 * 2, # block limit is hit with two transactions
@@ -90,7 +91,9 @@ def london_plus_miner(request, base_db, genesis_state):
9091
# VMs up to, but not including, London
9192
@pytest.fixture(params=MAINNET_VMS[0:9])
9293
def pre_london_miner(request, base_db, genesis_state):
93-
klass = _configure_mining_chain('EndsBeforeLondon', request)
94+
vm_under_test = request.param
95+
96+
klass = _configure_mining_chain('EndsBeforeLondon', MAINNET_VMS[0], vm_under_test)
9497
header_fields = dict(
9598
difficulty=1,
9699
gas_limit=100000, # arbitrary, just enough for testing
@@ -145,41 +148,37 @@ def test_base_fee_evolution(
145148
EIP_3541_CREATE_AND_CREATE2_REVERT_TEST_CASES
146149
)
147150
def test_revert_on_reserved_0xEF_byte_for_CREATE_and_CREATE2_post_london(
148-
london_plus_miner, transaction_context, funded_address, code, data,
151+
london_plus_miner, funded_address, code, data,
149152
):
150153
chain = london_plus_miner
151-
state = chain.get_vm().state
154+
vm = chain.get_vm()
152155

153156
# test positive case from https://eips.ethereum.org/EIPS/eip-3541#test-cases
154-
create_message = Message(
157+
successful_create_computation = vm.execute_bytecode(
158+
origin=funded_address,
155159
to=funded_address,
156160
sender=funded_address,
157161
value=0,
158162
code=code,
159163
data=decode_hex("0x60fe60005360016000f3"),
160164
gas=400000,
161-
)
162-
163-
successful_create_computation = state.computation_class.apply_create_message(
164-
state, create_message, transaction_context
165+
gas_price=1,
165166
)
166167

167168
assert successful_create_computation.is_success
168-
# gas used varies between 32261 and 32270 depending on test case
169+
# assert only the appropriate gas is consumed, not all the gas. This falls within a range
169170
assert 32261 <= successful_create_computation.get_gas_used() <= 32270
170171

171172
# test parameterized negative cases
172-
reverted_create_message = Message(
173+
revert_create_computation = vm.execute_bytecode(
174+
origin=funded_address,
173175
to=funded_address,
174176
sender=funded_address,
175177
value=0,
176178
code=code,
177179
data=data,
178180
gas=40000,
179-
)
180-
181-
revert_create_computation = state.computation_class.apply_create_message(
182-
state, reverted_create_message, transaction_context
181+
gas_price=1,
183182
)
184183

185184
assert revert_create_computation.is_error
@@ -275,32 +274,30 @@ def test_state_revert_on_reserved_0xEF_byte_for_create_transaction_post_london(
275274
assert "0xef" in repr(reverted_computation.error).lower()
276275

277276
assert reverted_computation.get_nonce(funded_address) == 1 # assert nonce is still 1
278-
# reverted txn only consumes gas:
279-
assert end_balance == new_balance - mined_header.gas_used
277+
# reverted txn consumes the gas:
278+
assert mined_header.gas_used == 60000
279+
assert end_balance == new_balance - 60000
280280

281281

282282
@pytest.mark.parametrize(
283283
"code, data",
284284
EIP_3541_CREATE_AND_CREATE2_REVERT_TEST_CASES
285285
)
286286
def test_state_does_not_revert_on_reserved_0xEF_byte_for_CREATE_and_CREATE2_pre_london(
287-
pre_london_miner, transaction_context, funded_address, code, data,
287+
pre_london_miner, funded_address, code, data,
288288
):
289289
chain = pre_london_miner
290290
vm = chain.get_vm()
291-
state = vm.state
292291

293-
create_message = Message(
292+
computation = vm.execute_bytecode(
293+
origin=funded_address,
294294
to=funded_address,
295295
sender=funded_address,
296296
value=0,
297297
code=code,
298298
data=data,
299299
gas=40000,
300-
)
301-
302-
computation = state.computation_class.apply_create_message(
303-
state, create_message, transaction_context
300+
gas_price=1,
304301
)
305302

306303
if computation.is_error:
@@ -311,7 +308,7 @@ def test_state_does_not_revert_on_reserved_0xEF_byte_for_CREATE_and_CREATE2_pre_
311308

312309
else:
313310
assert computation.is_success
314-
# gas used varies between 32670 and 38470 depending on test case
311+
# assert only the appropriate gas is consumed, not all the gas. This falls within a range
315312
assert 32261 <= computation.get_gas_used() <= 38470
316313
assert computation.get_gas_refund() == 0
317314

0 commit comments

Comments
 (0)