예제 #1
0
def test_valid_signature_future_committee(spec, state):
    # NOTE: the `state` provided is at genesis and the process to select
    # sync committees currently returns the same committee for the first and second
    # periods at genesis.
    # To get a distinct committee so we can generate an "old" signature, we need to advance
    # 2 EPOCHS_PER_SYNC_COMMITTEE_PERIOD periods.
    current_epoch = spec.get_current_epoch(state)
    old_current_sync_committee = state.current_sync_committee
    old_next_sync_committee = state.next_sync_committee

    epoch_in_future_sync_committee_period = current_epoch + 2 * spec.EPOCHS_PER_SYNC_COMMITTEE_PERIOD
    slot_in_future_sync_committee_period = epoch_in_future_sync_committee_period * spec.SLOTS_PER_EPOCH
    transition_to(spec, state, slot_in_future_sync_committee_period)

    sync_committee = state.current_sync_committee
    next_sync_committee = state.next_sync_committee

    assert next_sync_committee != sync_committee
    assert sync_committee != old_current_sync_committee
    assert sync_committee != old_next_sync_committee

    committee_indices = compute_committee_indices(spec, state, sync_committee)

    block = build_empty_block_for_next_slot(spec, state)
    block.body.sync_aggregate = spec.SyncAggregate(
        sync_committee_bits=[True] * len(committee_indices),
        sync_committee_signature=compute_aggregate_sync_committee_signature(
            spec,
            state,
            block.slot - 1,
            committee_indices,
        )
    )

    yield from run_sync_committee_processing(spec, state, block)
def run_sync_committees_progress_test(spec, state):
    first_sync_committee = state.current_sync_committee.copy()
    second_sync_committee = state.next_sync_committee.copy()

    current_period = spec.compute_sync_committee_period(
        spec.get_current_epoch(state))
    next_period = current_period + 1
    next_period_start_epoch = next_period * spec.EPOCHS_PER_SYNC_COMMITTEE_PERIOD
    next_period_start_slot = next_period_start_epoch * spec.SLOTS_PER_EPOCH
    end_slot_of_current_period = next_period_start_slot - 1
    transition_to(spec, state, end_slot_of_current_period)

    # Ensure assignments have not changed:
    assert state.current_sync_committee == first_sync_committee
    assert state.next_sync_committee == second_sync_committee

    yield from run_epoch_processing_with(spec, state,
                                         'process_sync_committee_updates')

    # Can compute the third committee having computed final balances in the last epoch
    # of this `EPOCHS_PER_SYNC_COMMITTEE_PERIOD`
    third_sync_committee = spec.get_next_sync_committee(state)

    # Ensure assignments have changed:
    assert state.next_sync_committee != second_sync_committee
    if current_period > 0:
        assert state.current_sync_committee != first_sync_committee
    else:
        # Current and next are duplicated in genesis period so remain stable
        assert state.current_sync_committee == first_sync_committee

    # Ensure expected committees were calculated
    assert state.current_sync_committee == second_sync_committee
    assert state.next_sync_committee == third_sync_committee
예제 #3
0
def test_out_of_bound_offset(spec, state):
    # TODO: Handle this edge case properly
    if not is_full_crosslink(spec, state):
        # skip
        return

    beacon_state = transition_to_valid_shard_slot(spec, state)
    shard = 0
    slot = (
        beacon_state.shard_states[shard].slot +
        spec.SHARD_BLOCK_OFFSETS[spec.MAX_SHARD_BLOCKS_PER_ATTESTATION - 1] +
        1  # out-of-bound
    )
    transition_to(spec, beacon_state, slot)

    shard_state = beacon_state.shard_states[shard]
    signed_shard_block = build_shard_block(spec,
                                           beacon_state,
                                           shard,
                                           slot=beacon_state.slot,
                                           signed=True)

    yield from run_shard_blocks(spec,
                                shard_state,
                                signed_shard_block,
                                beacon_state,
                                valid=False)
def finalize_on_12(spec, state, epoch, sufficient_support, messed_up_target):
    assert epoch > 2
    transition_to(spec, state, spec.SLOTS_PER_EPOCH * epoch - 1)  # skip ahead to just before epoch

    # 43210 -- epochs ago
    # 210xx  -- justification bitfield indices (pre shift)
    # 3210x -- justification bitfield indices (post shift)
    # 001*. -- justification bitfield contents, . = this epoch, * is being justified now
    # checkpoints for the epochs ago:
    c1, c2, _, _, _ = get_checkpoints(spec, epoch)
    put_checkpoints_in_block_roots(spec, state, [c1, c2])

    old_finalized = state.finalized_checkpoint
    state.previous_justified_checkpoint = c2
    state.current_justified_checkpoint = c2
    state.justification_bits = spec.Bitvector[spec.JUSTIFICATION_BITS_LENGTH]()
    state.justification_bits[0] = 1  # mock 2nd latest epoch as justified (this is pre-shift)
    # mock the 1st latest epoch as justifiable, with 2nd as source
    add_mock_attestations(spec, state,
                          epoch=epoch - 1,
                          source=c2,
                          target=c1,
                          sufficient_support=sufficient_support,
                          messed_up_target=messed_up_target)

    # process!
    yield from run_process_just_and_fin(spec, state)

    assert state.previous_justified_checkpoint == c2  # changed to old current
    if sufficient_support and not messed_up_target:
        assert state.current_justified_checkpoint == c1  # changed to 1st latest
        assert state.finalized_checkpoint == c2  # finalized previous justified epoch
    else:
        assert state.current_justified_checkpoint == c2  # still old current
        assert state.finalized_checkpoint == old_finalized  # no new finalized
def finalize_on_234(spec, state, epoch, sufficient_support):
    assert epoch > 4
    transition_to(spec, state, spec.SLOTS_PER_EPOCH * epoch - 1)  # skip ahead to just before epoch

    # 43210 -- epochs ago
    # 3210x -- justification bitfield indices
    # 11*0. -- justification bitfield contents, . = this epoch, * is being justified now
    # checkpoints for the epochs ago:
    c1, c2, c3, c4, _ = get_checkpoints(spec, epoch)
    put_checkpoints_in_block_roots(spec, state, [c1, c2, c3, c4])

    old_finalized = state.finalized_checkpoint
    state.previous_justified_checkpoint = c4
    state.current_justified_checkpoint = c3
    state.justification_bits = spec.Bitvector[spec.JUSTIFICATION_BITS_LENGTH]()
    state.justification_bits[1:3] = [1, 1]  # mock 3rd and 4th latest epochs as justified (indices are pre-shift)
    # mock the 2nd latest epoch as justifiable, with 4th as source
    add_mock_attestations(spec, state,
                          epoch=epoch - 2,
                          source=c4,
                          target=c2,
                          sufficient_support=sufficient_support)

    # process!
    yield from run_process_just_and_fin(spec, state)

    assert state.previous_justified_checkpoint == c3  # changed to old current
    if sufficient_support:
        assert state.current_justified_checkpoint == c2  # changed to 2nd latest
        assert state.finalized_checkpoint == c4  # finalized old previous justified epoch
    else:
        assert state.current_justified_checkpoint == c3  # still old current
        assert state.finalized_checkpoint == old_finalized  # no new finalized
예제 #6
0
def test_update_justified_single_on_store_finalized_chain(spec, state):
    store = get_genesis_forkchoice_store(spec, state)

    # [Mock store.best_justified_checkpoint]
    # Create a block at epoch 1
    next_epoch(spec, state)
    block = build_empty_block_for_next_slot(spec, state)
    state_transition_and_sign_block(spec, state, block)
    store.blocks[block.hash_tree_root()] = block.copy()
    store.block_states[block.hash_tree_root()] = state.copy()
    parent_block = block.copy()
    # To make compute_slots_since_epoch_start(current_slot) == 0, transition to the end of the epoch
    slot = state.slot + spec.SLOTS_PER_EPOCH - state.slot % spec.SLOTS_PER_EPOCH - 1
    transition_to(spec, state, slot)
    # Create a block at the start of epoch 2
    block = build_empty_block_for_next_slot(spec, state)
    # Mock state
    state.current_justified_checkpoint = spec.Checkpoint(
        epoch=spec.compute_epoch_at_slot(parent_block.slot),
        root=parent_block.hash_tree_root(),
    )
    state_transition_and_sign_block(spec, state, block)
    store.blocks[block.hash_tree_root()] = block
    store.block_states[block.hash_tree_root()] = state
    # Mock store.best_justified_checkpoint
    store.best_justified_checkpoint = state.current_justified_checkpoint.copy()

    run_on_tick(spec,
                store,
                store.genesis_time + state.slot * spec.config.SECONDS_PER_SLOT,
                new_justified_checkpoint=True)
def test_invalid_signature_previous_committee(spec, state):
    # NOTE: the `state` provided is at genesis and the process to select
    # sync committees currently returns the same committee for the first and second
    # periods at genesis.
    # To get a distinct committee so we can generate an "old" signature, we need to advance
    # 2 EPOCHS_PER_SYNC_COMMITTEE_PERIOD periods.
    current_epoch = spec.get_current_epoch(state)
    old_sync_committee = state.next_sync_committee

    epoch_in_future_sync_commitee_period = current_epoch + 2 * spec.EPOCHS_PER_SYNC_COMMITTEE_PERIOD
    slot_in_future_sync_committee_period = epoch_in_future_sync_commitee_period * spec.SLOTS_PER_EPOCH
    transition_to(spec, state, slot_in_future_sync_committee_period)

    # Use the previous sync committee to produce the signature.
    # Ensure that the pubkey sets are different.
    assert set(old_sync_committee.pubkeys) != set(state.current_sync_committee.pubkeys)
    committee_indices = compute_committee_indices(spec, state, old_sync_committee)

    block = build_empty_block_for_next_slot(spec, state)
    block.body.sync_aggregate = spec.SyncAggregate(
        sync_committee_bits=[True] * len(committee_indices),
        sync_committee_signature=compute_aggregate_sync_committee_signature(
            spec,
            state,
            block.slot - 1,
            committee_indices,
            block_root=block.parent_root,
        )
    )

    yield from run_sync_committee_processing(spec, state, block, expect_exception=True)
def test_invalid_signature_previous_committee(spec, state):
    # NOTE: the `state` provided is at genesis and the process to select
    # sync committees currently returns the same committee for the first and second
    # periods at genesis.
    # To get a distinct committee so we can generate an "old" signature, we need to advance
    # 2 EPOCHS_PER_SYNC_COMMITTEE_PERIOD periods.
    current_epoch = spec.get_current_epoch(state)
    previous_committee = state.next_sync_committee

    epoch_in_future_sync_commitee_period = current_epoch + 2 * spec.EPOCHS_PER_SYNC_COMMITTEE_PERIOD
    slot_in_future_sync_committee_period = epoch_in_future_sync_commitee_period * spec.SLOTS_PER_EPOCH
    transition_to(spec, state, slot_in_future_sync_committee_period)

    pubkeys = [validator.pubkey for validator in state.validators]
    committee = [pubkeys.index(pubkey) for pubkey in previous_committee.pubkeys]

    yield 'pre', state

    block = build_empty_block_for_next_slot(spec, state)
    block.body.sync_committee_bits = [True] * len(committee)
    block.body.sync_committee_signature = compute_aggregate_sync_committee_signature(
        spec,
        state,
        block.slot - 1,
        committee,
    )

    yield 'blocks', [block]
    expect_assertion_error(lambda: spec.process_sync_committee(state, block.body))
    yield 'post', None
def test_sync_committees_progress_not_genesis(spec, state):
    # Transition out of the genesis epoch period to test non-exceptional case
    assert spec.get_current_epoch(state) == spec.GENESIS_EPOCH
    slot_in_next_period = state.slot + spec.EPOCHS_PER_SYNC_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
    transition_to(spec, state, slot_in_next_period)

    yield from run_sync_committees_progress_test(spec, state)
def test_success_previous_epoch(spec, state):
    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)

    yield from run_attestation_processing(spec, state, attestation)
def run_sync_committees_progress_test(spec, state):
    first_sync_committee = state.current_sync_committee
    second_sync_committee = state.next_sync_committee

    current_period = spec.get_current_epoch(
        state) // spec.EPOCHS_PER_SYNC_COMMITTEE_PERIOD
    next_period = current_period + 1
    next_period_start_epoch = next_period * spec.EPOCHS_PER_SYNC_COMMITTEE_PERIOD
    next_period_start_slot = next_period_start_epoch * spec.SLOTS_PER_EPOCH
    end_slot_of_current_period = next_period_start_slot - 1
    transition_to(spec, state, end_slot_of_current_period)

    # Ensure assignments have not changed:
    assert state.current_sync_committee == first_sync_committee
    assert state.next_sync_committee == second_sync_committee

    yield from run_epoch_processing_with(spec, state,
                                         'process_sync_committee_updates')

    # Can compute the third committee having computed final balances in the last epoch
    # of this `EPOCHS_PER_SYNC_COMMITTEE_PERIOD`
    third_sync_committee = spec.get_next_sync_committee(state)

    assert state.current_sync_committee == second_sync_committee
    assert state.next_sync_committee == third_sync_committee
예제 #12
0
def test_compute_subnets_for_sync_committee_slot_period_boundary(state, spec):
    # Transition to the end of the period
    transition_to(
        spec, state,
        spec.SLOTS_PER_EPOCH * spec.EPOCHS_PER_SYNC_COMMITTEE_PERIOD - 1)

    next_slot_epoch = spec.compute_epoch_at_slot(state.slot + 1)
    assert (spec.compute_sync_committee_period(spec.get_current_epoch(state))
            != spec.compute_sync_committee_period(next_slot_epoch))
    some_sync_committee_members = list(
        (
            _subnet_for_sync_committee_index(spec, i),
            pubkey,
        )
        # use next_sync_committee
        for i, pubkey in enumerate(state.next_sync_committee.pubkeys))
    expected_subnets_by_pubkey = _get_expected_subnets_by_pubkey(
        some_sync_committee_members)

    for _, pubkey in some_sync_committee_members:
        validator_index = _validator_index_for_pubkey(state, pubkey)
        subnets = spec.compute_subnets_for_sync_committee(
            state, validator_index)
        expected_subnets = expected_subnets_by_pubkey[pubkey]
        assert subnets == expected_subnets
예제 #13
0
def test_on_attestation_beacon_block_not_in_store(spec, state):
    store = get_genesis_forkchoice_store(spec, state)
    time = store.time + spec.SECONDS_PER_SLOT * (spec.SLOTS_PER_EPOCH + 1)
    spec.on_tick(store, time)

    # move to immediately before next epoch to make block new target
    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)

    # store target in store
    spec.on_block(store, signed_target_block)

    head_block = build_empty_block_for_next_slot(spec, state)
    state_transition_and_sign_block(spec, state, head_block)

    # do not add head block to store

    attestation = get_valid_attestation(spec,
                                        state,
                                        slot=head_block.slot,
                                        signed=True)
    assert attestation.data.target.root == target_block.hash_tree_root()
    assert attestation.data.beacon_block_root == head_block.hash_tree_root()

    run_on_attestation(spec, state, store, attestation, False)
def test_sync_committees_progress(spec, state):
    current_epoch = spec.get_current_epoch(state)
    # NOTE: if not in the genesis epoch, period math below needs to be
    # adjusted relative to the current epoch
    assert current_epoch == 0

    first_sync_committee = state.current_sync_committee
    second_sync_committee = state.next_sync_committee

    slot_at_end_of_current_period = spec.EPOCHS_PER_SYNC_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH - 1
    transition_to(spec, state, slot_at_end_of_current_period)

    # Ensure assignments have not changed:
    assert state.current_sync_committee == first_sync_committee
    assert state.next_sync_committee == second_sync_committee

    yield from run_epoch_processing_with(spec, state,
                                         'process_sync_committee_updates')

    # Can compute the third committee having computed final balances in the last epoch
    # of this `EPOCHS_PER_SYNC_COMMITTEE_PERIOD`
    third_sync_committee = spec.get_sync_committee(
        state, 2 * spec.EPOCHS_PER_SYNC_COMMITTEE_PERIOD)

    assert state.current_sync_committee == second_sync_committee
    assert state.next_sync_committee == third_sync_committee
def test_late_success(spec, state):
    attestation = get_valid_late_attestation(spec, state, signed=True)

    transition_to(spec, state,
                  state.slot + spec.MIN_ATTESTATION_INCLUSION_DELAY + 1)

    yield from run_attestation_processing(spec, state, attestation)
예제 #16
0
def test_validator_withdrawal_reenable_after_custody_reveal(spec, state):
    transition_to_valid_shard_slot(spec, state)
    transition_to(spec, state, state.slot + 1)  # Make len(offset_slots) == 1
    spec.initiate_validator_exit(state, 0)
    assert state.validators[0].withdrawable_epoch < spec.FAR_FUTURE_EPOCH

    next_epoch_via_block(spec, state)

    assert state.validators[0].withdrawable_epoch == spec.FAR_FUTURE_EPOCH

    while spec.get_current_epoch(state) < state.validators[0].exit_epoch:
        next_epoch_via_block(spec, state)

    while (state.validators[0].next_custody_secret_to_reveal <=
           spec.get_custody_period_for_validator(
               0, state.validators[0].exit_epoch - 1)):
        custody_key_reveal = get_valid_custody_key_reveal(spec,
                                                          state,
                                                          validator_index=0)
        _, _, _ = run_custody_key_reveal_processing(spec, state,
                                                    custody_key_reveal)

    yield from run_process_custody_final_updates(spec, state)

    assert state.validators[0].withdrawable_epoch < spec.FAR_FUTURE_EPOCH
예제 #17
0
def test_on_attestation_target_checkpoint_not_in_store_diff_slot(spec, state):
    store = get_genesis_forkchoice_store(spec, state)
    time = store.time + spec.SECONDS_PER_SLOT * (spec.SLOTS_PER_EPOCH + 1)
    spec.on_tick(store, time)

    # move to two slots before next epoch to make target block one before an empty slot
    next_epoch = spec.get_current_epoch(state) + 1
    transition_to(spec, state,
                  spec.compute_start_slot_at_epoch(next_epoch) - 2)

    target_block = build_empty_block_for_next_slot(spec, state)
    signed_target_block = state_transition_and_sign_block(
        spec, state, target_block)

    # add target block to store
    spec.on_block(store, signed_target_block)

    # target checkpoint state is not yet in store

    attestation_slot = target_block.slot + 1
    transition_to(spec, state, attestation_slot)
    attestation = get_valid_attestation(spec,
                                        state,
                                        slot=attestation_slot,
                                        signed=True)
    assert attestation.data.target.root == target_block.hash_tree_root()

    run_on_attestation(spec, state, store, attestation)
예제 #18
0
def test_on_time_empty_custody_bits_blocks(spec, state):
    attestation = get_valid_late_attestation(spec, state, signed=True)

    assert not any(attestation.custody_bits_blocks)

    transition_to(spec, state, state.slot + spec.MIN_ATTESTATION_INCLUSION_DELAY)

    yield from run_attestation_processing(spec, state, attestation, False)
def test_after_epoch_slots(spec, state):
    attestation = get_valid_attestation(spec, state, signed=True, on_time=False)

    # increment past latest inclusion slot
    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)
예제 #20
0
def fill_block_shard_transitions_by_attestations(spec, state, block):
    block.body.shard_transitions = [spec.ShardTransition()] * spec.MAX_SHARDS
    for attestation in block.body.attestations:
        shard = spec.get_shard(state, attestation)
        if attestation.data.slot == state.slot:
            temp_state = state.copy()
            transition_to(spec, temp_state, slot=block.slot)
            shard_transition = spec.get_shard_transition(temp_state, shard, [])
            block.body.shard_transitions[shard] = shard_transition
def test_no_winning_root(spec, state):
    if not is_full_crosslink(spec, state):
        # Skip this test
        return

    state, shard, target_shard_slot = get_initial_env(spec,
                                                      state,
                                                      target_len_offset_slot=1)
    init_slot = state.slot

    # Create SignedShardBlock at init_slot
    shard_block = build_shard_block(spec,
                                    state,
                                    shard,
                                    slot=init_slot,
                                    body=get_sample_shard_block_body(
                                        spec, is_max=True),
                                    signed=True)

    # Transition state to target shard slot
    transition_to(spec, state, target_shard_slot)

    # Create a shard_transitions that would be included at beacon block `target_shard_slot + 1`
    shard_transitions = get_shard_transitions(spec, state,
                                              {shard: [shard_block]})
    shard_transition = shard_transitions[shard]
    committee_index = get_committee_index_of_shard(spec, state, state.slot,
                                                   shard)
    attestation = get_valid_attestation(
        spec,
        state,
        index=committee_index,
        shard_transition=shard_transition,
        # Decrease attested participants to 1/3 committee
        filter_participant_set=lambda committee: set(
            list(committee)[:len(committee) // 3]),
        signed=True,
        on_time=True,
    )

    next_slot(spec, state)

    _, _, _ = run_attestation_processing(spec, state, attestation)

    _, winning_roots = spec.get_shard_winning_roots(state, [attestation])
    assert len(winning_roots) == 0

    # No winning root, shard_transitions[shard] is empty
    shard_transitions = [spec.ShardTransition()] * spec.MAX_SHARDS
    pre_shard_states = state.shard_states.copy()
    yield from run_shard_transitions_processing(spec, state, shard_transitions,
                                                [attestation])

    for pending_attestation in state.current_epoch_attestations:
        assert bool(pending_attestation.crosslink_success) is False

    assert state.shard_states == pre_shard_states
예제 #22
0
def test_custody_key_reveal(spec, state):
    transition_to_valid_shard_slot(spec, state)
    transition_to(spec, state, state.slot + spec.EPOCHS_PER_CUSTODY_PERIOD * spec.SLOTS_PER_EPOCH)

    block = build_empty_block(spec, state, slot=state.slot + 1)
    custody_key_reveal = get_valid_custody_key_reveal(spec, state)
    block.body.custody_key_reveals = [custody_key_reveal]

    yield from run_beacon_block(spec, state, block)
def test_validator_withdrawal_delay(spec, state):
    transition_to_valid_shard_slot(spec, state)
    transition_to(spec, state, state.slot + 1)  # Make len(offset_slots) == 1
    spec.initiate_validator_exit(state, 0)
    assert state.validators[0].withdrawable_epoch < spec.FAR_FUTURE_EPOCH

    yield from run_process_custody_final_updates(spec, state)

    assert state.validators[0].withdrawable_epoch == spec.FAR_FUTURE_EPOCH
예제 #24
0
def test_custody_slashing(spec, state):
    # NOTE: this test is only for full crosslink (minimal config), not for mainnet
    if not is_full_crosslink(spec, state):
        # skip
        return

    state = transition_to_valid_shard_slot(spec, state)

    # Build shard block
    shard = 0
    committee_index = get_committee_index_of_shard(spec, state, state.slot,
                                                   shard)
    # Create slashable shard block body
    validator_index = spec.get_beacon_committee(state, state.slot,
                                                committee_index)[0]
    custody_secret = get_custody_secret(spec, state, validator_index)
    slashable_body = get_custody_slashable_test_vector(spec,
                                                       custody_secret,
                                                       length=100,
                                                       slashable=True)
    shard_block = build_shard_block(spec,
                                    state,
                                    shard,
                                    body=slashable_body,
                                    slot=state.slot,
                                    signed=True)
    shard_block_dict: Dict[spec.Shard, Sequence[spec.SignedShardBlock]] = {
        shard: [shard_block]
    }
    shard_transitions = get_shard_transitions(spec, state, shard_block_dict)

    attestation = get_valid_on_time_attestation(
        spec,
        state,
        index=committee_index,
        shard_transition=shard_transitions[shard],
        signed=True,
    )
    block = build_empty_block(spec, state, slot=state.slot + 1)
    block.body.attestations = [attestation]
    block.body.shard_transitions = shard_transitions

    _, _, _ = run_beacon_block(spec, state, block)

    transition_to(
        spec, state, state.slot + spec.SLOTS_PER_EPOCH *
        (spec.EPOCHS_PER_CUSTODY_PERIOD - 1))

    block = build_empty_block(spec, state, slot=state.slot + 1)
    custody_slashing = get_valid_custody_slashing(spec, state, attestation,
                                                  shard_transitions[shard],
                                                  custody_secret,
                                                  slashable_body)
    block.body.custody_slashings = [custody_slashing]

    yield from run_beacon_block(spec, state, block)
예제 #25
0
def test_custody_slashing(spec, state):
    transition_to_valid_shard_slot(spec, state)

    # Build shard block
    shard = 0
    committee_index = get_committee_index_of_shard(spec, state, state.slot,
                                                   shard)
    # Create slashable shard block body
    validator_index = spec.get_beacon_committee(state, state.slot,
                                                committee_index)[0]
    custody_secret = spec.get_custody_secret(
        state,
        validator_index,
        privkeys[validator_index],
        spec.get_current_epoch(state),
    )
    slashable_body = get_custody_slashable_test_vector(spec,
                                                       custody_secret,
                                                       length=100,
                                                       slashable=True)
    shard_block = build_shard_block(spec,
                                    state,
                                    shard,
                                    body=slashable_body,
                                    slot=state.slot,
                                    signed=True)
    shard_block_dict: Dict[spec.Shard, Sequence[spec.SignedShardBlock]] = {
        shard: [shard_block]
    }
    shard_transitions = get_shard_transitions(spec, state, shard_block_dict)

    attestation = get_valid_on_time_attestation(
        spec,
        state,
        index=committee_index,
        shard_transition=shard_transitions[shard],
        signed=True,
    )
    block = build_empty_block(spec, state, slot=state.slot + 1)
    block.body.attestations = [attestation]
    block.body.shard_transitions = shard_transitions

    _, _, _ = run_beacon_block(spec, state, block)

    transition_to(
        spec, state, state.slot + spec.SLOTS_PER_EPOCH *
        (spec.EPOCHS_PER_CUSTODY_PERIOD - 1))

    block = build_empty_block(spec, state, slot=state.slot + 1)
    custody_slashing = get_valid_custody_slashing(spec, state, attestation,
                                                  shard_transitions[shard],
                                                  custody_secret,
                                                  slashable_body)
    block.body.custody_slashings = [custody_slashing]

    yield from run_beacon_block(spec, state, block)
def test_wrong_shard_transition_root(spec, state):
    if not is_full_crosslink(spec, state):
        # Skip this test
        return

    state, shard, target_shard_slot = get_initial_env(spec,
                                                      state,
                                                      target_len_offset_slot=1)
    init_slot = state.slot

    # Create SignedShardBlock at init_slot
    shard_block = build_shard_block(spec,
                                    state,
                                    shard,
                                    slot=init_slot,
                                    body=get_sample_shard_block_body(
                                        spec, is_max=True),
                                    signed=True)

    # Transition state to target shard slot
    transition_to(spec, state, target_shard_slot)

    # Create a shard_transitions that would be included at beacon block `target_shard_slot + 1`
    shard_transitions = get_shard_transitions(spec, state,
                                              {shard: [shard_block]})
    shard_transition = shard_transitions[shard]
    wrong_shard_transition = shard_transition.copy()
    wrong_shard_transition.shard_states[
        shard].gasprice = shard_transition.shard_states[shard].gasprice + 1
    committee_index = get_committee_index_of_shard(spec, state, state.slot,
                                                   shard)
    attestation = get_valid_attestation(
        spec,
        state,
        index=committee_index,
        shard_transition=wrong_shard_transition,
        signed=True,
        on_time=True,
    )
    attestations = [attestation]

    next_slot(spec, state)

    run_attestation_processing(spec, state, attestation)

    # Check if winning root != shard_transition.hash_tree_root()
    _, winning_roots = spec.get_shard_winning_roots(state, attestations)
    assert len(winning_roots) == 1
    shard_transition = shard_transitions[shard]
    assert winning_roots[0] != shard_transition.hash_tree_root()

    yield from run_shard_transitions_processing(spec,
                                                state,
                                                shard_transitions,
                                                attestations,
                                                valid=False)
def test_validator_withdrawal_resume_after_chunk_challenge_response(spec, state):
    transition_to_valid_shard_slot(spec, state)
    transition_to(spec, state, state.slot + 1)  # Make len(offset_slots) == 1
    shard = 0
    offset_slots = spec.get_offset_slots(state, shard)
    shard_transition = get_sample_shard_transition(spec, state.slot, [2**15 // 3] * len(offset_slots))
    attestation = get_valid_on_time_attestation(spec, state, index=shard, signed=True,
                                                shard_transition=shard_transition)

    transition_to(spec, state, state.slot + spec.MIN_ATTESTATION_INCLUSION_DELAY)

    _, _, _ = run_attestation_processing(spec, state, attestation)

    validator_index = spec.get_beacon_committee(
        state,
        attestation.data.slot,
        attestation.data.index
    )[0]

    spec.initiate_validator_exit(state, validator_index)
    assert state.validators[validator_index].withdrawable_epoch < spec.FAR_FUTURE_EPOCH

    next_epoch_via_block(spec, state)

    assert state.validators[validator_index].withdrawable_epoch == spec.FAR_FUTURE_EPOCH

    while spec.get_current_epoch(state) < state.validators[validator_index].exit_epoch:
        next_epoch_via_block(spec, state)

    while (state.validators[validator_index].next_custody_secret_to_reveal
           <= spec.get_custody_period_for_validator(
               validator_index,
               state.validators[validator_index].exit_epoch - 1)):
        custody_key_reveal = get_valid_custody_key_reveal(spec, state, validator_index=validator_index)
        _, _, _ = run_custody_key_reveal_processing(spec, state, custody_key_reveal)

    next_epoch_via_block(spec, state)

    challenge = get_valid_chunk_challenge(spec, state, attestation, shard_transition)

    _, _, _ = run_chunk_challenge_processing(spec, state, challenge)

    next_epoch_via_block(spec, state)

    assert state.validators[validator_index].withdrawable_epoch == spec.FAR_FUTURE_EPOCH

    chunk_challenge_index = state.custody_chunk_challenge_index - 1
    custody_response = get_valid_custody_chunk_response(
        spec, state, challenge, chunk_challenge_index, block_length_or_custody_data=2**15 // 3)

    _, _, _ = run_custody_chunk_response_processing(spec, state, custody_response)

    yield from run_process_custody_final_updates(spec, state)

    assert state.validators[validator_index].withdrawable_epoch < spec.FAR_FUTURE_EPOCH
def test_validator_not_slashed_after_reveal(spec, state):
    transition_to(spec, state, spec.EPOCHS_PER_CUSTODY_PERIOD * spec.SLOTS_PER_EPOCH)
    custody_key_reveal = get_valid_custody_key_reveal(spec, state)

    _, _, _ = run_custody_key_reveal_processing(spec, state, custody_key_reveal)

    assert state.validators[0].slashed == 0

    transition_to(spec, state, state.slot + spec.EPOCHS_PER_CUSTODY_PERIOD * spec.SLOTS_PER_EPOCH)

    yield from run_process_challenge_deadlines(spec, state)

    assert state.validators[0].slashed == 0
예제 #29
0
def run_successful_crosslink_tests(spec, state, target_len_offset_slot):
    state, shard, target_shard_slot = get_initial_env(spec, state,
                                                      target_len_offset_slot)
    init_slot = state.slot

    # Create SignedShardBlock at init_slot
    shard_block = build_shard_block(spec,
                                    state,
                                    shard,
                                    slot=init_slot,
                                    body=get_sample_shard_block_body(
                                        spec, is_max=True),
                                    signed=True)

    # Transition state to target shard slot
    transition_to(spec, state, target_shard_slot)

    # Create a shard_transitions that would be included at beacon block `target_shard_slot + 1`
    shard_block_dict = {shard: [shard_block]}
    attestations, shard_transitions = get_attestations_and_shard_transitions(
        spec, state, shard_block_dict)

    next_slot(spec, state)

    for attestation in attestations:
        _, _, _ = run_attestation_processing(spec, state, attestation)

    _, winning_roots = spec.get_shard_winning_roots(state, attestations)
    assert len(winning_roots) == 1
    shard_transition = shard_transitions[shard]
    assert winning_roots[0] == shard_transition.hash_tree_root()

    pre_gasprice = state.shard_states[shard].gasprice
    pre_shard_states = state.shard_states.copy()
    yield from run_shard_transitions_processing(spec, state, shard_transitions,
                                                attestations)

    for index, shard_state in enumerate(state.shard_states):
        if index == shard:
            assert shard_state != pre_shard_states[index]
            assert shard_state == shard_transition.shard_states[
                len(shard_transition.shard_states) - 1]
            assert shard_state.latest_block_root == shard_block.message.hash_tree_root(
            )
            if target_len_offset_slot == 1:
                assert shard_state.gasprice > pre_gasprice
        else:
            assert shard_state == pre_shard_states[index]

    for pending_attestation in state.current_epoch_attestations:
        assert bool(pending_attestation.crosslink_success) is True
예제 #30
0
def test_eth1_vote_no_reset(spec, state):
    assert spec.EPOCHS_PER_ETH1_VOTING_PERIOD > 1
    # skip ahead to the end of the epoch
    transition_to(spec, state, spec.SLOTS_PER_EPOCH - 1)

    for i in range(state.slot + 1):  # add a vote for each skipped slot.
        state.eth1_data_votes.append(
            spec.Eth1Data(deposit_root=b'\xaa' * 32,
                          deposit_count=state.eth1_deposit_index,
                          block_hash=b'\xbb' * 32))

    yield from run_process_final_updates(spec, state)

    assert len(state.eth1_data_votes) == spec.SLOTS_PER_EPOCH