berachain / beacon-kit

A modular framework for building EVM consensus clients β›΅οΈβœ¨
https://berachain.com
Other
82 stars 45 forks source link

perf(ssz): remove bytes buffer from hasher #1637

Closed calbera closed 1 week ago

calbera commented 1 week ago

Summary by CodeRabbit

coderabbitai[bot] commented 1 week ago

[!WARNING]

Review failed

The pull request is closed.

Walkthrough

The recent changes across several files within the mod/primitives/pkg/merkle and mod/primitives/pkg/crypto packages involve a comprehensive refactor of hashing and Merkle tree functionalities. This includes restructuring types and functions, migrating functionality between packages, adding new structures like RootHasher, and optimizing hashing via parallel processing and hardware acceleration. Moreover, the changes also feature updating dependencies and improving testing with mock implementations for better verification.

Changes

Files (Grouped) Change Summary
mod/primitives/pkg/merkle/hasher.go Refactored hashing functions and types, added RootHasher and RootHashFn for efficient leaf hashing.
mod/primitives/pkg/crypto/hasher.go Moved and refactored hashing functionality from merkle to crypto, with interface adjustments.
mod/primitives/pkg/crypto/sha256.go Renamed Sum256 to Sha256 for SHA-256 checksum calculation in the crypto package.
mod/primitives/pkg/eip4844/kzg_commitment.go Updated crypto imports and sha256 usage to crypto.Sha256 in the ToVersionedHash method.
mod/primitives/pkg/ssz/merkleizer/merkleizer.go Refactored to use crypto.NewHasher and updated Merkle tree functions to utilize rootHasher.
mod/primitives/pkg/merkle/root.go Added new root construction functions and optimized parallel hashing functionalities.
mod/primitives/pkg/merkle/hasher_test.go Updated imports and initialization of hashers to use the new crypto package within test functions.
mod/primitives/pkg/merkle/proof.go Updated imports to use crypto package and adjusted hash function assignments in RootFromBranch.
mod/primitives/pkg/ssz/index.go Replaced sha256 functions with crypto functions for Merkle root calculations.
mod/primitives/pkg/crypto/mocks/hash_fn.mock.go Introduced mocked implementation for HashFn type for improved test verification.
mod/primitives/pkg/crypto/mocks/hasher.mock.go Added mock implementation for Hasher type with functions for Combi, Hash, and MixIn.
mod/primitives/pkg/merkle/zero/zero.go Modified to import crypto and use crypto.Hash in InitZeroHashes.
mod/primitives/pkg/storage/go.mod Added new indirect dependencies cpuid/v2 and sha256-simd.
mod/state-transition/pkg/core/state_processor_randao.go Updated sha256 usage to crypto.Sha256 for calculating revealHash.

Poem

In the land of code, a shift was made,
Hashers and Merkle trees got an upgrade,
With crypto at heart, new roots will sprout,
Leaves and branches, never a doubt.
Mockers came to test and play,
Ensuring every bit was in place to stay.
Cheers to the changes, oh code so grand,
Built by the rabbit's clever hand. πŸ‡πŸš€


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 71.87500% with 9 lines in your changes missing coverage. Please review.

Project coverage is 22.27%. Comparing base (00feff1) to head (011214b).

:exclamation: Current head 011214b differs from pull request most recent head 6d6a3d0

Please upload reports for the commit 6d6a3d0 to get more accurate results.

Additional details and impacted files [![Impacted file tree graph](https://app.codecov.io/gh/berachain/beacon-kit/pull/1637/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/1637?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=berachain) ```diff @@ Coverage Diff @@ ## main #1637 +/- ## =========================================== - Coverage 70.45% 22.27% -48.19% =========================================== Files 8 267 +259 Lines 88 11786 +11698 Branches 18 18 =========================================== + Hits 62 2625 +2563 - Misses 22 9032 +9010 - Partials 4 129 +125 ``` | [Files](https://app.codecov.io/gh/berachain/beacon-kit/pull/1637?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/crypto/sha256.go](https://app.codecov.io/gh/berachain/beacon-kit/pull/1637?src=pr&el=tree&filepath=mod%2Fprimitives%2Fpkg%2Fcrypto%2Fsha256.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=berachain#diff-bW9kL3ByaW1pdGl2ZXMvcGtnL2NyeXB0by9zaGEyNTYuZ28=) | `81.25% <100.00%> (ΓΈ)` | | | [mod/primitives/pkg/eip4844/kzg\_commitment.go](https://app.codecov.io/gh/berachain/beacon-kit/pull/1637?src=pr&el=tree&filepath=mod%2Fprimitives%2Fpkg%2Feip4844%2Fkzg_commitment.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=berachain#diff-bW9kL3ByaW1pdGl2ZXMvcGtnL2VpcDQ4NDQva3pnX2NvbW1pdG1lbnQuZ28=) | `100.00% <100.00%> (ΓΈ)` | | | [mod/primitives/pkg/merkle/zero/zero.go](https://app.codecov.io/gh/berachain/beacon-kit/pull/1637?src=pr&el=tree&filepath=mod%2Fprimitives%2Fpkg%2Fmerkle%2Fzero%2Fzero.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=berachain#diff-bW9kL3ByaW1pdGl2ZXMvcGtnL21lcmtsZS96ZXJvL3plcm8uZ28=) | `100.00% <100.00%> (ΓΈ)` | | | [mod/primitives/pkg/ssz/merkleizer/merkleizer.go](https://app.codecov.io/gh/berachain/beacon-kit/pull/1637?src=pr&el=tree&filepath=mod%2Fprimitives%2Fpkg%2Fssz%2Fmerkleizer%2Fmerkleizer.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=berachain#diff-bW9kL3ByaW1pdGl2ZXMvcGtnL3Nzei9tZXJrbGVpemVyL21lcmtsZWl6ZXIuZ28=) | `35.89% <100.00%> (ΓΈ)` | | | [mod/primitives/pkg/crypto/hasher.go](https://app.codecov.io/gh/berachain/beacon-kit/pull/1637?src=pr&el=tree&filepath=mod%2Fprimitives%2Fpkg%2Fcrypto%2Fhasher.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=berachain#diff-bW9kL3ByaW1pdGl2ZXMvcGtnL2NyeXB0by9oYXNoZXIuZ28=) | `68.75% <80.00%> (ΓΈ)` | | | [mod/primitives/pkg/merkle/proof.go](https://app.codecov.io/gh/berachain/beacon-kit/pull/1637?src=pr&el=tree&filepath=mod%2Fprimitives%2Fpkg%2Fmerkle%2Fproof.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=berachain#diff-bW9kL3ByaW1pdGl2ZXMvcGtnL21lcmtsZS9wcm9vZi5nbw==) | `86.95% <50.00%> (ΓΈ)` | | | [mod/primitives/pkg/merkle/tree.go](https://app.codecov.io/gh/berachain/beacon-kit/pull/1637?src=pr&el=tree&filepath=mod%2Fprimitives%2Fpkg%2Fmerkle%2Ftree.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=berachain#diff-bW9kL3ByaW1pdGl2ZXMvcGtnL21lcmtsZS90cmVlLmdv) | `75.75% <50.00%> (ΓΈ)` | | | [...tate-transition/pkg/core/state\_processor\_randao.go](https://app.codecov.io/gh/berachain/beacon-kit/pull/1637?src=pr&el=tree&filepath=mod%2Fstate-transition%2Fpkg%2Fcore%2Fstate_processor_randao.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=berachain#diff-bW9kL3N0YXRlLXRyYW5zaXRpb24vcGtnL2NvcmUvc3RhdGVfcHJvY2Vzc29yX3JhbmRhby5nbw==) | `0.00% <0.00%> (ΓΈ)` | | | [mod/primitives/pkg/merkle/hasher.go](https://app.codecov.io/gh/berachain/beacon-kit/pull/1637?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=) | `85.85% <83.33%> (ΓΈ)` | | | [mod/primitives/pkg/ssz/index.go](https://app.codecov.io/gh/berachain/beacon-kit/pull/1637?src=pr&el=tree&filepath=mod%2Fprimitives%2Fpkg%2Fssz%2Findex.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=berachain#diff-bW9kL3ByaW1pdGl2ZXMvcGtnL3Nzei9pbmRleC5nbw==) | `18.69% <0.00%> (ΓΈ)` | | ... and [249 files with indirect coverage changes](https://app.codecov.io/gh/berachain/beacon-kit/pull/1637/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=berachain)
itsdevbear commented 1 week ago

despite this making sense, it feels weird how the function needs to take two "hasher things"

calbera commented 1 week ago

@itsdevbear i can put the functions on the Hasher but it feels like coupling two things that are not related

itsdevbear commented 1 week ago

did you sync against Bartio? all gucci?