Skip to content

Commit

Permalink
Merge pull request #1177 from ethereum/block-state-root-fix
Browse files Browse the repository at this point in the history
Fix #1146 block state root issue
  • Loading branch information
protolambda authored Jun 14, 2019
2 parents e4704e0 + f834f72 commit 7cf0dcd
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 34 deletions.
13 changes: 13 additions & 0 deletions test_libs/pyspec/eth2spec/test/helpers/state.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from eth2spec.test.helpers.block import sign_block


def get_balance(state, index):
return state.balances[index]

Expand All @@ -23,3 +26,13 @@ def get_state_root(spec, state, slot) -> bytes:
"""
assert slot < state.slot <= slot + spec.SLOTS_PER_HISTORICAL_ROOT
return state.latest_state_roots[slot % spec.SLOTS_PER_HISTORICAL_ROOT]


def state_transition_and_sign_block(spec, state, block):
"""
State transition via the provided ``block``
then package the block with the state root and signature.
"""
spec.state_transition(state, block)
block.state_root = state.hash_tree_root()
sign_block(spec, state, block)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from eth2spec.test.context import spec_state_test, with_all_phases
from eth2spec.test.helpers.state import (
next_epoch,
next_slot
next_slot,
state_transition_and_sign_block,
)
from eth2spec.test.helpers.block import apply_empty_block, sign_block
from eth2spec.test.helpers.attestations import (
Expand All @@ -27,11 +28,14 @@ def run_process_crosslinks(spec, state, valid=True):
block = build_empty_block_for_next_slot(spec, state)
block.slot = slot
sign_block(spec, state, block)
spec.state_transition(state, block)
state_transition_and_sign_block(spec, state, block)

# cache state before epoch transition
spec.process_slot(state)

# process components of epoch transition before processing crosslinks
spec.process_justification_and_finalization(state)

yield 'pre', state
spec.process_crosslinks(state)
yield 'post', state
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from eth2spec.phase0.spec import state_transition
from eth2spec.test.helpers.block import build_empty_block_for_next_slot, sign_block
from eth2spec.test.helpers.state import next_epoch
from eth2spec.test.helpers.state import next_epoch, state_transition_and_sign_block
from eth2spec.test.context import spec_state_test, with_all_phases


Expand All @@ -16,7 +15,7 @@ def run_process_registry_updates(spec, state, valid=True):
block = build_empty_block_for_next_slot(spec, state)
block.slot = slot
sign_block(spec, state, block)
state_transition(state, block)
state_transition_and_sign_block(spec, state, block)

# cache state before epoch transition
spec.process_slot(state)
Expand Down
60 changes: 33 additions & 27 deletions test_libs/pyspec/eth2spec/test/sanity/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from eth2spec.utils.ssz.ssz_impl import signing_root
from eth2spec.utils.bls import bls_sign

from eth2spec.test.helpers.state import get_balance
from eth2spec.test.helpers.state import get_balance, state_transition_and_sign_block
# from eth2spec.test.helpers.transfers import get_valid_transfer
from eth2spec.test.helpers.block import build_empty_block_for_next_slot, sign_block
from eth2spec.test.helpers.keys import privkeys, pubkeys
Expand All @@ -25,9 +25,10 @@ def test_empty_block_transition(spec, state):
yield 'pre', state

block = build_empty_block_for_next_slot(spec, state, signed=True)
yield 'blocks', [block], List[spec.BeaconBlock]

spec.state_transition(state, block)
state_transition_and_sign_block(spec, state, block)

yield 'blocks', [block], List[spec.BeaconBlock]
yield 'post', state

assert len(state.eth1_data_votes) == pre_eth1_votes + 1
Expand All @@ -44,9 +45,10 @@ def test_skipped_slots(spec, state):
block = build_empty_block_for_next_slot(spec, state)
block.slot += 3
sign_block(spec, state, block)
yield 'blocks', [block], List[spec.BeaconBlock]

spec.state_transition(state, block)
state_transition_and_sign_block(spec, state, block)

yield 'blocks', [block], List[spec.BeaconBlock]
yield 'post', state

assert state.slot == block.slot
Expand All @@ -64,9 +66,10 @@ def test_empty_epoch_transition(spec, state):
block = build_empty_block_for_next_slot(spec, state)
block.slot += spec.SLOTS_PER_EPOCH
sign_block(spec, state, block)
yield 'blocks', [block], List[spec.BeaconBlock]

spec.state_transition(state, block)
state_transition_and_sign_block(spec, state, block)

yield 'blocks', [block], List[spec.BeaconBlock]
yield 'post', state

assert state.slot == block.slot
Expand All @@ -84,9 +87,10 @@ def test_empty_epoch_transition(spec, state):
# block = build_empty_block_for_next_slot(spec, state)
# block.slot += spec.SLOTS_PER_EPOCH * 5
# sign_block(spec, state, block, proposer_index=0)
# yield 'blocks', [block], List[spec.BeaconBlock]

# spec.state_transition(state, block)
# state_transition_and_sign_block(spec, state, block)

# yield 'blocks', [block], List[spec.BeaconBlock]
# yield 'post', state

# assert state.slot == block.slot
Expand All @@ -113,9 +117,10 @@ def test_proposer_slashing(spec, state):
block = build_empty_block_for_next_slot(spec, state)
block.body.proposer_slashings.append(proposer_slashing)
sign_block(spec, state, block)
yield 'blocks', [block], List[spec.BeaconBlock]

spec.state_transition(state, block)
state_transition_and_sign_block(spec, state, block)

yield 'blocks', [block], List[spec.BeaconBlock]
yield 'post', state

# check if slashed
Expand Down Expand Up @@ -147,9 +152,10 @@ def test_attester_slashing(spec, state):
block = build_empty_block_for_next_slot(spec, state)
block.body.attester_slashings.append(attester_slashing)
sign_block(spec, state, block)
yield 'blocks', [block], List[spec.BeaconBlock]

spec.state_transition(state, block)
state_transition_and_sign_block(spec, state, block)

yield 'blocks', [block], List[spec.BeaconBlock]
yield 'post', state

slashed_validator = state.validator_registry[validator_index]
Expand Down Expand Up @@ -185,9 +191,9 @@ def test_deposit_in_block(spec, state):
block.body.deposits.append(deposit)
sign_block(spec, state, block)

yield 'blocks', [block], List[spec.BeaconBlock]
state_transition_and_sign_block(spec, state, block)

spec.state_transition(state, block)
yield 'blocks', [block], List[spec.BeaconBlock]
yield 'post', state

assert len(state.validator_registry) == initial_registry_len + 1
Expand All @@ -213,9 +219,9 @@ def test_deposit_top_up(spec, state):
block.body.deposits.append(deposit)
sign_block(spec, state, block)

yield 'blocks', [block], List[spec.BeaconBlock]
state_transition_and_sign_block(spec, state, block)

spec.state_transition(state, block)
yield 'blocks', [block], List[spec.BeaconBlock]
yield 'post', state

assert len(state.validator_registry) == initial_registry_len
Expand All @@ -238,7 +244,7 @@ def test_attestation(spec, state):
attestation_block.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY
attestation_block.body.attestations.append(attestation)
sign_block(spec, state, attestation_block)
spec.state_transition(state, attestation_block)
state_transition_and_sign_block(spec, state, attestation_block)

assert len(state.current_epoch_attestations) == pre_current_attestations_len + 1

Expand All @@ -248,7 +254,7 @@ def test_attestation(spec, state):
epoch_block = build_empty_block_for_next_slot(spec, state)
epoch_block.slot += spec.SLOTS_PER_EPOCH
sign_block(spec, state, epoch_block)
spec.state_transition(state, epoch_block)
state_transition_and_sign_block(spec, state, epoch_block)

yield 'blocks', [attestation_block, epoch_block], List[spec.BeaconBlock]
yield 'post', state
Expand Down Expand Up @@ -287,15 +293,15 @@ def test_voluntary_exit(spec, state):
initiate_exit_block = build_empty_block_for_next_slot(spec, state)
initiate_exit_block.body.voluntary_exits.append(voluntary_exit)
sign_block(spec, state, initiate_exit_block)
spec.state_transition(state, initiate_exit_block)
state_transition_and_sign_block(spec, state, initiate_exit_block)

assert state.validator_registry[validator_index].exit_epoch < spec.FAR_FUTURE_EPOCH

# Process within epoch transition
exit_block = build_empty_block_for_next_slot(spec, state)
exit_block.slot += spec.SLOTS_PER_EPOCH
sign_block(spec, state, exit_block)
spec.state_transition(state, exit_block)
state_transition_and_sign_block(spec, state, exit_block)

yield 'blocks', [initiate_exit_block, exit_block], List[spec.BeaconBlock]
yield 'post', state
Expand Down Expand Up @@ -326,9 +332,9 @@ def test_voluntary_exit(spec, state):
# block.body.transfers.append(transfer)
# sign_block(spec, state, block)

# yield 'blocks', [block], List[spec.BeaconBlock]
# state_transition_and_sign_block(spec, state, block)

# spec.state_transition(state, block)
# yield 'blocks', [block], List[spec.BeaconBlock]
# yield 'post', state

# sender_balance = get_balance(state, sender_index)
Expand All @@ -354,7 +360,7 @@ def test_balance_driven_status_transitions(spec, state):
block = build_empty_block_for_next_slot(spec, state)
block.slot += spec.SLOTS_PER_EPOCH
sign_block(spec, state, block)
spec.state_transition(state, block)
state_transition_and_sign_block(spec, state, block)

yield 'blocks', [block], List[spec.BeaconBlock]
yield 'post', state
Expand All @@ -371,7 +377,7 @@ def test_historical_batch(spec, state):
yield 'pre', state

block = build_empty_block_for_next_slot(spec, state, signed=True)
spec.state_transition(state, block)
state_transition_and_sign_block(spec, state, block)

yield 'blocks', [block], List[spec.BeaconBlock]
yield 'post', state
Expand All @@ -392,15 +398,15 @@ def test_historical_batch(spec, state):
# blocks = []
# for _ in range(spec.SLOTS_PER_ETH1_VOTING_PERIOD - 1):
# block = build_empty_block_for_next_slot(spec, state)
# spec.state_transition(state, block)
# state_transition_and_sign_block(spec, state, block)
# expected_votes += 1
# assert len(state.eth1_data_votes) == expected_votes
# blocks.append(block)

# block = build_empty_block_for_next_slot(spec, state)
# blocks.append(block)

# spec.state_transition(state, block)
# state_transition_and_sign_block(spec, state, block)

# yield 'blocks', [block], List[spec.BeaconBlock]
# yield 'post', state
Expand Down
4 changes: 2 additions & 2 deletions test_libs/pyspec/eth2spec/test/test_finality.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import List

from eth2spec.test.context import spec_state_test, never_bls, with_all_phases
from eth2spec.test.helpers.state import next_epoch
from eth2spec.test.helpers.state import next_epoch, state_transition_and_sign_block
from eth2spec.test.helpers.block import build_empty_block_for_next_slot, apply_empty_block
from eth2spec.test.helpers.attestations import get_valid_attestation

Expand Down Expand Up @@ -54,7 +54,7 @@ def next_epoch_with_attestations(spec,
prev_attestation = get_valid_attestation(spec, post_state, slot_to_attest)
block.body.attestations.append(prev_attestation)

spec.state_transition(post_state, block)
state_transition_and_sign_block(spec, post_state, block)
blocks.append(block)

return state, blocks, post_state
Expand Down

0 comments on commit 7cf0dcd

Please sign in to comment.