From bd1c71b82ed905321781f1be8452535c60d56d7c Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Wed, 16 Oct 2019 18:47:19 +0900 Subject: [PATCH] simplify index --- specs/core/0_beacon-chain.md | 57 +------------------ specs/validator/0_beacon-chain-validator.md | 4 +- .../eth2spec/test/helpers/attestations.py | 2 +- .../pyspec/eth2spec/test/helpers/state.py | 8 +-- .../test_process_attestation.py | 3 +- ..._process_justification_and_finalization.py | 4 +- 6 files changed, 8 insertions(+), 70 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 72cc4edb8..e0d6c108b 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -490,7 +490,6 @@ class BeaconState(Container): validators: List[Validator, VALIDATOR_REGISTRY_LIMIT] balances: List[Gwei, VALIDATOR_REGISTRY_LIMIT] # Shuffling - start_index: uint64 randao_mixes: Vector[Hash, EPOCHS_PER_HISTORICAL_VECTOR] # Slashings slashings: Vector[Gwei, EPOCHS_PER_SLASHINGS_VECTOR] # Per-epoch sums of slashed effective balances @@ -881,9 +880,7 @@ def get_crosslink_committee(state: BeaconState, slot: Slot, index: uint64) -> Se """ epoch = compute_epoch_of_slot(slot) committees_per_slot = get_committees_per_slot(state, slot) - slot_start_index = get_slot_start_index(state, slot) - slot_offset = (index + MAX_COMMITTEES_PER_SLOT - slot_start_index) % MAX_COMMITTEES_PER_SLOT - epoch_offset = slot_offset + (slot % SLOTS_PER_EPOCH) * committees_per_slot + epoch_offset = index + (slot % SLOTS_PER_EPOCH) * committees_per_slot return compute_committee( indices=get_active_validator_indices(state, epoch), @@ -893,47 +890,6 @@ def get_crosslink_committee(state: BeaconState, slot: Slot, index: uint64) -> Se ) ``` -#### `get_slot_start_index` - -```python -def get_slot_start_index(state: BeaconState, slot: Slot) -> uint64: - """ - Return the start index of the 0th committee at ``slot``. - """ - epoch = compute_epoch_of_slot(slot) - committees_per_slot = get_committees_per_slot(state, slot) - start_index = get_start_index(state, epoch) - slot_start_index = ((slot % SLOTS_PER_EPOCH) * committees_per_slot + start_index) % MAX_COMMITTEES_PER_SLOT - return slot_start_index -``` - -#### `get_start_index` - -```python -def get_start_index(state: BeaconState, epoch: Epoch) -> uint64: - """ - Return the start index of the 0th committee at ``epoch``. - """ - assert epoch <= get_current_epoch(state) + 1 - check_epoch = Epoch(get_current_epoch(state) + 1) - index = (state.start_index + get_index_delta(state, get_current_epoch(state))) % MAX_COMMITTEES_PER_SLOT - MAX_COMMITTEES_PER_EPOCH = MAX_COMMITTEES_PER_SLOT * SLOTS_PER_EPOCH - while check_epoch > epoch: - check_epoch -= Epoch(1) - index = (index + MAX_COMMITTEES_PER_EPOCH - get_index_delta(state, check_epoch)) % MAX_COMMITTEES_PER_SLOT - return index -``` - -#### `get_index_delta` - -```python -def get_index_delta(state: BeaconState, epoch: Epoch) -> uint64: - """ - Return the amount to increase ``state.start_index`` at ``epoch``. - """ - return get_committees_per_slot(state, compute_start_slot_of_epoch(epoch)) * SLOTS_PER_EPOCH -``` - #### `get_beacon_proposer_index` ```python @@ -1417,8 +1373,6 @@ def process_final_updates(state: BeaconState) -> None: if next_epoch % (SLOTS_PER_HISTORICAL_ROOT // SLOTS_PER_EPOCH) == 0: historical_batch = HistoricalBatch(block_roots=state.block_roots, state_roots=state.state_roots) state.historical_roots.append(hash_tree_root(historical_batch)) - # Update start shard - state.start_index = (state.start_index + get_index_delta(state, current_epoch)) % MAX_COMMITTEES_PER_SLOT # Rotate current/previous epoch attestations state.previous_epoch_attestations = state.current_epoch_attestations state.current_epoch_attestations = [] @@ -1545,15 +1499,8 @@ def process_attester_slashing(state: BeaconState, attester_slashing: AttesterSla ```python def process_attestation(state: BeaconState, attestation: Attestation) -> None: data = attestation.data - assert data.index < MAX_COMMITTEES_PER_SLOT - slot_start_index = get_slot_start_index(state, data.slot) - if data.index < slot_start_index: - test_index = data.index + MAX_COMMITTEES_PER_SLOT - else: - test_index = data.index - assert slot_start_index <= test_index < slot_start_index + get_committees_per_slot(state, data.slot) + assert data.index < get_committees_per_slot(state, data.slot) assert data.target.epoch in (get_previous_epoch(state), get_current_epoch(state)) - assert data.slot + MIN_ATTESTATION_INCLUSION_DELAY <= state.slot <= data.slot + SLOTS_PER_EPOCH committee = get_crosslink_committee(state, data.slot, data.index) diff --git a/specs/validator/0_beacon-chain-validator.md b/specs/validator/0_beacon-chain-validator.md index b0e64d6c9..51f560243 100644 --- a/specs/validator/0_beacon-chain-validator.md +++ b/specs/validator/0_beacon-chain-validator.md @@ -149,9 +149,7 @@ def get_committee_assignment(state: BeaconState, start_slot = compute_start_slot_of_epoch(epoch) for slot in range(start_slot, start_slot + SLOTS_PER_EPOCH): - slot_start_index = get_slot_start_index(state, Slot(slot)) - for i in range(get_committees_per_slot(state, Slot(slot))): - index = (slot_start_index + i) % MAX_COMMITTEES_PER_SLOT + for index in range(get_committees_per_slot(state, Slot(slot))): committee = get_crosslink_committee(state, Slot(slot), index) if validator_index in committee: return committee, index, Slot(slot) diff --git a/test_libs/pyspec/eth2spec/test/helpers/attestations.py b/test_libs/pyspec/eth2spec/test/helpers/attestations.py index 879d878ac..afd51b0bd 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/attestations.py +++ b/test_libs/pyspec/eth2spec/test/helpers/attestations.py @@ -42,7 +42,7 @@ def get_valid_attestation(spec, state, slot=None, index=None, signed=False): if slot is None: slot = state.slot if index is None: - index = spec.get_slot_start_index(state, slot) + index = 0 attestation_data = build_attestation_data(spec, state, slot, index) diff --git a/test_libs/pyspec/eth2spec/test/helpers/state.py b/test_libs/pyspec/eth2spec/test/helpers/state.py index ffa59fafd..68b8283bc 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/state.py +++ b/test_libs/pyspec/eth2spec/test/helpers/state.py @@ -54,18 +54,14 @@ def next_epoch_with_attestations(spec, slot_to_attest = post_state.slot - spec.MIN_ATTESTATION_INCLUSION_DELAY + 1 committees_per_slot = spec.get_committees_per_slot(state, slot_to_attest) if slot_to_attest >= spec.compute_start_slot_of_epoch(spec.get_current_epoch(post_state)): - slot_start_index = spec.get_slot_start_index(state, slot_to_attest) - for i in range(committees_per_slot): - index = (slot_start_index + i) % spec.MAX_COMMITTEES_PER_SLOT + for index in range(committees_per_slot): cur_attestation = get_valid_attestation(spec, post_state, slot_to_attest, index=index) block.body.attestations.append(cur_attestation) if fill_prev_epoch: slot_to_attest = post_state.slot - spec.SLOTS_PER_EPOCH + 1 committees_per_slot = spec.get_committees_per_slot(state, slot_to_attest) - slot_start_index = spec.get_slot_start_index(state, slot_to_attest) - for i in range(committees_per_slot): - index = (slot_start_index + i) % spec.MAX_COMMITTEES_PER_SLOT + for index in range(committees_per_slot): prev_attestation = get_valid_attestation(spec, post_state, slot_to_attest, index=index) block.body.attestations.append(prev_attestation) diff --git a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_attestation.py b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_attestation.py index 229eb85b3..e3ccb5d9f 100644 --- a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_attestation.py +++ b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_attestation.py @@ -140,8 +140,7 @@ def test_wrong_index_for_committee_signature(spec, state): def test_wrong_index_for_slot(spec, state): committees_per_slot = spec.get_committees_per_slot(state, state.slot) assert committees_per_slot < spec.MAX_COMMITTEES_PER_SLOT - slot_start_index = spec.get_slot_start_index(state, state.slot) - index = slot_start_index + committees_per_slot + index = committees_per_slot attestation = get_valid_attestation(spec, state) state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY diff --git a/test_libs/pyspec/eth2spec/test/phase_0/epoch_processing/test_process_justification_and_finalization.py b/test_libs/pyspec/eth2spec/test/phase_0/epoch_processing/test_process_justification_and_finalization.py index 25d8c083f..8fc3f9866 100644 --- a/test_libs/pyspec/eth2spec/test/phase_0/epoch_processing/test_process_justification_and_finalization.py +++ b/test_libs/pyspec/eth2spec/test/phase_0/epoch_processing/test_process_justification_and_finalization.py @@ -28,9 +28,7 @@ def add_mock_attestations(spec, state, epoch, source, target, sufficient_support start_slot = spec.compute_start_slot_of_epoch(epoch) for slot in range(start_slot, start_slot + spec.SLOTS_PER_EPOCH): committees_per_slot = spec.get_committees_per_slot(state, slot) - slot_start_index = spec.get_slot_start_index(state, slot) - for i in range(committees_per_slot): - index = (slot_start_index + i) % spec.MAX_COMMITTEES_PER_SLOT + for index in range(committees_per_slot): # Check if we already have had sufficient balance. (and undone if we don't want it). # If so, do not create more attestations. (we do not have empty pending attestations normally anyway) if remaining_balance < 0: