Skip to content

Commit da1dcb2

Browse files
committed
feat:Add more helper
Signed-off-by: Chen Kai <281165273grape@gmail.com>
1 parent 4d5ad70 commit da1dcb2

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/consensus/helpers/validator.zig

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const epoch_helper = @import("../../consensus/helpers/epoch.zig");
1111
const shuffle_helper = @import("../../consensus/helpers/shuffle.zig");
1212
const balance_helper = @import("../../consensus/helpers/balance.zig");
1313
const committee_helper = @import("../../consensus/helpers/committee.zig");
14+
const finality_helper = @import("../../consensus/helpers/finality.zig");
1415

1516
/// Check if a validator is active at a given epoch.
1617
/// 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.
@@ -555,6 +556,44 @@ pub fn getEligibleValidatorIndices(state: *consensus.BeaconState, allocator: std
555556
return eligible.toOwnedSlice();
556557
}
557558

559+
pub fn processInactivityUpdates(state: *consensus.BeaconState, allocator: std.mem.Allocator) !void {
560+
// Skip the genesis epoch as score updates are based on the previous epoch participation
561+
if (epoch_helper.getCurrentEpoch(state) == constants.GENESIS_EPOCH) {
562+
return;
563+
}
564+
565+
const participating_indices = try getUnslashedParticipatingIndices(state, constants.TIMELY_TARGET_FLAG_INDEX, epoch_helper.getPreviousEpoch(state), allocator);
566+
defer allocator.free(participating_indices);
567+
568+
const eligible_indices = try getEligibleValidatorIndices(state, allocator);
569+
defer allocator.free(eligible_indices);
570+
571+
for (eligible_indices) |index| {
572+
// Increase the inactivity score of inactive validators
573+
if (std.mem.containsAtLeast(primitives.ValidatorIndex, participating_indices, 1, &[_]primitives.ValidatorIndex{index})) {
574+
state.inactivityScores()[index] -= @min(1, state.inactivityScores()[index]);
575+
} else {
576+
state.inactivityScores()[index] += configs.ActiveConfig.get().INACTIVITY_SCORE_BIAS;
577+
}
578+
// Decrease the inactivity score of all eligible validators during a leak-free epoch
579+
if (!finality_helper.isInInactivityLeak(state)) {
580+
state.inactivityScores()[index] -= @min(configs.ActiveConfig.get().INACTIVITY_SCORE_RECOVERY_RATE, state.inactivityScores()[index]);
581+
}
582+
}
583+
}
584+
585+
pub fn getBaseReward(state: *const consensus.BeaconState, index: primitives.ValidatorIndex, allocator: std.mem.Allocator) !primitives.Gwei {
586+
const increments = state.validators()[index].effective_balance / preset.ActivePreset.get().EFFECTIVE_BALANCE_INCREMENT;
587+
const base_reward_per_increment = try getBaseRewardPerIncrement(state, allocator);
588+
return increments * base_reward_per_increment;
589+
}
590+
591+
pub fn getBaseRewardPerIncrement(state: *const consensus.BeaconState, allocator: std.mem.Allocator) !primitives.Gwei {
592+
const total_balance = try balance_helper.getTotalActiveBalance(state, allocator);
593+
const sqrt_balance = std.math.sqrt(total_balance);
594+
return @as(primitives.Gwei, @divFloor(preset.ActivePreset.get().EFFECTIVE_BALANCE_INCREMENT * preset.ActivePreset.get().BASE_REWARD_FACTOR, sqrt_balance));
595+
}
596+
558597
test "test getBalanceChurnLimit" {
559598
preset.ActivePreset.set(preset.Presets.minimal);
560599
defer preset.ActivePreset.reset();

src/primitives/utils.zig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const std = @import("std");
2+
const constants = @import("constants.zig");
23
const testing = std.testing;
34

45
pub fn ceilLog2(x: usize) u64 {
@@ -15,6 +16,19 @@ pub fn floorLog2(x: usize) u64 {
1516
return @as(u64, @bitSizeOf(u64) - @clz(x) - 1);
1617
}
1718

19+
pub fn integerSquareroot(n: u64) u64 {
20+
if (n == std.math.maxInt(u64)) {
21+
return constants.UINT64_MAX_SQRT; // UINT64_MAX_SQRT
22+
}
23+
var x = n;
24+
var y = (x + 1) / 2;
25+
while (y < x) {
26+
x = y;
27+
y = (x + n / x) / 2;
28+
}
29+
return x;
30+
}
31+
1832
test "ceilLog2 with valid inputs" {
1933
try testing.expectEqual(@as(u64, 0), ceilLog2(1));
2034
try testing.expectEqual(@as(u64, 1), ceilLog2(2));

0 commit comments

Comments
 (0)