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

fix(miner): Reset gas meter in prepare proposal. #1270

Merged
merged 4 commits into from
Nov 2, 2023
Merged

fix(miner): Reset gas meter in prepare proposal. #1270

merged 4 commits into from
Nov 2, 2023

Conversation

itsdevbear
Copy link
Contributor

@itsdevbear itsdevbear commented Nov 2, 2023

Summary by CodeRabbit

Refactor:

  • The miner.go file in the cosmos/miner module has been refactored for better gas management. This includes the addition of gas refunding and resetting, and reordering of code execution for efficiency.
  • The ProcessPayloadEnvelope function in the processor.go file of the cosmos/x/evm/keeper module has been updated to refund and reset gas meters after execution, ensuring accurate gas accounting.

These changes aim to improve the efficiency and accuracy of gas management in the system, leading to more predictable and reliable operation.

Copy link

coderabbitai bot commented Nov 2, 2023

Walkthrough

The changes primarily focus on improving the gas management in the Cosmos blockchain codebase. The updates include the addition of gas refunding and resetting mechanisms, reordering of code execution, and the removal of state plugin priming. These modifications enhance the efficiency and robustness of the system.

Changes

File Path Summary
cosmos/miner/miner.go The code has been refactored to include gas refunding and resetting, reordering of code execution, and removal of state plugin priming.
cosmos/x/evm/.../processor.go The ProcessPayloadEnvelope function now includes defer statements to refund and reset the gasMeter and blockGasMeter after execution.

🐇🍂

As the leaves fall, so does the gas,

Refunded, reset, in a class of its own.

Code reordered, state priming passed,

In the heart of autumn, our code has grown.

Celebrate the changes, for they are vast,

In the world of Cosmos, we're never alone.

So here's to the code, changing so fast,

To the rhythm of keystrokes, a melodious tone. 🎵🍁


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

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 949a826 and d8fb3e0.
Files ignored due to filter (2)
  • cosmos/go.mod
  • cosmos/go.sum
Files selected for processing (2)
  • cosmos/miner/miner.go (3 hunks)
  • cosmos/x/evm/keeper/processor.go (1 hunks)
Additional comments: 6
cosmos/x/evm/keeper/processor.go (1)
  • 54-55: Good error handling here, wrapping the error provides more context which can be useful for debugging.
cosmos/miner/miner.go (5)
  • 24-31: The import statement for storetypes package has been added. Ensure that this package is being used in the code and that it is a necessary import.

  • 96-98: The PreBlocker function is being called with nil as the second argument. Ensure that this is the intended behavior and that the function can handle a nil argument.

  • 104-110: The gas meter is being reset here. This is a critical operation and should be handled with care. Ensure that this operation does not introduce any side effects or vulnerabilities.

  • 113-115: The SetLatestQueryContext function is being called here. Ensure that the context being passed is the correct one and that it is being set at the right time in the execution flow.

  • 118-119: The buildBlock function is being called here. Ensure that the context being passed is the correct one and that it is being set at the right time in the execution flow.

Comment on lines 48 to +52
// Reset GasMeter to 0.
gasMeter.RefundGas(gasMeter.GasConsumed(), "reset before evm block")
blockGasMeter.RefundGas(blockGasMeter.GasConsumed(), "reset before evm block")
defer gasMeter.ConsumeGas(gasMeter.GasConsumed(), "reset after evm")
defer gasMeter.RefundGas(gasMeter.GasConsumed(), "reset after evm")
defer blockGasMeter.RefundGas(blockGasMeter.GasConsumed(), "reset after evm")
Copy link

Choose a reason for hiding this comment

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

The use of defer here is appropriate for ensuring the gas meters are reset after the function execution. However, it's important to note that if an error occurs and the function exits early, the deferred statements will still execute. This could potentially lead to incorrect gas meter readings if the function doesn't complete its intended operations. Consider adding error handling to prevent this scenario.

-	defer gasMeter.RefundGas(gasMeter.GasConsumed(), "reset after evm")
-	defer blockGasMeter.RefundGas(blockGasMeter.GasConsumed(), "reset after evm")
+	defer func() {
+		if err == nil {
+			gasMeter.RefundGas(gasMeter.GasConsumed(), "reset after evm")
+			blockGasMeter.RefundGas(blockGasMeter.GasConsumed(), "reset after evm")
+		}
+	}()

Commitable suggestion (Beta)
Suggested change
// Reset GasMeter to 0.
gasMeter.RefundGas(gasMeter.GasConsumed(), "reset before evm block")
blockGasMeter.RefundGas(blockGasMeter.GasConsumed(), "reset before evm block")
defer gasMeter.ConsumeGas(gasMeter.GasConsumed(), "reset after evm")
defer gasMeter.RefundGas(gasMeter.GasConsumed(), "reset after evm")
defer blockGasMeter.RefundGas(blockGasMeter.GasConsumed(), "reset after evm")
// Reset GasMeter to 0.
gasMeter.RefundGas(gasMeter.GasConsumed(), "reset before evm block")
blockGasMeter.RefundGas(blockGasMeter.GasConsumed(), "reset before evm block")
defer func() {
if err == nil {
gasMeter.RefundGas(gasMeter.GasConsumed(), "reset after evm")
blockGasMeter.RefundGas(blockGasMeter.GasConsumed(), "reset after evm")
}
}()

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

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between d8fb3e0 and 15af6f3.
Files ignored due to filter (2)
  • e2e/testapp/go.mod
  • e2e/testapp/go.sum
Files selected for processing (1)
  • cosmos/miner/miner.go (3 hunks)
Additional comments: 2
cosmos/miner/miner.go (2)
  • 24-31: The new import "cosmossdk.io/store/types" is used for gas metering configuration. Ensure that the package is correctly imported and used.

  • 92-97: The PreBlocker and BeginBlocker are run to get the chain into the state it'll be in when the EVM transaction actually runs. Ensure that these functions are correctly implemented and handle errors properly.

Comment on lines 100 to 116
return nil, err
}

ctx.GasMeter().RefundGas(ctx.GasMeter().GasConsumed(), "prepare proposal")
ctx.BlockGasMeter().RefundGas(ctx.BlockGasMeter().GasConsumed(), "prepare proposal")
ctx = ctx.WithKVGasConfig(storetypes.GasConfig{}).
WithTransientKVGasConfig(storetypes.GasConfig{}).
WithGasMeter(storetypes.NewInfiniteGasMeter())

// We have to prime the state plugin.
if err = m.keeper.SetLatestQueryContext(ctx); err != nil {
return nil, err
}

// Trigger the geth miner to build a block.
if payloadEnvelopeBz, err = m.buildBlock(ctx); err != nil {
return nil, err
Copy link

Choose a reason for hiding this comment

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

The gas meter is refunded and reset, and the context is updated with a new gas configuration. The state plugin is primed with the updated context. The block is then built. Ensure that the gas metering and block building processes are correctly implemented and handle errors properly.

- ctx.GasMeter().RefundGas(ctx.GasMeter().GasConsumed(), "prepare proposal")
- ctx.BlockGasMeter().RefundGas(ctx.BlockGasMeter().GasConsumed(), "prepare proposal")
- ctx = ctx.WithKVGasConfig(storetypes.GasConfig{}).
-		WithTransientKVGasConfig(storetypes.GasConfig{}).
-		WithGasMeter(storetypes.NewInfiniteGasMeter())
+ gasConsumed := ctx.GasMeter().GasConsumed()
+ ctx.GasMeter().RefundGas(gasConsumed, "prepare proposal")
+ blockGasConsumed := ctx.BlockGasMeter().GasConsumed()
+ ctx.BlockGasMeter().RefundGas(blockGasConsumed, "prepare proposal")
+ ctx = ctx.WithKVGasConfig(storetypes.GasConfig{}).
+		WithTransientKVGasConfig(storetypes.GasConfig{}).
+		WithGasMeter(storetypes.NewInfiniteGasMeter())

Commitable suggestion (Beta)
Suggested change
return nil, err
}
ctx.GasMeter().RefundGas(ctx.GasMeter().GasConsumed(), "prepare proposal")
ctx.BlockGasMeter().RefundGas(ctx.BlockGasMeter().GasConsumed(), "prepare proposal")
ctx = ctx.WithKVGasConfig(storetypes.GasConfig{}).
WithTransientKVGasConfig(storetypes.GasConfig{}).
WithGasMeter(storetypes.NewInfiniteGasMeter())
// We have to prime the state plugin.
if err = m.keeper.SetLatestQueryContext(ctx); err != nil {
return nil, err
}
// Trigger the geth miner to build a block.
if payloadEnvelopeBz, err = m.buildBlock(ctx); err != nil {
return nil, err
gasConsumed := ctx.GasMeter().GasConsumed()
ctx.GasMeter().RefundGas(gasConsumed, "prepare proposal")
blockGasConsumed := ctx.BlockGasMeter().GasConsumed()
ctx.BlockGasMeter().RefundGas(blockGasConsumed, "prepare proposal")
ctx = ctx.WithKVGasConfig(storetypes.GasConfig{}).
WithTransientKVGasConfig(storetypes.GasConfig{}).
WithGasMeter(storetypes.NewInfiniteGasMeter())
// We have to prime the state plugin.
if err = m.keeper.SetLatestQueryContext(ctx); err != nil {
return nil, err
}
// Trigger the geth miner to build a block.
if payloadEnvelopeBz, err = m.buildBlock(ctx); err != nil {
return nil, err
}

Copy link

codecov bot commented Nov 2, 2023

Codecov Report

Merging #1270 (1aa9047) into main (949a826) will decrease coverage by 0.04%.
The diff coverage is 0.00%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1270      +/-   ##
==========================================
- Coverage   48.73%   48.70%   -0.04%     
==========================================
  Files          84       84              
  Lines        4838     4841       +3     
==========================================
  Hits         2358     2358              
- Misses       2306     2309       +3     
  Partials      174      174              
Files Coverage Δ
cosmos/x/evm/keeper/processor.go 0.00% <0.00%> (ø)

@itsdevbear itsdevbear merged commit bcbdd0d into main Nov 2, 2023
14 checks passed
@itsdevbear itsdevbear deleted the fix-123 branch November 2, 2023 17:34
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