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

fix(state-transitions): Make setting of eth1 deposit index backward compatible #2202

Merged
merged 9 commits into from
Dec 3, 2024

Conversation

abi87
Copy link
Collaborator

@abi87 abi87 commented Dec 3, 2024

A proposal to replace #2201

  • backward compatility ensured
  • added logs for debugging visibility
  • unit tests use clean chain spec without fork version mess

Summary by CodeRabbit

Release Notes

  • New Features

    • Enhanced logging for deposit processing and state transitions, providing better visibility into the processes.
  • Bug Fixes

    • Improved error handling in various methods, ensuring clearer context in error messages.
    • Adjusted loop structures in the pruning method to prevent unintended behavior.
  • Documentation

    • Updated comments in test functions for better clarity on expected behaviors and outcomes.
  • Refactor

    • Removed unused variables and streamlined type definitions in test files to enhance code quality.

Copy link
Contributor

coderabbitai bot commented Dec 3, 2024

Walkthrough

The pull request introduces several modifications across various files, primarily enhancing logging and error handling in deposit processing and state management. Key changes include the addition of logging statements in methods related to block building and state processing, as well as adjustments to error handling practices. The updates streamline the code by removing unused variables and improving the clarity of comments in test cases. Overall, the changes maintain the existing logic while improving the visibility and robustness of the deposit and validator management processes.

Changes

File Path Change Summary
mod/beacon/validator/block_builder.go Added logging in buildBlockBody to track local deposits, including starting index and count. Minor code organization adjustments.
mod/state-transition/pkg/core/core_test.go Removed unused variable dummyExecutionPayload. Minor restructuring of type definitions.
mod/state-transition/pkg/core/state_processor.go Added logging in ProcessSlots for Eth1 deposit index adjustments based on conditions. Maintained existing control flow and logic.
mod/state-transition/pkg/core/state_processor_genesis_test.go Updated comments in test functions to clarify expected deposit index behavior. Adjusted assertions in tests.
mod/state-transition/pkg/core/state_processor_staking.go Enhanced deposit processing logic with new variable for current slot. Improved error handling and logging in various methods related to deposits and validators.
mod/state-transition/pkg/core/state_processor_staking_test.go Replaced BoonetChainSpecType with BetnetChainSpecType in tests. Updated BeaconBlockBody construction to include inflation withdrawal.
mod/state-transition/pkg/core/state_processor_withdrawals.go Added logging and refined error handling in processWithdrawalsDefault.
mod/state-transition/pkg/core/validation_deposits.go Refined error handling in validateGenesisDeposits and validateNonGenesisDeposits. Enhanced logging clarity.
mod/storage/pkg/deposit/store.go Changed logging levels from Info to Debug in various methods. Enhanced error handling with contextual information. Corrected loop structure in Prune.

Possibly related PRs

Suggested labels

Ready for Review

Suggested reviewers

  • itsdevbear
  • abi87

Poem

🐇 In the meadow where deposits flow,
A logger's tale begins to grow.
With every block, the numbers dance,
Enhancing clarity at every chance.
Oh, the rabbits cheer, as errors fade,
In a world of logs, our progress is made! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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 Dec 3, 2024

Codecov Report

Attention: Patch coverage is 62.50000% with 30 lines in your changes missing coverage. Please review.

Project coverage is 27.94%. Comparing base (0c10121) to head (9aeaadd).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...ate-transition/pkg/core/state_processor_staking.go 76.19% 6 Missing and 4 partials ⚠️
mod/storage/pkg/deposit/store.go 35.71% 9 Missing ⚠️
mod/beacon/validator/block_builder.go 0.00% 4 Missing ⚠️
mod/state-transition/pkg/core/state_processor.go 0.00% 4 Missing ⚠️
...transition/pkg/core/state_processor_withdrawals.go 76.92% 2 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2202      +/-   ##
==========================================
- Coverage   27.98%   27.94%   -0.04%     
==========================================
  Files         366      366              
  Lines       16986    17032      +46     
  Branches       13       13              
==========================================
+ Hits         4753     4760       +7     
- Misses      11916    11956      +40     
+ Partials      317      316       -1     
Files with missing lines Coverage Δ
...d/state-transition/pkg/core/validation_deposits.go 36.13% <100.00%> (-0.85%) ⬇️
...transition/pkg/core/state_processor_withdrawals.go 34.25% <76.92%> (-15.46%) ⬇️
mod/beacon/validator/block_builder.go 0.00% <0.00%> (ø)
mod/state-transition/pkg/core/state_processor.go 40.84% <0.00%> (-0.59%) ⬇️
mod/storage/pkg/deposit/store.go 59.00% <35.71%> (+8.55%) ⬆️
...ate-transition/pkg/core/state_processor_staking.go 68.83% <76.19%> (+1.79%) ⬆️

... and 2 files with indirect coverage changes

Comment on lines +86 to +91
case sp.cs.DepositEth1ChainID() == spec.BoonetEth1ChainID &&
slot < math.U64(spec.BoonetFork2Height):
// Boonet pre fork2 has a bug which makes DepositEth1ChainID point to
// next deposit index, not latest processed deposit index.
// We keep it for backward compatibility.
depositIndex++
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This can be removed as soon as fork activates

@calbera calbera changed the title wip: making set eth1 deposit index backward compatible chore: Make setting of eth1 deposit index backward compatible Dec 3, 2024
@abi87 abi87 marked this pull request as ready for review December 3, 2024 21:59
@calbera calbera marked this pull request as ready for review December 3, 2024 21:59
@calbera calbera requested a review from itsdevbear as a code owner December 3, 2024 21:59
@calbera calbera requested review from calbera and removed request for itsdevbear December 3, 2024 21:59
@abi87 abi87 changed the title chore: Make setting of eth1 deposit index backward compatible fix(state-transitions): Make setting of eth1 deposit index backward compatible Dec 3, 2024
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 (1)
mod/storage/pkg/deposit/store.go (1)

Line range hint 148-170: Fix the incorrect loop in the Prune method

The loop for i := range end is incorrect because end is a uint64, and you cannot range over an integer. This will cause a compilation error.

Apply this diff to fix the loop:

-func (kv *KVStore[DepositT]) Prune(start, end uint64) error {
-	kv.logger.Debug(
-		"Prune request",
-		"start", start,
-		"end", end,
-	)
-	if start > end {
-		return fmt.Errorf(
-			"DepositKVStore Prune start: %d, end: %d: %w",
-			start, end, pruner.ErrInvalidRange,
-		)
-	}
-
-	var ctx = context.TODO()
-	kv.mu.Lock()
-	defer kv.mu.Unlock()
-	for i := range end {
-		// This only errors if the key passed in cannot be encoded.
-		if err := kv.store.Remove(ctx, start+i); err != nil {
-			return errors.Wrapf(err, "failed to prune deposit %d", start+i)
-		}
-	}
+func (kv *KVStore[DepositT]) Prune(start, end uint64) error {
+	kv.logger.Debug(
+		"Prune request",
+		"start", start,
+		"end", end,
+	)
+	if start > end {
+		return fmt.Errorf(
+			"DepositKVStore Prune start: %d, end: %d: %w",
+			start, end, pruner.ErrInvalidRange,
+		)
+	}
+
+	var ctx = context.TODO()
+	kv.mu.Lock()
+	defer kv.mu.Unlock()
+	for i := start; i < end; i++ {
+		// This only errors if the key passed in cannot be encoded.
+		if err := kv.store.Remove(ctx, i); err != nil {
+			return errors.Wrapf(err, "failed to prune deposit %d", i)
+		}
+	}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Reviewing files that changed from the base of the PR and between 0c10121 and 9aeaadd.

📒 Files selected for processing (9)
  • mod/beacon/validator/block_builder.go (1 hunks)
  • mod/state-transition/pkg/core/core_test.go (0 hunks)
  • mod/state-transition/pkg/core/state_processor.go (1 hunks)
  • mod/state-transition/pkg/core/state_processor_genesis_test.go (2 hunks)
  • mod/state-transition/pkg/core/state_processor_staking.go (3 hunks)
  • mod/state-transition/pkg/core/state_processor_staking_test.go (21 hunks)
  • mod/state-transition/pkg/core/state_processor_withdrawals.go (1 hunks)
  • mod/state-transition/pkg/core/validation_deposits.go (1 hunks)
  • mod/storage/pkg/deposit/store.go (7 hunks)
💤 Files with no reviewable changes (1)
  • mod/state-transition/pkg/core/core_test.go
🧰 Additional context used
📓 Learnings (4)
mod/state-transition/pkg/core/state_processor_genesis_test.go (2)
Learnt from: abi87
PR: berachain/beacon-kit#2114
File: mod/state-transition/pkg/core/state_processor_genesis_test.go:82-108
Timestamp: 2024-11-12T11:12:56.774Z
Learning: In `mod/state-transition/pkg/core/state_processor_genesis_test.go`, the `deposits` array is part of the test setup and not individual test cases, so adding comments to explain each deposit is not necessary.
Learnt from: abi87
PR: berachain/beacon-kit#2114
File: mod/state-transition/pkg/core/state_processor_genesis_test.go:42-77
Timestamp: 2024-11-12T11:12:56.773Z
Learning: Documentation comments for type aliases in test code are not necessary.
mod/state-transition/pkg/core/validation_deposits.go (1)
Learnt from: abi87
PR: berachain/beacon-kit#2119
File: mod/state-transition/pkg/core/state_processor_genesis.go:109-118
Timestamp: 2024-11-12T11:12:56.773Z
Learning: In `InitializePreminedBeaconStateFromEth1` in `mod/state-transition/pkg/core/state_processor_genesis.go`, we enforce that there is a single deposit per validator in the genesis format; therefore, handling multiple deposits per validator is not necessary.
mod/storage/pkg/deposit/store.go (1)
Learnt from: abi87
PR: berachain/beacon-kit#2115
File: mod/storage/pkg/deposit/store.go:77-80
Timestamp: 2024-11-24T21:25:28.207Z
Learning: In the Go file `mod/storage/pkg/deposit/store.go`, within the `GetDepositsByIndex` function, the `depRange` parameter comes from validated parameters, ensuring that integer overflow is not possible when calculating `endIdx = startIndex + depRange`.
mod/state-transition/pkg/core/state_processor_staking_test.go (2)
Learnt from: abi87
PR: berachain/beacon-kit#2119
File: mod/state-transition/pkg/core/state_processor_staking_test.go:424-440
Timestamp: 2024-11-12T11:12:56.773Z
Learning: In the `TestTransitionHittingValidatorsCap` function in `mod/state-transition/pkg/core/state_processor_staking_test.go`, adding additional assertions to verify validator set size after initialization is unnecessary.
Learnt from: abi87
PR: berachain/beacon-kit#2142
File: mod/state-transition/pkg/core/state_processor_staking_test.go:154-168
Timestamp: 2024-11-12T11:12:56.774Z
Learning: In the `TestTransitionUpdateValidators` function in `mod/state-transition/pkg/core/state_processor_staking_test.go`, adding additional assertions for balance relationships is not necessary.
🔇 Additional comments (13)
mod/state-transition/pkg/core/state_processor_staking.go (3)

125-134: Balance increase and logging are correctly implemented

The validator's balance is correctly increased, and relevant information is logged appropriately.


333-342: New validator creation is handled correctly with proper logging

The addValidatorInternal function correctly adds a new validator, increases the balance, and logs the details effectively.


74-95: 🧹 Nitpick (assertive)

Ensure backward compatibility code is revisited after fork activation

The code adjusts depositIndex for the Bartio and Boonet chains to maintain backward compatibility due to known bugs. Remember to revisit and remove this conditional incrementing of depositIndex post-fork activation to prevent unnecessary adjustments.

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

82-85: Logging level changed to Debug

Updating the logging level to Debug for GetDepositsByIndex helps reduce log verbosity and is appropriate for detailed debugging information.

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

146-148: Logging enhanced for deposit processing range

The added logging provides useful information about the range of deposits being processed, which aids in debugging and monitoring.

mod/state-transition/pkg/core/state_processor_genesis_test.go (2)

127-128: Clarified comments on deposit index handling for Betnet

The comment now clearly explains that on Betnet, the deposit index is set to the last accepted deposit, improving the understanding of the test expectations.


250-254: Clarified comments on deposit index handling for Bartio

The updated comment accurately reflects that on Bartio, the deposit index is off by one, aligning with the chain's specific behavior and enhancing test clarity.

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

276-281: Proper error handling added for setting the next withdrawal validator index

Adding error handling for st.SetNextWithdrawalValidatorIndex ensures that any issues setting the next index are caught and handled appropriately, improving robustness.

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

256-259: 🧹 Nitpick (assertive)

Consider reducing log level to Debug as previously suggested.

As noted in a previous review, this logging could be at Debug level since it's primarily for development/debugging purposes.


255-259: Verify the impact of deposit index adjustment.

The deposit index adjustment is specific to Boonet network and Fork2 height. This network-specific fix should be well documented and tested.

Run the following script to verify the deposit index adjustment implementation:

✅ Verification successful

The search results provide good context about the deposit index adjustment. Let me verify the implementation details further with additional scripts.


Based on the search results, I can now provide a verified response about the deposit index adjustment implementation:

The deposit index adjustment at BoonetFork2Height is a well-implemented fix for a known issue

The implementation is properly handled with:

  • Clear boundary conditions checking slot against BoonetFork2Height
  • Proper error handling and logging during the adjustment
  • Consistent behavior across multiple components (state processor, validation, block builder)
  • Test coverage for deposit index verification
  • The fix decrements the deposit index by 1 to correct the off-by-one error inherited from Bartio

The adjustment is specifically designed to fix a pre-fork2 bug where DepositEth1ChainID was pointing to the next deposit index instead of the latest processed deposit index. The implementation includes proper validation skipping before the fork and appropriate handling after the fork activation.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the deposit index adjustment implementation and its tests
# Test: Search for related test cases and documentation
rg -A 5 "BoonetFork2Height" 

Length of output: 5062


Script:

#!/bin/bash
# Search for test cases specifically related to deposit index adjustment
rg -A 10 "GetEth1DepositIndex|SetEth1DepositIndex" mod/state-transition/pkg/core/

# Look for any documentation about this feature
fd README.md | xargs rg -i "deposit.*index|fork.*2"

Length of output: 11542

mod/beacon/validator/block_builder.go (1)

314-317: LGTM! Helpful logging addition.

The logging of deposit processing details (start_index and num_deposits) improves visibility and helps track deposit index adjustments.

mod/state-transition/pkg/core/state_processor_staking_test.go (2)

147-148: Improved epoch transition testing logic.

The loop condition for epoch transitions has been updated to be more precise:

-for i := 0; i < int(cs.SlotsPerEpoch()); i++ {
+currEpoch := cs.SlotToEpoch(blk.GetSlot())
+for currEpoch == cs.SlotToEpoch(blk.GetSlot()+1) {

This change makes the tests more accurate by ensuring they run until the epoch actually changes.

Also applies to: 304-305, 690-692, 863-865, 1102-1104


44-44: Verify the impact of changing chain spec type from Boonet to Betnet.

The test file has been updated to use BetnetChainSpecType instead of BoonetChainSpecType. This change seems significant and needs verification.

Run the following script to understand the chain spec type usage:

Also applies to: 211-211, 596-596, 750-750, 932-932

✅ Verification successful

Let me gather more context about the chain spec types and their usage.


Let me check the chain spec implementations to understand the differences between these chain types.


The change from Boonet to Betnet chain spec type is intentional and correct

Based on the code analysis:

  1. Boonet has special cases and legacy behaviors (pre-fork2) that need to be maintained for backward compatibility
  2. Betnet is a newer chain spec that inherits from the base spec without the legacy behaviors
  3. The test file changes align with this transition, ensuring tests run against the newer, cleaner chain spec implementation

The change is part of moving away from the legacy Boonet implementation that had several special cases and bugs that needed workarounds (like deposit index handling, emergency minting, etc.).

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the chain spec type usage across the codebase
# Test: Search for chain spec type usage
rg -A 3 "BoonetChainSpecType|BetnetChainSpecType"

Length of output: 3256


Script:

#!/bin/bash
# Check for chain spec type implementations and their differences
ast-grep --pattern 'func $_(ChainSpec) {
  $$$
}'

# Also check for any references to these chain types in test files
rg -g "**/*_test.go" "setupChain.*ChainSpecType" -B 2 -A 2

Length of output: 4036


Script:

#!/bin/bash
# Look for chain spec implementations
rg -g "!*test.go" "func.*ChainSpec\(\)" -A 10

# Also check for any configuration differences
rg -g "!*test.go" "Betnet|Boonet" -B 2 -A 2

Length of output: 13624

Comment on lines +282 to +287
sp.logger.Info(
"Processed withdrawals",
"num_withdrawals", numWithdrawals,
"bera_inflation", float64(
payloadWithdrawals[0].GetAmount().Unwrap())/math.GweiPerWei,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

Ensure consistent terminology for inflation withdrawal in logging

Consider using consistent terminology in logging messages. The code references "EVM inflation," but the log key is "bera_inflation." Aligning these terms improves clarity.

@abi87 abi87 merged commit e86ff36 into main Dec 3, 2024
15 checks passed
@abi87 abi87 deleted the set-eth1-deposit-index-backward-compatible branch December 3, 2024 22:17
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.

2 participants