Skip to content

Commit

Permalink
feat:Add more helper
Browse files Browse the repository at this point in the history
Signed-off-by: Chen Kai <[email protected]>
  • Loading branch information
GrapeBaBa committed Nov 4, 2024
1 parent 857083e commit 12ec2ac
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/consensus/helpers/finality.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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");
const altair = @import("../../consensus/altair/types.zig");
const electra = @import("../../consensus/electra/types.zig");
const epoch_helper = @import("../../consensus/helpers/epoch.zig");

pub fn getFinalizationDelay(state: *const consensus.BeaconState) u64 {
return epoch_helper.getPreviousEpoch(state) - state.finalizedCheckpointEpoch();
}

pub fn isInInactivityLeak(state: *const consensus.BeaconState) bool {
return getFinalizationDelay(state) > preset.ActivePreset.get().MIN_EPOCHS_TO_INACTIVITY_PENALTY;
}
16 changes: 16 additions & 0 deletions src/consensus/helpers/validator.zig
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,22 @@ pub fn getUnslashedParticipatingIndices(
return result_slice;
}

pub fn getEligibleValidatorIndices(state: *consensus.BeaconState,allocator:std.mem.Allocator) ![]primitives.ValidatorIndex {
const previous_epoch = epoch_helper.getPreviousEpoch(state);
var eligible = std.ArrayList(primitives.ValidatorIndex).init(allocator);
defer eligible.deinit();

for (state.validators(), 0..) |v, index| {
if (isActiveValidator(&v, previous_epoch) or
(v.slashed and previous_epoch + 1 < v.withdrawable_epoch)) {
try eligible.append(@as(primitives.ValidatorIndex, index));
}
}

return eligible.toOwnedSlice();
}


test "test getBalanceChurnLimit" {
preset.ActivePreset.set(preset.Presets.minimal);
defer preset.ActivePreset.reset();
Expand Down
1 change: 1 addition & 0 deletions src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub const bls = @import("./bls/bls.zig");
pub const deposit_helper = @import("consensus/helpers/deposit.zig");
pub const voluntary_exit_helper = @import("consensus/helpers/voluntary_exit.zig");
pub const justification_finalization_helper = @import("consensus/helpers/justification_finalization.zig");
pub const finality = @import("consensus/helpers/finality.zig");

test {
@import("std").testing.refAllDeclsRecursive(@This());
Expand Down

0 comments on commit 12ec2ac

Please sign in to comment.