def exit_validator(state: BeaconState, index: ValidatorIndex, slots_per_epoch: int, activation_exit_delay: int) -> BeaconState: """ Exit the validator with the given ``index``. Return the updated state (immutable). """ validator = state.validator_registry[index] delayed_activation_exit_epoch = get_delayed_activation_exit_epoch( state.current_epoch(slots_per_epoch), activation_exit_delay, ) # The following updates only occur if not previous exited if validator.exit_epoch <= delayed_activation_exit_epoch: return state validator = validator.copy( exit_epoch=delayed_activation_exit_epoch, ) state = state.update_validator_registry(index, validator) return state
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
def activate_validator(state: BeaconState, index: ValidatorIndex, genesis: bool, genesis_slot: SlotNumber, entry_exit_delay: int) -> BeaconState: """ Activate the validator with the given ``index``. Return the updated state (immutable). """ # Update validator.activation_slot validator = state.validator_registry[index] validator = validator.copy( activation_slot=genesis_slot if genesis else (state.slot + entry_exit_delay)) state = state.update_validator_registry(index, validator) # Update state.validator_registry_delta_chain_tip # TODO: use tree hashing new_validator_registry_delta_chain_tip = ValidatorRegistryDeltaBlock( latest_registry_delta_root=state.validator_registry_delta_chain_tip, validator_index=index, pubkey=validator.pubkey, slot=validator.activation_slot, flag=ValidatorRegistryDeltaFlag.ACTIVATION, ).root state = state.copy(validator_registry_delta_chain_tip= new_validator_registry_delta_chain_tip, ) return state
def prepare_validator_for_withdrawal(state: BeaconState, index: ValidatorIndex) -> BeaconState: """ Set the validator with the given ``index`` with ``WITHDRAWABLE`` flag. """ validator = state.validator_registry[index] validator = validator.copy( status_flags=validator.status_flags | ValidatorStatusFlags.WITHDRAWABLE ) state = state.update_validator_registry(index, validator) return state
def initiate_validator_exit(state: BeaconState, index: ValidatorIndex) -> BeaconState: """ Initiate exit for the validator with the given ``index``. Return the updated state (immutable). """ validator = state.validator_registry[index] validator = validator.copy(initiated_exit=True, ) state = state.update_validator_registry(index, validator) return state
def initiate_validator_exit(state: BeaconState, index: ValidatorIndex) -> BeaconState: """ Initiate exit for the validator with the given ``index``. Return the updated state (immutable). """ validator = state.validator_registry[index] validator = validator.copy(status_flags=validator.status_flags | ValidatorStatusFlags.INITIATED_EXIT) state = state.update_validator_registry(index, validator) return state
def prepare_validator_for_withdrawal( state: BeaconState, index: ValidatorIndex, slots_per_epoch: int, min_validator_withdrawability_delay: int) -> BeaconState: """ Set the validator with the given ``index`` as withdrawable ``MIN_VALIDATOR_WITHDRAWABILITY_DELAY`` after the current epoch. """ validator = state.validator_registry[index].copy( withdrawable_epoch=(state.current_epoch(slots_per_epoch) + min_validator_withdrawability_delay)) state = state.update_validator_registry(index, validator) return state
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
def exit_validator(state: BeaconState, index: ValidatorIndex, entry_exit_delay: int) -> BeaconState: """ Exit the validator with the given ``index``. Return the updated state (immutable). """ validator = state.validator_registry[index] # The following updates only occur if not previous exited if validator.exit_slot <= state.slot + entry_exit_delay: return state # Update state.validator_registry_exit_count state = state.copy( validator_registry_exit_count=state.validator_registry_exit_count + 1, ) # Update validator.exit_slot and exit_slot.exit_count validator = validator.copy( exit_slot=state.slot + entry_exit_delay, exit_count=state.validator_registry_exit_count, ) state = state.update_validator_registry(index, validator) # Update state.validator_registry_delta_chain_tip # TODO: use tree hashing new_validator_registry_delta_chain_tip = ValidatorRegistryDeltaBlock( latest_registry_delta_root=state.validator_registry_delta_chain_tip, validator_index=index, pubkey=validator.pubkey, slot=validator.exit_slot, flag=ValidatorRegistryDeltaFlag.EXIT, ).root state = state.copy(validator_registry_delta_chain_tip= new_validator_registry_delta_chain_tip, ) return state