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 <281165273grape@gmail.com>
  • Loading branch information
GrapeBaBa committed Nov 5, 2024
1 parent 4d5ad70 commit da1dcb2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/consensus/helpers/validator.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const epoch_helper = @import("../../consensus/helpers/epoch.zig");
const shuffle_helper = @import("../../consensus/helpers/shuffle.zig");
const balance_helper = @import("../../consensus/helpers/balance.zig");
const committee_helper = @import("../../consensus/helpers/committee.zig");
const finality_helper = @import("../../consensus/helpers/finality.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.
Expand Down Expand Up @@ -555,6 +556,44 @@ pub fn getEligibleValidatorIndices(state: *consensus.BeaconState, allocator: std
return eligible.toOwnedSlice();
}

pub fn processInactivityUpdates(state: *consensus.BeaconState, allocator: std.mem.Allocator) !void {
// Skip the genesis epoch as score updates are based on the previous epoch participation
if (epoch_helper.getCurrentEpoch(state) == constants.GENESIS_EPOCH) {
return;
}

const participating_indices = try getUnslashedParticipatingIndices(state, constants.TIMELY_TARGET_FLAG_INDEX, epoch_helper.getPreviousEpoch(state), allocator);
defer allocator.free(participating_indices);

const eligible_indices = try getEligibleValidatorIndices(state, allocator);
defer allocator.free(eligible_indices);

for (eligible_indices) |index| {
// Increase the inactivity score of inactive validators
if (std.mem.containsAtLeast(primitives.ValidatorIndex, participating_indices, 1, &[_]primitives.ValidatorIndex{index})) {
state.inactivityScores()[index] -= @min(1, state.inactivityScores()[index]);
} else {
state.inactivityScores()[index] += configs.ActiveConfig.get().INACTIVITY_SCORE_BIAS;
}
// Decrease the inactivity score of all eligible validators during a leak-free epoch
if (!finality_helper.isInInactivityLeak(state)) {
state.inactivityScores()[index] -= @min(configs.ActiveConfig.get().INACTIVITY_SCORE_RECOVERY_RATE, state.inactivityScores()[index]);
}
}
}

pub fn getBaseReward(state: *const consensus.BeaconState, index: primitives.ValidatorIndex, allocator: std.mem.Allocator) !primitives.Gwei {
const increments = state.validators()[index].effective_balance / preset.ActivePreset.get().EFFECTIVE_BALANCE_INCREMENT;
const base_reward_per_increment = try getBaseRewardPerIncrement(state, allocator);
return increments * base_reward_per_increment;
}

pub fn getBaseRewardPerIncrement(state: *const consensus.BeaconState, allocator: std.mem.Allocator) !primitives.Gwei {
const total_balance = try balance_helper.getTotalActiveBalance(state, allocator);
const sqrt_balance = std.math.sqrt(total_balance);
return @as(primitives.Gwei, @divFloor(preset.ActivePreset.get().EFFECTIVE_BALANCE_INCREMENT * preset.ActivePreset.get().BASE_REWARD_FACTOR, sqrt_balance));
}

test "test getBalanceChurnLimit" {
preset.ActivePreset.set(preset.Presets.minimal);
defer preset.ActivePreset.reset();
Expand Down
14 changes: 14 additions & 0 deletions src/primitives/utils.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("std");
const constants = @import("constants.zig");
const testing = std.testing;

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

pub fn integerSquareroot(n: u64) u64 {
if (n == std.math.maxInt(u64)) {
return constants.UINT64_MAX_SQRT; // UINT64_MAX_SQRT
}
var x = n;
var y = (x + 1) / 2;
while (y < x) {
x = y;
y = (x + n / x) / 2;
}
return x;
}

test "ceilLog2 with valid inputs" {
try testing.expectEqual(@as(u64, 0), ceilLog2(1));
try testing.expectEqual(@as(u64, 1), ceilLog2(2));
Expand Down

0 comments on commit da1dcb2

Please sign in to comment.