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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cosmos/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
cosmossdk.io/log v1.2.1
cosmossdk.io/math v1.1.3-rc.1
cosmossdk.io/store v1.0.0-rc.0
cosmossdk.io/x/evidence v0.0.0-20230818115413-c402c51a1508
cosmossdk.io/x/tx v0.11.0
github.com/btcsuite/btcd v0.23.2
github.com/btcsuite/btcd/btcutil v1.1.3
Expand Down
1 change: 1 addition & 0 deletions cosmos/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ cosmossdk.io/math v1.1.3-rc.1 h1:NebCNWDqb1MJRNfvxr4YY7d8FSYgkuB3L75K6xvM+Zo=
cosmossdk.io/math v1.1.3-rc.1/go.mod h1:l2Gnda87F0su8a/7FEKJfFdJrM0JZRXQaohlgJeyQh0=
cosmossdk.io/store v1.0.0-rc.0 h1:9DwOjuUYxDtYxn/REkTxGQAmxlIGfRroB35MQ8TrxF4=
cosmossdk.io/store v1.0.0-rc.0/go.mod h1:FtBDOJmwtOZfmKKF65bKZbTYgS3bDNjjo3nP76dAegk=
cosmossdk.io/x/evidence v0.0.0-20230818115413-c402c51a1508 h1:R9H1lDpcPSkrLOnt6IDE38o0Wp8xE/+BAxocb0oyX4I=
cosmossdk.io/x/tx v0.11.0 h1:Ak2LIC06bXqPbpMIEorkQbwVddRvRys1sL3Cjm+KPfs=
cosmossdk.io/x/tx v0.11.0/go.mod h1:tzuC7JlfGivYuIO32JbvvY3Ft9s6FK1+r0/nGHiHwtM=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
Expand Down
21 changes: 16 additions & 5 deletions cosmos/miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

import (
"context"
"fmt"

storetypes "cosmossdk.io/store/types"

abci "github.com/cometbft/cometbft/abci/types"

Expand Down Expand Up @@ -90,11 +93,6 @@
err error
)

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

// We have to run the PreBlocker && BeginBlocker to get the chain into the state
// it'll be in when the EVM transaction actually runs.
if _, err = m.app.PreBlocker(ctx, nil); err != nil {
Expand All @@ -103,6 +101,19 @@
return nil, err
}

fmt.Println(ctx.GasMeter().GasConsumed(), "REEE")

Check failure on line 104 in cosmos/miner/miner.go

View workflow job for this annotation

GitHub Actions / ci (lint, polaris-linux-latest, 1.21.3)

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)

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
Expand Down
3 changes: 2 additions & 1 deletion cosmos/x/evm/keeper/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func (k *Keeper) ProcessPayloadEnvelope(
// 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")
Comment on lines 50 to +54
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")
}
}()


if err = envelope.UnmarshalJSON(msg.Data); err != nil {
return nil, fmt.Errorf("failed to unmarshal payload envelope: %w", err)
Expand Down
Loading