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

Commit

Permalink
fix(miner): Reset gas meter in prepare proposal. (#1270)
Browse files Browse the repository at this point in the history
  • Loading branch information
itsdevbear authored Nov 2, 2023
1 parent 949a826 commit bcbdd0d
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 19 deletions.
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
18 changes: 13 additions & 5 deletions cosmos/miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ package miner
import (
"context"

storetypes "cosmossdk.io/store/types"

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

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -90,11 +92,6 @@ func (m *Miner) PrepareProposal(
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 +100,17 @@ func (m *Miner) PrepareProposal(
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
Expand Down
78 changes: 78 additions & 0 deletions cosmos/miner/msgs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2023, Berachain Foundation. All rights reserved.
// Use of this software is govered by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.

package miner

import (
evidence "cosmossdk.io/x/evidence/types"

sdk "github.com/cosmos/cosmos-sdk/types"
crisis "github.com/cosmos/cosmos-sdk/x/crisis/types"
gov "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govbeta "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
slashing "github.com/cosmos/cosmos-sdk/x/slashing/types"
staking "github.com/cosmos/cosmos-sdk/x/staking/types"
)

var (
// DefaultAllowedMsgs are messages that can be submitted by external users.
DefaultAllowedMsgs = map[string]sdk.Msg{
// crisis
"cosmos.crisis.v1beta1.MsgVerifyInvariant": &crisis.MsgVerifyInvariant{},
"cosmos.crisis.v1beta1.MsgVerifyInvariantResponse": nil,

// evidence
"cosmos.evidence.v1beta1.Equivocation": nil,
"cosmos.evidence.v1beta1.MsgSubmitEvidence": &evidence.MsgSubmitEvidence{},
"cosmos.evidence.v1beta1.MsgSubmitEvidenceResponse": nil,

// gov
"cosmos.gov.v1.MsgDeposit": &gov.MsgDeposit{},
"cosmos.gov.v1.MsgDepositResponse": nil,
"cosmos.gov.v1.MsgVote": &gov.MsgVote{},
"cosmos.gov.v1.MsgVoteResponse": nil,
"cosmos.gov.v1.MsgVoteWeighted": &gov.MsgVoteWeighted{},
"cosmos.gov.v1.MsgVoteWeightedResponse": nil,
"cosmos.gov.v1beta1.MsgDeposit": &govbeta.MsgDeposit{},
"cosmos.gov.v1beta1.MsgDepositResponse": nil,
"cosmos.gov.v1beta1.MsgVote": &govbeta.MsgVote{},
"cosmos.gov.v1beta1.MsgVoteResponse": nil,
"cosmos.gov.v1beta1.MsgVoteWeighted": &govbeta.MsgVoteWeighted{},
"cosmos.gov.v1beta1.MsgVoteWeightedResponse": nil,
"cosmos.gov.v1beta1.TextProposal": nil,

// slashing
"cosmos.slashing.v1beta1.MsgUnjail": &slashing.MsgUnjail{},
"cosmos.slashing.v1beta1.MsgUnjailResponse": nil,

// staking
"cosmos.staking.v1beta1.MsgCreateValidator": &staking.MsgCreateValidator{},
"cosmos.staking.v1beta1.MsgCreateValidatorResponse": nil,
"cosmos.staking.v1beta1.MsgEditValidator": &staking.MsgEditValidator{},
"cosmos.staking.v1beta1.MsgEditValidatorResponse": nil,

// tx
"cosmos.tx.v1beta1.Tx": nil,

// upgrade
"cosmos.upgrade.v1beta1.CancelSoftwareUpgradeProposal": nil,
"cosmos.upgrade.v1beta1.SoftwareUpgradeProposal": nil,
}
)
7 changes: 6 additions & 1 deletion cosmos/x/evm/keeper/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"context"
"fmt"

storetypes "cosmossdk.io/store/types"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/ethereum/go-ethereum/beacon/engine"
Expand All @@ -48,7 +50,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")

if err = envelope.UnmarshalJSON(msg.Data); err != nil {
return nil, fmt.Errorf("failed to unmarshal payload envelope: %w", err)
Expand All @@ -60,6 +63,8 @@ func (k *Keeper) ProcessPayloadEnvelope(
}

// Prepare should be moved to the blockchain? THIS IS VERY HOOD YES NEEDS TO BE MOVED.
ctx = sCtx.WithKVGasConfig(storetypes.GasConfig{}).
WithTransientKVGasConfig(storetypes.GasConfig{})
k.chain.PreparePlugins(ctx)
if err = k.chain.InsertBlockAndSetHead(block); err != nil {
return nil, err
Expand Down
12 changes: 7 additions & 5 deletions e2e/testapp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ require (
github.com/onsi/gomega v1.27.10
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0
pkg.berachain.dev/polaris/cosmos v0.0.0-20231012162131-c1edabdcf30e
pkg.berachain.dev/polaris/eth v0.0.0-20231010191645-a5ed99983be4
pkg.berachain.dev/polaris/cosmos v0.0.0-20231101202254-949a826ae454
pkg.berachain.dev/polaris/eth v0.0.0-20231031220135-f3bf0d0ee45f
)

require (
Expand All @@ -52,7 +52,10 @@ require (
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 // indirect
github.com/bits-and-blooms/bitset v1.8.0 // indirect
github.com/btcsuite/btcd v0.23.2 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/btcsuite/btcd/btcutil v1.1.3 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect
github.com/bytedance/sonic v1.10.0 // indirect
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
Expand Down Expand Up @@ -89,7 +92,6 @@ require (
github.com/dgraph-io/badger/v2 v2.2007.4 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/docker/docker v24.0.7+incompatible // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/dvsekhvalnov/jose2go v1.5.0 // indirect
github.com/emicklei/dot v1.6.0 // indirect
Expand Down Expand Up @@ -266,8 +268,8 @@ require (
gotest.tools/v3 v3.5.1 // indirect
nhooyr.io/websocket v1.8.6 // indirect
pgregory.net/rapid v1.1.0 // indirect
pkg.berachain.dev/polaris/contracts v0.0.0-20231010191645-a5ed99983be4 // indirect
pkg.berachain.dev/polaris/lib v0.0.0-20231010191645-a5ed99983be4 // indirect
pkg.berachain.dev/polaris/contracts v0.0.0-20231023174626-bf146d519cd3 // indirect
pkg.berachain.dev/polaris/lib v0.0.0-20231031220135-f3bf0d0ee45f // indirect
rsc.io/tmplfunc v0.0.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Loading

0 comments on commit bcbdd0d

Please sign in to comment.