def test_activate_validator(is_genesis, filled_beacon_state, genesis_epoch,
                            epoch_length, entry_exit_delay,
                            max_deposit_amount):
    validator_count = 10
    state = filled_beacon_state.copy(
        validator_registry=tuple(
            mock_validator_record(
                pubkey=index.to_bytes(48, 'big'),
                is_active=False,
            ) for index in range(validator_count)),
        validator_balances=(max_deposit_amount, ) * validator_count,
    )
    index = 1
    # Check that the `index`th validator in `state` is inactivated
    assert state.validator_registry[index].activation_epoch == FAR_FUTURE_EPOCH

    result_state = activate_validator(
        state=state,
        index=index,
        is_genesis=is_genesis,
        genesis_epoch=genesis_epoch,
        epoch_length=epoch_length,
        entry_exit_delay=entry_exit_delay,
    )

    if is_genesis:
        assert result_state.validator_registry[
            index].activation_epoch == genesis_epoch
    else:
        assert (result_state.validator_registry[index].activation_epoch ==
                get_entry_exit_effect_epoch(
                    state.current_epoch(epoch_length),
                    entry_exit_delay,
                ))
def exit_validator(state: BeaconState,
                   index: ValidatorIndex,
                   epoch_length: int,
                   entry_exit_delay: int) -> BeaconState:
    """
    Exit the validator with the given ``index``.
    Return the updated state (immutable).
    """
    validator = state.validator_registry[index]

    entry_exit_effect_epoch = get_entry_exit_effect_epoch(
        state.current_epoch(epoch_length),
        entry_exit_delay,
    )

    # The following updates only occur if not previous exited
    if validator.exit_epoch <= entry_exit_effect_epoch:
        return state

    # Update state.validator_registry_exit_count
    state = state.copy(
        validator_registry_exit_count=state.validator_registry_exit_count + 1,
    )

    # Update validator.exit_epoch and exit_epoch.exit_count
    validator = validator.copy(
        exit_epoch=state.current_epoch(epoch_length) + entry_exit_delay,
        exit_count=state.validator_registry_exit_count,
    )
    state = state.update_validator_registry(index, validator)

    return state
Exemple #3
0
def test_get_entry_exit_effect_epoch(entry_exit_delay):
    epoch = random.randint(0, FAR_FUTURE_EPOCH)
    entry_exit_effect_epoch = get_entry_exit_effect_epoch(
        epoch,
        entry_exit_delay,
    )
    assert entry_exit_effect_epoch == (epoch + 1 + entry_exit_delay)
def activate_validator(state: BeaconState, index: ValidatorIndex,
                       is_genesis: bool, genesis_epoch: EpochNumber,
                       epoch_length: int,
                       entry_exit_delay: int) -> BeaconState:
    """
    Activate the validator with the given ``index``.
    Return the updated state (immutable).
    """
    # Update validator.activation_epoch
    validator = state.validator_registry[index].copy(
        activation_epoch=genesis_epoch
        if is_genesis else get_entry_exit_effect_epoch(
            state.current_epoch(epoch_length),
            entry_exit_delay,
        ))
    state = state.update_validator_registry(index, validator)

    return state