Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ssz): Cleanup Merkleizer #1668

Merged
merged 10 commits into from
Jul 1, 2024
Merged

feat(ssz): Cleanup Merkleizer #1668

merged 10 commits into from
Jul 1, 2024

Conversation

itsdevbear
Copy link
Contributor

@itsdevbear itsdevbear commented Jul 1, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced cryptographic functionality with new methods for packing and chunking data.
    • Improved merkleization support for vectors and lists, offering more robust data handling.
  • Refactor

    • Updated various method signatures and types to use new Merkleizer for better consistency and performance.
  • Chores

    • Introduced new imports to support enhanced cryptographic operations.

@itsdevbear itsdevbear requested a review from ocnc as a code owner July 1, 2024 03:13
Copy link
Contributor

coderabbitai bot commented Jul 1, 2024

Warning

Rate limit exceeded

@itsdevbear has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 14 minutes and 2 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Commits

Files that changed from the base of the PR and between 5890748 and 0b4132c.

Walkthrough

The recent changes involve updating how Merkle trees are handled within various parts of the codebase. Specifically, the merkleizer type has been renamed to Merkleizer, and its usage has been refactored across multiple files to improve consistency and functionality. This includes adjusting several method signatures to use pointers to Merkleizer, adding new Merkleization methods, and updating import statements accordingly.

Changes

Files Change Summary
mod/consensus-types/pkg/types/payload.go Updated ToHeader method to use a pointer to merkleizer.Merkleizer instead of engineprimitives.TxsMerkleizer.
mod/engine-primitives/pkg/engine-primitives/transactions.go Modified HashTreeRootWith method in Transactions type to use a pointer to merkleizer.Merkleizer.
mod/primitives/pkg/ssz/merkleizer/helpers.go Added functions for packing SSZ-marshallable elements into byte slices and partitioning byte slices into chunks.
mod/primitives/pkg/ssz/merkleizer/merkleizer.go Renamed merkleizer to Merkleizer, adjusted method receivers, and added new Merkleization methods.
mod/state-transition/pkg/core/state_processor.go Replaced engineprimitives.TxsMerkleizer with *merkleizer.Merkleizer for the txsMerkleizer field in StateProcessor struct.
mod/state-transition/pkg/core/types.go Updated ToHeader method in ExecutionPayload type to use *merkleizer.Merkleizer for txsMerkleizer.

Poem

In code where bytes do weave and spin,
The trees of Merkle, strong within,
With Merkleizer now we dance,
Through transactions framed by chance.
New methods found, new paths unfurled,
In digital realms, a brighter world.
- CodeRabbit 🌟


Tip

Early access features: enabled

We are currently testing the following features in early access:

  • OpenAI gpt-4o model for code reviews and chat: OpenAI claims that this model is better at understanding and generating code than the previous models. We seek your feedback over the next few weeks before making it generally available.

Note:

  • You can enable or disable early access features from the CodeRabbit UI or by updating the CodeRabbit configuration file.
  • Please join our Discord Community to provide feedback and report issues.
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • 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 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 for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@itsdevbear itsdevbear enabled auto-merge (squash) July 1, 2024 03:15
Copy link

codecov bot commented Jul 1, 2024

Codecov Report

Attention: Patch coverage is 8.82353% with 124 lines in your changes missing coverage. Please review.

Project coverage is 22.31%. Comparing base (f2087fd) to head (0b4132c).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1668      +/-   ##
==========================================
- Coverage   22.70%   22.31%   -0.39%     
==========================================
  Files         270      265       -5     
  Lines       11804    11687     -117     
  Branches       18       18              
==========================================
- Hits         2680     2608      -72     
+ Misses       9002     8964      -38     
+ Partials      122      115       -7     
Files Coverage Δ
mod/consensus-types/pkg/types/payload.go 92.92% <ø> (ø)
...e-primitives/pkg/engine-primitives/transactions.go 81.25% <ø> (ø)
mod/primitives/pkg/merkle/hasher.go 93.93% <100.00%> (ø)
mod/primitives/pkg/merkle/tree.go 77.34% <100.00%> (+1.58%) ⬆️
mod/primitives/pkg/ssz/tree/index.go 18.69% <ø> (ø)
mod/primitives/pkg/ssz/tree/tree.go 0.00% <ø> (ø)
mod/state-transition/pkg/core/state_processor.go 0.00% <ø> (ø)
mod/primitives/pkg/ssz/vector.go 22.72% <27.77%> (-19.28%) ⬇️
mod/primitives/pkg/ssz/list.go 0.00% <0.00%> (ø)
mod/primitives/pkg/ssz/merkleizer/helpers.go 0.00% <0.00%> (ø)
... and 1 more

... and 1 file with indirect coverage changes

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 5

Outside diff range comments (1)
mod/consensus-types/pkg/types/payload.go (1)

Line range hint 71-73: Consider error handling improvements.

The error group context is ignored in the goroutines. Consider passing the context to the goroutines and handling context cancellation.

g.Go(func() error {
	select {
	case <-ctx.Done():
		return ctx.Err()
	default:
		var txsRootErr error
		txsRoot, txsRootErr = engineprimitives.Transactions(
			e.GetTransactions(),
		).HashTreeRootWith(txsMerkleizer)
		return txsRootErr
	}
})

g.Go(func() error {
	select {
	case <-ctx.Done():
		return ctx.Err()
	default:
		var withdrawalsRootErr error
		wds := ssz.ListFromElements(
			maxWithdrawalsPerPayload,
			e.GetWithdrawals()...)
		withdrawalsRoot, withdrawalsRootErr = wds.HashTreeRoot()
		return withdrawalsRootErr
	}
})
Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

Commits

Files that changed from the base of the PR and between f2087fd and 5890748.

Files selected for processing (6)
  • mod/consensus-types/pkg/types/payload.go (2 hunks)
  • mod/engine-primitives/pkg/engine-primitives/transactions.go (1 hunks)
  • mod/primitives/pkg/ssz/merkleizer/helpers.go (2 hunks)
  • mod/primitives/pkg/ssz/merkleizer/merkleizer.go (3 hunks)
  • mod/state-transition/pkg/core/state_processor.go (2 hunks)
  • mod/state-transition/pkg/core/types.go (2 hunks)
Additional comments not posted (4)
mod/engine-primitives/pkg/engine-primitives/transactions.go (1)

52-52: Ensure proper error handling.

The HashTreeRootWith method relies on the merkleizer.MerkleizeByteSlice and merkleizer.MerkleizeListComposite methods. Ensure these methods handle errors correctly and propagate them as needed.

Verification successful

Ensure proper error handling.

The MerkleizeByteSlice method relies on the Merkleize method, which correctly handles and propagates errors. Additionally, the MerkleizeVectorCompositeOrContainer method also handles errors appropriately.

  • MerkleizeByteSlice method calls Merkleize, which checks for errors and returns them as needed.
  • MerkleizeVectorCompositeOrContainer method directly handles errors and returns them properly.

Locations Reviewed:

  • mod/primitives/pkg/ssz/merkleizer/merkleizer.go (Lines 62-67, 89-104, 177-224)
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify error handling in MerkleizeByteSlice and MerkleizeListComposite methods.

# Test: Search for the method implementations. Expect: Correct error handling.
ast-grep --lang go --pattern $'func (m *Merkleizer[RootT, T]) MerkleizeByteSlice($_) (RootT, error) {
  $$$
  if err != nil {
    return RootT{}, err
  }
  $$$
}' && ast-grep --lang go --pattern $'func (m *Merkleizer[RootT, T]) MerkleizeListComposite($_, $_) (RootT, error) {
  $$$
  if err != nil {
    return RootT{}, err
  }
  $$$
}'

Length of output: 335



Script:

#!/bin/bash
# Description: List all methods in the Merkleizer type and review their implementations for error handling.

# Step 1: Find the file containing the Merkleizer type definition.
merkleizer_file=$(fd -t f .go | xargs grep -l 'type Merkleizer')

# Step 2: List all methods associated with the Merkleizer type.
ast-grep --lang go --pattern 'func (m *Merkleizer[RootT, T]) $_($_) ($_, error) {
  $$$
}' $merkleizer_file

Length of output: 3082



Script:

#!/bin/bash
# Description: Verify error handling in the Merkleize method.

# Step 1: Find the file containing the Merkleizer type definition.
merkleizer_file=$(fd -t f .go | xargs grep -l 'type Merkleizer')

# Step 2: Search for the Merkleize method implementation.
ast-grep --lang go --pattern 'func (m *Merkleizer[RootT, T]) Merkleize($_, $_) (RootT, error) {
  $$$
}' $merkleizer_file

Length of output: 4048

mod/state-transition/pkg/core/types.go (1)

178-178: Ensure proper error handling in the ToHeader method.

The ToHeader method relies on the merkleizer.Merkleizer type. Ensure that this method handles errors correctly and propagates them as needed.

mod/consensus-types/pkg/types/payload.go (1)

Line range hint 65-70: Ensure proper initialization and usage of the new Merkleizer type.

The function now accepts a pointer to merkleizer.Merkleizer. Ensure that the Merkleizer is properly initialized and used throughout the codebase.

Verification successful

Line range hint 65-70: Proper initialization and usage of the new Merkleizer type confirmed.

The Merkleizer type is properly initialized and used throughout the codebase, including within the ToHeader function in mod/consensus-types/pkg/types/payload.go.

  • mod/consensus-types/pkg/types/payload.go
  • mod/engine-primitives/pkg/engine-primitives/transactions.go
  • mod/state-transition/pkg/core/state_processor.go
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all instances of `Merkleizer` initialization and usage.

# Test: Search for the `Merkleizer` initialization. Expect: Proper initialization.
rg --type python $'merkleizer.New'

Length of output: 64



Script:

#!/bin/bash
# Description: Verify all instances of `Merkleizer` initialization and usage.

# Test: Search for the `Merkleizer` initialization. Expect: Proper initialization.
rg --type go 'merkleizer.New'

# Test: Search for the usage of `Merkleizer`. Expect: Proper usage.
rg --type go 'merkleizer.Merkleizer'

Length of output: 1701

mod/state-transition/pkg/core/state_processor.go (1)

80-80: Ensure proper initialization and usage of the new Merkleizer type.

The StateProcessor struct now uses a pointer to merkleizer.Merkleizer. Ensure that the Merkleizer is properly initialized and used throughout the codebase.

Verification successful

Ensure proper initialization and usage of the new Merkleizer type.

The Merkleizer is properly initialized and used throughout the codebase. Verified instances include state_processor.go, state_processor_genesis.go, and other relevant files.

  • mod/state-transition/pkg/core/state_processor.go
  • mod/state-transition/pkg/core/state_processor_genesis.go
  • mod/primitives/pkg/ssz/vector.go
  • mod/primitives/pkg/ssz/list.go
  • mod/engine-primitives/pkg/engine-primitives/transactions.go
  • mod/consensus-types/pkg/types/deposit.go
  • mod/consensus-types/pkg/types/body.go
  • mod/cli/pkg/commands/genesis/root.go
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all instances of `Merkleizer` initialization and usage.

# Test: Search for the `Merkleizer` initialization. Expect: Proper initialization.
rg --type python $'merkleizer.New'

Length of output: 64



Script:

#!/bin/bash
# Description: Verify all instances of `Merkleizer` initialization and usage.

# Test: Search for the `Merkleizer` initialization. Expect: Proper initialization.
rg --type go 'merkleizer.New'

# Test: Search for the `Merkleizer` type usage. Expect: Proper usage throughout the codebase.
rg --type go 'merkleizer.Merkleizer'

Length of output: 1701

Comment on lines +51 to +77
// pack packs a list of SSZ-marshallable elements into a single byte slice.
func pack[
RootT ~[32]byte,
T interface {
MarshalSSZ() ([]byte, error)
},
](
values []T,
) ([]RootT, uint64, error) {
// pack(values): Given ordered objects of the same basic type:
// Serialize values into bytes.
// If not aligned to a multiple of BYTES_PER_CHUNK bytes,
// right-pad with zeroes to the next multiple.
// Partition the bytes into BYTES_PER_CHUNK-byte chunks.
// Return the chunks.
var packed []byte
for _, el := range values {
buf, err := el.MarshalSSZ()
if err != nil {
return nil, 0, err
}
packed = append(packed, buf...)
}

chunks, numChunks := chunkifyBytes[RootT](packed)
return chunks, numChunks, nil
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider adding unit tests for the pack function.

The pack function is a critical part of the merkleization process. Ensure it is thoroughly tested.

Do you want me to generate unit tests for the pack function or open a GitHub issue to track this task?

Comment on lines 79 to 92
// chunkifyBytes partitions a byte slice into chunks of a given length.
func chunkifyBytes[RootT ~[32]byte](input []byte) (
[]RootT, uint64,
) {
//nolint:mnd // we add 31 in order to round up the division.
numChunks := max((len(input)+31)/constants.RootLength, 1)
// TODO: figure out how to safely chunk these bytes.
chunks := make([]RootT, numChunks)
for i := range chunks {
copy(chunks[i][:], input[32*i:])
}
//#nosec:G701 // numChunks is always >= 1.
return chunks, uint64(numChunks)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Tip

Codebase Verification

Ensure safe chunking in chunkifyBytes.

The TODO comment within the chunkifyBytes function indicates that the safety of the chunking process needs to be addressed. Ensure that the chunking logic handles edge cases and potential errors.

  • File: mod/primitives/pkg/ssz/merkleizer/helpers.go
  • Lines: 79-92
Analysis chain

Ensure safe chunking in chunkifyBytes.

The TODO comment indicates that the safety of the chunking process needs to be addressed. Ensure that the chunking logic handles edge cases and potential errors.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify safety of the chunking process in `chunkifyBytes`.

# Test: Search for the `chunkifyBytes` method implementation. Expect: Correct handling of edge cases and errors.
ast-grep --lang go --pattern $'func chunkifyBytes[RootT ~[32]byte]($_) (chunks []RootT, numChunks uint64) {
  $$$
  // TODO: figure out how to safely chunk these bytes.
  $$$
}'

Length of output: 178



Script:

#!/bin/bash
# Description: Locate the `chunkifyBytes` function and review its implementation.

# Search for the `chunkifyBytes` function in the codebase.
ast-grep --lang go --pattern $'func chunkifyBytes[RootT ~[32]byte]($_) (chunks []RootT, numChunks uint64) {
  $$$
}'

Length of output: 117



Script:

#!/bin/bash
# Description: Locate the `chunkifyBytes` function using ripgrep.

# Search for the `chunkifyBytes` function in the codebase.
rg 'func chunkifyBytes'

Length of output: 122



Script:

#!/bin/bash
# Description: Display the content of helpers.go to review the implementation of the chunkifyBytes function.

# Display the content of the file containing the chunkifyBytes function.
cat mod/primitives/pkg/ssz/merkleizer/helpers.go

Length of output: 3533

Comment on lines 54 to 85
func (m *Merkleizer[RootT, T]) MerkleizeBasic(
value T,
) (RootT, error) {
return m.MerkleizeVectorBasic([]T{value})
}

// MerkleizeByteSlice hashes a byteslice by chunkifying it and returning the
// corresponding HTR as if it were a fixed vector of bytes of the given length.
func (m *merkleizer[RootT, T]) MerkleizeByteSlice(
func (m *Merkleizer[RootT, T]) MerkleizeByteSlice(
input []byte,
) (RootT, error) {
chunks, numChunks := chunkifyBytes[RootT](input)
return m.Merkleize(chunks, numChunks)
}

/* -------------------------------------------------------------------------- */
/* Vector */
/* -------------------------------------------------------------------------- */

// MerkleizeVectorBasic implements the SSZ merkleization algorithm
// for a vector of basic types.
func (m *Merkleizer[RootT, T]) MerkleizeVectorBasic(
value []T,
) (RootT, error) {
// merkleize(pack(value))
// if value is a basic object or a vector of basic objects.
packed, _, err := pack[RootT](value)
if err != nil {
return [32]byte{}, err
}
return m.Merkleize(packed)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider adding unit tests for the MerkleizeBasic and MerkleizeVectorBasic methods.

These methods are critical parts of the merkleization process. Ensure they are thoroughly tested.

Do you want me to generate unit tests for the MerkleizeBasic and MerkleizeVectorBasic methods or open a GitHub issue to track this task?

Comment on lines 110 to 136
// MerkleizeListBasic implements the SSZ merkleization algorithm for a list of
// basic types.
func (m *Merkleizer[RootT, T]) MerkleizeListBasic(
value []T,
chunkCount uint64,
) (RootT, error) {
// mix_in_length(
// merkleize(
// pack(value),
// limit=chunk_count(type),
// ),
// len(value),
// )
// if value is a list of basic objects.
packed, _, err := pack[RootT](value)
if err != nil {
return [32]byte{}, err
}

root, err := m.Merkleize(
packed, chunkCount,
)
if err != nil {
return [32]byte{}, err
}
return MixinLength(root, uint64(len(value))), nil
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider adding unit tests for the MerkleizeListBasic method.

This method is a critical part of the merkleization process. Ensure it is thoroughly tested.

Do you want me to generate unit tests for the MerkleizeListBasic method or open a GitHub issue to track this task?

Comment on lines 33 to 36
type Merkleizer[
RootT ~[32]byte, T SSZObject[RootT],
] struct {
rootHasher merkle.RootHasher[RootT]
Copy link
Contributor

Choose a reason for hiding this comment

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

Tip

Codebase Verification

Update all references to merkleizer.Merkleizer.

The Merkleizer struct has been renamed, but there are still references to merkleizer.Merkleizer in the codebase. Please update all instances to reflect the new name.

  • mod/state-transition/pkg/core/types.go
  • mod/state-transition/pkg/core/state_processor.go
  • mod/state-transition/pkg/core/state_processor_payload.go
  • mod/engine-primitives/pkg/engine-primitives/transactions.go
  • mod/consensus-types/pkg/types/payload.go
Analysis chain

Rename the Merkleizer struct.

The Merkleizer struct has been renamed from merkleizer. Ensure that all references to this type have been updated accordingly.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all references to the `Merkleizer` struct.

# Test: Search for references to the `Merkleizer` struct. Expect: Only the new name is used.
rg --type go $'Merkleizer'

Length of output: 3004



Script:

#!/bin/bash
# Description: Verify if the `merkleizer` package has been updated and if the `Merkleizer` struct was renamed.

# Test: Search for the definition of the `Merkleizer` struct in the `merkleizer` package.
rg --type go --files-with-matches 'type Merkleizer' mod/primitives/pkg/ssz/merkleizer/

Length of output: 136

@itsdevbear itsdevbear merged commit fe819a0 into main Jul 1, 2024
17 checks passed
@itsdevbear itsdevbear deleted the merkle-boys branch July 1, 2024 03:46
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