Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

fix(chain): Fake FinalizeBlock in PrepareProposal #1272

Merged
merged 8 commits into from
Nov 2, 2023
Merged

fix(chain): Fake FinalizeBlock in PrepareProposal #1272

merged 8 commits into from
Nov 2, 2023

Conversation

itsdevbear
Copy link
Member

@itsdevbear itsdevbear commented Nov 2, 2023

Summary by CodeRabbit

Refactor:

  • Removed the "statedb" field from the "blockchain" struct in eth/core/chain.go, indicating a refactoring of state management functionality.
  • Updated eth/core/chain_writer.go to create a new state.StateDB object within WriteGenesisBlock and InsertBlockAndSetHead functions, enhancing the modularity of state management.
  • Modified WriteBlockAndSetHead and writeBlockWithState functions to accept a state.StateDB parameter, improving the flexibility of state changes commitment.

These changes streamline the state management process, potentially improving the performance and maintainability of the code.

Copy link

coderabbitai bot commented Nov 2, 2023

Walkthrough

The changes primarily revolve around the refactoring of state management in the Ethereum core. The state.StateDB object is now created within the functions that require it, instead of being a field in the blockchain struct. This change indicates a shift towards a more function-scoped state management approach.

Changes

File Summary
eth/core/chain.go The import statement for the "pkg.berachain.dev/polaris/eth/core/state" package has been removed. The "statedb" field of type "state.StateDB" from the "blockchain" struct has also been removed.
eth/core/chain_writer.go The state.StateDB object is now created within the WriteGenesisBlock, InsertBlockAndSetHead, WriteBlockAndSetHead, and writeBlockWithState functions. This object is passed around to other functions as needed.
cosmos/x/evm/plugins/state/plugin.go The Prepare function in the plugin struct has been modified. The latestQueryContext field is updated with a modified sdk.UnwrapSDKContext value. The modified value includes the addition of WithKVGasConfig and WithTransientKVGasConfig methods, which set the gas configuration for the context.

Poem

🍂 As autumn leaves fall, so does old code retire,
In the heart of Ethereum, a refactor transpires.
StateDB, once global, now dances with grace,
From function to function, it finds its new place.
🐇 Hopping through changes, we rabbits delight,
In the glow of the moon, and the code shining bright.
So here's to the coders, their spirits ignite,
Making blockchain more robust, in the cool autumn night. 🌙


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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 a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai help to get help.
  • @coderabbitai resolve to resolve all the CodeRabbit review comments.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.json

Copy link

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between a097397 and 6ee586c.
Files selected for processing (2)
  • eth/core/chain.go (3 hunks)
  • eth/core/chain_writer.go (5 hunks)
Additional comments: 7
eth/core/chain.go (3)
  • 33-38: The import statement for the "pkg.berachain.dev/polaris/eth/core/state" package has been removed. This indicates that the "state" package is no longer used in this file. Ensure that this change does not break any functionality.

  • 67-72: The "statedb" field of type "state.StateDB" has been removed from the "blockchain" struct. This suggests a change in how state management is handled. Make sure that the state management functionality is correctly implemented elsewhere.

  • 125-130: The "statedb" field is no longer initialized in the "NewChain" function. This is consistent with the removal of the "statedb" field from the "blockchain" struct. Ensure that the state management functionality is correctly implemented elsewhere.

eth/core/chain_writer.go (4)
  • 46-50: The WriteGenesisBlock function now creates a new state.StateDB object and passes it to WriteBlockAndSetHead. This is a significant change as it means a new state is created for each block, which could improve the efficiency and accuracy of state management.

  • 63-84: The InsertBlockAndSetHead function creates a new state.StateDB object and passes it to Process and ValidateState. It also passes the state object to WriteBlockAndSetHead. This change is consistent with the new approach of creating a new state for each block.

  • 90-98: The WriteBlockAndSetHead function now takes a state.StateDB parameter and passes it to writeBlockWithState. This change is consistent with the new approach of creating a new state for each block.

  • 176-182: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [159-182]

The writeBlockWithState function now takes a state.StateDB parameter and uses it to commit the state changes. This change is consistent with the new approach of creating a new state for each block.

Copy link

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 6ee586c and 4c1cfd6.
Files selected for processing (1)
  • cosmos/x/evm/plugins/state/plugin.go (1 hunks)
Additional comments: 1
cosmos/x/evm/plugins/state/plugin.go (1)
  • 140-146: The Prepare function has been updated to set the latestQueryContext field with a modified sdk.UnwrapSDKContext value. The modified value includes the addition of WithKVGasConfig and WithTransientKVGasConfig methods, which set the gas configuration for the context. Ensure that this change does not affect the functionality of the Prepare function and that the gas configuration is correctly applied.

Copy link

codecov bot commented Nov 2, 2023

Codecov Report

Merging #1272 (bb47675) into main (a097397) will not change coverage.
The diff coverage is n/a.

❗ Current head bb47675 differs from pull request most recent head 6bf6261. Consider uploading reports for the commit 6bf6261 to get more accurate results

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #1272   +/-   ##
=======================================
  Coverage   48.73%   48.73%           
=======================================
  Files          84       84           
  Lines        4838     4838           
=======================================
  Hits         2358     2358           
  Misses       2306     2306           
  Partials      174      174           

Copy link

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

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 4c1cfd6 and 3de07ce.
Files selected for processing (2)
  • cosmos/x/evm/plugins/state/plugin.go (2 hunks)
  • eth/core/chain_writer.go (5 hunks)
Additional comments: 4
eth/core/chain_writer.go (4)
  • 46-50: The WriteGenesisBlock function now creates a new state.StateDB object and passes it to WriteBlockAndSetHead. This is a good practice as it ensures that each block has its own state, reducing the risk of state corruption.

  • 60-90: The InsertBlockAndSetHead function now creates a new state.StateDB object and passes it to Process and ValidateState. This is a good practice as it ensures that each block has its own state, reducing the risk of state corruption.

  • 95-104: The WriteBlockAndSetHead function now takes a state.StateDB parameter and passes it to writeBlockWithState. This is a good practice as it ensures that each block has its own state, reducing the risk of state corruption.

  • 163-169: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [163-188]

The writeBlockWithState function now takes a state.StateDB parameter and uses it to commit state changes. This is a good practice as it ensures that each block has its own state, reducing the risk of state corruption.

Comment on lines 140 to 146
// Prepare sets up the context on the state plugin for use in JSON-RPC calls.
// Prepare implements `core.StatePlugin`.
func (p *plugin) Prepare(ctx context.Context) {
p.latestQueryContext = sdk.UnwrapSDKContext(ctx)
p.latestQueryContext, _ = sdk.UnwrapSDKContext(ctx).
WithKVGasConfig(storetypes.GasConfig{}).
WithTransientKVGasConfig(storetypes.GasConfig{}).CacheContext()
}
Copy link

Choose a reason for hiding this comment

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

The error returned by CacheContext() is ignored. It's a good practice to handle all errors to prevent unexpected behavior.

- p.latestQueryContext, _ = sdk.UnwrapSDKContext(ctx).
+ p.latestQueryContext, err = sdk.UnwrapSDKContext(ctx).
+ if err != nil {
+     // handle error
+ }

Commitable suggestion (Beta)
Suggested change
// Prepare sets up the context on the state plugin for use in JSON-RPC calls.
// Prepare implements `core.StatePlugin`.
func (p *plugin) Prepare(ctx context.Context) {
p.latestQueryContext = sdk.UnwrapSDKContext(ctx)
p.latestQueryContext, _ = sdk.UnwrapSDKContext(ctx).
WithKVGasConfig(storetypes.GasConfig{}).
WithTransientKVGasConfig(storetypes.GasConfig{}).CacheContext()
}
// Prepare sets up the context on the state plugin for use in JSON-RPC calls.
// Prepare implements `core.StatePlugin`.
func (p *plugin) Prepare(ctx context.Context) {
p.latestQueryContext, err := sdk.UnwrapSDKContext(ctx)
if err != nil {
// handle error
}
p.latestQueryContext = p.latestQueryContext.
WithKVGasConfig(storetypes.GasConfig{}).
WithTransientKVGasConfig(storetypes.GasConfig{}).CacheContext()
}

Comment on lines 538 to 542
if p.latestQueryContext.MultiStore() == nil {
ctx = p.latestQueryContext.WithEventManager(sdk.NewEventManager())
} else {
ctx, _ = p.latestQueryContext.CacheContext()
ctx, _ = p.latestQueryContext.WithEventManager(sdk.NewEventManager()).CacheContext()
}
Copy link

Choose a reason for hiding this comment

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

The error returned by CacheContext() is ignored. It's a good practice to handle all errors to prevent unexpected behavior.

- ctx, _ = p.latestQueryContext.WithEventManager(sdk.NewEventManager()).CacheContext()
+ ctx, err = p.latestQueryContext.WithEventManager(sdk.NewEventManager()).CacheContext()
+ if err != nil {
+     // handle error
+ }

Commitable suggestion (Beta)
Suggested change
if p.latestQueryContext.MultiStore() == nil {
ctx = p.latestQueryContext.WithEventManager(sdk.NewEventManager())
} else {
ctx, _ = p.latestQueryContext.CacheContext()
ctx, _ = p.latestQueryContext.WithEventManager(sdk.NewEventManager()).CacheContext()
}
if p.latestQueryContext.MultiStore() == nil {
ctx = p.latestQueryContext.WithEventManager(sdk.NewEventManager())
} else {
ctx, err := p.latestQueryContext.WithEventManager(sdk.NewEventManager()).CacheContext()
if err != nil {
// handle error
}
}

@itsdevbear itsdevbear changed the title fix(chain): Create a new statedb everyblock. fix(chain): Fake FinalizeBlock in PrepareProposal Nov 2, 2023
Copy link

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 3de07ce and bb47675.
Files selected for processing (1)
  • cosmos/miner/miner.go (2 hunks)
Additional comments: 2
cosmos/miner/miner.go (2)
  • 87-89: Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 97-103: The PreBlocker method is now called with a modified argument &abci.RequestFinalizeBlock that includes fields from the req argument. This change modifies the behavior of the function and requires a review.

Copy link

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between bb47675 and 6bf6261.
Files selected for processing (1)
  • cosmos/miner/miner.go (2 hunks)
Additional comments: 2
cosmos/miner/miner.go (2)
  • 85-91: The PrepareProposal function now takes an additional argument req *abci.RequestPrepareProposal. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 97-104: The PreBlocker method is now called with a modified argument &abci.RequestFinalizeBlock that includes fields from the req argument. This change modifies the behavior of the function by passing additional information to the PreBlocker method. Ensure that the PreBlocker method can handle these additional fields correctly.

@itsdevbear itsdevbear merged commit 3f15a11 into main Nov 2, 2023
13 checks passed
@itsdevbear itsdevbear deleted the sdb2 branch November 2, 2023 22:14
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant