Skip to content

Commit f03d2a4

Browse files
cloudgrayaljo242
andauthored
test(mempool): add integration test (#512)
* WIP: test(mempool): add integration test * WIP: test(mempool): add integration test * test(mempool): refactor integration test * test(mempool): fix integration test * test(mempool): refactor integration test * chore: modify comments * test(mempool): add PrepareProposal check to integration test * chore: fix lint * chore: update changelog * test(mempool): modify testcode * chore: fix lint * chore: fix lint * chore: remove fmt unnecessary logs * chore: change file name * Update CHANGELOG.md --------- Co-authored-by: Alex | Interchain Labs <[email protected]>
1 parent 2a9e687 commit f03d2a4

File tree

6 files changed

+705
-504
lines changed

6 files changed

+705
-504
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- [\#467](https://github.com/cosmos/evm/pull/467) Replace GlobalEVMMempool by passing to JSONRPC on initiate.
2222
- [\#352](https://github.com/cosmos/evm/pull/352) Remove the creation of a Geth EVM instance, stateDB during the AnteHandler balance check.
2323
- [\#496](https://github.com/cosmos/evm/pull/496) Simplify mempool instantiation by using configs instead of objects.
24+
- [\#512](https://github.com/cosmos/evm/pull/512) Add integration test for appside mempool.
2425
- [\#568](https://github.com/cosmos/evm/pull/568) Avoid unnecessary block notifications when the event bus is already set up.
2526
- [\#511](https://github.com/cosmos/evm/pull/511) Minor code cleanup for `AddPrecompileFn`.
2627
- [\#544](https://github.com/cosmos/evm/pull/544) Parse logs from the txResult.Data and avoid emitting EVM events to cosmos-sdk events.

evmd/tests/integration/mempool/mempool_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package mempool
22

33
import (
4-
"github.com/cosmos/evm/evmd/tests/integration"
54
"testing"
65

6+
"github.com/cosmos/evm/evmd/tests/integration"
7+
78
"github.com/stretchr/testify/suite"
89

910
"github.com/cosmos/evm/tests/integration/mempool"
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package mempool
2+
3+
import (
4+
"encoding/hex"
5+
"fmt"
6+
"math/big"
7+
8+
abci "github.com/cometbft/cometbft/abci/types"
9+
"github.com/cometbft/cometbft/crypto/tmhash"
10+
11+
"github.com/cosmos/evm/testutil/integration/base/factory"
12+
"github.com/cosmos/evm/testutil/keyring"
13+
evmtypes "github.com/cosmos/evm/x/vm/types"
14+
15+
sdkmath "cosmossdk.io/math"
16+
17+
sdk "github.com/cosmos/cosmos-sdk/types"
18+
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
19+
)
20+
21+
// Constants
22+
const (
23+
TxGas = 100_000
24+
)
25+
26+
// createCosmosSendTransactionWithKey creates a simple bank send transaction with the specified key
27+
func (s *IntegrationTestSuite) createCosmosSendTx(key keyring.Key, gasPrice *big.Int) sdk.Tx {
28+
feeDenom := "aatom"
29+
30+
fromAddr := key.AccAddr
31+
toAddr := s.keyring.GetKey(1).AccAddr
32+
amount := sdk.NewCoins(sdk.NewInt64Coin(feeDenom, 1000))
33+
34+
bankMsg := banktypes.NewMsgSend(fromAddr, toAddr, amount)
35+
36+
gasPriceConverted := sdkmath.NewIntFromBigInt(gasPrice)
37+
38+
txArgs := factory.CosmosTxArgs{
39+
Msgs: []sdk.Msg{bankMsg},
40+
GasPrice: &gasPriceConverted,
41+
}
42+
tx, err := s.factory.BuildCosmosTx(key.Priv, txArgs)
43+
s.Require().NoError(err)
44+
45+
return tx
46+
}
47+
48+
// createEVMTransaction creates an EVM transaction using the provided key
49+
func (s *IntegrationTestSuite) createEVMValueTransferTx(key keyring.Key, nonce int, gasPrice *big.Int) sdk.Tx {
50+
to := s.keyring.GetKey(1).Addr
51+
52+
if nonce < 0 {
53+
s.Require().NoError(fmt.Errorf("nonce must be non-negative"))
54+
}
55+
56+
ethTxArgs := evmtypes.EvmTxArgs{
57+
Nonce: uint64(nonce),
58+
To: &to,
59+
Amount: big.NewInt(1000),
60+
GasLimit: TxGas,
61+
GasPrice: gasPrice,
62+
Input: nil,
63+
}
64+
tx, err := s.factory.GenerateSignedEthTx(key.Priv, ethTxArgs)
65+
s.Require().NoError(err)
66+
67+
return tx
68+
}
69+
70+
// createEVMContractDeployTx creates an EVM transaction for contract deployment
71+
func (s *IntegrationTestSuite) createEVMContractDeployTx(key keyring.Key, gasPrice *big.Int, data []byte) sdk.Tx {
72+
ethTxArgs := evmtypes.EvmTxArgs{
73+
Nonce: 0,
74+
To: nil,
75+
Amount: nil,
76+
GasLimit: TxGas,
77+
GasPrice: gasPrice,
78+
Input: data,
79+
}
80+
tx, err := s.factory.GenerateSignedEthTx(key.Priv, ethTxArgs)
81+
s.Require().NoError(err)
82+
83+
return tx
84+
}
85+
86+
// checkTxs call abci CheckTx for multipile transactions
87+
func (s *IntegrationTestSuite) checkTxs(txs []sdk.Tx) error {
88+
for _, tx := range txs {
89+
if err := s.checkTx(tx); err != nil {
90+
return err
91+
}
92+
}
93+
return nil
94+
}
95+
96+
// checkTxs call abci CheckTx for a transaction
97+
func (s *IntegrationTestSuite) checkTx(tx sdk.Tx) error {
98+
txBytes, err := s.network.App.GetTxConfig().TxEncoder()(tx)
99+
if err != nil {
100+
return fmt.Errorf("failed to encode cosmos tx: %w", err)
101+
}
102+
103+
_, err = s.network.App.CheckTx(&abci.RequestCheckTx{
104+
Tx: txBytes,
105+
Type: abci.CheckTxType_New,
106+
})
107+
if err != nil {
108+
return fmt.Errorf("failed to encode cosmos tx: %w", err)
109+
}
110+
111+
return nil
112+
}
113+
114+
// getTxHashes returns transaction hashes for multiple transactions
115+
func (s *IntegrationTestSuite) getTxHashes(txs []sdk.Tx) []string {
116+
txHashes := []string{}
117+
for _, tx := range txs {
118+
txHash := s.getTxHash(tx)
119+
txHashes = append(txHashes, txHash)
120+
}
121+
122+
return txHashes
123+
}
124+
125+
// getTxHash returns transaction hash for a transaction
126+
func (s *IntegrationTestSuite) getTxHash(tx sdk.Tx) string {
127+
txEncoder := s.network.App.GetTxConfig().TxEncoder()
128+
txBytes, err := txEncoder(tx)
129+
s.Require().NoError(err)
130+
131+
return hex.EncodeToString(tmhash.Sum(txBytes))
132+
}
133+
134+
// calculateCosmosGasPrice calculates the gas price for a Cosmos transaction
135+
func (s *IntegrationTestSuite) calculateCosmosGasPrice(feeAmount int64, gasLimit uint64) *big.Int {
136+
return new(big.Int).Div(big.NewInt(feeAmount), big.NewInt(int64(gasLimit))) //#nosec G115 -- not concern, test
137+
}
138+
139+
// calculateCosmosEffectiveTip calculates the effective tip for a Cosmos transaction
140+
// This aligns with EVM transaction prioritization: effective_tip = gas_price - base_fee
141+
func (s *IntegrationTestSuite) calculateCosmosEffectiveTip(feeAmount int64, gasLimit uint64, baseFee *big.Int) *big.Int {
142+
gasPrice := s.calculateCosmosGasPrice(feeAmount, gasLimit)
143+
if baseFee == nil || baseFee.Sign() == 0 {
144+
return gasPrice // No base fee, effective tip equals gas price
145+
}
146+
147+
if gasPrice.Cmp(baseFee) < 0 {
148+
return big.NewInt(0) // Gas price lower than base fee, effective tip is zero
149+
}
150+
151+
return new(big.Int).Sub(gasPrice, baseFee)
152+
}

0 commit comments

Comments
 (0)