add compute_offset_slots

This commit is contained in:
Danny Ryan 2020-04-03 09:19:56 -06:00
parent 613f368c00
commit f2c2da95ed
No known key found for this signature in database
GPG Key ID: 2765A792E42CE07A
3 changed files with 14 additions and 17 deletions

View File

@ -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)

View File

@ -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])

View File

@ -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])