Skip to content

Commit 65e6f4d

Browse files
committed
Failing blockchain tests now emit a state diff
- Instead of failing with "these block hashes do not match", blockchain tests now fail with "this part of the state tree does not match". - The tests were inefficient, they first checked that the state hash matched and then checked that the states matched. It's unlikely the states will ever not match! - Some validations needed to be deferred until after state-tree comparison, or else they would fail the test early with an unhelpful message.
1 parent f616298 commit 65e6f4d

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

eth/tools/fixtures/helpers.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,10 @@ def new_chain_from_fixture(fixture: Dict[str, Any],
193193
)
194194

195195

196-
def apply_fixture_block_to_chain(block_fixture: Dict[str, Any],
197-
chain: BaseChain) -> Tuple[BaseBlock, BaseBlock, BaseBlock]:
196+
def apply_fixture_block_to_chain(
197+
block_fixture: Dict[str, Any],
198+
chain: BaseChain,
199+
perform_validation: bool=True) -> Tuple[BaseBlock, BaseBlock, BaseBlock]:
198200
'''
199201
:return: (premined_block, mined_block, rlp_encoded_mined_block)
200202
'''
@@ -209,7 +211,7 @@ def apply_fixture_block_to_chain(block_fixture: Dict[str, Any],
209211

210212
block = rlp.decode(block_fixture['rlp'], sedes=block_class)
211213

212-
mined_block, _, _ = chain.import_block(block)
214+
mined_block, _, _ = chain.import_block(block, perform_validation=perform_validation)
213215

214216
rlp_encoded_mined_block = rlp.encode(mined_block, sedes=block_class)
215217

tests/json-fixtures/test_blockchain.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,10 @@ def test_blockchain_fixtures(fixture_data, fixture):
111111
# 2 - loop over blocks:
112112
# - apply transactions
113113
# - mine block
114-
# 4 - profit!!
114+
# 3 - diff resulting state with expected state
115+
# 4 - check that all previous blocks were valid
115116

117+
mined_blocks = list()
116118
for block_fixture in fixture['blocks']:
117119
should_be_good_block = 'blockHeader' in block_fixture
118120

@@ -121,8 +123,12 @@ def test_blockchain_fixtures(fixture_data, fixture):
121123
continue
122124

123125
if should_be_good_block:
124-
(block, mined_block, block_rlp) = apply_fixture_block_to_chain(block_fixture, chain)
125-
assert_mined_block_unchanged(block, mined_block)
126+
(block, mined_block, block_rlp) = apply_fixture_block_to_chain(
127+
block_fixture,
128+
chain,
129+
perform_validation=False # we manually validate below
130+
)
131+
mined_blocks.append((block, mined_block))
126132
else:
127133
try:
128134
apply_fixture_block_to_chain(block_fixture, chain)
@@ -133,6 +139,10 @@ def test_blockchain_fixtures(fixture_data, fixture):
133139
raise AssertionError("Block should have caused a validation error")
134140

135141
latest_block_hash = chain.get_canonical_block_by_number(chain.get_block().number - 1).hash
136-
assert latest_block_hash == fixture['lastblockhash']
142+
if latest_block_hash != fixture['lastblockhash']:
143+
verify_account_db(fixture['postState'], chain.get_vm().state.account_db)
144+
assert False, 'the state must be different if the hashes are'
137145

138-
verify_account_db(fixture['postState'], chain.get_vm().state.account_db)
146+
for block, mined_block in mined_blocks:
147+
assert_mined_block_unchanged(block, mined_block)
148+
chain.validate_block(block)

0 commit comments

Comments
 (0)