Conversation
This is also unused in lodestar-ts
These functions are related to penalties. So might as well just name them that.
… utils/process_proposer_lookahead.zig
Summary of ChangesHello @spiral-ladder, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request focuses on a significant internal refactoring to improve the organization and maintainability of the codebase. It involves relocating various utility functions and modules from a generic 'utils' directory to more semantically appropriate locations. This restructuring aims to clarify module responsibilities, reduce dependency complexity, and enhance the overall code structure without introducing new features or altering existing functionality. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request refactors the codebase by moving several utility modules out of the utils directory into more domain-specific locations, improving code organization. The changes also involve replacing a custom sha256 wrapper with direct calls to std.crypto, which is a good simplification.
My review focuses on a few key areas:
- Memory Safety: I've identified a couple of potential memory leaks in error-handling paths within the new
EpochShuffling.zigmodule. These should be addressed to ensure robustness. - Style Guide Adherence: There are instances of
usizebeing used for counts and indices, which goes against the repository's style guide preference for explicitly-sized integers. - Code Clarity: A comment in
EpochShuffling.zigcould be improved for clarity. - Testing: The new
RootCache.zigmodule is missing unit tests.
Overall, this is a positive refactoring. Addressing the identified issues will further improve the code quality.
| const committees = try buildCommitteesFromShuffling(allocator, shuffling); | ||
|
|
||
| const epoch_shuffling_ptr = try allocator.create(EpochShuffling); | ||
| errdefer allocator.destroy(epoch_shuffling_ptr); | ||
| epoch_shuffling_ptr.* = EpochShuffling{ | ||
| .allocator = allocator, | ||
| .epoch = epoch, | ||
| .active_indices = active_indices, | ||
| .shuffling = shuffling, | ||
| .committees = committees, | ||
| .committees_per_slot = computeCommitteeCount(active_indices.len), | ||
| }; | ||
|
|
||
| return epoch_shuffling_ptr; |
There was a problem hiding this comment.
There's a potential memory leak here. If buildCommitteesFromShuffling succeeds but allocator.create(EpochShuffling) on line 43 fails, the memory allocated for committees will be leaked. You should handle the cleanup of committees in the error path. Using a catch block on the allocation is a clean way to handle this.
const committees = try buildCommitteesFromShuffling(allocator, shuffling);
const epoch_shuffling_ptr = allocator.create(EpochShuffling) catch |err| {
for (committees) |c| allocator.free(c);
return err;
};
epoch_shuffling_ptr.* = EpochShuffling{
.allocator = allocator,
.epoch = epoch,
.active_indices = active_indices,
.shuffling = shuffling,
.committees = committees,
.committees_per_slot = computeCommitteeCount(active_indices.len),
};
return epoch_shuffling_ptr;
| for (0..preset.SLOTS_PER_EPOCH) |slot| { | ||
| const slot_committees = try allocator.alloc(Committee, committees_per_slot); | ||
| for (0..committees_per_slot) |committee_index| { | ||
| const index = slot * committees_per_slot + committee_index; | ||
| const start_offset = @divFloor(active_validator_count * index, committee_count); | ||
| const end_offset = @divFloor(active_validator_count * (index + 1), committee_count); | ||
| slot_committees[committee_index] = shuffling[start_offset..end_offset]; | ||
| } | ||
| epoch_committees[slot] = slot_committees; | ||
| } |
There was a problem hiding this comment.
There's a potential memory leak in buildCommitteesFromShuffling. If allocator.alloc on line 82 fails for any slot > 0, the memory allocated for slot_committees in previous iterations will be leaked because it's already been stored in epoch_committees. An errdefer should be used to clean up these allocations on error.
| /// the internal last-level committee shared the same data with `shuffling` so don't need to free it | ||
| committees: EpochCommittees, | ||
|
|
||
| committees_per_slot: usize, |
There was a problem hiding this comment.
The style guide (line 44) recommends using explicitly-sized types and avoiding usize. This struct field and other variables and function parameters/return types in this file use usize (e.g., active_validator_count, computeCommitteeCount). Please consider changing them to an explicitly-sized integer type like u64 for consistency and to adhere to the style guide.
References
- Use explicitly-sized types like
u32for everything, avoid architecture-specificusize. (link)
| } | ||
| self.allocator.free(self.active_indices); | ||
| self.allocator.free(self.shuffling); | ||
| // no need to free `commitees` because it's stack allocation |
There was a problem hiding this comment.
This comment is a bit misleading. While the committees array itself is part of the struct and not separately allocated, its elements are slices pointing to heap-allocated memory. The for loop above correctly frees this memory. The comment might cause confusion for future readers.
// The `committees` array itself is part of the struct, but its elements are slices that point to
// heap-allocated memory, which are freed in the loop above.
| self.allocator.destroy(self); | ||
| } | ||
|
|
||
| // TODO: unit tests |
…+ some changes - rename `innerShuffleList` to `InnerShuffleList` - shove `ShufflingManager` into `InnerShuffleList` - shove `InnerShuffleList` into `EpochShuffling` namespace since it's only used there
|
Closing this for now since it's stale and we already have a tracking issue: #169 |
opening a draft PR first while i work on this