Esempio n. 1
0
def test_bad_previous_crosslink(state):
    next_epoch(state)
    attestation = get_valid_attestation(state)
    for _ in range(spec.MIN_ATTESTATION_INCLUSION_DELAY):
        next_slot(state)

    state.current_crosslinks[attestation.data.shard].epoch += 10

    pre_state, post_state = run_attestation_processing(state, attestation, False)

    return pre_state, attestation, post_state
def test_success_surround(state):
    next_epoch(state)
    state.current_justified_epoch += 1
    attester_slashing = get_valid_attester_slashing(state)

    # set attestion1 to surround attestation 2
    attester_slashing.attestation_1.data.source_epoch = attester_slashing.attestation_2.data.source_epoch - 1
    attester_slashing.attestation_1.data.slot = attester_slashing.attestation_2.data.slot + spec.SLOTS_PER_EPOCH

    pre_state, post_state = run_attester_slashing_processing(state, attester_slashing)

    return pre_state, attester_slashing, post_state
def test_success_withdrawable(state):
    next_epoch(state)

    transfer = get_valid_transfer(state)

    # withdrawable_epoch in past so can transfer
    state.validator_registry[
        transfer.sender].withdrawable_epoch = get_current_epoch(state) - 1

    pre_state, post_state = run_transfer_processing(state, transfer)

    return pre_state, transfer, post_state
Esempio n. 4
0
def test_activation(state):
    index = 0
    assert is_active_validator(state.validator_registry[index], get_current_epoch(state))

    # Mock a new deposit
    state.validator_registry[index].activation_eligibility_epoch = spec.FAR_FUTURE_EPOCH
    state.validator_registry[index].activation_epoch = spec.FAR_FUTURE_EPOCH
    state.validator_registry[index].effective_balance = spec.MAX_EFFECTIVE_BALANCE
    assert not is_active_validator(state.validator_registry[index], get_current_epoch(state))

    pre_state = deepcopy(state)

    blocks = []
    for _ in range(spec.ACTIVATION_EXIT_DELAY + 1):
        block = next_epoch(state)
        blocks.append(block)

    assert state.validator_registry[index].activation_eligibility_epoch != spec.FAR_FUTURE_EPOCH
    assert state.validator_registry[index].activation_epoch != spec.FAR_FUTURE_EPOCH
    assert is_active_validator(
        state.validator_registry[index],
        get_current_epoch(state),
    )

    return pre_state, blocks, state
Esempio n. 5
0
def test_single_crosslink_update_from_current_epoch(state):
    next_epoch(state)

    attestation = get_valid_attestation(state)

    fill_aggregate_attestation(state, attestation)
    add_attestation_to_state(state, attestation, state.slot + spec.MIN_ATTESTATION_INCLUSION_DELAY)

    assert len(state.current_epoch_attestations) == 1

    pre_state, post_state = run_process_crosslinks(state)

    shard = attestation.data.shard
    assert post_state.previous_crosslinks[shard] != post_state.current_crosslinks[shard]
    assert pre_state.current_crosslinks[shard] != post_state.current_crosslinks[shard]

    return pre_state, post_state
Esempio n. 6
0
def test_single_crosslink_update_from_previous_epoch(state):
    next_epoch(state)

    attestation = get_valid_attestation(state)

    fill_aggregate_attestation(state, attestation)
    add_attestation_to_state(state, attestation, state.slot + spec.SLOTS_PER_EPOCH)

    assert len(state.previous_epoch_attestations) == 1

    pre_state, post_state = run_process_crosslinks(state)
    crosslink_deltas = get_crosslink_deltas(state)

    shard = attestation.data.shard
    assert post_state.previous_crosslinks[shard] != post_state.current_crosslinks[shard]
    assert pre_state.current_crosslinks[shard] != post_state.current_crosslinks[shard]
    # ensure rewarded
    for index in get_crosslink_committee_for_attestation(state, attestation.data):
        assert crosslink_deltas[0][index] > 0
        assert crosslink_deltas[1][index] == 0

    return pre_state, post_state
Esempio n. 7
0
def test_ejection(state):
    index = 0
    assert is_active_validator(state.validator_registry[index], get_current_epoch(state))
    assert state.validator_registry[index].exit_epoch == spec.FAR_FUTURE_EPOCH

    # Mock an ejection
    state.validator_registry[index].effective_balance = spec.EJECTION_BALANCE

    pre_state = deepcopy(state)

    blocks = []
    for _ in range(spec.ACTIVATION_EXIT_DELAY + 1):
        block = next_epoch(state)
        blocks.append(block)

    assert state.validator_registry[index].exit_epoch != spec.FAR_FUTURE_EPOCH
    assert not is_active_validator(
        state.validator_registry[index],
        get_current_epoch(state),
    )

    return pre_state, blocks, state
Esempio n. 8
0
def test_double_late_crosslink(state):
    next_epoch(state)
    state.slot += 4

    attestation_1 = get_valid_attestation(state)
    fill_aggregate_attestation(state, attestation_1)

    # add attestation_1 in the next epoch
    next_epoch(state)
    add_attestation_to_state(state, attestation_1, state.slot + 1)

    for slot in range(spec.SLOTS_PER_EPOCH):
        attestation_2 = get_valid_attestation(state)
        if attestation_2.data.shard == attestation_1.data.shard:
            break
        next_slot(state)
    fill_aggregate_attestation(state, attestation_2)

    # add attestation_2 in the next epoch after attestation_1 has
    # already updated the relevant crosslink
    next_epoch(state)
    add_attestation_to_state(state, attestation_2, state.slot + 1)

    assert len(state.previous_epoch_attestations) == 1
    assert len(state.current_epoch_attestations) == 0

    pre_state, post_state = run_process_crosslinks(state)
    crosslink_deltas = get_crosslink_deltas(state)

    shard = attestation_2.data.shard

    # ensure that the current crosslinks were not updated by the second attestation
    assert post_state.previous_crosslinks[shard] == post_state.current_crosslinks[shard]
    # ensure no reward, only penalties for the failed crosslink
    for index in get_crosslink_committee_for_attestation(state, attestation_2.data):
        assert crosslink_deltas[0][index] == 0
        assert crosslink_deltas[1][index] > 0

    return pre_state, post_state