Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
fca99f2
WIP: test(mempool): add integration test
cloudgray Aug 21, 2025
3591815
WIP: test(mempool): add integration test
cloudgray Aug 22, 2025
ad31857
test(mempool): refactor integration test
cloudgray Aug 25, 2025
a656805
test(mempool): fix integration test
cloudgray Aug 25, 2025
5a384a7
test(mempool): refactor integration test
cloudgray Aug 25, 2025
d74f5ad
chore: modify comments
cloudgray Aug 25, 2025
9a4d096
Merge branch 'main' into test/appside-mempool
cloudgray Aug 25, 2025
abbe071
test(mempool): add PrepareProposal check to integration test
cloudgray Aug 26, 2025
603c890
chore: fix lint
cloudgray Aug 26, 2025
bc522eb
chore: update changelog
cloudgray Aug 26, 2025
375fee6
Merge branch 'main' into test/appside-mempool
cloudgray Aug 26, 2025
dbe047b
test(mempool): modify testcode
cloudgray Aug 26, 2025
29fd29f
chore: fix lint
cloudgray Aug 26, 2025
142fbca
chore: fix lint
cloudgray Aug 26, 2025
5d29424
fix(ante): remove bond denom from allowed fee denom
cloudgray Aug 27, 2025
66811c5
fix(mempool): calculation of priority of cosmos tx
cloudgray Aug 27, 2025
9449d73
test(mempool): fix mempool integration test
cloudgray Aug 27, 2025
60a2cc7
Merge branch 'main' into fix/cosmos-tx-priority
cloudgray Aug 27, 2025
8c25160
add unit test for cosmos tx feeCoins validation
cloudgray Aug 29, 2025
155b2b9
chore: fix lint
cloudgray Sep 1, 2025
db33377
chore: modify test helper function and comments
cloudgray Sep 1, 2025
272d598
Merge branch 'main' into fix/cosmos-tx-priority
cloudgray Sep 9, 2025
ca3cdb2
chore: resolve merge conflict
cloudgray Sep 9, 2025
ac9cd72
chore: update CHANGELOG.md
cloudgray Sep 9, 2025
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
- [\#442](https://github.com/cosmos/evm/pull/442) Prevent nil pointer by checking error in gov precompile FromResponse.
- [\#387](https://github.com/cosmos/evm/pull/387) (Experimental) EVM-compatible appside mempool
- [\#476](https://github.com/cosmos/evm/pull/476) Add revert error e2e tests for contract and precompile calls
- [\#534](https://github.com/cosmos/evm/pull/534) Align cosmos tx priority with priority setup of anteHandler

### FEATURES

Expand Down
11 changes: 6 additions & 5 deletions ante/cosmos/min_gas_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,8 @@ func (mpd MinGasPriceDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate
feeCoins := feeTx.GetFee()
evmDenom := evmtypes.GetEVMCoinDenom()

// only allow user to pass in aatom and stake native token as transaction fees
// allow use stake native tokens for fees is just for unit tests to pass
//
// TODO: is the handling of stake necessary here? Why not adjust the tests to contain the correct denom?
validFees := len(feeCoins) == 0 || (len(feeCoins) == 1 && slices.Contains([]string{evmDenom, sdk.DefaultBondDenom}, feeCoins.GetDenomByIndex(0)))
// only allow user to pass in evm coin as transaction fee
validFees := IsValidFeeCoins(feeCoins, []string{evmDenom})
if !validFees && !simulate {
return ctx, fmt.Errorf("expected only native token %s for fee, but got %s", evmDenom, feeCoins.String())
}
Expand Down Expand Up @@ -94,3 +91,7 @@ func (mpd MinGasPriceDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate

return next(ctx, tx, simulate)
}

func IsValidFeeCoins(feeCoins sdk.Coins, allowedDenoms []string) bool {
return len(feeCoins) == 0 || (len(feeCoins) == 1 && slices.Contains(allowedDenoms, feeCoins.GetDenomByIndex(0)))
}
96 changes: 96 additions & 0 deletions ante/cosmos/min_gas_price_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package cosmos_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/cosmos/evm/ante/cosmos"

sdkmath "cosmossdk.io/math"

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

func TestIsValidFeeCoins(t *testing.T) {
tests := []struct {
name string
feeCoins sdk.Coins
allowedDenoms []string
expectedResult bool
}{
{
name: "empty fee coins should be valid",
feeCoins: sdk.Coins{},
allowedDenoms: []string{"uatom"},
expectedResult: true,
},
{
name: "nil fee coins should be valid",
feeCoins: nil,
allowedDenoms: []string{"uatom"},
expectedResult: true,
},
{
name: "single allowed denom should be valid",
feeCoins: sdk.NewCoins(sdk.NewCoin("uatom", sdkmath.NewInt(1000))),
allowedDenoms: []string{"uatom"},
expectedResult: true,
},
{
name: "single allowed denom from multiple allowed should be valid",
feeCoins: sdk.NewCoins(sdk.NewCoin("uatom", sdkmath.NewInt(1000))),
allowedDenoms: []string{"uatom", "stake", "wei"},
expectedResult: true,
},
{
name: "single disallowed denom should be invalid",
feeCoins: sdk.NewCoins(sdk.NewCoin("forbidden", sdkmath.NewInt(1000))),
allowedDenoms: []string{"uatom"},
expectedResult: false,
},
{
name: "single disallowed denom with empty allowed list should be invalid",
feeCoins: sdk.NewCoins(sdk.NewCoin("uatom", sdkmath.NewInt(1000))),
allowedDenoms: []string{},
expectedResult: false,
},
{
name: "multiple fee coins should be invalid",
feeCoins: sdk.NewCoins(sdk.NewCoin("uatom", sdkmath.NewInt(1000)), sdk.NewCoin("stake", sdkmath.NewInt(500))),
allowedDenoms: []string{"uatom", "stake"},
expectedResult: false,
},
{
name: "multiple fee coins with one allowed should be invalid",
feeCoins: sdk.NewCoins(sdk.NewCoin("uatom", sdkmath.NewInt(1000)), sdk.NewCoin("forbidden", sdkmath.NewInt(500))),
allowedDenoms: []string{"uatom"},
expectedResult: false,
},
{
name: "empty allowed denoms with empty fee coins should be valid",
feeCoins: sdk.Coins{},
allowedDenoms: []string{},
expectedResult: true,
},
{
name: "case sensitive denom matching",
feeCoins: sdk.NewCoins(sdk.NewCoin("UATOM", sdkmath.NewInt(1000))),
allowedDenoms: []string{"uatom"},
expectedResult: false,
},
{
name: "zero amount allowed denom should be valid",
feeCoins: sdk.NewCoins(sdk.NewCoin("uatom", sdkmath.NewInt(0))),
allowedDenoms: []string{"uatom"},
expectedResult: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := cosmos.IsValidFeeCoins(tt.feeCoins, tt.allowedDenoms)
require.Equal(t, tt.expectedResult, result, "IsValidFeeCoins returned unexpected result")
})
}
}
41 changes: 38 additions & 3 deletions mempool/iterator.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package mempool

import (
"math"
"math/big"

ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/holiman/uint256"

"github.com/cosmos/evm/mempool/miner"
"github.com/cosmos/evm/mempool/txpool"
cosmosevmtypes "github.com/cosmos/evm/types"
msgtypes "github.com/cosmos/evm/x/vm/types"

"cosmossdk.io/log"
"cosmossdk.io/math"
sdkmath "cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/mempool"
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
)

var _ mempool.Iterator = &EVMMempoolIterator{}
Expand Down Expand Up @@ -255,7 +258,29 @@ func (i *EVMMempoolIterator) extractCosmosEffectiveTip(tx sdk.Tx) *uint256.Int {
return nil // Transaction doesn't implement FeeTx interface
}

bondDenomFeeAmount := math.ZeroInt()
// default to `MaxInt64` when there's no extension option.
maxPriorityPrice := sdkmath.LegacyNewDec(math.MaxInt64)

if hasExtOptsTx, ok := feeTx.(authante.HasExtensionOptionsTx); ok {
for _, opt := range hasExtOptsTx.GetExtensionOptions() {
if extOpt, ok := opt.GetCachedValue().(*cosmosevmtypes.ExtensionOptionDynamicFeeTx); ok {
maxPriorityPrice = extOpt.MaxPriorityPrice
if maxPriorityPrice.IsNil() {
maxPriorityPrice = sdkmath.LegacyZeroDec()
}
break
}
}
}

// Convert gasTipCap (LegacyDec) to *uint256.Int for comparison
gasTipCap, overflow := uint256.FromBig(maxPriorityPrice.BigInt())
if overflow {
i.logger.Debug("overflowed on gas tip cap calculation")
return nil
}

bondDenomFeeAmount := sdkmath.ZeroInt()
fees := feeTx.GetFee()
for _, coin := range fees {
if coin.Denom == i.bondDenom {
Expand All @@ -264,8 +289,14 @@ func (i *EVMMempoolIterator) extractCosmosEffectiveTip(tx sdk.Tx) *uint256.Int {
}
}

gas := sdkmath.NewIntFromUint64(feeTx.GetGas())
if gas.IsZero() {
i.logger.Debug("Cosmos transaction has zero gas")
return nil
}

// Calculate gas price: fee_amount / gas_limit
gasPrice, overflow := uint256.FromBig(bondDenomFeeAmount.Quo(math.NewIntFromUint64(feeTx.GetGas())).BigInt())
gasPrice, overflow := uint256.FromBig(bondDenomFeeAmount.Quo(gas).BigInt())
if overflow {
i.logger.Debug("overflowed on gas price calculation")
return nil
Expand All @@ -287,6 +318,10 @@ func (i *EVMMempoolIterator) extractCosmosEffectiveTip(tx sdk.Tx) *uint256.Int {
}

effectiveTip := new(uint256.Int).Sub(gasPrice, baseFee)
if effectiveTip.Cmp(gasTipCap) > 0 {
effectiveTip = gasTipCap

Choose a reason for hiding this comment

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

I suggest to use context priority here too. Since if cosmos tx doesn't set TxFeeChecker, it falls thru checkTxFeeWIthValidatorMinGasPrices which calculates priority based on gas price.

Therefore, in this case, there would be discrepancy in the priority of the context and the priority calculated in this function.

My suggestion is getting a context with blockchain and then retain priority from that context. In this case, this function extractCosmosEffectiveTip might not needed.

Copy link
Member

Choose a reason for hiding this comment

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

I agree with this. We should lean into fully supporting context priority. Alongside this, we should set the default context priority for Cosmos EVM to be the effective gas tip, allowing people to modify it if they wish.

}

i.logger.Debug("calculated effective tip", "gas_price", gasPrice.String(), "base_fee", baseFee.String(), "effective_tip", effectiveTip.String())
return effectiveTip
}
Expand Down
14 changes: 4 additions & 10 deletions mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,12 @@ func NewExperimentalEVMMempool(getCtxCallback func(height int64, prove bool) (sd
defaultConfig := sdkmempool.PriorityNonceMempoolConfig[math.Int]{}
defaultConfig.TxPriority = sdkmempool.TxPriority[math.Int]{
GetTxPriority: func(goCtx context.Context, tx sdk.Tx) math.Int {
cosmosTxFee, ok := tx.(sdk.FeeTx)
if !ok {
ctx := sdk.UnwrapSDKContext(goCtx)
priority := ctx.Priority()
if priority < 0 {
return math.ZeroInt()
}
found, coin := cosmosTxFee.GetFee().Find(bondDenom)
if !found {
return math.ZeroInt()
}

gasPrice := coin.Amount.Quo(math.NewIntFromUint64(cosmosTxFee.GetGas()))

return gasPrice
return math.NewIntFromUint64(uint64(priority))
},
Compare: func(a, b math.Int) int {
return a.BigInt().Cmp(b.BigInt())
Expand Down
21 changes: 15 additions & 6 deletions tests/integration/mempool/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mempool
import (
"encoding/hex"
"fmt"
"math"
"math/big"

abci "github.com/cometbft/cometbft/abci/types"
Expand All @@ -23,7 +24,9 @@ const (
TxGas = 100_000
)

// createCosmosSendTransactionWithKey creates a simple bank send transaction with the specified key
var MaxGasTipCap = big.NewInt(math.MaxInt64)

// createCosmosSendTx creates a simple bank send transaction with the provided key and gasPrice
func (s *IntegrationTestSuite) createCosmosSendTx(key keyring.Key, gasPrice *big.Int) sdk.Tx {
feeDenom := "aatom"

Expand All @@ -42,10 +45,11 @@ func (s *IntegrationTestSuite) createCosmosSendTx(key keyring.Key, gasPrice *big
tx, err := s.factory.BuildCosmosTx(key.Priv, txArgs)
s.Require().NoError(err)

fmt.Printf("DEBUG: Created cosmos transaction successfully\n")
return tx
}

// createEVMTransaction creates an EVM transaction using the provided key
// createEVMValueTransferTx creates an EVM value transfer transaction using the provided key and gasPrice
func (s *IntegrationTestSuite) createEVMValueTransferTx(key keyring.Key, nonce int, gasPrice *big.Int) sdk.Tx {
to := s.keyring.GetKey(1).Addr

Expand All @@ -67,7 +71,7 @@ func (s *IntegrationTestSuite) createEVMValueTransferTx(key keyring.Key, nonce i
return tx
}

// createEVMContractDeployTx creates an EVM transaction for contract deployment
// createEVMContractDeployTx creates an EVM contract deploy transaction using the provided key and gasPrice
func (s *IntegrationTestSuite) createEVMContractDeployTx(key keyring.Key, gasPrice *big.Int, data []byte) sdk.Tx {
ethTxArgs := evmtypes.EvmTxArgs{
Nonce: 0,
Expand All @@ -93,7 +97,7 @@ func (s *IntegrationTestSuite) checkTxs(txs []sdk.Tx) error {
return nil
}

// checkTxs call abci CheckTx for a transaction
// checkTx calls abci CheckTx for a transaction
func (s *IntegrationTestSuite) checkTx(tx sdk.Tx) error {
txBytes, err := s.network.App.GetTxConfig().TxEncoder()(tx)
if err != nil {
Expand Down Expand Up @@ -138,7 +142,7 @@ func (s *IntegrationTestSuite) calculateCosmosGasPrice(feeAmount int64, gasLimit

// calculateCosmosEffectiveTip calculates the effective tip for a Cosmos transaction
// This aligns with EVM transaction prioritization: effective_tip = gas_price - base_fee
func (s *IntegrationTestSuite) calculateCosmosEffectiveTip(feeAmount int64, gasLimit uint64, baseFee *big.Int) *big.Int {
func (s *IntegrationTestSuite) calculateCosmosEffectiveTip(feeAmount int64, gasLimit uint64, gasTipCap, baseFee *big.Int) *big.Int {
gasPrice := s.calculateCosmosGasPrice(feeAmount, gasLimit)
if baseFee == nil || baseFee.Sign() == 0 {
return gasPrice // No base fee, effective tip equals gas price
Expand All @@ -148,5 +152,10 @@ func (s *IntegrationTestSuite) calculateCosmosEffectiveTip(feeAmount int64, gasL
return big.NewInt(0) // Gas price lower than base fee, effective tip is zero
}

return new(big.Int).Sub(gasPrice, baseFee)
effectiveTip := new(big.Int).Sub(gasPrice, baseFee)
if effectiveTip.Cmp(gasTipCap) > 0 {
effectiveTip = gasTipCap
}

return effectiveTip
}
Loading
Loading