Skip to content

[wip] un-utils#167

Closed
spiral-ladder wants to merge 9 commits intomainfrom
bing/un-utils
Closed

[wip] un-utils#167
spiral-ladder wants to merge 9 commits intomainfrom
bing/un-utils

Conversation

@spiral-ladder
Copy link
Copy Markdown
Collaborator

opening a draft PR first while i work on this

@spiral-ladder spiral-ladder self-assigned this Jan 14, 2026
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @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

  • Utility Module Refactoring: Several general-purpose utility modules previously located in src/state_transition/utils/ have been reorganized and moved to more specific, higher-level directories or integrated directly into their primary consuming modules. This includes EpochShuffling, RootCache, validator.zig, and penalties.zig.
  • Standard Library SHA256 Adoption: The custom sha256.zig utility file has been removed, and all its usages across the codebase have been replaced with the standard Zig std.crypto.hash.sha2.Sha256 module, streamlining cryptographic operations.
  • Epoch Shuffling and Root Caching Centralization: Dedicated modules for EpochShuffling (now at src/state_transition/EpochShuffling.zig) and RootCache (now at src/state_transition/cache/RootCache.zig) have been created, centralizing their respective logic and improving modularity.
  • Proposer Lookahead Integration: The initializeProposerLookahead function has been moved from its standalone utility file (src/state_transition/utils/process_proposer_lookahead.zig) directly into src/state_transition/slot/upgrade_state_to_fulu.zig, indicating a tighter coupling with the Fulu upgrade process.

🧠 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 Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.zig module. These should be addressed to ensure robustness.
  • Style Guide Adherence: There are instances of usize being used for counts and indices, which goes against the repository's style guide preference for explicitly-sized integers.
  • Code Clarity: A comment in EpochShuffling.zig could be improved for clarity.
  • Testing: The new RootCache.zig module is missing unit tests.

Overall, this is a positive refactoring. Addressing the identified issues will further improve the code quality.

Comment on lines +41 to +54
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;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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;

Comment on lines +81 to +90
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;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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
  1. Use explicitly-sized types like u32 for everything, avoid architecture-specific usize. (link)

}
self.allocator.free(self.active_indices);
self.allocator.free(self.shuffling);
// no need to free `commitees` because it's stack allocation
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

I see a TODO for adding unit tests. It's important to add tests for this new cache component to ensure its correctness and prevent future regressions.

…+ some

changes

- rename `innerShuffleList` to `InnerShuffleList`
- shove `ShufflingManager` into `InnerShuffleList`
- shove `InnerShuffleList` into `EpochShuffling` namespace since it's
  only used there
@spiral-ladder spiral-ladder mentioned this pull request Jan 15, 2026
9 tasks
@spiral-ladder
Copy link
Copy Markdown
Collaborator Author

Closing this for now since it's stale and we already have a tracking issue: #169

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant