berachain / beacon-kit

A modular framework for building EVM consensus clients ⛵️✨
https://berachain.com
Other
82 stars 45 forks source link

chore(ssz): clean up merkleizer's usage of bytes buffer(s) #1625

Closed itsdevbear closed 1 week ago

itsdevbear commented 1 week ago

Summary by CodeRabbit

coderabbitai[bot] commented 1 week ago

[!IMPORTANT]

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The recent changes introduce intermediate and padding buffers to the merkleizer struct to improve the efficiency of buffer allocations in various merkleization methods. Buffers have also been optimized in the bytes package for reusability and allocation efficiency. Additionally, merkleizer_utils.go offers new helper functions for chunk padding, packing, and byte partitioning, while BuildParentTreeRoots is simplified for cleaner code execution.

Changes

File(s) Change Summary
mod/primitives/pkg/ssz/merkleizer.go Added intermediateBuffer and paddingBuffer to merkleizer; updated methods to use these buffers
mod/primitives/pkg/bytes/buffer.go Refined Buffer interface and comments for efficiency and clarity
mod/primitives/pkg/ssz/merkleizer_utils.go Introduced utility functions for padding chunks, packing elements, and partitioning byte slices
mod/primitives/pkg/merkle/hasher.go Simplified BuildParentTreeRoots by directly returning the result

Poem

🐰 Amid the bytes and chunks so grand,
New buffers lend a helping hand.
Merkle trees stand strong and tall,
With padding smooth, they never fall.
Efficiency reigns in code so bright,
A rabbit’s joy, all day and night. 🌟🐇


Tips ### Chat There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai): - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit .` - `Generate unit testing code for this file.` - `Open a follow-up GitHub issue for this discussion.` - Files and specific lines of code (under the "Files changed" tab): Tag `@coderabbitai` in a new review comment at the desired location with your query. Examples: - `@coderabbitai generate unit testing code for this file.` - `@coderabbitai modularize this function.` - PR comments: Tag `@coderabbitai` in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples: - `@coderabbitai generate interesting stats about this repository and render them as a table.` - `@coderabbitai show all the console.log statements in this repository.` - `@coderabbitai read src/utils.ts and generate unit testing code.` - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.` - `@coderabbitai help me debug CodeRabbit configuration file.` Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. ### CodeRabbit Commands (invoked as PR comments) - `@coderabbitai pause` to pause the reviews on a PR. - `@coderabbitai resume` to resume the paused reviews. - `@coderabbitai review` to trigger an incremental review. This is useful when automatic reviews are disabled for the repository. - `@coderabbitai full review` to do a full review from scratch and review all the files again. - `@coderabbitai summary` to regenerate the summary of the PR. - `@coderabbitai resolve` resolve all the CodeRabbit review comments. - `@coderabbitai configuration` to show the current CodeRabbit configuration for the repository. - `@coderabbitai help` to get help. Additionally, you can add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed. ### CodeRabbit Configration File (`.coderabbit.yaml`) - You can programmatically configure CodeRabbit by adding a `.coderabbit.yaml` file to the root of your repository. - Please see the [configuration documentation](https://docs.coderabbit.ai/guides/configure-coderabbit) for more information. - If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: `# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json` ### Documentation and Community - Visit our [Documentation](https://coderabbit.ai/docs) for detailed information on how to use CodeRabbit. - Join our [Discord Community](https://discord.com/invite/GsXnASn26c) to get help, request features, and share feedback. - Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.
codecov[bot] commented 1 week ago

Codecov Report

Attention: Patch coverage is 53.96825% with 29 lines in your changes missing coverage. Please review.

Project coverage is 23.02%. Comparing base (de48919) to head (b739105).

Additional details and impacted files [![Impacted file tree graph](https://app.codecov.io/gh/berachain/beacon-kit/pull/1625/graphs/tree.svg?width=650&height=150&src=pr&token=0l5iJ3ZbzV&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=berachain)](https://app.codecov.io/gh/berachain/beacon-kit/pull/1625?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=berachain) ```diff @@ Coverage Diff @@ ## main #1625 +/- ## ======================================= Coverage 23.02% 23.02% ======================================= Files 261 262 +1 Lines 11469 11469 Branches 18 18 ======================================= Hits 2641 2641 Misses 8702 8702 Partials 126 126 ``` | [Files](https://app.codecov.io/gh/berachain/beacon-kit/pull/1625?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=berachain) | Coverage Δ | | |---|---|---| | [mod/primitives/pkg/bytes/buffer.go](https://app.codecov.io/gh/berachain/beacon-kit/pull/1625?src=pr&el=tree&filepath=mod%2Fprimitives%2Fpkg%2Fbytes%2Fbuffer.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=berachain#diff-bW9kL3ByaW1pdGl2ZXMvcGtnL2J5dGVzL2J1ZmZlci5nbw==) | `100.00% <100.00%> (ø)` | | | [mod/primitives/pkg/merkle/hasher.go](https://app.codecov.io/gh/berachain/beacon-kit/pull/1625?src=pr&el=tree&filepath=mod%2Fprimitives%2Fpkg%2Fmerkle%2Fhasher.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=berachain#diff-bW9kL3ByaW1pdGl2ZXMvcGtnL21lcmtsZS9oYXNoZXIuZ28=) | `89.42% <100.00%> (-0.30%)` | :arrow_down: | | [mod/primitives/pkg/ssz/merkleizer.go](https://app.codecov.io/gh/berachain/beacon-kit/pull/1625?src=pr&el=tree&filepath=mod%2Fprimitives%2Fpkg%2Fssz%2Fmerkleizer.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=berachain#diff-bW9kL3ByaW1pdGl2ZXMvcGtnL3Nzei9tZXJrbGVpemVyLmdv) | `34.88% <53.33%> (-3.49%)` | :arrow_down: | | [mod/primitives/pkg/ssz/merkleizer\_utils.go](https://app.codecov.io/gh/berachain/beacon-kit/pull/1625?src=pr&el=tree&filepath=mod%2Fprimitives%2Fpkg%2Fssz%2Fmerkleizer_utils.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=berachain#diff-bW9kL3ByaW1pdGl2ZXMvcGtnL3Nzei9tZXJrbGVpemVyX3V0aWxzLmdv) | `52.17% <52.17%> (ø)` | |
calbera commented 1 week ago

general golang test for appending slices from 2 different buffers that we currently do here (TODO is to remove this in favor of the same underlying buffer).

func main() {
    a := make([][32]byte, 10)
    b := make([][32]byte, 10)

    for i := range a {
        a[i][0] = byte(i)
    }

    for i := range b {
        b[i][0] = byte(i + 10)
    }

    fmt.Println("Before append:")
    fmt.Println("a:", a) // outputs: "a: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
    fmt.Println("b:", b) // outputs: "b: [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]"

    newSlice := append(a[0:5], b[0:3]...)

    fmt.Println("After append:")
    fmt.Println("a:", a) // outputs: "a: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
    fmt.Println("b:", b) // outputs: "b: [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]"
    fmt.Println("newSlice:", newSlice) // outputs: "newSlice: [0, 1, 2, 3, 4, 10, 11, 12]"
}