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(types): Move deposits to new ssz lib #1810

Merged
merged 2 commits into from
Jul 29, 2024
Merged

feat(types): Move deposits to new ssz lib #1810

merged 2 commits into from
Jul 29, 2024

Conversation

itsdevbear
Copy link
Member

@itsdevbear itsdevbear commented Jul 29, 2024

Summary by CodeRabbit

  • New Features

    • Introduced a new Deposits type for managing deposit entries with enhanced serialization and hashing capabilities.
    • Implemented SSZ encoding and decoding for better data handling in blockchain applications.
  • Bug Fixes

    • Simplified the error handling in the deposit hash tree root calculation, enhancing performance and efficiency.
  • Refactor

    • Removed unnecessary type alias and function definitions related to deposits, streamlining the codebase.

@itsdevbear itsdevbear requested a review from ocnc as a code owner July 29, 2024 21:40
Copy link
Contributor

coderabbitai bot commented Jul 29, 2024

Walkthrough

The recent changes streamline the handling of deposits within the consensus module. Key modifications include the removal of redundant error checking in the GetTopLevelRoots method and the introduction of a new file dedicated to deposit serialization and hashing. These enhancements simplify the code structure, improve performance, and shift focus away from the Merkle tree approach, potentially leading to a more efficient implementation for managing deposits in the blockchain context.

Changes

Files Change Summary
mod/consensus-types/pkg/types/body.go, mod/consensus-types/pkg/types/deposit.go The GetTopLevelRoots method was simplified by removing error checking for deposits' hash tree root calculation. The merkle package import and the Deposits type alias were removed, indicating a shift away from certain dependencies.
mod/consensus-types/pkg/types/deposits.go A new file was created defining a Deposits type for handling deposit serialization using SSZ encoding. Key functions for SSZ size calculation, encoding/decoding, and hash tree root computation were implemented.

Poem

In a burrow deep, where the code hops free,
Changes sprout and dance like leaves on a tree.
With deposits now streamlined, oh what a delight,
Simplified flows make the future so bright!
A joyful leap for a rabbit so spry,
Celebrating crisp code as we reach for the sky! 🐇✨


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 Configuration 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.

Copy link

codecov bot commented Jul 29, 2024

Codecov Report

Attention: Patch coverage is 50.00000% with 10 lines in your changes missing coverage. Please review.

Project coverage is 25.58%. Comparing base (f56806c) to head (68a3cf0).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1810      +/-   ##
==========================================
- Coverage   25.59%   25.58%   -0.01%     
==========================================
  Files         327      328       +1     
  Lines       14484    14488       +4     
  Branches       21       21              
==========================================
  Hits         3707     3707              
- Misses      10647    10651       +4     
  Partials      130      130              
Files Coverage Δ
mod/consensus-types/pkg/types/body.go 63.22% <100.00%> (+2.11%) ⬆️
mod/consensus-types/pkg/types/deposit.go 96.00% <ø> (-0.30%) ⬇️
mod/consensus-types/pkg/types/deposits.go 41.17% <41.17%> (ø)

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: 1

Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

Commits

Files that changed from the base of the PR and between f56806c and 1cc7a75.

Files selected for processing (3)
  • mod/consensus-types/pkg/types/body.go (1 hunks)
  • mod/consensus-types/pkg/types/deposit.go (2 hunks)
  • mod/consensus-types/pkg/types/deposits.go (1 hunks)
Additional comments not posted (19)
mod/consensus-types/pkg/types/deposits.go (3)

16-19: LGTM!

The SizeSSZ function correctly calculates the SSZ encoded size for the Deposits type.


21-35: LGTM!

The DefineSSZ function correctly defines the SSZ encoding for the Deposits type.


37-40: LGTM!

The HashTreeRoot function correctly calculates the hash tree root for the Deposits type.

mod/consensus-types/pkg/types/deposit.go (16)

Line range hint 41-48:
LGTM!

The NewDeposit function correctly initializes a Deposit struct with the provided parameters.


Line range hint 50-55:
LGTM!

The New function correctly delegates the creation of a Deposit instance to the NewDeposit function.


Line range hint 57-66:
LGTM!

The VerifySignature function correctly verifies the deposit data and signature using the provided signatureVerificationFn.


Line range hint 70-76:
LGTM!

The DefineSSZ function correctly defines the SSZ encoding for the Deposit type.


Line range hint 78-82:
LGTM!

The MarshalSSZ function correctly marshals the Deposit object to SSZ format using ssz.EncodeToBytes.


Line range hint 84-86:
LGTM!

The UnmarshalSSZ function correctly unmarshals the Deposit object from SSZ format using ssz.DecodeFromBytes.


Line range hint 88-90:
LGTM!

The SizeSSZ function correctly returns the constant DepositSize.


Line range hint 92-94:
LGTM!

The HashTreeRoot function correctly computes the Merkleization of the Deposit object using ssz.HashSequential.


Line range hint 98-104:
LGTM!

The MarshalSSZTo function correctly marshals the Deposit object into a pre-allocated byte slice.


Line range hint 106-126:
LGTM!

The HashTreeRootWith function correctly hashes the Deposit object with a hasher using the fastssz library.


Line range hint 128-130:
LGTM!

The GetTree function correctly hashes the Deposit object using fastssz.ProofTree.


Line range hint 134-136:
LGTM!

The GetAmount function correctly returns the deposit amount in gwei.


Line range hint 138-140:
LGTM!

The GetPubkey function correctly returns the public key of the validator specified in the deposit.


Line range hint 142-144:
LGTM!

The GetIndex function correctly returns the index of the deposit in the deposit contract.


Line range hint 146-148:
LGTM!

The GetSignature function correctly returns the signature of the deposit data.


Line range hint 150-152:
LGTM!

The GetWithdrawalCredentials function correctly returns the staking credentials of the deposit.

return nil, err
}

layer[3] = Deposits(b.GetDeposits()).HashTreeRoot()
Copy link
Contributor

Choose a reason for hiding this comment

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

Re-add error checking for deposits' hash tree root calculation.

The removal of error checking for the deposits' hash tree root calculation might lead to unhandled errors. Consider re-adding the error checking to ensure robustness.

-  layer[3] = Deposits(b.GetDeposits()).HashTreeRoot()
+  layer[3], err = Deposits(b.GetDeposits()).HashTreeRoot()
+  if err != nil {
+    return nil, err
+  }

Committable suggestion was skipped due to low confidence.

@itsdevbear itsdevbear merged commit e0b0239 into main Jul 29, 2024
18 checks passed
@itsdevbear itsdevbear deleted the deposits branch July 29, 2024 21:48
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: 1

Outside diff range comments (3)
mod/consensus-types/pkg/types/deposit.go (2)

Line range hint 35-77:
Remove the redundant New method.

The New method is redundant as it calls NewDeposit. Consider removing it to avoid unnecessary duplication.

-// New creates a new Deposit instance.
-func (d *Deposit) New(
-	pubkey crypto.BLSPubkey,
-	credentials WithdrawalCredentials,
-	amount math.Gwei,
-	signature crypto.BLSSignature,
-	index uint64,
-) *Deposit {
-	return NewDeposit(
-		pubkey, credentials, amount, signature, index,
-	)
-}

Line range hint 79-118:
LGTM! Consider adding comments for clarity.

The SSZ encoding functions are implemented correctly. Adding comments to DefineSSZ would improve readability.

// DefineSSZ defines the SSZ encoding for the Deposit object.
func (d *Deposit) DefineSSZ(c *ssz.Codec) {
	// Define the encoding for the Pubkey field
	ssz.DefineStaticBytes(c, &d.Pubkey)
	// Define the encoding for the Credentials field
	ssz.DefineStaticBytes(c, &d.Credentials)
	// Define the encoding for the Amount field
	ssz.DefineUint64(c, &d.Amount)
	// Define the encoding for the Signature field
	ssz.DefineStaticBytes(c, &d.Signature)
	// Define the encoding for the Index field
	ssz.DefineUint64(c, &d.Index)
}
mod/consensus-types/pkg/types/body.go (1)

Line range hint 40-315:
Re-add error checking for deposits' hash tree root calculation.

The removal of error checking for the deposits' hash tree root calculation might lead to unhandled errors. Consider re-adding the error checking to ensure robustness.

-  layer[3] = Deposits(b.GetDeposits()).HashTreeRoot()
+  layer[3], err = Deposits(b.GetDeposits()).HashTreeRoot()
+  if err != nil {
+    return nil, err
+  }
Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

Commits

Files that changed from the base of the PR and between 1cc7a75 and 68a3cf0.

Files selected for processing (3)
  • mod/consensus-types/pkg/types/body.go (3 hunks)
  • mod/consensus-types/pkg/types/deposit.go (2 hunks)
  • mod/consensus-types/pkg/types/deposits.go (1 hunks)
Additional comments not posted (8)
mod/consensus-types/pkg/types/deposits.go (2)

1-21: LGTM!

The license header and package declaration are standard and correct.


23-30: LGTM!

The imports and type alias are appropriate and correct.

mod/consensus-types/pkg/types/deposit.go (4)

Line range hint 1-21:
LGTM!

The license header and package declaration are standard and correct.


Line range hint 23-33:
LGTM!

The imports and constants are appropriate and correct.


Line range hint 120-153:
LGTM!

The FastSSZ functions are implemented correctly.


Line range hint 155-199:
LGTM!

The getters and setters are implemented correctly.

mod/consensus-types/pkg/types/body.go (2)

Line range hint 1-21:
LGTM!

The license header and package declaration are standard and correct.


Line range hint 23-38:
LGTM!

The imports and constants are appropriate and correct.

Comment on lines +36 to +60
// SizeSSZ returns the SSZ encoded size in bytes for the Deposits.
func (ds Deposits) SizeSSZ(bool) uint32 {
return ssz.SizeSliceOfStaticObjects(([]*Deposit)(ds))
}

// DefineSSZ defines the SSZ encoding for the Deposits object.
func (ds Deposits) DefineSSZ(c *ssz.Codec) {
c.DefineDecoder(func(*ssz.Decoder) {
ssz.DefineSliceOfStaticObjectsContent(
c, (*[]*Deposit)(&ds), constants.MaxDepositsPerBlock)
})
c.DefineEncoder(func(*ssz.Encoder) {
ssz.DefineSliceOfStaticObjectsContent(
c, (*[]*Deposit)(&ds), constants.MaxDepositsPerBlock)
})
c.DefineHasher(func(*ssz.Hasher) {
ssz.DefineSliceOfStaticObjectsOffset(
c, (*[]*Deposit)(&ds), constants.MaxDepositsPerBlock)
})
}

// HashTreeRoot returns the hash tree root of the Deposits.
func (ds Deposits) HashTreeRoot() common.Root {
return ssz.HashSequential(ds)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM! Consider adding comments for clarity.

The SSZ encoding functions are implemented correctly. Adding comments to DefineSSZ would improve readability.

// DefineSSZ defines the SSZ encoding for the Deposits object.
func (ds Deposits) DefineSSZ(c *ssz.Codec) {
	c.DefineDecoder(func(*ssz.Decoder) {
		// Define the decoding logic for Deposits
		ssz.DefineSliceOfStaticObjectsContent(
			c, (*[]*Deposit)(&ds), constants.MaxDepositsPerBlock)
	})
	c.DefineEncoder(func(*ssz.Encoder) {
		// Define the encoding logic for Deposits
		ssz.DefineSliceOfStaticObjectsContent(
			c, (*[]*Deposit)(&ds), constants.MaxDepositsPerBlock)
	})
	c.DefineHasher(func(*ssz.Hasher) {
		// Define the hashing logic for Deposits
		ssz.DefineSliceOfStaticObjectsOffset(
			c, (*[]*Deposit)(&ds), constants.MaxDepositsPerBlock)
	})
}

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