Skip to content

Commit

Permalink
Problem: duplicated feemarket params load
Browse files Browse the repository at this point in the history
more cleanup

fix lint

fix test
  • Loading branch information
yihuang committed Mar 19, 2024
1 parent c39e892 commit fa1ca8a
Show file tree
Hide file tree
Showing 23 changed files with 145 additions and 100 deletions.
6 changes: 3 additions & 3 deletions app/ante/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (suite *AnteTestSuite) TestAnteHandler() {
suite.Require().NoError(acc.SetSequence(1))
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)

suite.app.EvmKeeper.SetBalance(suite.ctx, addr, big.NewInt(10000000000))
suite.app.EvmKeeper.SetBalance(suite.ctx, addr, big.NewInt(10000000000), evmtypes.DefaultEVMDenom)

suite.app.FeeMarketKeeper.SetBaseFee(suite.ctx, big.NewInt(100))
}
Expand Down Expand Up @@ -1206,7 +1206,7 @@ func (suite *AnteTestSuite) TestAnteHandlerWithDynamicTxFee() {
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)

suite.ctx = suite.ctx.WithIsCheckTx(tc.checkTx).WithIsReCheckTx(tc.reCheckTx)
suite.app.EvmKeeper.SetBalance(suite.ctx, addr, big.NewInt((ethparams.InitialBaseFee+10)*100000))
suite.app.EvmKeeper.SetBalance(suite.ctx, addr, big.NewInt((ethparams.InitialBaseFee+10)*100000), evmtypes.DefaultEVMDenom)
_, err := suite.anteHandler(suite.ctx, tc.txFn(), false)
if tc.expPass {
suite.Require().NoError(err)
Expand Down Expand Up @@ -1335,7 +1335,7 @@ func (suite *AnteTestSuite) TestAnteHandlerWithParams() {
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)

suite.ctx = suite.ctx.WithIsCheckTx(true)
suite.app.EvmKeeper.SetBalance(suite.ctx, addr, big.NewInt((ethparams.InitialBaseFee+10)*100000))
suite.app.EvmKeeper.SetBalance(suite.ctx, addr, big.NewInt((ethparams.InitialBaseFee+10)*100000), evmtypes.DefaultEVMDenom)
_, err := suite.anteHandler(suite.ctx, tc.txFn(), false)
if tc.expErr == nil {
suite.Require().NoError(err)
Expand Down
11 changes: 8 additions & 3 deletions app/ante/eip712.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,26 @@ func init() {
// transactions, as defined by the presence of an ExtensionOptionsWeb3Tx extension.
func NewLegacyCosmosAnteHandlerEip712(ctx sdk.Context, options HandlerOptions, extra ...sdk.AnteDecorator) sdk.AnteHandler {
evmParams := options.EvmKeeper.GetParams(ctx)
feemarketParams := options.FeeMarketKeeper.GetParams(ctx)
evmDenom := evmParams.EvmDenom
chainID := options.EvmKeeper.ChainID()
chainCfg := evmParams.GetChainConfig()
ethCfg := chainCfg.EthereumConfig(chainID)
var txFeeChecker authante.TxFeeChecker
if options.DynamicFeeChecker {
txFeeChecker = NewDynamicFeeChecker(ethCfg, &evmParams, &feemarketParams)
}
decorators := []sdk.AnteDecorator{
RejectMessagesDecorator{}, // reject MsgEthereumTxs
// disable the Msg types that cannot be included on an authz.MsgExec msgs field
NewAuthzLimiterDecorator(options.DisabledAuthzMsgs),
authante.NewSetUpContextDecorator(),
authante.NewValidateBasicDecorator(),
authante.NewTxTimeoutHeightDecorator(),
NewMinGasPriceDecorator(options.FeeMarketKeeper, evmDenom),
NewMinGasPriceDecorator(options.FeeMarketKeeper, evmDenom, &feemarketParams),
authante.NewValidateMemoDecorator(options.AccountKeeper),
authante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker),
NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, txFeeChecker),
// SetPubKeyDecorator must be called before all signature verification decorators
authante.NewSetPubKeyDecorator(options.AccountKeeper),
authante.NewValidateSigCountDecorator(options.AccountKeeper),
Expand All @@ -75,7 +80,7 @@ func NewLegacyCosmosAnteHandlerEip712(ctx sdk.Context, options HandlerOptions, e
NewLegacyEip712SigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
authante.NewIncrementSequenceDecorator(options.AccountKeeper),
ibcante.NewRedundantRelayDecorator(options.IBCKeeper),
NewGasWantedDecorator(options.FeeMarketKeeper, ethCfg),
NewGasWantedDecorator(options.FeeMarketKeeper, ethCfg, &feemarketParams),
}
decorators = append(decorators, extra...)
return sdk.ChainAnteDecorators(decorators...)
Expand Down
10 changes: 5 additions & 5 deletions app/ante/fee_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
"github.com/ethereum/go-ethereum/params"
ethermint "github.com/evmos/ethermint/types"
"github.com/evmos/ethermint/x/evm/types"
feemarkettypes "github.com/evmos/ethermint/x/feemarket/types"
)

// NewDynamicFeeChecker returns a `TxFeeChecker` that applies a dynamic fee to
Expand All @@ -37,7 +39,7 @@ import (
// - when `ExtensionOptionDynamicFeeTx` is omitted, `tipFeeCap` defaults to `MaxInt64`.
// - when london hardfork is not enabled, it fallbacks to SDK default behavior (validator min-gas-prices).
// - Tx priority is set to `effectiveGasPrice / DefaultPriorityReduction`.
func NewDynamicFeeChecker(k DynamicFeeEVMKeeper) authante.TxFeeChecker {
func NewDynamicFeeChecker(ethCfg *params.ChainConfig, evmParams *types.Params, feemarketParams *feemarkettypes.Params) authante.TxFeeChecker {
return func(ctx sdk.Context, tx sdk.Tx) (sdk.Coins, int64, error) {
feeTx, ok := tx.(sdk.FeeTx)
if !ok {
Expand All @@ -49,11 +51,9 @@ func NewDynamicFeeChecker(k DynamicFeeEVMKeeper) authante.TxFeeChecker {
return checkTxFeeWithValidatorMinGasPrices(ctx, feeTx)
}

params := k.GetParams(ctx)
denom := params.EvmDenom
ethCfg := params.ChainConfig.EthereumConfig(k.ChainID())
denom := evmParams.EvmDenom

baseFee := k.GetBaseFee(ctx, ethCfg)
baseFee := types.GetBaseFee(ctx.BlockHeight(), ethCfg, feemarketParams)
if baseFee == nil {
// london hardfork is not enabled: fallback to min-gas-prices logic
return checkTxFeeWithValidatorMinGasPrices(ctx, feeTx)
Expand Down
21 changes: 16 additions & 5 deletions app/ante/fee_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ import (
ethermint "github.com/evmos/ethermint/types"
"github.com/evmos/ethermint/x/evm/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"
feemarkettypes "github.com/evmos/ethermint/x/feemarket/types"
)

var _ DynamicFeeEVMKeeper = MockEVMKeeper{}

type MockEVMKeeper struct {
BaseFee *big.Int
EnableLondonHF bool
Expand All @@ -34,7 +33,11 @@ func (m MockEVMKeeper) GetBaseFee(ctx sdk.Context, ethCfg *params.ChainConfig) *
}

func (m MockEVMKeeper) GetParams(ctx sdk.Context) evmtypes.Params {
return evmtypes.DefaultParams()
params := evmtypes.DefaultParams()
if !m.EnableLondonHF {
params.ChainConfig.LondonBlock = nil
}
return params
}

func (m MockEVMKeeper) ChainID() *big.Int {
Expand All @@ -61,7 +64,7 @@ func TestSDKTxFeeChecker(t *testing.T) {
testCases := []struct {
name string
ctx sdk.Context
keeper DynamicFeeEVMKeeper
keeper MockEVMKeeper
buildTx func() sdk.Tx
expFees string
expPriority int64
Expand Down Expand Up @@ -207,7 +210,15 @@ func TestSDKTxFeeChecker(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
fees, priority, err := NewDynamicFeeChecker(tc.keeper)(tc.ctx, tc.buildTx())
evmParams := tc.keeper.GetParams(tc.ctx)
feemarketParams := feemarkettypes.Params{
NoBaseFee: false,
BaseFee: sdk.NewIntFromBigInt(tc.keeper.BaseFee),
}
chainID := tc.keeper.ChainID()
chainCfg := evmParams.GetChainConfig()
ethCfg := chainCfg.EthereumConfig(chainID)
fees, priority, err := NewDynamicFeeChecker(ethCfg, &evmParams, &feemarketParams)(tc.ctx, tc.buildTx())
if tc.expSuccess {
require.Equal(t, tc.expFees, fees.String())
require.Equal(t, tc.expPriority, priority)
Expand Down
6 changes: 5 additions & 1 deletion app/ante/fee_market.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/params"
feemarkettypes "github.com/evmos/ethermint/x/feemarket/types"
)

// GasWantedDecorator keeps track of the gasWanted amount on the current block in transient store
Expand All @@ -29,16 +30,19 @@ import (
type GasWantedDecorator struct {
feeMarketKeeper FeeMarketKeeper
ethCfg *params.ChainConfig
feemarketParams *feemarkettypes.Params
}

// NewGasWantedDecorator creates a new NewGasWantedDecorator
func NewGasWantedDecorator(
feeMarketKeeper FeeMarketKeeper,
ethCfg *params.ChainConfig,
feemarketParams *feemarkettypes.Params,
) GasWantedDecorator {
return GasWantedDecorator{
feeMarketKeeper,
ethCfg,
feemarketParams,
}
}

Expand All @@ -52,7 +56,7 @@ func (gwd GasWantedDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bo
}

gasWanted := feeTx.GetGas()
isBaseFeeEnabled := gwd.feeMarketKeeper.GetBaseFeeEnabled(ctx)
isBaseFeeEnabled := gwd.feemarketParams.IsBaseFeeEnabled(ctx.BlockHeight())

// Add total gasWanted to cumulative in block transientStore in FeeMarket module
if isBaseFeeEnabled {
Expand Down
3 changes: 2 additions & 1 deletion app/ante/fee_market_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ func (suite *AnteTestSuite) TestGasWantedDecorator() {
suite.SetupTest()

evmParams := suite.app.EvmKeeper.GetParams(suite.ctx)
feemarketParams := suite.app.FeeMarketKeeper.GetParams(suite.ctx)
chainID := suite.app.EvmKeeper.ChainID()
chainCfg := evmParams.GetChainConfig()
ethCfg := chainCfg.EthereumConfig(chainID)

dec := ante.NewGasWantedDecorator(suite.app.FeeMarketKeeper, ethCfg)
dec := ante.NewGasWantedDecorator(suite.app.FeeMarketKeeper, ethCfg, &feemarketParams)
from, fromPrivKey := tests.NewAddrKey()
to := tests.GenerateAddress()

Expand Down
23 changes: 13 additions & 10 deletions app/ante/fees.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

ethtypes "github.com/ethereum/go-ethereum/core/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"
feemarkettypes "github.com/evmos/ethermint/x/feemarket/types"
)

// MinGasPriceDecorator will check if the transaction's fee is at least as large
Expand All @@ -32,8 +33,9 @@ import (
// If fee is high enough, then call next AnteHandler
// CONTRACT: Tx must implement FeeTx to use MinGasPriceDecorator
type MinGasPriceDecorator struct {
feesKeeper FeeMarketKeeper
evmDenom string
feesKeeper FeeMarketKeeper
evmDenom string
feemarketParams *feemarkettypes.Params
}

// EthMinGasPriceDecorator will check if the transaction's fee is at least as large
Expand All @@ -42,8 +44,9 @@ type MinGasPriceDecorator struct {
// if London hard fork or fee market params (EIP-1559) are enabled.
// If fee is high enough, then call next AnteHandler
type EthMinGasPriceDecorator struct {
feesKeeper FeeMarketKeeper
baseFee *big.Int
feesKeeper FeeMarketKeeper
baseFee *big.Int
feemarketParams *feemarkettypes.Params
}

// EthMempoolFeeDecorator will check if the transaction's effective fee is at least as large
Expand All @@ -59,14 +62,14 @@ type EthMempoolFeeDecorator struct {

// NewMinGasPriceDecorator creates a new MinGasPriceDecorator instance used only for
// Cosmos transactions.
func NewMinGasPriceDecorator(fk FeeMarketKeeper, evmDenom string) MinGasPriceDecorator {
return MinGasPriceDecorator{feesKeeper: fk, evmDenom: evmDenom}
func NewMinGasPriceDecorator(fk FeeMarketKeeper, evmDenom string, feemarketParams *feemarkettypes.Params) MinGasPriceDecorator {
return MinGasPriceDecorator{feesKeeper: fk, evmDenom: evmDenom, feemarketParams: feemarketParams}
}

// NewEthMinGasPriceDecorator creates a new MinGasPriceDecorator instance used only for
// Ethereum transactions.
func NewEthMinGasPriceDecorator(fk FeeMarketKeeper, baseFee *big.Int) EthMinGasPriceDecorator {
return EthMinGasPriceDecorator{feesKeeper: fk, baseFee: baseFee}
func NewEthMinGasPriceDecorator(fk FeeMarketKeeper, baseFee *big.Int, feemarketParams *feemarkettypes.Params) EthMinGasPriceDecorator {
return EthMinGasPriceDecorator{feesKeeper: fk, baseFee: baseFee, feemarketParams: feemarketParams}
}

// NewEthMempoolFeeDecorator creates a new NewEthMempoolFeeDecorator instance used only for
Expand All @@ -84,7 +87,7 @@ func (mpd MinGasPriceDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate
return ctx, errorsmod.Wrapf(errortypes.ErrInvalidType, "invalid transaction type %T, expected sdk.FeeTx", tx)
}

minGasPrice := mpd.feesKeeper.GetParams(ctx).MinGasPrice
minGasPrice := mpd.feemarketParams.MinGasPrice

// Short-circuit if min gas price is 0 or if simulating
if minGasPrice.IsZero() || simulate {
Expand Down Expand Up @@ -126,7 +129,7 @@ func (mpd MinGasPriceDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate
// AnteHandle ensures that the that the effective fee from the transaction is greater than the
// minimum global fee, which is defined by the MinGasPrice (parameter) * GasLimit (tx argument).
func (empd EthMinGasPriceDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
minGasPrice := empd.feesKeeper.GetParams(ctx).MinGasPrice
minGasPrice := empd.feemarketParams.MinGasPrice

// short-circuit if min gas price is 0
if minGasPrice.IsZero() {
Expand Down
6 changes: 4 additions & 2 deletions app/ante/fees_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ func (s *AnteTestSuite) TestMinGasPriceDecorator() {
s.Run(et.name+"_"+tc.name, func() {
// s.SetupTest(et.isCheckTx)
ctx := s.ctx.WithIsReCheckTx(et.isCheckTx)
dec := ante.NewMinGasPriceDecorator(s.app.FeeMarketKeeper, evmtypes.DefaultEVMDenom)
feemarketParams := s.app.FeeMarketKeeper.GetParams(ctx)
dec := ante.NewMinGasPriceDecorator(s.app.FeeMarketKeeper, evmtypes.DefaultEVMDenom, &feemarketParams)
_, err := dec.AnteHandle(ctx, tc.malleate(), et.simulate, NextFn)

if tc.expPass || (et.simulate && tc.allowPassOnSimulate) {
Expand Down Expand Up @@ -339,8 +340,9 @@ func (s *AnteTestSuite) TestEthMinGasPriceDecorator() {
chainCfg := evmParams.GetChainConfig()
ethCfg := chainCfg.EthereumConfig(chainID)
baseFee := s.app.EvmKeeper.GetBaseFee(s.ctx, ethCfg)
feemarketParams := s.app.FeeMarketKeeper.GetParams(s.ctx)

dec := ante.NewEthMinGasPriceDecorator(s.app.FeeMarketKeeper, baseFee)
dec := ante.NewEthMinGasPriceDecorator(s.app.FeeMarketKeeper, baseFee, &feemarketParams)
_, err := dec.AnteHandle(s.ctx, tx, et.simulate, NextFn)

if tc.expPass {
Expand Down
29 changes: 18 additions & 11 deletions app/ante/handler_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ type HandlerOptions struct {
SigGasConsumer func(meter sdk.GasMeter, sig signing.SignatureV2, params authtypes.Params) error
MaxTxGasWanted uint64
ExtensionOptionChecker ante.ExtensionOptionChecker
TxFeeChecker ante.TxFeeChecker
DisabledAuthzMsgs []string
ExtraDecorators []sdk.AnteDecorator
// use dynamic fee checker or the cosmos-sdk default one for native transactions
DynamicFeeChecker bool
DisabledAuthzMsgs []string
ExtraDecorators []sdk.AnteDecorator
}

func (options HandlerOptions) validate() error {
Expand All @@ -69,22 +70,23 @@ func (options HandlerOptions) validate() error {

func newEthAnteHandler(ctx sdk.Context, options HandlerOptions, extra ...sdk.AnteDecorator) sdk.AnteHandler {
evmParams := options.EvmKeeper.GetParams(ctx)
feemarketParams := options.FeeMarketKeeper.GetParams(ctx)
evmDenom := evmParams.EvmDenom
chainID := options.EvmKeeper.ChainID()
chainCfg := evmParams.GetChainConfig()
ethCfg := chainCfg.EthereumConfig(chainID)
baseFee := options.EvmKeeper.GetBaseFee(ctx, ethCfg)
baseFee := evmtypes.GetBaseFee(ctx.BlockHeight(), ethCfg, &feemarketParams)
decorators := []sdk.AnteDecorator{
NewEthSetUpContextDecorator(options.EvmKeeper), // outermost AnteDecorator. SetUpContext must be called first
NewEthMempoolFeeDecorator(evmDenom, baseFee), // Check eth effective gas price against minimal-gas-prices
NewEthMinGasPriceDecorator(options.FeeMarketKeeper, baseFee), // Check eth effective gas price against the global MinGasPrice
NewEthSetUpContextDecorator(options.EvmKeeper), // outermost AnteDecorator. SetUpContext must be called first
NewEthMempoolFeeDecorator(evmDenom, baseFee), // Check eth effective gas price against minimal-gas-prices
NewEthMinGasPriceDecorator(options.FeeMarketKeeper, baseFee, &feemarketParams), // Check eth effective gas price against the global MinGasPrice
NewEthValidateBasicDecorator(&evmParams, baseFee),
NewEthSigVerificationDecorator(chainID),
NewEthAccountVerificationDecorator(options.AccountKeeper, options.EvmKeeper, evmDenom),
NewCanTransferDecorator(options.EvmKeeper, baseFee, &evmParams, ethCfg),
NewEthGasConsumeDecorator(options.EvmKeeper, options.MaxTxGasWanted, ethCfg, evmDenom, baseFee),
NewEthIncrementSenderSequenceDecorator(options.AccountKeeper), // innermost AnteDecorator.
NewGasWantedDecorator(options.FeeMarketKeeper, ethCfg),
NewGasWantedDecorator(options.FeeMarketKeeper, ethCfg, &feemarketParams),
NewEthEmitEventDecorator(options.EvmKeeper), // emit eth tx hash and index at the very last ante handler.
}
decorators = append(decorators, extra...)
Expand All @@ -93,10 +95,15 @@ func newEthAnteHandler(ctx sdk.Context, options HandlerOptions, extra ...sdk.Ant

func newCosmosAnteHandler(ctx sdk.Context, options HandlerOptions, extra ...sdk.AnteDecorator) sdk.AnteHandler {
evmParams := options.EvmKeeper.GetParams(ctx)
feemarketParams := options.FeeMarketKeeper.GetParams(ctx)
evmDenom := evmParams.EvmDenom
chainID := options.EvmKeeper.ChainID()
chainCfg := evmParams.GetChainConfig()
ethCfg := chainCfg.EthereumConfig(chainID)
var txFeeChecker ante.TxFeeChecker
if options.DynamicFeeChecker {
txFeeChecker = NewDynamicFeeChecker(ethCfg, &evmParams, &feemarketParams)
}
decorators := []sdk.AnteDecorator{
RejectMessagesDecorator{}, // reject MsgEthereumTxs
// disable the Msg types that cannot be included on an authz.MsgExec msgs field
Expand All @@ -105,18 +112,18 @@ func newCosmosAnteHandler(ctx sdk.Context, options HandlerOptions, extra ...sdk.
ante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker),
ante.NewValidateBasicDecorator(),
ante.NewTxTimeoutHeightDecorator(),
NewMinGasPriceDecorator(options.FeeMarketKeeper, evmDenom),
NewMinGasPriceDecorator(options.FeeMarketKeeper, evmDenom, &feemarketParams),
ante.NewValidateMemoDecorator(options.AccountKeeper),
ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker),
NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, txFeeChecker),
// SetPubKeyDecorator must be called before all signature verification decorators
ante.NewSetPubKeyDecorator(options.AccountKeeper),
ante.NewValidateSigCountDecorator(options.AccountKeeper),
ante.NewSigGasConsumeDecorator(options.AccountKeeper, options.SigGasConsumer),
ante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
ante.NewIncrementSequenceDecorator(options.AccountKeeper),
ibcante.NewRedundantRelayDecorator(options.IBCKeeper),
NewGasWantedDecorator(options.FeeMarketKeeper, ethCfg),
NewGasWantedDecorator(options.FeeMarketKeeper, ethCfg, &feemarketParams),
}
decorators = append(decorators, extra...)
return sdk.ChainAnteDecorators(decorators...)
Expand Down
Loading

0 comments on commit fa1ca8a

Please sign in to comment.