Skip to content

Commit 603ea47

Browse files
committed
feat:add more helper
Signed-off-by: Chen Kai <281165273grape@gmail.com>
1 parent fa07e45 commit 603ea47

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const std = @import("std");
2+
const primitives = @import("../../primitives/types.zig");
3+
const consensus = @import("../../consensus/types.zig");
4+
const configs = @import("../../configs/config.zig");
5+
const constants = @import("../../primitives/constants.zig");
6+
const preset = @import("../../presets/preset.zig");
7+
const phase0 = @import("../../consensus/phase0/types.zig");
8+
const altair = @import("../../consensus/altair/types.zig");
9+
const electra = @import("../../consensus/electra/types.zig");
10+
const epoch_helper = @import("../../consensus/helpers/epoch.zig");
11+
const shuffle_helper = @import("../../consensus/helpers/shuffle.zig");
12+
const balance_helper = @import("../../consensus/helpers/balance.zig");
13+
const committee_helper = @import("../../consensus/helpers/committee.zig");
14+
15+
// pub fn weighJustificationAndFinalization(
16+
// state: *consensus.BeaconState,
17+
// total_active_balance: primitives.Gwei,
18+
// previous_epoch_target_balance: primitives.Gwei,
19+
// current_epoch_target_balance: primitives.Gwei,
20+
// ) void {
21+
// const previous_epoch = epoch_helper.getPreviousEpoch(state);
22+
// const current_epoch = epoch_helper.getCurrentEpoch(state);
23+
// const old_previous_justified_checkpoint = state.p;
24+
// const old_current_justified_checkpoint = state.current_justified_checkpoint;
25+
//
26+
// // Process justifications
27+
// state.previous_justified_checkpoint = state.current_justified_checkpoint;
28+
//
29+
// // Shift justification bits
30+
// var i: usize = constants.JUSTIFICATION_BITS_LENGTH - 1;
31+
// while (i > 0) : (i -= 1) {
32+
// state.justification_bits[i] = state.justification_bits[i - 1];
33+
// }
34+
// state.justification_bits[0] = 0;
35+
//
36+
// if (previous_epoch_target_balance * 3 >= total_active_balance * 2) {
37+
// state.current_justified_checkpoint = consensus.Checkpoint{
38+
// .epoch = previous_epoch,
39+
// .root = getBlockRoot(state, previous_epoch),
40+
// };
41+
// state.justification_bits[1] = 1;
42+
// }
43+
//
44+
// if (current_epoch_target_balance * 3 >= total_active_balance * 2) {
45+
// state.current_justified_checkpoint = Checkpoint{
46+
// .epoch = current_epoch,
47+
// .root = getBlockRoot(state, current_epoch),
48+
// };
49+
// state.justification_bits[0] = 1;
50+
// }
51+
//
52+
// // Process finalizations
53+
// const bits = state.justification_bits;
54+
//
55+
// // The 2nd/3rd/4th most recent epochs are justified, the 2nd using the 4th as source
56+
// if (allBitsSet(bits[1..4]) and old_previous_justified_checkpoint.epoch + 3 == current_epoch) {
57+
// state.finalized_checkpoint = old_previous_justified_checkpoint;
58+
// }
59+
// // The 2nd/3rd most recent epochs are justified, the 2nd using the 3rd as source
60+
// if (allBitsSet(bits[1..3]) and old_previous_justified_checkpoint.epoch + 2 == current_epoch) {
61+
// state.finalized_checkpoint = old_previous_justified_checkpoint;
62+
// }
63+
// // The 1st/2nd/3rd most recent epochs are justified, the 1st using the 3rd as source
64+
// if (allBitsSet(bits[0..3]) and old_current_justified_checkpoint.epoch + 2 == current_epoch) {
65+
// state.finalized_checkpoint = old_current_justified_checkpoint;
66+
// }
67+
// // The 1st/2nd most recent epochs are justified, the 1st using the 2nd as source
68+
// if (allBitsSet(bits[0..2]) and old_current_justified_checkpoint.epoch + 1 == current_epoch) {
69+
// state.finalized_checkpoint = old_current_justified_checkpoint;
70+
// }
71+
// }
72+
73+
fn allBitsSet(bits: []const bool) bool {
74+
return !std.mem.containsAtLeast(bool, bits, 1, false);
75+
}

src/consensus/helpers/validator.zig

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,48 @@ pub fn getPendingBalanceToWithdraw(state: *const consensus.BeaconState, validato
497497
return total;
498498
}
499499

500+
pub fn getUnslashedParticipatingIndices(
501+
state: *const consensus.BeaconState,
502+
flagIndex: u3,
503+
epoch: primitives.Epoch,
504+
allocator: std.mem.Allocator,
505+
) ![]primitives.ValidatorIndex {
506+
var result = std.AutoHashMap(primitives.ValidatorIndex, void).init(allocator);
507+
defer result.deinit();
508+
509+
const currentEpoch = epoch_helper.getCurrentEpoch(state);
510+
const previousEpoch = epoch_helper.getPreviousEpoch(state);
511+
512+
if (epoch != previousEpoch and epoch != currentEpoch) {
513+
return error.InvalidEpoch;
514+
}
515+
516+
const epochParticipation = if (epoch == currentEpoch)
517+
state.currentEpochParticipation()
518+
else
519+
state.previousEpochParticipation();
520+
521+
const activeValidatorIndices = try getActiveValidatorIndices(state, epoch, allocator);
522+
defer allocator.free(activeValidatorIndices);
523+
524+
for (activeValidatorIndices) |index| {
525+
if (primitives.hasFlag(epochParticipation[index], flagIndex) and
526+
!state.validators()[index].slashed)
527+
{
528+
try result.put(index, {});
529+
}
530+
}
531+
532+
const result_slice = try allocator.alloc(primitives.ValidatorIndex, result.count());
533+
var i: usize = 0;
534+
var iterator = result.keyIterator();
535+
while (iterator.next()) |key| {
536+
result_slice[i] = key.*;
537+
i += 1;
538+
}
539+
return result_slice;
540+
}
541+
500542
test "test getBalanceChurnLimit" {
501543
preset.ActivePreset.set(preset.Presets.minimal);
502544
defer preset.ActivePreset.reset();

src/primitives/types.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ pub fn setOrAppendList(comptime T: type, list: []T, index: ValidatorIndex, value
125125
}
126126
}
127127

128+
pub fn hasFlag(flags: ParticipationFlags, flagIndex: u3) bool {
129+
const flag = @as(ParticipationFlags, @intCast(@as(u8, 1) << flagIndex));
130+
return (flags & flag) == flag;
131+
}
132+
133+
128134
test "test ExecutionBranch length" {
129135
const ExecutionBranchLength = @typeInfo(ExecutionBranch).array.len;
130136
try std.testing.expectEqual(4, ExecutionBranchLength);

src/root.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub const bls_helper = @import("consensus/helpers/bls.zig");
2929
pub const bls = @import("./bls/bls.zig");
3030
pub const deposit_helper = @import("consensus/helpers/deposit.zig");
3131
pub const voluntary_exit_helper = @import("consensus/helpers/voluntary_exit.zig");
32+
pub const justification_finalization_helper = @import("consensus/helpers/justification_finalization.zig");
3233

3334
test {
3435
@import("std").testing.refAllDeclsRecursive(@This());

0 commit comments

Comments
 (0)