-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Chen Kai <[email protected]>
- Loading branch information
Showing
7 changed files
with
249 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
const std = @import("std"); | ||
const primitives = @import("../../primitives/types.zig"); | ||
const consensus = @import("../../consensus/types.zig"); | ||
const configs = @import("../../configs/config.zig"); | ||
const constants = @import("../../primitives/constants.zig"); | ||
const preset = @import("../../presets/preset.zig"); | ||
const phase0 = @import("../../consensus/phase0/types.zig"); | ||
|
||
/// Check if a validator is active at a given epoch. | ||
/// A validator is active if the current epoch is greater than or equal to the validator's activation epoch and less than the validator's exit epoch. | ||
/// @param validator The validator to check. | ||
/// @param epoch The epoch to check. | ||
/// @return True if the validator is active, false otherwise. | ||
/// Spec pseudocode definition: | ||
/// | ||
/// def is_active_validator(validator: Validator, epoch: Epoch) -> bool: | ||
/// """ | ||
/// Check if ``validator`` is active. | ||
/// """ | ||
/// return validator.activation_epoch <= epoch < validator.exit_epoch | ||
pub fn isActiveValidator(validator: *const consensus.Validator, epoch: primitives.Epoch) bool { | ||
return validator.activation_epoch <= epoch and epoch < validator.exit_epoch; | ||
} | ||
|
||
/// isEligibleForActivationQueue carries out the logic for IsEligibleForActivationQueue | ||
/// Spec pseudocode definition: | ||
/// | ||
/// def is_eligible_for_activation_queue(validator: Validator) -> bool: | ||
/// """ | ||
/// Check if ``validator`` is eligible to be placed into the activation queue. | ||
/// """ | ||
/// return ( | ||
/// validator.activation_eligibility_epoch == FAR_FUTURE_EPOCH | ||
/// and validator.effective_balance == MAX_EFFECTIVE_BALANCE | ||
/// ) | ||
pub fn isEligibleForActivationQueue(validator: *const consensus.Validator) bool { | ||
return validator.activation_eligibility_epoch == constants.FAR_FUTURE_EPOCH and | ||
validator.effective_balance == preset.ActivePreset.get().MIN_ACTIVATION_BALANCE; | ||
} | ||
|
||
/// isEligibleForActivation checks if a validator is eligible for activation. | ||
/// A validator is eligible for activation if it is not yet activated and its activation eligibility epoch is less than or equal to the finalized epoch. | ||
/// @param state The beacon state. | ||
/// @param validator The validator to check. | ||
/// @return True if the validator is eligible for activation, false otherwise. | ||
/// Spec pseudocode definition: | ||
/// | ||
/// def is_eligible_for_activation(state: BeaconState, validator: Validator) -> bool: | ||
/// """ | ||
/// Check if ``validator`` is eligible for activation. | ||
/// """ | ||
/// return ( | ||
/// validator.activation_eligibility_epoch <= state.finalized_checkpoint.epoch | ||
/// and validator.activation_epoch == FAR_FUTURE_EPOCH | ||
/// ) | ||
pub fn isEligibleForActivation(state: *const consensus.BeaconState, validator: *const consensus.Validator) bool { | ||
return | ||
// Placement in queue is finalized | ||
validator.activation_eligibility_epoch <= state.finalizedCheckpointEpoch() and | ||
// Has not yet been activated | ||
validator.activation_epoch == constants.FAR_FUTURE_EPOCH; | ||
} | ||
|
||
test "test isActiveValidator" { | ||
const validator = consensus.Validator{ | ||
.pubkey = undefined, | ||
.withdrawal_credentials = undefined, | ||
.effective_balance = 0, | ||
.slashed = false, | ||
.activation_eligibility_epoch = 0, | ||
.activation_epoch = 0, | ||
.exit_epoch = 10, | ||
.withdrawable_epoch = 0, | ||
}; | ||
const epoch: primitives.Epoch = 5; | ||
const result = isActiveValidator(&validator, epoch); | ||
try std.testing.expectEqual(result, true); | ||
} | ||
|
||
test "test isEligibleForActivationQueue" { | ||
preset.ActivePreset.set(preset.Presets.mainnet); | ||
const validator = consensus.Validator{ | ||
.pubkey = undefined, | ||
.withdrawal_credentials = undefined, | ||
.effective_balance = preset.ActivePreset.get().MIN_ACTIVATION_BALANCE, | ||
.slashed = false, | ||
.activation_eligibility_epoch = constants.FAR_FUTURE_EPOCH, | ||
.activation_epoch = 0, | ||
.exit_epoch = 0, | ||
.withdrawable_epoch = 0, | ||
}; | ||
const result = isEligibleForActivationQueue(&validator); | ||
try std.testing.expectEqual(result, true); | ||
} | ||
|
||
test "test isEligibleForActivation" { | ||
const finalized_checkpoint = consensus.Checkpoint{ | ||
.epoch = 5, | ||
.root = .{0} ** 32, | ||
}; | ||
const state = consensus.BeaconState{ | ||
.phase0 = phase0.BeaconState{ | ||
.genesis_time = 0, | ||
.genesis_validators_root = undefined, | ||
.slot = 0, | ||
.fork = undefined, | ||
.block_roots = undefined, | ||
.state_roots = undefined, | ||
.historical_roots = undefined, | ||
.eth1_data = undefined, | ||
.eth1_data_votes = undefined, | ||
.eth1_deposit_index = 0, | ||
.validators = undefined, | ||
.balances = undefined, | ||
.randao_mixes = undefined, | ||
.slashings = undefined, | ||
.previous_epoch_attestations = undefined, | ||
.current_epoch_attestations = undefined, | ||
.justification_bits = undefined, | ||
.previous_justified_checkpoint = undefined, | ||
.current_justified_checkpoint = undefined, | ||
.finalized_checkpoint = @constCast(&finalized_checkpoint), | ||
.latest_block_header = undefined, | ||
}, | ||
}; | ||
|
||
const validator = consensus.Validator{ | ||
.pubkey = undefined, | ||
.withdrawal_credentials = undefined, | ||
.effective_balance = 0, | ||
.slashed = false, | ||
.activation_eligibility_epoch = 0, | ||
.activation_epoch = constants.FAR_FUTURE_EPOCH, | ||
.exit_epoch = 0, | ||
.withdrawable_epoch = 0, | ||
}; | ||
|
||
const result = isEligibleForActivation(&state, &validator); | ||
try std.testing.expectEqual(result, true); | ||
|
||
const validator2 = consensus.Validator{ | ||
.pubkey = undefined, | ||
.withdrawal_credentials = undefined, | ||
.effective_balance = 0, | ||
.slashed = false, | ||
.activation_eligibility_epoch = 10, | ||
.activation_epoch = constants.FAR_FUTURE_EPOCH, | ||
.exit_epoch = 0, | ||
.withdrawable_epoch = 0, | ||
}; | ||
|
||
const result2 = isEligibleForActivation(&state, &validator2); | ||
try std.testing.expectEqual(result2, false); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters