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

chore(codec): Remove usage of reflect #1825

Merged
merged 5 commits into from
Jul 30, 2024
Merged

chore(codec): Remove usage of reflect #1825

merged 5 commits into from
Jul 30, 2024

Conversation

itsdevbear
Copy link
Member

@itsdevbear itsdevbear commented Jul 30, 2024

Summary by CodeRabbit

  • New Features

    • Added Empty methods for multiple types (Deposit, Eth1Data, Fork, Validator, BeaconBlockHeader, and more) to simplify instantiation of empty instances.
  • Improvements

    • Simplified the Empty method in BeaconBlock, enhancing usability by removing the need for version parameters.
    • Enhanced type safety across several interfaces by introducing constraints.Empty requirements, ensuring type instances can be created in a default state.
  • Bug Fixes

    • Streamlined test cases related to the Empty method, improving clarity and reducing redundancy.

@itsdevbear itsdevbear requested a review from ocnc as a code owner July 30, 2024 23:31
Copy link
Contributor

coderabbitai bot commented Jul 30, 2024

Walkthrough

The recent changes streamline various structures in the codebase by introducing an Empty method across multiple types, providing a standard way to instantiate empty instances. This simplifies method signatures and enhances the usability of these types by eliminating unnecessary parameters and complex logic. Additionally, the introduction of type constraints improves type safety and consistency, encouraging better practices in type management throughout the code.

Changes

Files Change Summary
mod/consensus-types/pkg/types/block.go, block_test.go Simplified Empty method for BeaconBlock to remove fork version parameter; updated tests accordingly, removing panic checks for unsupported versions.
mod/consensus-types/pkg/types/deposit.go, eth1data.go, fork.go, header.go, validator.go Added Empty method to respective structs, providing a straightforward way to create new instances initialized to zero values.
mod/da/pkg/types/sidecars.go Simplified Empty method implementation in BlobSidecars, eliminating redundant assignment to the receiver.
mod/primitives/pkg/constraints/basic.go Introduced new Empty interface constraint, allowing types to implement an Empty method without parameters, enhancing generic programming capabilities.
mod/runtime/pkg/middleware/types.go, p2p/noop.go, p2p/types.go Enhanced type constraints for BeaconBlock interface to require Empty method; improved type parameterization for flexibility and type safety.
mod/storage/pkg/beacondb/encoding/ssz_value.go, kvstore.go, types.go, deposit/store.go Modified type constraints to require Empty method alongside existing constraints; improved generic type handling for KVStore, Deposit, and Validator interfaces.

Sequence Diagram(s)

sequenceDiagram
    participant A as Caller
    participant B as BeaconBlock
    participant C as Deposit
    participant D as Eth1Data

    A->>B: Empty() // New call to create empty block
    B-->>A: Returns new BeaconBlock

    A->>C: Empty() // New call to create empty deposit
    C-->>A: Returns new Deposit

    A->>D: Empty() // New call to create empty Eth1Data
    D-->>A: Returns new Eth1Data
Loading

In the meadow bright and fair,
A rabbit hops without a care,
With structures new and types so light,
It sings of changes, pure delight!
"Empty methods, oh what fun!
Code flows smoother, work's well done!"
🐇✨


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 30, 2024

Codecov Report

Attention: Patch coverage is 10.52632% with 17 lines in your changes missing coverage. Please review.

Project coverage is 24.70%. Comparing base (3d547c9) to head (f0390af).

Files Patch % Lines
mod/storage/pkg/beacondb/encoding/ssz_value.go 0.00% 3 Missing ⚠️
mod/consensus-types/pkg/types/deposit.go 0.00% 2 Missing ⚠️
mod/consensus-types/pkg/types/eth1data.go 0.00% 2 Missing ⚠️
mod/consensus-types/pkg/types/fork.go 0.00% 2 Missing ⚠️
mod/consensus-types/pkg/types/header.go 0.00% 2 Missing ⚠️
mod/consensus-types/pkg/types/validator.go 0.00% 2 Missing ⚠️
mod/runtime/pkg/p2p/noop.go 0.00% 2 Missing ⚠️
mod/da/pkg/types/sidecars.go 0.00% 1 Missing ⚠️
mod/storage/pkg/deposit/store.go 0.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1825      +/-   ##
==========================================
- Coverage   24.71%   24.70%   -0.02%     
==========================================
  Files         327      327              
  Lines       14201    14194       -7     
  Branches       19       19              
==========================================
- Hits         3510     3506       -4     
+ Misses      10569    10566       -3     
  Partials      122      122              
Files Coverage Δ
mod/consensus-types/pkg/types/block.go 84.82% <100.00%> (+0.92%) ⬆️
mod/storage/pkg/beacondb/kvstore.go 0.00% <ø> (ø)
mod/da/pkg/types/sidecars.go 23.52% <0.00%> (+0.45%) ⬆️
mod/storage/pkg/deposit/store.go 0.00% <0.00%> (ø)
mod/consensus-types/pkg/types/deposit.go 93.50% <0.00%> (-2.50%) ⬇️
mod/consensus-types/pkg/types/eth1data.go 82.60% <0.00%> (-3.76%) ⬇️
mod/consensus-types/pkg/types/fork.go 81.39% <0.00%> (-3.98%) ⬇️
mod/consensus-types/pkg/types/header.go 75.64% <0.00%> (-2.00%) ⬇️
mod/consensus-types/pkg/types/validator.go 92.24% <0.00%> (-1.62%) ⬇️
mod/runtime/pkg/p2p/noop.go 0.00% <0.00%> (ø)
... and 1 more

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

Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

Commits

Files that changed from the base of the PR and between 3d547c9 and f0390af.

Files selected for processing (17)
  • mod/consensus-types/pkg/types/block.go (1 hunks)
  • mod/consensus-types/pkg/types/block_test.go (2 hunks)
  • mod/consensus-types/pkg/types/deposit.go (1 hunks)
  • mod/consensus-types/pkg/types/eth1data.go (1 hunks)
  • mod/consensus-types/pkg/types/fork.go (1 hunks)
  • mod/consensus-types/pkg/types/header.go (1 hunks)
  • mod/consensus-types/pkg/types/validator.go (1 hunks)
  • mod/da/pkg/types/sidecars.go (1 hunks)
  • mod/primitives/pkg/constraints/basic.go (1 hunks)
  • mod/runtime/pkg/middleware/types.go (1 hunks)
  • mod/runtime/pkg/p2p/noop.go (2 hunks)
  • mod/runtime/pkg/p2p/types.go (1 hunks)
  • mod/storage/pkg/beacondb/encoding/ssz_value.go (1 hunks)
  • mod/storage/pkg/beacondb/kvstore.go (2 hunks)
  • mod/storage/pkg/beacondb/types.go (1 hunks)
  • mod/storage/pkg/deposit/store.go (2 hunks)
  • mod/storage/pkg/deposit/types.go (1 hunks)
Additional comments not posted (20)
mod/storage/pkg/beacondb/types.go (1)

30-33: LGTM! Verify the impact on implementing types.

The addition of constraints.Empty[SelfT] promotes consistency and type safety. Ensure that all types used as SelfT satisfy the new constraint.

mod/runtime/pkg/p2p/noop.go (2)

32-35: Approved: Enhanced type constraints for NoopGossipHandler.

The addition of constraints.Empty[DataT] improves type safety and ensures that DataT can instantiate an empty instance.


55-56: Approved: Simplified instance creation in Request method.

Using the Empty method to create a new instance of DataT instead of reflection simplifies the code and improves type safety.

mod/primitives/pkg/constraints/basic.go (1)

45-48: Approved: Addition of Empty interface.

The Empty interface enhances type safety and expressiveness in generic programming by enforcing that types can provide a default empty instance.

mod/runtime/pkg/middleware/types.go (2)

34-39: Approved: Improved BeaconBlock interface.

Renaming the type parameter to SelfT and adding the constraints.Empty[SelfT] constraint improve clarity and type safety by ensuring that SelfT can provide a default empty instance.


39-39: Approved: Updated NewFromSSZ method signature.

Returning SelfT instead of T reinforces the self-referential nature of the interface, enhancing type safety.

mod/storage/pkg/deposit/store.go (2)

46-48: LGTM! But verify the usage of the new type parameterization.

The type parameterization change improves type safety and flexibility.

However, ensure that all usages of KVStore are updated to match the new type parameterization.


52-54: LGTM! But verify the usage of the new function signature.

The function signature change ensures consistency with the updated type parameterization.

However, ensure that all usages of NewStore are updated to match the new function signature.

Verification successful

Verified: All usages of NewStore match the new function signature.

The function signature change has been correctly applied across the codebase, ensuring consistency with the updated type parameterization.

  • mod/storage/pkg/deposit/store.go
  • mod/storage/pkg/block/store.go
  • mod/node-core/pkg/components/block_store.go
  • mod/node-core/pkg/components/deposit_store.go
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all usages of `NewStore` match the new function signature.

# Test: Search for the function usage. Expect: Only occurrences of the new function signature.
rg --type go -A 5 $'NewStore'

Length of output: 2338

mod/storage/pkg/beacondb/encoding/ssz_value.go (2)

40-43: LGTM! But verify the usage of the updated Decode method.

The method implementation change improves type safety and clarity by avoiding reflection.

However, ensure that all usages of the Decode method are updated to match the new implementation.

Verification successful

Verify the usage of the updated Decode method.

The search results did not show any external usage of the SSZValueCodec's Decode method. To ensure thorough verification, refine the search to specifically look for the usage of SSZValueCodec's Decode method across the codebase.


No external usages of the SSZValueCodec's Decode method found.

The refined search did not produce any results, indicating that the Decode method from SSZValueCodec is not currently utilized elsewhere in the codebase. The verification is complete.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all usages of the `Decode` method match the new implementation.

# Test: Search for the method usage. Expect: Only occurrences of the new implementation.
rg --type go -A 5 $'Decode'

Length of output: 24936


Script:

#!/bin/bash
# Description: Verify all usages of the `Decode` method from `SSZValueCodec` match the new implementation.

# Test: Search for the method usage. Expect: Only occurrences of the new implementation.
rg --type go 'SSZValueCodec\[.*\]\.Decode' -A 5

Length of output: 49


29-32: LGTM! But verify the usage of the new type parameterization.

The type parameterization change ensures that any type used with SSZValueCodec must provide a way to instantiate an empty value.

However, ensure that all usages of SSZValueCodec are updated to match the new type parameterization.

Verification successful

Verification Successful!

The types used with SSZValueCodec (DepositT, ForkT, Eth1DataT, ValidatorT, BeaconBlockHeaderT) correctly implement both the Empty method and the constraints.SSZMarshallable interface.

  • DepositT
  • ForkT
  • Eth1DataT
  • ValidatorT
  • BeaconBlockHeaderT

All usages of SSZValueCodec are updated to match the new type parameterization.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all usages of `SSZValueCodec` match the new type parameterization.

# Test: Search for the type usage. Expect: Only occurrences of the new type parameterization.
rg --type go -A 5 $'SSZValueCodec'

Length of output: 4886


Script:

#!/bin/bash
# Description: Verify that the types used with SSZValueCodec implement the required interfaces and methods.

# Test: Search for the definitions of the types used with SSZValueCodec.
# Expect: Each type should implement constraints.SSZMarshallable and have an Empty method.

# Types to check
types=("DepositT" "ForkT" "Eth1DataT" "ValidatorT" "BeaconBlockHeaderT")

# Search for type definitions and implementations
for type in "${types[@]}"; do
  echo "Checking type: $type"
  rg --type go "type $type" -A 10
  rg --type go "func (.*) Empty" -A 5
done

Length of output: 23993


Script:

#!/bin/bash
# Description: Verify that the types used with SSZValueCodec implement the constraints.SSZMarshallable interface.

# Test: Search for the implementations of the constraints.SSZMarshallable interface.
# Expect: Each type should implement constraints.SSZMarshallable.

# Types to check
types=("DepositT" "ForkT" "Eth1DataT" "ValidatorT" "BeaconBlockHeaderT")

# Search for interface implementations
for type in "${types[@]}"; do
  echo "Checking type: $type"
  rg --type go "type $type" -A 10
  rg --type go "func (.*) MarshalSSZ" -A 5
done

Length of output: 123133

mod/da/pkg/types/sidecars.go (1)

38-38: LGTM! But verify the usage of the updated Empty method.

The method implementation change enhances clarity and conciseness by eliminating unnecessary variable assignment.

However, ensure that all usages of the Empty method are updated to match the new implementation.

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

57-60: LGTM!

The Empty method correctly returns a new instance of Fork with default values.

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

54-57: LGTM!

The Empty method correctly returns a new instance of Eth1Data with default values.

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

142-144: LGTM!

The modification to the TestBeaconBlockEmpty function correctly aligns with the new Empty method signature and verifies that the method returns a non-nil instance of BeaconBlock.

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

74-77: LGTM! The Empty method is a useful addition.

The method provides a straightforward way to create an empty Deposit instance.

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

49-50: LGTM! The simplification of the Empty method improves usability.

The removal of the forkVersion parameter reduces complexity and potential runtime errors.

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

77-80: LGTM! The Empty method is a useful addition.

The method provides a straightforward way to create an empty BeaconBlockHeader instance.

mod/storage/pkg/beacondb/kvstore.go (2)

38-45: LGTM! Enhanced type constraints improve type safety and consistency.

The updated type constraints for BeaconBlockHeaderT, Eth1DataT, and ForkT ensure that these types are serializable and can provide an empty instance, which enhances type safety and consistency in the KVStore struct.

Also applies to: 51-55


116-123: LGTM! Enhanced type constraints improve type safety and consistency.

The updated type constraints for BeaconBlockHeaderT, Eth1DataT, and ForkT ensure that these types are serializable and can provide an empty instance, which enhances type safety and consistency in the New function.

Also applies to: 129-133

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

98-101: LGTM! The Empty method enhances usability.

The Empty method provides a straightforward way to instantiate an empty Validator, improving the usability of the Validator type.

Comment on lines +27 to 29
constraints.Empty[SelfT]
NewFromSSZ([]byte, uint32) (SelfT, error)
}
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

Verify the constraints.Empty[SelfT] constraint for BeaconBlock interfaces

The following BeaconBlock interfaces do not include the constraints.Empty[SelfT] constraint:

  • mod/storage/pkg/block/types.go
  • mod/storage/pkg/pruner/types.go
  • mod/storage/pkg/manager/types.go
  • mod/runtime/pkg/encoding/types.go
  • mod/da/pkg/store/types.go
  • mod/da/pkg/blob/types.go
  • mod/execution/pkg/deposit/types.go
  • mod/beacon/blockchain/types.go
  • mod/beacon/validator/types.go
  • mod/beacon/block_store/types.go

Please verify if these interfaces are intended to be different or if they need to be updated to include the new constraint.

Analysis chain

LGTM! Verify the impact on implementing types.

The addition of constraints.Empty[SelfT] promotes consistency and type safety. Ensure that all types implementing BeaconBlock satisfy the new constraint.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all types implementing `BeaconBlock` satisfy the new constraint.

# Test: Search for all types implementing `BeaconBlock`. Expect: All occurrences of the new constraint.
rg --type go -A 5 $'type BeaconBlock'

Length of output: 25960

Comment on lines +29 to 32
type Deposit[DepositT any] interface {
constraints.SSZMarshallable
constraints.Empty[DepositT]
GetIndex() math.U64
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

Issues Found: Types using DepositT interface{} do not satisfy the new constraint.

The addition of constraints.Empty[DepositT] promotes consistency and type safety. However, the following types using DepositT interface{} do not satisfy this new constraint and need to be updated:

  • mod/node-api/backend/mocks/deposit_store.mock.go
  • mod/node-api/backend/mocks/deposit.mock.go

Please update these types to ensure they satisfy the constraints.Empty[DepositT] constraint.

Analysis chain

LGTM! Verify the impact on implementing types.

The addition of constraints.Empty[DepositT] promotes consistency and type safety. Ensure that all types used as DepositT satisfy the new constraint.


Verify the impact on implementing types.

Ensure that all types used as DepositT in Deposit[DepositT any] satisfy the new constraints.Empty[DepositT] constraint.


Verify the impact on implementing types.

Ensure that all types used as DepositT in Deposit[DepositT any] satisfy the new constraints.Empty[DepositT] constraint. Pay special attention to types using DepositT interface{} as they might not satisfy the new constraint.


Verify the impact on implementing types.

Ensure that all types used as DepositT in Deposit[DepositT any] satisfy the new constraints.Empty[DepositT] constraint. Pay special attention to types using DepositT interface{} as they might not satisfy the new constraint.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all types used as `DepositT` satisfy the new constraint.

# Test: Search for all types implementing `Deposit`. Expect: All occurrences of the new constraint.
rg --type go -A 5 $'type Deposit'

Length of output: 10368


Script:

#!/bin/bash
# Description: Verify all types used as `DepositT` satisfy the new constraint.

# Search for all types implementing `Deposit` and `DepositStore` and check their constraints.
rg --type go -A 5 'type Deposit' | grep -E 'Deposit\[|DepositStore\[|DepositT'

Length of output: 2767


Script:

#!/bin/bash
# Description: Verify all types used as `DepositT` satisfy the new constraint.

# Search for all types implementing `Deposit` and `DepositStore` and check their constraints.
rg --type go 'type Deposit' -A 5 | grep -E 'Deposit\[|DepositStore\[|DepositT' -B 5

Length of output: 7843


Script:

#!/bin/bash
# Description: Verify all types used as `DepositT` satisfy the new constraint.

# Search for the definition and constraints of `DepositT` in the relevant files.
rg --type go 'DepositT' -A 5 -B 5

Length of output: 104078

@itsdevbear itsdevbear merged commit 053f21e into main Jul 30, 2024
18 checks passed
@itsdevbear itsdevbear deleted the remove-reflect branch July 30, 2024 23:47
itsdevbear added a commit that referenced this pull request Aug 3, 2024
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