Skip to content

Commit acd05a1

Browse files
committed
refactor some commonly used constants among VM tests into fixtures
1 parent 7d619a4 commit acd05a1

File tree

3 files changed

+78
-63
lines changed

3 files changed

+78
-63
lines changed

tests/core/vm/conftest.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,30 @@
44
from eth.vm.transaction_context import BaseTransactionContext
55

66

7-
NORMALIZED_ADDRESS_A = "0x0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6"
8-
NORMALIZED_ADDRESS_B = "0xcd1722f3947def4cf144679da39c4c32bdc35681"
9-
CANONICAL_ADDRESS_A = to_canonical_address("0x0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6")
10-
CANONICAL_ADDRESS_B = to_canonical_address("0xcd1722f3947def4cf144679da39c4c32bdc35681")
7+
@pytest.fixture
8+
def normalized_address_a():
9+
return "0x0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6"
10+
11+
12+
@pytest.fixture
13+
def normalized_address_b():
14+
return "0xcd1722f3947def4cf144679da39c4c32bdc35681"
1115

1216

1317
@pytest.fixture
14-
def transaction_context():
18+
def canonical_address_a(normalized_address_a):
19+
return to_canonical_address(normalized_address_a)
20+
21+
22+
@pytest.fixture
23+
def canonical_address_b(normalized_address_b):
24+
return to_canonical_address(normalized_address_b)
25+
26+
27+
@pytest.fixture
28+
def transaction_context(canonical_address_b):
1529
tx_context = BaseTransactionContext(
1630
gas_price=1,
17-
origin=CANONICAL_ADDRESS_B,
31+
origin=canonical_address_b,
1832
)
1933
return tx_context
20-

tests/core/vm/test_base_computation.py

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from eth.vm.transaction_context import (
1818
BaseTransactionContext,
1919
)
20-
from tests.core.vm.conftest import CANONICAL_ADDRESS_A, CANONICAL_ADDRESS_B, NORMALIZED_ADDRESS_A
2120

2221

2322
class DummyComputation(BaseComputation):
@@ -36,10 +35,10 @@ def get_intrinsic_gas(self):
3635

3736

3837
@pytest.fixture
39-
def message():
38+
def message(canonical_address_a, canonical_address_b):
4039
message = Message(
41-
to=CANONICAL_ADDRESS_A,
42-
sender=CANONICAL_ADDRESS_B,
40+
to=canonical_address_a,
41+
sender=canonical_address_b,
4342
value=100,
4443
data=b'',
4544
code=b'',
@@ -59,23 +58,23 @@ def computation(message, transaction_context):
5958

6059

6160
@pytest.fixture
62-
def child_message(computation):
61+
def child_message(computation, canonical_address_b):
6362
child_message = computation.prepare_child_message(
6463
gas=100,
65-
to=CANONICAL_ADDRESS_B,
64+
to=canonical_address_b,
6665
value=200,
6766
data=b'',
6867
code=b''
6968
)
7069
return child_message
7170

7271

73-
def test_is_origin_computation(computation, transaction_context):
72+
def test_is_origin_computation(computation, transaction_context, canonical_address_a):
7473
assert computation.is_origin_computation
7574
message2 = Message(
76-
to=CANONICAL_ADDRESS_A,
75+
to=canonical_address_a,
7776
# Different sender than the tx context origin
78-
sender=CANONICAL_ADDRESS_A,
77+
sender=canonical_address_a,
7978
value=100,
8079
data=b'',
8180
code=b'',
@@ -132,87 +131,91 @@ def test_extend_memory_doesnt_increase_until_32_bytes_are_used(computation):
132131
assert computation._gas_meter.gas_remaining == 94
133132

134133

135-
def test_register_accounts_for_deletion_raises_if_address_isnt_canonical(computation):
134+
def test_register_accounts_for_deletion_raises_if_address_isnt_canonical(
135+
computation, normalized_address_a
136+
):
136137
with pytest.raises(ValidationError):
137-
computation.register_account_for_deletion(NORMALIZED_ADDRESS_A)
138+
computation.register_account_for_deletion(normalized_address_a)
138139

139140

140-
def test_register_accounts_for_deletion_cannot_register_the_same_address_twice(computation):
141-
computation.register_account_for_deletion(CANONICAL_ADDRESS_A)
141+
def test_register_accounts_for_deletion_cannot_register_the_same_address_twice(
142+
computation, canonical_address_a
143+
):
144+
computation.register_account_for_deletion(canonical_address_a)
142145
with pytest.raises(ValueError):
143-
computation.register_account_for_deletion(CANONICAL_ADDRESS_A)
146+
computation.register_account_for_deletion(canonical_address_a)
144147

145148

146-
def test_register_accounts_for_deletion(computation):
147-
computation.register_account_for_deletion(CANONICAL_ADDRESS_A)
148-
assert computation.accounts_to_delete[computation.msg.storage_address] == CANONICAL_ADDRESS_A
149+
def test_register_accounts_for_deletion(computation, canonical_address_a, canonical_address_b):
150+
computation.register_account_for_deletion(canonical_address_a)
151+
assert computation.accounts_to_delete[computation.msg.storage_address] == canonical_address_a
149152
# Another account can be registered for deletion
150-
computation.msg.storage_address = CANONICAL_ADDRESS_B
151-
computation.register_account_for_deletion(CANONICAL_ADDRESS_A)
153+
computation.msg.storage_address = canonical_address_b
154+
computation.register_account_for_deletion(canonical_address_a)
152155

153156

154157
def test_get_accounts_for_deletion_starts_empty(computation):
155158
assert computation.get_accounts_for_deletion() == ()
156159

157160

158-
def test_get_accounts_for_deletion_returns(computation):
159-
computation.register_account_for_deletion(CANONICAL_ADDRESS_A)
161+
def test_get_accounts_for_deletion_returns(computation, canonical_address_a, canonical_address_b):
162+
computation.register_account_for_deletion(canonical_address_a)
160163
# Get accounts for deletion returns the correct account
161-
assert computation.get_accounts_for_deletion() == ((CANONICAL_ADDRESS_A, CANONICAL_ADDRESS_A),)
164+
assert computation.get_accounts_for_deletion() == ((canonical_address_a, canonical_address_a),)
162165
# Get accounts for deletion can return multiple accounts
163-
computation.msg.storage_address = CANONICAL_ADDRESS_B
164-
computation.register_account_for_deletion(CANONICAL_ADDRESS_B)
166+
computation.msg.storage_address = canonical_address_b
167+
computation.register_account_for_deletion(canonical_address_b)
165168
accounts_for_deletion = sorted(computation.get_accounts_for_deletion(),
166169
key=lambda item: item[0])
167-
assert CANONICAL_ADDRESS_A == accounts_for_deletion[0][0]
168-
assert CANONICAL_ADDRESS_B == accounts_for_deletion[1][0]
170+
assert canonical_address_a == accounts_for_deletion[0][0]
171+
assert canonical_address_b == accounts_for_deletion[1][0]
169172

170173

171174
def test_add_log_entry_starts_empty(computation):
172175
assert computation.get_log_entries() == ()
173176

174177

175-
def test_add_log_entry_raises_if_address_isnt_canonical(computation):
178+
def test_add_log_entry_raises_if_address_isnt_canonical(computation, normalized_address_a):
176179
with pytest.raises(ValidationError):
177-
computation.add_log_entry(NORMALIZED_ADDRESS_A, [1, 2, 3], b'')
180+
computation.add_log_entry(normalized_address_a, [1, 2, 3], b'')
178181

179182

180-
def test_add_log_entry_raises_if_topic_elements_arent_uint256(computation):
183+
def test_add_log_entry_raises_if_topic_elements_arent_uint256(computation, canonical_address_a):
181184
with pytest.raises(ValidationError):
182-
computation.add_log_entry(CANONICAL_ADDRESS_A, [-1, 2, 3], b'')
185+
computation.add_log_entry(canonical_address_a, [-1, 2, 3], b'')
183186
with pytest.raises(ValidationError):
184-
computation.add_log_entry(CANONICAL_ADDRESS_A, ['1', 2, 3], b'')
187+
computation.add_log_entry(canonical_address_a, ['1', 2, 3], b'')
185188

186189

187-
def test_add_log_entry_raises_if_data_isnt_in_bytes(computation):
190+
def test_add_log_entry_raises_if_data_isnt_in_bytes(computation, canonical_address_a):
188191
with pytest.raises(ValidationError):
189-
computation.add_log_entry(CANONICAL_ADDRESS_A, [1, 2, 3], 1)
192+
computation.add_log_entry(canonical_address_a, [1, 2, 3], 1)
190193
with pytest.raises(ValidationError):
191-
computation.add_log_entry(CANONICAL_ADDRESS_A, [1, 2, 3], '')
194+
computation.add_log_entry(canonical_address_a, [1, 2, 3], '')
192195

193196

194-
def test_add_log_entry(computation):
197+
def test_add_log_entry(computation, canonical_address_a):
195198
# Adds log entry to log entries
196-
computation.add_log_entry(CANONICAL_ADDRESS_A, [1, 2, 3], b'')
199+
computation.add_log_entry(canonical_address_a, [1, 2, 3], b'')
197200
assert computation.get_log_entries() == tuple(
198201
[(b'\x0fW.R\x95\xc5\x7f\x15\x88o\x9b&>/m-l\x7b^\xc6', [1, 2, 3], b'')])
199202
# Can add multiple entries
200-
computation.add_log_entry(CANONICAL_ADDRESS_A, [4, 5, 6], b'2')
201-
computation.add_log_entry(CANONICAL_ADDRESS_A, [7, 8, 9], b'3')
203+
computation.add_log_entry(canonical_address_a, [4, 5, 6], b'2')
204+
computation.add_log_entry(canonical_address_a, [7, 8, 9], b'3')
202205

203206
assert len(computation.get_log_entries()) == 3
204207

205208

206-
def test_get_log_entries(computation):
207-
computation.add_log_entry(CANONICAL_ADDRESS_A, [1, 2, 3], b'')
209+
def test_get_log_entries(computation, canonical_address_a):
210+
computation.add_log_entry(canonical_address_a, [1, 2, 3], b'')
208211
assert computation.get_log_entries() == (
209212
(b'\x0fW.R\x95\xc5\x7f\x15\x88o\x9b&>/m-l\x7b^\xc6', [1, 2, 3], b''),)
210213

211214

212-
def test_get_log_entries_order_with_children(computation, child_message):
213-
parent_log = (CANONICAL_ADDRESS_A, [1, 2, 3], b'')
214-
parent_log2 = (CANONICAL_ADDRESS_A, [4, 5, 6], b'2')
215-
child_log = (CANONICAL_ADDRESS_A, [1, 2, 3], b'child')
215+
def test_get_log_entries_order_with_children(computation, child_message, canonical_address_a):
216+
parent_log = (canonical_address_a, [1, 2, 3], b'')
217+
parent_log2 = (canonical_address_a, [4, 5, 6], b'2')
218+
child_log = (canonical_address_a, [1, 2, 3], b'child')
216219
computation.add_log_entry(*parent_log)
217220
child_computation = computation.apply_child_computation(child_message)
218221
# Pretend the child computation logged something.
@@ -228,18 +231,18 @@ def test_get_log_entries_order_with_children(computation, child_message):
228231
assert logs[2] == parent_log2
229232

230233

231-
def test_get_log_entries_with_vmerror(computation):
234+
def test_get_log_entries_with_vmerror(computation, canonical_address_a):
232235
# Trigger an out of gas error causing get log entries to be ()
233-
computation.add_log_entry(CANONICAL_ADDRESS_A, [1, 2, 3], b'')
236+
computation.add_log_entry(canonical_address_a, [1, 2, 3], b'')
234237
with computation:
235238
raise VMError('Triggered VMError for tests')
236239
assert computation.is_error
237240
assert computation.get_log_entries() == ()
238241

239242

240-
def test_get_log_entries_with_revert(computation):
243+
def test_get_log_entries_with_revert(computation, canonical_address_a):
241244
# Trigger an out of gas error causing get log entries to be ()
242-
computation.add_log_entry(CANONICAL_ADDRESS_A, [1, 2, 3], b'')
245+
computation.add_log_entry(canonical_address_a, [1, 2, 3], b'')
243246
with computation:
244247
raise Revert('Triggered VMError for tests')
245248
assert computation.is_error

tests/core/vm/test_frontier_computation.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,20 @@
66
from eth.vm.forks.frontier.computation import (
77
FrontierComputation,
88
)
9-
from tests.core.vm.conftest import CANONICAL_ADDRESS_A, CANONICAL_ADDRESS_B
109

1110

1211
@pytest.fixture
13-
def state(chain_without_block_validation):
12+
def state(chain_without_block_validation, canonical_address_a):
1413
state = chain_without_block_validation.get_vm().state
15-
state.set_balance(CANONICAL_ADDRESS_A, 1000)
14+
state.set_balance(canonical_address_a, 1000)
1615
return state
1716

1817

1918
@pytest.fixture
20-
def message():
19+
def message(canonical_address_a, canonical_address_b):
2120
message = Message(
22-
to=CANONICAL_ADDRESS_A,
23-
sender=CANONICAL_ADDRESS_B,
21+
to=canonical_address_a,
22+
sender=canonical_address_b,
2423
value=100,
2524
data=b'',
2625
code=b'',
@@ -40,10 +39,10 @@ def computation(message, transaction_context, state):
4039

4140

4241
@pytest.fixture
43-
def child_message(computation):
42+
def child_message(computation, canonical_address_b):
4443
child_message = computation.prepare_child_message(
4544
gas=100,
46-
to=CANONICAL_ADDRESS_B,
45+
to=canonical_address_b,
4746
value=200,
4847
data=b'',
4948
code=b''

0 commit comments

Comments
 (0)