-
Notifications
You must be signed in to change notification settings - Fork 16
feat(stf): zero-alloc processSyncAggregate
#269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -126,7 +126,7 @@ pub fn BitVector(comptime _length: comptime_int) type { | |
| /// Allocates and returns an `ArrayList` of indices where the bit at the index of `self` is set to `true`. | ||
| /// | ||
| /// Caller must call `deinit` on the returned list | ||
| pub fn intersectValues( | ||
| pub fn intersectValuesAlloc( | ||
| self: *const @This(), | ||
| comptime T: type, | ||
| allocator: std.mem.Allocator, | ||
|
|
@@ -148,6 +148,31 @@ pub fn BitVector(comptime _length: comptime_int) type { | |
| } | ||
| return indices; | ||
| } | ||
|
|
||
| /// Returns a slice into `out` of values where the corresponding bit is set to `true`. | ||
| pub fn intersectValues( | ||
| self: *const @This(), | ||
| comptime T: type, | ||
| values: *const [length]T, | ||
| out: *[length]T, | ||
| ) []T { | ||
| var i: usize = 0; | ||
| for (0..byte_len) |i_byte| { | ||
| var b = self.data[i_byte]; | ||
| // Kernighan's algorithm to count the set bits instead of going through 0..8 for every byte | ||
| while (b != 0) { | ||
| const lsb: usize = @as(u8, @ctz(b)); // Get the index of least significant bit | ||
| const bit_index = i_byte * 8 + lsb; | ||
| out[i] = values[bit_index]; | ||
| i += 1; | ||
| // The `b - 1` flips the bits starting from `lsb` index | ||
| // And `&` will reset the last bit at `lsb` index | ||
| b &= b - 1; | ||
| } | ||
|
Comment on lines
+159
to
+171
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The style guide (rule 44) recommends avoiding Please consider changing them to a fixed-size type like References
|
||
| } | ||
|
|
||
| return out[0..i]; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -382,7 +407,7 @@ test "BitVectorType - intersectValues" { | |
| var values: [16]u8 = undefined; | ||
| for (0..tc.bit_len) |i| values[i] = @intCast(i); | ||
|
|
||
| var actual = try b.intersectValues(u8, allocator, &values); | ||
| var actual = try b.intersectValuesAlloc(u8, allocator, &values); | ||
| defer actual.deinit(); | ||
| try std.testing.expectEqualSlices(u8, tc.expected, actual.items); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,7 +24,6 @@ const decreaseBalance = balance_utils.decreaseBalance; | |
|
|
||
| pub fn processSyncAggregate( | ||
| comptime fork: ForkSeq, | ||
| allocator: Allocator, | ||
| config: *const BeaconConfig, | ||
| epoch_cache: *const EpochCache, | ||
| state: *BeaconState(fork), | ||
|
|
@@ -37,23 +36,23 @@ pub fn processSyncAggregate( | |
|
|
||
| // different from the spec but not sure how to get through signature verification for default/empty SyncAggregate in the spec test | ||
| if (verify_signatures) { | ||
| const participant_indices = try sync_committee_bits.intersectValues( | ||
| var participant_buf: [preset.SYNC_COMMITTEE_SIZE]ValidatorIndex = undefined; | ||
| const participant_indices = sync_committee_bits.intersectValues( | ||
| ValidatorIndex, | ||
| allocator, | ||
| committee_indices, | ||
| &participant_buf, | ||
| ); | ||
| defer participant_indices.deinit(); | ||
|
|
||
| // When there's no participation we cons ider the signature valid and just ignore it | ||
| if (participant_indices.items.len > 0) { | ||
| if (participant_indices.len > 0) { | ||
| const previous_slot = @max(try state.slot(), 1) - 1; | ||
| const root_signed = try getBlockRootAtSlot(fork, state, previous_slot); | ||
| const domain = try config.getDomain(epoch_cache.epoch, c.DOMAIN_SYNC_COMMITTEE, previous_slot); | ||
|
|
||
| const pubkeys = try allocator.alloc(bls.PublicKey, participant_indices.items.len); | ||
| defer allocator.free(pubkeys); | ||
| for (0..participant_indices.items.len) |i| { | ||
| pubkeys[i] = epoch_cache.index_to_pubkey.items[participant_indices.items[i]]; | ||
| var pubkeys_buf: [preset.SYNC_COMMITTEE_SIZE]bls.PublicKey = undefined; | ||
| const pubkeys = pubkeys_buf[0..participant_indices.len]; | ||
| for (0..participant_indices.len) |i| { | ||
| pubkeys[i] = epoch_cache.index_to_pubkey.items[participant_indices[i]]; | ||
| } | ||
|
|
||
| var signing_root: Root = undefined; | ||
|
|
@@ -123,9 +122,10 @@ pub fn getSyncCommitteeSignatureSet( | |
| ) !?AggregatedSignatureSet { | ||
| const signature = sync_aggregate.sync_committee_signature; | ||
|
|
||
| var participant_buf: [preset.SYNC_COMMITTEE_SIZE]ValidatorIndex = undefined; | ||
| const participant_indices_ = if (participant_indices) |pi| pi else blk: { | ||
| const committee_indices = @as(*const [preset.SYNC_COMMITTEE_SIZE]u64, @ptrCast(epoch_cache.current_sync_committee_indexed.get().getValidatorIndices())); | ||
| break :blk (try sync_aggregate.sync_committee_bits.intersectValues(ValidatorIndex, allocator, committee_indices)).items; | ||
| break :blk sync_aggregate.sync_committee_bits.intersectValues(ValidatorIndex, committee_indices, &participant_buf); | ||
| }; | ||
|
Comment on lines
+125
to
129
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To minimize the scope of References
|
||
| // When there's no participation we consider the signature valid and just ignore it | ||
| if (participant_indices_.len == 0) { | ||
|
|
@@ -208,7 +208,6 @@ test "process sync aggregate - sanity" { | |
|
|
||
| const res = processSyncAggregate( | ||
| .electra, | ||
| allocator, | ||
| config, | ||
| epoch_cache, | ||
| fork_state, | ||
|
|
@@ -221,7 +220,6 @@ test "process sync aggregate - sanity" { | |
| try sync_aggregate.sync_committee_bits.set(1, true); | ||
| try processSyncAggregate( | ||
| .electra, | ||
| allocator, | ||
| config, | ||
| epoch_cache, | ||
| fork_state, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the style guide (rule 51), all function arguments should be asserted. This function is missing checks for its pointer arguments (
self,values,out).Please add assertions at the beginning of the function body to ensure these pointers are not null. This improves safety by catching potential null pointer dereferences early.
References