From f2c2da95ed871735d35ef757641f92bd1d31133a Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Fri, 3 Apr 2020 09:19:56 -0600 Subject: [PATCH] add compute_offset_slots --- specs/phase1/beacon-chain.md | 19 +++++++++++-------- .../eth2spec/test/helpers/attestations.py | 7 ++----- .../test/helpers/phase1/attestations.py | 5 +---- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/specs/phase1/beacon-chain.md b/specs/phase1/beacon-chain.md index 9c0cd0a6c..5079ec5c5 100644 --- a/specs/phase1/beacon-chain.md +++ b/specs/phase1/beacon-chain.md @@ -517,11 +517,18 @@ def get_latest_slot_for_shard(state: BeaconState, shard: Shard) -> Slot: return state.shard_states[shard].slot ``` +#### `compute_offset_slots` + +```python +def compute_offset_slots(start_slot: Slot, end_slot: Slot) -> Sequence[Slot]: + return [Slot(start_slot + x) for x in SHARD_BLOCK_OFFSETS if start_slot + x < end_slot] +``` + #### `get_offset_slots` ```python -def get_offset_slots(state: BeaconState, latest_shard_slot: Slot) -> Sequence[Slot]: - return [Slot(latest_shard_slot + x) for x in SHARD_BLOCK_OFFSETS if latest_shard_slot + x < state.slot] +def get_offset_slots(state: BeaconState, shard: Shard) -> Sequence[Slot]: + return compute_offset_slots(state.shard_states[shard].slot, state.slot) ``` ### Predicates @@ -630,14 +637,13 @@ def validate_attestation(state: BeaconState, attestation: Attestation) -> None: assert attestation.data.source == state.previous_justified_checkpoint shard = get_shard(state, attestation) - latest_shard_slot = get_latest_slot_for_shard(state, shard) # Type 1: on-time attestations if attestation.custody_bits_blocks != []: # Ensure on-time attestation assert data.slot + MIN_ATTESTATION_INCLUSION_DELAY == state.slot # Correct data root count - assert len(attestation.custody_bits_blocks) == len(get_offset_slots(state, latest_shard_slot)) + assert len(attestation.custody_bits_blocks) == len(get_offset_slots(state, shard)) # Correct parent block root assert data.beacon_block_root == get_block_root_at_slot(state, get_previous_slot(state.slot)) # Type 2: no shard transition, no custody bits # TODO: could only allow for older attestations. @@ -655,11 +661,8 @@ def validate_attestation(state: BeaconState, attestation: Attestation) -> None: ```python def apply_shard_transition(state: BeaconState, shard: Shard, transition: ShardTransition) -> None: - # Slot the attestation starts counting from - latest_slot = get_latest_slot_for_shard(state, shard) - # Correct data root count - offset_slots = get_offset_slots(state, latest_slot) + offset_slots = get_offset_slots(state, shard) assert ( len(transition.shard_data_roots) == len(transition.shard_states) diff --git a/tests/core/pyspec/eth2spec/test/helpers/attestations.py b/tests/core/pyspec/eth2spec/test/helpers/attestations.py index af989a9c0..7daaf159e 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/attestations.py +++ b/tests/core/pyspec/eth2spec/test/helpers/attestations.py @@ -1,7 +1,7 @@ from typing import List from eth2spec.test.context import expect_assertion_error -from eth2spec.test.helpers.state import next_slot, state_transition_and_sign_block +from eth2spec.test.helpers.state import state_transition_and_sign_block from eth2spec.test.helpers.block import build_empty_block_for_next_slot from eth2spec.test.helpers.keys import privkeys from eth2spec.utils import bls @@ -77,10 +77,7 @@ def build_attestation_data(spec, state, slot, index): def convert_to_valid_on_time_attestation(spec, state, attestation, signed=False): shard = spec.get_shard(state, attestation) - - next_state = state.copy() - next_slot(spec, next_state) - offset_slots = spec.get_offset_slots(next_state, spec.get_latest_slot_for_shard(next_state, shard)) + offset_slots = spec.compute_offset_slots(spec.get_latest_slot_for_shard(state, shard), state.slot + 1) for offset_slot in offset_slots: attestation.custody_bits_blocks.append( Bitlist[spec.MAX_VALIDATORS_PER_COMMITTEE]([0 for _ in attestation.aggregation_bits]) diff --git a/tests/core/pyspec/eth2spec/test/helpers/phase1/attestations.py b/tests/core/pyspec/eth2spec/test/helpers/phase1/attestations.py index b97cc5d46..676908f85 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/phase1/attestations.py +++ b/tests/core/pyspec/eth2spec/test/helpers/phase1/attestations.py @@ -3,7 +3,6 @@ from eth2spec.utils import bls from eth2spec.test.helpers.keys import privkeys import eth2spec.test.helpers.attestations as phase0_attestations -from eth2spec.test.helpers.state import next_slot def get_valid_on_time_attestation(spec, state, index=None, signed=False): @@ -15,10 +14,8 @@ def get_valid_on_time_attestation(spec, state, index=None, signed=False): attestation = phase0_attestations.get_valid_attestation(spec, state, state.slot, index, False) shard = spec.get_shard(state, attestation) + offset_slots = spec.compute_offset_slots(spec.get_latest_slot_for_shard(state, shard), state.slot + 1) - next_state = state.copy() - next_slot(spec, next_state) - offset_slots = spec.get_offset_slots(next_state, spec.get_latest_slot_for_shard(next_state, shard)) for offset_slot in offset_slots: attestation.custody_bits_blocks.append( Bitlist[spec.MAX_VALIDATORS_PER_COMMITTEE]([0 for _ in attestation.aggregation_bits])