berachain / beacon-kit

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

feat(ssz): SSZTypes #1626

Closed itsdevbear closed 1 week ago

itsdevbear commented 1 week ago

Summary by CodeRabbit

coderabbitai[bot] commented 1 week ago

[!WARNING]

Review failed

The pull request is closed.

Walkthrough

This update introduces the SSZ (Simple Serialize) library for basic and composite data types in the types and ssz packages. The changes include defining SSZ-compatible types and their methods, adding merkleizers, and implementing marshaling and unmarshaling functions. Additionally, certain functions have been removed or adjusted to align with the updated SSZ implementation. Tests have been updated to verify the correctness of serialization logic.


Changes

File(s) Change Summary
.../types/basic.go Introduced SSZ types (bool, uint8, uint16, uint32, uint64, byte) with methods for SSZ operations and hash tree root computation.
.../types/constants.go Defined constants (BytesPerChunk, BytesPerLengthOffset, BitsPerByte) for SSZ serialization.
.../types/interfaces.go Introduced Merkleizer interface with methods for basic and composite types' merkleization.
.../types/vector_basic.go, .../types/vector_composite.go Added SSZ vectors for basic and composite types with methods for SSZ serialization and hash tree root computation.
.../types/list_basic.go, .../types/list_composite.go Added SSZ lists for basic and composite types with methods for SSZ serialization and hash tree root computation.
.../vector.go Introduced SSZ vectors for basic and composite types, including SSZ operations and vector conversion functions.
.../vector_test.go Added test cases for SSZ vectors, including size calculations, hashing, marshaling, and unmarshaling operations.
.../serialization.go Removed redundant (un)marshal functions and focused on bit manipulation functions for improved clarity and performance.
.../index.go Renamed package from merkle to ssz and updated related imports.
.../index_test.go Renamed package references from merkle to ssz in test cases.
.../constraints.go Introduced Base, Basic, and Composite interfaces defining SSZ serialization and hash tree root calculation methods.
.../interfaces.go Introduced BaseMerkleizer, BasicMerkleizer, CompositeMerkleizer interfaces for SSZ merkleization operations for different SSZ types.

Sequence Diagram(s)

To visualize the changes, we can create a high-level sequence diagram for the SSZ serialization process of a basic type (e.g., uint32):

sequenceDiagram
    participant App as Application
    participant SSZUInt32 as SSZUInt32
    participant Merkleizer as BasicMerkleizer
    App->>SSZUInt32: Create instance
    App->>SSZUInt32: MarshalSSZ()
    SSZUInt32-->>App: Serialized bytes
    App->>Merkleizer: HashTreeRoot(SSZUInt32)
    Merkleizer-->>App: Computed Hash Root

Poem

πŸ‡ Amid the bytes and roots so bright,
SSZ forms take joyful flight.
Bits and chunks dance in array,
Vector, listβ€”they find their way.
Marshaled, hashed, they hold their ground,
In merkle's world, they're tightly bound.
A coder's heart in data's light,
The rabbit's joy takes graceful height. πŸŽ‰


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

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 70.45%. Comparing base (de48919) to head (5065a46).

:exclamation: Current head 5065a46 differs from pull request most recent head 5575df8

Please upload reports for the commit 5575df8 to get more accurate results.

Additional details and impacted files [![Impacted file tree graph](https://app.codecov.io/gh/berachain/beacon-kit/pull/1626/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/1626?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=berachain) ```diff @@ Coverage Diff @@ ## main #1626 +/- ## =========================================== + Coverage 23.02% 70.45% +47.42% =========================================== Files 261 8 -253 Lines 11469 88 -11381 Branches 18 18 =========================================== - Hits 2641 62 -2579 + Misses 8702 22 -8680 + Partials 126 4 -122 ``` [see 253 files with indirect coverage changes](https://app.codecov.io/gh/berachain/beacon-kit/pull/1626/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=berachain)
kocubinski commented 1 week ago

@itsdevbear with regard to schema definition and parsing they’re separate concerns, in schema.go one parser could be substituted for another. In other words if the beacon monolith defines fields with these types a slightly different parser is needed to get the reflection right, and that could be plugged in.

It looks like that PR could end up being part of a substitute for fastssz though which is probably good.

On the other hand, if you commit to wrapping all types in an SSZ monolithic struct maybe reflection wouldn't even be needed? The generic impl could return the metadata required to build the schema? Not sure yet.