Skip to content

Commit 3a905d0

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. - minor: tools.fixtures.helpers.chain_vm_configuration now support Constantinople
1 parent f616298 commit 3a905d0

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

eth/tools/fixtures/helpers.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
BaseVM,
4141
)
4242
from eth.vm.forks import (
43+
ConstantinopleVM,
4344
ByzantiumVM,
4445
TangerineWhistleVM,
4546
FrontierVM,
@@ -123,6 +124,10 @@ def chain_vm_configuration(fixture: Dict[str, Any]) -> Iterable[Tuple[int, Type[
123124
return (
124125
(0, ByzantiumVM),
125126
)
127+
elif network == 'Constantinople':
128+
return (
129+
(0, ConstantinopleVM),
130+
)
126131
elif network == 'FrontierToHomesteadAt5':
127132
HomesteadVM = BaseHomesteadVM.configure(support_dao_fork=False)
128133
return (
@@ -193,8 +198,10 @@ def new_chain_from_fixture(fixture: Dict[str, Any],
193198
)
194199

195200

196-
def apply_fixture_block_to_chain(block_fixture: Dict[str, Any],
197-
chain: BaseChain) -> Tuple[BaseBlock, BaseBlock, BaseBlock]:
201+
def apply_fixture_block_to_chain(
202+
block_fixture: Dict[str, Any],
203+
chain: BaseChain,
204+
perform_validation: bool=True) -> Tuple[BaseBlock, BaseBlock, BaseBlock]:
198205
'''
199206
:return: (premined_block, mined_block, rlp_encoded_mined_block)
200207
'''
@@ -209,7 +216,7 @@ def apply_fixture_block_to_chain(block_fixture: Dict[str, Any],
209216

210217
block = rlp.decode(block_fixture['rlp'], sedes=block_class)
211218

212-
mined_block, _, _ = chain.import_block(block)
219+
mined_block, _, _ = chain.import_block(block, perform_validation=perform_validation)
213220

214221
rlp_encoded_mined_block = rlp.encode(mined_block, sedes=block_class)
215222

tests/json-fixtures/test_blockchain.py

+16-6
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,18 +123,26 @@ 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:
128-
apply_fixture_block_to_chain(block_fixture, chain)
134+
(block, mined_block, block_rlp) = apply_fixture_block_to_chain(block_fixture, chain)
129135
except (TypeError, rlp.DecodingError, rlp.DeserializationError, ValidationError) as err:
130136
# failure is expected on this bad block
131137
pass
132138
else:
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)