2019-03-06 06:14:54 +00:00
|
|
|
use super::errors::{ExitInvalid as Invalid, ExitValidationError as Error};
|
2019-03-06 04:22:45 +00:00
|
|
|
use ssz::SignedRoot;
|
|
|
|
use types::*;
|
|
|
|
|
2019-03-06 05:24:56 +00:00
|
|
|
/// Indicates if an `Exit` is valid to be included in a block in the current epoch of the given
|
|
|
|
/// state.
|
2019-03-06 04:22:45 +00:00
|
|
|
///
|
2019-03-06 05:24:56 +00:00
|
|
|
/// Returns `Ok(())` if the `Exit` is valid, otherwise indicates the reason for invalidity.
|
2019-03-06 04:22:45 +00:00
|
|
|
///
|
2019-03-17 01:25:37 +00:00
|
|
|
/// Spec v0.5.0
|
2019-03-06 04:22:45 +00:00
|
|
|
pub fn verify_exit(
|
|
|
|
state: &BeaconState,
|
|
|
|
exit: &VoluntaryExit,
|
|
|
|
spec: &ChainSpec,
|
2019-03-20 04:57:41 +00:00
|
|
|
) -> Result<(), Error> {
|
|
|
|
verify_exit_parametric(state, exit, spec, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Like `verify_exit` but doesn't run checks which may become true in future states.
|
|
|
|
pub fn verify_exit_time_independent_only(
|
|
|
|
state: &BeaconState,
|
|
|
|
exit: &VoluntaryExit,
|
|
|
|
spec: &ChainSpec,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
verify_exit_parametric(state, exit, spec, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parametric version of `verify_exit` that skips some checks if `time_independent_only` is true.
|
|
|
|
fn verify_exit_parametric(
|
|
|
|
state: &BeaconState,
|
|
|
|
exit: &VoluntaryExit,
|
|
|
|
spec: &ChainSpec,
|
|
|
|
time_independent_only: bool,
|
2019-03-06 04:22:45 +00:00
|
|
|
) -> Result<(), Error> {
|
|
|
|
let validator = state
|
|
|
|
.validator_registry
|
|
|
|
.get(exit.validator_index as usize)
|
2019-03-07 05:15:38 +00:00
|
|
|
.ok_or_else(|| Error::Invalid(Invalid::ValidatorUnknown(exit.validator_index)))?;
|
2019-03-06 04:22:45 +00:00
|
|
|
|
2019-03-17 01:25:37 +00:00
|
|
|
// Verify that the validator has not yet exited.
|
2019-03-06 04:22:45 +00:00
|
|
|
verify!(
|
2019-03-17 01:25:37 +00:00
|
|
|
validator.exit_epoch == spec.far_future_epoch,
|
|
|
|
Invalid::AlreadyExited(exit.validator_index)
|
2019-03-06 04:22:45 +00:00
|
|
|
);
|
|
|
|
|
2019-03-17 01:25:37 +00:00
|
|
|
// Verify that the validator has not yet initiated.
|
|
|
|
verify!(
|
|
|
|
!validator.initiated_exit,
|
|
|
|
Invalid::AlreadyInitiatedExited(exit.validator_index)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Exits must specify an epoch when they become valid; they are not valid before then.
|
2019-03-06 04:22:45 +00:00
|
|
|
verify!(
|
2019-03-20 04:57:41 +00:00
|
|
|
time_independent_only || state.current_epoch(spec) >= exit.epoch,
|
2019-03-17 01:25:37 +00:00
|
|
|
Invalid::FutureEpoch {
|
|
|
|
state: state.current_epoch(spec),
|
|
|
|
exit: exit.epoch
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Must have been in the validator set long enough.
|
|
|
|
let lifespan = state.slot.epoch(spec.slots_per_epoch) - validator.activation_epoch;
|
|
|
|
verify!(
|
|
|
|
lifespan >= spec.persistent_committee_period,
|
|
|
|
Invalid::TooYoungToLeave {
|
|
|
|
lifespan,
|
|
|
|
expected: spec.persistent_committee_period,
|
|
|
|
}
|
2019-03-06 04:22:45 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let message = exit.signed_root();
|
|
|
|
let domain = spec.get_domain(exit.epoch, Domain::Exit, &state.fork);
|
|
|
|
|
|
|
|
verify!(
|
|
|
|
exit.signature
|
|
|
|
.verify(&message[..], domain, &validator.pubkey),
|
|
|
|
Invalid::BadSignature
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|