Update testing and restrict to incremental block transitions

This commit is contained in:
protolambda 2020-05-13 16:46:28 +02:00
parent 524b84df78
commit 68442c2eef
No known key found for this signature in database
GPG Key ID: EC89FDBB2B4C7623
6 changed files with 21 additions and 11 deletions

View File

@ -1219,7 +1219,7 @@ def verify_block_signature(state: BeaconState, signed_block: SignedBeaconBlock)
```python ```python
def process_slots(state: BeaconState, slot: Slot) -> None: def process_slots(state: BeaconState, slot: Slot) -> None:
assert state.slot <= slot assert state.slot < slot
while state.slot < slot: while state.slot < slot:
process_slot(state) process_slot(state)
# Process epoch on the start slot of the next epoch # Process epoch on the start slot of the next epoch

View File

@ -243,6 +243,7 @@ def fill_aggregate_attestation(spec, state, attestation, signed=False):
def add_attestations_to_state(spec, state, attestations, slot): def add_attestations_to_state(spec, state, attestations, slot):
if state.slot < slot:
spec.process_slots(state, slot) spec.process_slots(state, slot)
for attestation in attestations: for attestation in attestations:
spec.process_attestation(state, attestation) spec.process_attestation(state, attestation)

View File

@ -15,6 +15,7 @@ def get_proposer_index_maybe(spec, state, slot, proposer_index=None):
" Signing block is slow due to transition for proposer index calculation.") " Signing block is slow due to transition for proposer index calculation.")
# use stub state to get proposer index of future slot # use stub state to get proposer index of future slot
stub_state = state.copy() stub_state = state.copy()
if stub_state.slot < slot:
spec.process_slots(stub_state, slot) spec.process_slots(stub_state, slot)
proposer_index = spec.get_beacon_proposer_index(stub_state) proposer_index = spec.get_beacon_proposer_index(stub_state)
return proposer_index return proposer_index
@ -52,7 +53,10 @@ def sign_block(spec, state, block, proposer_index=None):
def transition_unsigned_block(spec, state, block): def transition_unsigned_block(spec, state, block):
if state.slot < block.slot:
spec.process_slots(state, block.slot) spec.process_slots(state, block.slot)
assert state.latest_block_header.slot < block.slot # There may not already be a block in this slot or past it.
assert state.slot == block.slot # The block must be for this slot
spec.process_block(state, block) spec.process_block(state, block)
@ -74,6 +78,7 @@ def build_empty_block(spec, state, slot=None):
if slot < state.slot: if slot < state.slot:
raise Exception("build_empty_block cannot build blocks for past slots") raise Exception("build_empty_block cannot build blocks for past slots")
if slot > state.slot: if slot > state.slot:
if state.slot < slot:
# transition forward in copied state to grab relevant data from state # transition forward in copied state to grab relevant data from state
state = state.copy() state = state.copy()
spec.process_slots(state, slot) spec.process_slots(state, slot)

View File

@ -17,6 +17,7 @@ def next_slots(spec, state, slots):
""" """
Transition given slots forward. Transition given slots forward.
""" """
if slots > 0:
spec.process_slots(state, state.slot + slots) spec.process_slots(state, state.slot + slots)
@ -35,6 +36,7 @@ def next_epoch(spec, state):
Transition to the start slot of the next epoch Transition to the start slot of the next epoch
""" """
slot = state.slot + spec.SLOTS_PER_EPOCH - (state.slot % spec.SLOTS_PER_EPOCH) slot = state.slot + spec.SLOTS_PER_EPOCH - (state.slot % spec.SLOTS_PER_EPOCH)
if slot > state.slot:
spec.process_slots(state, slot) spec.process_slots(state, slot)

View File

@ -18,6 +18,7 @@ def run_epoch_processing_to(spec, state, process_name: str):
slot = state.slot + (spec.SLOTS_PER_EPOCH - state.slot % spec.SLOTS_PER_EPOCH) slot = state.slot + (spec.SLOTS_PER_EPOCH - state.slot % spec.SLOTS_PER_EPOCH)
# transition state to slot before epoch state transition # transition state to slot before epoch state transition
if state.slot < slot - 1:
spec.process_slots(state, slot - 1) spec.process_slots(state, slot - 1)
# start transitioning, do one slot update before the epoch itself. # start transitioning, do one slot update before the epoch itself.

View File

@ -51,6 +51,7 @@ def test_double_empty_epoch(spec, state):
@with_all_phases @with_all_phases
@spec_state_test @spec_state_test
def test_over_epoch_boundary(spec, state): def test_over_epoch_boundary(spec, state):
if spec.SLOTS_PER_EPOCH > 1:
spec.process_slots(state, state.slot + (spec.SLOTS_PER_EPOCH // 2)) spec.process_slots(state, state.slot + (spec.SLOTS_PER_EPOCH // 2))
yield 'pre', state yield 'pre', state
slots = spec.SLOTS_PER_EPOCH slots = spec.SLOTS_PER_EPOCH