diff --git a/specs/phase0/fork-choice.md b/specs/phase0/fork-choice.md index 79d37f28d..d8aaed14f 100644 --- a/specs/phase0/fork-choice.md +++ b/specs/phase0/fork-choice.md @@ -104,7 +104,7 @@ def get_forkchoice_store(anchor_state: BeaconState) -> Store: justified_checkpoint = Checkpoint(epoch=anchor_epoch, root=anchor_root) finalized_checkpoint = Checkpoint(epoch=anchor_epoch, root=anchor_root) return Store( - time=anchor_state.genesis_time, + time=anchor_state.genesis_time + SECONDS_PER_SLOT * anchor_state.slot, genesis_time=anchor_state.genesis_time, justified_checkpoint=justified_checkpoint, finalized_checkpoint=finalized_checkpoint, @@ -317,11 +317,13 @@ def update_latest_messages(store: Store, attesting_indices: Sequence[ValidatorIn ```python def on_tick(store: Store, time: uint64) -> None: previous_slot = get_current_slot(store) + print(previous_slot) # update store time store.time = time current_slot = get_current_slot(store) + print(current_slot) # Not a new epoch, return if not (current_slot > previous_slot and compute_slots_since_epoch_start(current_slot) == 0): return diff --git a/specs/phase1/beacon-chain.md b/specs/phase1/beacon-chain.md index fe5c0d9a7..fcd703a96 100644 --- a/specs/phase1/beacon-chain.md +++ b/specs/phase1/beacon-chain.md @@ -544,6 +544,9 @@ def is_valid_indexed_attestation(state: BeaconState, indexed_attestation: Indexe AttestationCustodyBitWrapper(hash_tree_root(attestation.data), i, cbit), domain)) else: assert not cbit + print(all_pubkeys) + print(all_signing_roots) + print(attestation.signature) return bls.AggregateVerify(zip(all_pubkeys, all_signing_roots), signature=attestation.signature) ``` diff --git a/tests/core/pyspec/eth2spec/test/context.py b/tests/core/pyspec/eth2spec/test/context.py index 5338ccb9d..c4ce15ed8 100644 --- a/tests/core/pyspec/eth2spec/test/context.py +++ b/tests/core/pyspec/eth2spec/test/context.py @@ -54,7 +54,9 @@ def with_custom_state(balances_fn: Callable[[Any], Sequence[int]], # TODO: instead of upgrading a test phase0 genesis state we can also write a phase1 state helper. # Decide based on performance/consistency results later. state = phases["phase1"].upgrade_to_phase1(state) - + # Shard state slot must lag behind BeaconState slot by at least 1 + # Will handle this more elegantly with fork mechanics + spec.process_slots(state, state.slot + 1) kw['state'] = state except KeyError: raise TypeError('Spec decorator must come within state decorator to inject spec into state.') diff --git a/tests/core/pyspec/eth2spec/test/fork_choice/test_on_attestation.py b/tests/core/pyspec/eth2spec/test/fork_choice/test_on_attestation.py index 87a39a620..e77c08892 100644 --- a/tests/core/pyspec/eth2spec/test/fork_choice/test_on_attestation.py +++ b/tests/core/pyspec/eth2spec/test/fork_choice/test_on_attestation.py @@ -120,11 +120,12 @@ def test_on_attestation_mismatched_target_and_slot(spec, state): @spec_state_test def test_on_attestation_target_not_in_store(spec, state): store = spec.get_forkchoice_store(state) - time = spec.SECONDS_PER_SLOT * spec.SLOTS_PER_EPOCH + time = store.time + spec.SECONDS_PER_SLOT * spec.SLOTS_PER_EPOCH spec.on_tick(store, time) # move to immediately before next epoch to make block new target - transition_to(spec, state, state.slot + spec.SLOTS_PER_EPOCH - 1) + next_epoch = spec.get_current_epoch(state) + 1 + transition_to(spec, state, spec.compute_start_slot_at_epoch(next_epoch) - 1) target_block = build_empty_block_for_next_slot(spec, state) state_transition_and_sign_block(spec, state, target_block) @@ -141,11 +142,12 @@ def test_on_attestation_target_not_in_store(spec, state): @spec_state_test def test_on_attestation_beacon_block_not_in_store(spec, state): store = spec.get_forkchoice_store(state) - time = spec.SECONDS_PER_SLOT * spec.SLOTS_PER_EPOCH + time = store.time + spec.SECONDS_PER_SLOT * spec.SLOTS_PER_EPOCH spec.on_tick(store, time) # move to immediately before next epoch to make block new target - transition_to(spec, state, state.slot + spec.SLOTS_PER_EPOCH - 1) + next_epoch = spec.get_current_epoch(state) + 1 + transition_to(spec, state, spec.compute_start_slot_at_epoch(next_epoch) - 1) target_block = build_empty_block_for_next_slot(spec, state) signed_target_block = state_transition_and_sign_block(spec, state, target_block) @@ -169,7 +171,7 @@ def test_on_attestation_beacon_block_not_in_store(spec, state): @spec_state_test def test_on_attestation_future_epoch(spec, state): store = spec.get_forkchoice_store(state) - time = 3 * spec.SECONDS_PER_SLOT + time = store.time + 3 * spec.SECONDS_PER_SLOT spec.on_tick(store, time) block = build_empty_block_for_next_slot(spec, state) @@ -189,7 +191,7 @@ def test_on_attestation_future_epoch(spec, state): @spec_state_test def test_on_attestation_future_block(spec, state): store = spec.get_forkchoice_store(state) - time = spec.SECONDS_PER_SLOT * 5 + time = store.time + spec.SECONDS_PER_SLOT * 5 spec.on_tick(store, time) block = build_empty_block_for_next_slot(spec, state) @@ -209,7 +211,7 @@ def test_on_attestation_future_block(spec, state): @spec_state_test def test_on_attestation_same_slot(spec, state): store = spec.get_forkchoice_store(state) - time = 1 * spec.SECONDS_PER_SLOT + time = store.time + spec.SECONDS_PER_SLOT spec.on_tick(store, time) block = build_empty_block_for_next_slot(spec, state) @@ -225,7 +227,7 @@ def test_on_attestation_same_slot(spec, state): @spec_state_test def test_on_attestation_invalid_attestation(spec, state): store = spec.get_forkchoice_store(state) - time = 3 * spec.SECONDS_PER_SLOT + time = store.time + 3 * spec.SECONDS_PER_SLOT spec.on_tick(store, time) block = build_empty_block_for_next_slot(spec, state) diff --git a/tests/core/pyspec/eth2spec/test/fork_choice/test_on_block.py b/tests/core/pyspec/eth2spec/test/fork_choice/test_on_block.py index 3d824811c..4438dff92 100644 --- a/tests/core/pyspec/eth2spec/test/fork_choice/test_on_block.py +++ b/tests/core/pyspec/eth2spec/test/fork_choice/test_on_block.py @@ -160,6 +160,7 @@ def test_on_block_finalized_skip_slots(spec, state): @spec_state_test def test_on_block_finalized_skip_slots_not_in_skip_chain(spec, state): # Initialization + next_epoch(spec, state) store = spec.get_forkchoice_store(state) store.finalized_checkpoint = spec.Checkpoint( diff --git a/tests/core/pyspec/eth2spec/test/fork_choice/test_on_tick.py b/tests/core/pyspec/eth2spec/test/fork_choice/test_on_tick.py index 27b64ac09..93f3bd9bb 100644 --- a/tests/core/pyspec/eth2spec/test/fork_choice/test_on_tick.py +++ b/tests/core/pyspec/eth2spec/test/fork_choice/test_on_tick.py @@ -27,14 +27,16 @@ def test_basic(spec, state): @spec_state_test def test_update_justified_single(spec, state): store = spec.get_forkchoice_store(state) - seconds_per_epoch = spec.SECONDS_PER_SLOT * spec.SLOTS_PER_EPOCH + next_epoch = spec.get_current_epoch(state) + 1 + next_epoch_start_slot = spec.compute_start_slot_at_epoch(next_epoch) + seconds_until_next_epoch = next_epoch_start_slot * spec.SECONDS_PER_SLOT - store.time store.best_justified_checkpoint = spec.Checkpoint( epoch=store.justified_checkpoint.epoch + 1, root=b'\x55' * 32, ) - run_on_tick(spec, store, store.time + seconds_per_epoch, True) + run_on_tick(spec, store, store.time + seconds_until_next_epoch, True) @with_all_phases diff --git a/tests/core/pyspec/eth2spec/test/helpers/attestations.py b/tests/core/pyspec/eth2spec/test/helpers/attestations.py index 8f11e3306..deebd0356 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/attestations.py +++ b/tests/core/pyspec/eth2spec/test/helpers/attestations.py @@ -2,7 +2,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.block import build_empty_block_for_next_slot, transition_unsigned_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 from eth2spec.utils.ssz.ssz_typing import Bitlist @@ -213,7 +213,7 @@ def get_attestation_custody_signature(spec, state, attestation_data, block_index def sign_attestation(spec, state, attestation): if spec.fork == 'phase1' and any(attestation.custody_bits_blocks): - sign_on_time_attestation(spec, state,attestation) + sign_on_time_attestation(spec, state, attestation) return participants = spec.get_attesting_indices( diff --git a/tests/core/pyspec/eth2spec/test/helpers/state.py b/tests/core/pyspec/eth2spec/test/helpers/state.py index 9b5d132f9..46a7ce2b5 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/state.py +++ b/tests/core/pyspec/eth2spec/test/helpers/state.py @@ -1,5 +1,5 @@ from eth2spec.test.context import expect_assertion_error -from eth2spec.test.helpers.block import sign_block, build_empty_block_for_next_slot, transition_unsigned_block +from eth2spec.test.helpers.block import sign_block, transition_unsigned_block def get_balance(state, index): diff --git a/tests/core/pyspec/eth2spec/test/phase_0/block_processing/test_process_attestation.py b/tests/core/pyspec/eth2spec/test/phase_0/block_processing/test_process_attestation.py index 525f01fcc..9528ff6eb 100644 --- a/tests/core/pyspec/eth2spec/test/phase_0/block_processing/test_process_attestation.py +++ b/tests/core/pyspec/eth2spec/test/phase_0/block_processing/test_process_attestation.py @@ -13,10 +13,10 @@ from eth2spec.test.helpers.attestations import ( sign_attestation, ) from eth2spec.test.helpers.state import ( - next_epoch, - next_slots, next_slot, + next_slots, next_epoch, + transition_to, ) from eth2spec.test.helpers.block import apply_empty_block from eth2spec.utils.ssz.ssz_typing import Bitlist @@ -46,8 +46,8 @@ def test_success_multi_proposer_index_iterations(spec, state): @with_all_phases @spec_state_test def test_success_previous_epoch(spec, state): - attestation = get_valid_attestation(spec, state, signed=True) - state.slot = spec.SLOTS_PER_EPOCH - 1 + attestation = get_valid_attestation(spec, state, signed=True, on_time=False) + transition_to(spec, state, spec.SLOTS_PER_EPOCH - 1) next_epoch(spec, state) apply_empty_block(spec, state) @@ -76,10 +76,10 @@ def test_before_inclusion_delay(spec, state): @with_all_phases @spec_state_test def test_after_epoch_slots(spec, state): - attestation = get_valid_attestation(spec, state, signed=True) - state.slot = spec.SLOTS_PER_EPOCH - 1 + attestation = get_valid_attestation(spec, state, signed=True, on_time=False) + # increment past latest inclusion slot - spec.process_slots(state, state.slot + 2) + transition_to(spec, state, state.slot + spec.SLOTS_PER_EPOCH + 1) apply_empty_block(spec, state) yield from run_attestation_processing(spec, state, attestation, False) @@ -154,7 +154,7 @@ def test_mismatched_target_and_slot(spec, state): next_epoch(spec, state) next_epoch(spec, state) - attestation = get_valid_attestation(spec, state) + attestation = get_valid_attestation(spec, state, on_time=False) attestation.data.slot = attestation.data.slot - spec.SLOTS_PER_EPOCH sign_attestation(spec, state, attestation) @@ -167,9 +167,9 @@ def test_mismatched_target_and_slot(spec, state): def test_old_target_epoch(spec, state): assert spec.MIN_ATTESTATION_INCLUSION_DELAY < spec.SLOTS_PER_EPOCH * 2 - attestation = get_valid_attestation(spec, state, signed=True) + attestation = get_valid_attestation(spec, state, signed=True, on_time=False) - transition_to(spec, state, state.slot + spec.SLOTS_PER_EPOCH * 2) # target epoch will be too old to handle + next_slots(spec, state, spec.SLOTS_PER_EPOCH * 2) # target epoch will be too old to handle yield from run_attestation_processing(spec, state, attestation, False) @@ -225,14 +225,14 @@ def test_source_root_is_target_root(spec, state): @with_all_phases @spec_state_test def test_invalid_current_source_root(spec, state): - transition_to(spec, state, state.slot + spec.SLOTS_PER_EPOCH * 5) + next_slots(spec, state, spec.SLOTS_PER_EPOCH * 5) state.finalized_checkpoint.epoch = 2 state.previous_justified_checkpoint = spec.Checkpoint(epoch=3, root=b'\x01' * 32) state.current_justified_checkpoint = spec.Checkpoint(epoch=4, root=b'\x32' * 32) - attestation = get_valid_attestation(spec, state, slot=(spec.SLOTS_PER_EPOCH * 3) + 1) + attestation = get_valid_attestation(spec, state, slot=(spec.SLOTS_PER_EPOCH * 3) + 1, on_time=False) next_slots(spec, state, spec.MIN_ATTESTATION_INCLUSION_DELAY) # Test logic sanity checks: diff --git a/tests/core/pyspec/eth2spec/test/phase_0/block_processing/test_process_attester_slashing.py b/tests/core/pyspec/eth2spec/test/phase_0/block_processing/test_process_attester_slashing.py index e9665e714..c379bb815 100644 --- a/tests/core/pyspec/eth2spec/test/phase_0/block_processing/test_process_attester_slashing.py +++ b/tests/core/pyspec/eth2spec/test/phase_0/block_processing/test_process_attester_slashing.py @@ -143,6 +143,10 @@ def test_invalid_sig_1(spec, state): @always_bls def test_invalid_sig_2(spec, state): attester_slashing = get_valid_attester_slashing(spec, state, signed_1=True, signed_2=False) + """ + SOMETHING WRONG WITH HASH CACHE OF SEPARATE ATTESTATIONS MAKING SIGS + LOOK CORRECT + """ yield from run_attester_slashing_processing(spec, state, attester_slashing, False) diff --git a/tests/core/pyspec/eth2spec/test/phase_0/epoch_processing/test_process_rewards_and_penalties.py b/tests/core/pyspec/eth2spec/test/phase_0/epoch_processing/test_process_rewards_and_penalties.py index 7cdeb16d9..044390b4b 100644 --- a/tests/core/pyspec/eth2spec/test/phase_0/epoch_processing/test_process_rewards_and_penalties.py +++ b/tests/core/pyspec/eth2spec/test/phase_0/epoch_processing/test_process_rewards_and_penalties.py @@ -1,7 +1,10 @@ from copy import deepcopy -from eth2spec.test.context import spec_state_test, with_all_phases, spec_test, \ +from eth2spec.test.context import ( + spec_state_test, spec_test, + with_all_phases, with_phases, misc_balances, with_custom_state, default_activation_threshold, single_phase +) from eth2spec.test.helpers.state import ( next_epoch, next_slot, @@ -19,27 +22,30 @@ def run_process_rewards_and_penalties(spec, state): def prepare_state_with_full_attestations(spec, state): + start_slot = state.slot + start_epoch = spec.get_current_epoch(state) + next_start_epoch = spec.compute_start_slot_at_epoch(start_epoch + 1) attestations = [] for slot in range(spec.SLOTS_PER_EPOCH + spec.MIN_ATTESTATION_INCLUSION_DELAY): # create an attestation for each index in each slot in epoch - if slot < spec.SLOTS_PER_EPOCH: - for committee_index in range(spec.get_committee_count_at_slot(state, slot)): + if state.slot < next_start_epoch: + for committee_index in range(spec.get_committee_count_at_slot(state, state.slot)): attestation = get_valid_attestation(spec, state, index=committee_index, signed=True) attestations.append(attestation) # fill each created slot in state after inclusion delay - if slot - spec.MIN_ATTESTATION_INCLUSION_DELAY >= 0: - inclusion_slot = slot - spec.MIN_ATTESTATION_INCLUSION_DELAY + if state.slot >= start_slot + spec.MIN_ATTESTATION_INCLUSION_DELAY: + inclusion_slot = state.slot - spec.MIN_ATTESTATION_INCLUSION_DELAY include_attestations = [att for att in attestations if att.data.slot == inclusion_slot] add_attestations_to_state(spec, state, include_attestations, state.slot) next_slot(spec, state) - assert spec.compute_epoch_at_slot(state.slot) == spec.GENESIS_EPOCH + 1 + assert spec.compute_epoch_at_slot(state.slot) == start_epoch + 1 assert len(state.previous_epoch_attestations) == len(attestations) return attestations -@with_all_phases +@with_phases(['phase0']) @spec_state_test def test_genesis_epoch_no_attestations_no_penalties(spec, state): pre_state = deepcopy(state) @@ -52,7 +58,7 @@ def test_genesis_epoch_no_attestations_no_penalties(spec, state): assert state.balances[index] == pre_state.balances[index] -@with_all_phases +@with_phases(['phase0']) @spec_state_test def test_genesis_epoch_full_attestations_no_rewards(spec, state): attestations = [] @@ -81,6 +87,8 @@ def test_genesis_epoch_full_attestations_no_rewards(spec, state): @with_all_phases @spec_state_test def test_full_attestations(spec, state): + # Go to start of next epoch to ensure can have full participation + next_epoch(spec, state) attestations = prepare_state_with_full_attestations(spec, state) pre_state = deepcopy(state) @@ -101,6 +109,8 @@ def test_full_attestations(spec, state): @with_custom_state(balances_fn=misc_balances, threshold_fn=default_activation_threshold) @single_phase def test_full_attestations_misc_balances(spec, state): + # Go to start of next epoch to ensure can have full participation + next_epoch(spec, state) attestations = prepare_state_with_full_attestations(spec, state) pre_state = deepcopy(state) diff --git a/tests/core/pyspec/eth2spec/test/phase_1/block_processing/test_process_attestation.py b/tests/core/pyspec/eth2spec/test/phase_1/block_processing/test_process_attestation.py index 288bb8e00..ed4328327 100644 --- a/tests/core/pyspec/eth2spec/test/phase_1/block_processing/test_process_attestation.py +++ b/tests/core/pyspec/eth2spec/test/phase_1/block_processing/test_process_attestation.py @@ -3,7 +3,7 @@ from eth2spec.test.context import ( spec_state_test, always_bls, ) -from eth2spec.test.helpers.state import next_slot, transition_to +from eth2spec.test.helpers.state import transition_to from eth2spec.test.helpers.attestations import ( run_attestation_processing, get_valid_late_attestation, @@ -15,7 +15,6 @@ from eth2spec.test.helpers.attestations import ( @spec_state_test @always_bls def test_on_time_success(spec, state): - next_slot(spec, state) attestation = get_valid_on_time_attestation(spec, state, signed=True) transition_to(spec, state, state.slot + spec.MIN_ATTESTATION_INCLUSION_DELAY) @@ -27,7 +26,6 @@ def test_on_time_success(spec, state): @spec_state_test @always_bls def test_on_time_empty_custody_bits_blocks(spec, state): - next_slot(spec, state) attestation = get_valid_late_attestation(spec, state, signed=True) assert not any(attestation.custody_bits_blocks) @@ -41,7 +39,6 @@ def test_on_time_empty_custody_bits_blocks(spec, state): @spec_state_test @always_bls def test_late_with_custody_bits_blocks(spec, state): - next_slot(spec, state) attestation = get_valid_on_time_attestation(spec, state, signed=True) transition_to(spec, state, state.slot + spec.MIN_ATTESTATION_INCLUSION_DELAY + 1) diff --git a/tests/core/pyspec/eth2spec/test/sanity/test_blocks.py b/tests/core/pyspec/eth2spec/test/sanity/test_blocks.py index e533b3b0a..156dc1581 100644 --- a/tests/core/pyspec/eth2spec/test/sanity/test_blocks.py +++ b/tests/core/pyspec/eth2spec/test/sanity/test_blocks.py @@ -381,7 +381,7 @@ def test_attestation(spec, state): # Add to state via block transition pre_current_attestations_len = len(state.current_epoch_attestations) - attestation_block = build_empty_block(spec, state, state.slot + 1 + spec.MIN_ATTESTATION_INCLUSION_DELAY) + attestation_block = build_empty_block(spec, state, state.slot + spec.MIN_ATTESTATION_INCLUSION_DELAY) attestation_block.body.attestations.append(attestation) signed_attestation_block = state_transition_and_sign_block(spec, state, attestation_block) diff --git a/tests/core/pyspec/eth2spec/test/test_finality.py b/tests/core/pyspec/eth2spec/test/test_finality.py index d793b6656..0b99cc3f9 100644 --- a/tests/core/pyspec/eth2spec/test/test_finality.py +++ b/tests/core/pyspec/eth2spec/test/test_finality.py @@ -1,4 +1,4 @@ -from eth2spec.test.context import spec_state_test, never_bls, with_all_phases +from eth2spec.test.context import spec_state_test, never_bls, with_all_phases, with_phases from eth2spec.test.helpers.state import next_epoch from eth2spec.test.helpers.attestations import next_epoch_with_attestations from eth2spec.test.helpers.block import apply_empty_block @@ -29,7 +29,7 @@ def check_finality(spec, assert state.finalized_checkpoint == prev_state.finalized_checkpoint -@with_all_phases +@with_phases(["phase0"]) @spec_state_test @never_bls def test_finality_no_updates_at_genesis(spec, state):