|
| 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