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,7 +243,8 @@ 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):
spec.process_slots(state, slot) if state.slot < 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,7 +15,8 @@ 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()
spec.process_slots(stub_state, slot) if stub_state.slot < 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):
spec.process_slots(state, block.slot) if state.slot < 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,9 +78,10 @@ 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:
# transition forward in copied state to grab relevant data from state if state.slot < slot:
state = state.copy() # transition forward in copied state to grab relevant data from state
spec.process_slots(state, slot) state = state.copy()
spec.process_slots(state, slot)
empty_block = spec.BeaconBlock() empty_block = spec.BeaconBlock()
empty_block.slot = slot empty_block.slot = slot

View File

@ -17,7 +17,8 @@ def next_slots(spec, state, slots):
""" """
Transition given slots forward. Transition given slots forward.
""" """
spec.process_slots(state, state.slot + slots) if slots > 0:
spec.process_slots(state, state.slot + slots)
def transition_to(spec, state, slot): def transition_to(spec, state, slot):
@ -35,7 +36,8 @@ 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)
spec.process_slots(state, slot) if slot > state.slot:
spec.process_slots(state, slot)
def get_state_root(spec, state, slot) -> bytes: def get_state_root(spec, state, slot) -> bytes:

View File

@ -18,7 +18,8 @@ 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
spec.process_slots(state, slot - 1) if state.slot < 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.
spec.process_slot(state) spec.process_slot(state)

View File

@ -51,7 +51,8 @@ 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):
spec.process_slots(state, state.slot + (spec.SLOTS_PER_EPOCH // 2)) if spec.SLOTS_PER_EPOCH > 1:
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
yield 'slots', slots yield 'slots', slots