Skip to content

Commit fa753c3

Browse files
authored
fix getBlock endpoints transactionIndex (#2123)
* fix getBlock endpoints transactionIndex * test
1 parent 4f92ca3 commit fa753c3

File tree

5 files changed

+40
-13
lines changed

5 files changed

+40
-13
lines changed

evmrpc/block.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ func EncodeTmBlock(
335335
if !fullTx {
336336
transactions = append(transactions, hash)
337337
} else {
338-
newTx := ethapi.NewRPCTransaction(ethtx, blockhash, number.Uint64(), uint64(blockTime.Second()), uint64(receipt.TransactionIndex), baseFeePerGas, chainConfig)
338+
newTx := ethapi.NewRPCTransaction(ethtx, blockhash, number.Uint64(), uint64(blockTime.Second()), uint64(len(transactions)), baseFeePerGas, chainConfig)
339339
transactions = append(transactions, newTx)
340340
}
341341
case *wasmtypes.MsgExecuteContract:
@@ -350,7 +350,7 @@ func EncodeTmBlock(
350350
if !fullTx {
351351
transactions = append(transactions, "0x"+hex.EncodeToString(th[:]))
352352
} else {
353-
ti := uint64(receipt.TransactionIndex)
353+
ti := uint64(len(transactions))
354354
to := k.GetEVMAddressOrDefault(ctx, sdk.MustAccAddressFromBech32(m.Contract))
355355
transactions = append(transactions, &ethapi.RPCTransaction{
356356
BlockHash: &blockhash,
@@ -388,6 +388,8 @@ func EncodeTmBlock(
388388
rpcTx.To = &recipientEvmAddr
389389
amt := m.Amount.AmountOf("usei").Mul(state.SdkUseiToSweiMultiplier)
390390
rpcTx.Value = (*hexutil.Big)(amt.BigInt())
391+
ti := uint64(len(transactions))
392+
rpcTx.TransactionIndex = (*hexutil.Uint64)(&ti)
391393
transactions = append(transactions, rpcTx)
392394
}
393395
}

evmrpc/block_test.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ func TestEncodeWasmExecuteMsg(t *testing.T) {
303303
require.Nil(t, err)
304304
txs := res["transactions"].([]interface{})
305305
require.Equal(t, 1, len(txs))
306-
ti := uint64(1)
306+
ti := uint64(0)
307307
bh := common.HexToHash(MockBlockID.Hash.String())
308308
to := common.Address(toSeiAddr)
309309
require.Equal(t, &ethapi.RPCTransaction{
@@ -332,6 +332,7 @@ func TestEncodeBankTransferMsg(t *testing.T) {
332332
ToAddress: toSeiAddr.String(),
333333
Amount: sdk.NewCoins(sdk.NewCoin("usei", sdk.OneInt())),
334334
})
335+
ti := uint64(0)
335336
tx := b.GetTx()
336337
bz, _ := Encoder(tx)
337338
resBlock := coretypes.ResultBlock{
@@ -366,14 +367,15 @@ func TestEncodeBankTransferMsg(t *testing.T) {
366367
bh := common.HexToHash(MockBlockID.Hash.String())
367368
to := common.Address(toSeiAddr)
368369
require.Equal(t, &ethapi.RPCTransaction{
369-
BlockHash: &bh,
370-
BlockNumber: (*hexutil.Big)(big.NewInt(MockHeight8)),
371-
From: fromEvmAddr,
372-
To: &to,
373-
Value: (*hexutil.Big)(big.NewInt(1_000_000_000_000)),
374-
Hash: common.Hash(sha256.Sum256(bz)),
375-
V: nil,
376-
R: nil,
377-
S: nil,
370+
BlockHash: &bh,
371+
BlockNumber: (*hexutil.Big)(big.NewInt(MockHeight8)),
372+
From: fromEvmAddr,
373+
To: &to,
374+
Value: (*hexutil.Big)(big.NewInt(1_000_000_000_000)),
375+
Hash: common.Hash(sha256.Sum256(bz)),
376+
TransactionIndex: (*hexutil.Uint64)(&ti),
377+
V: nil,
378+
R: nil,
379+
S: nil,
378380
}, txs[0].(*ethapi.RPCTransaction))
379381
}

evmrpc/tests/block_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,16 @@ func TestGetBlockByNumber(t *testing.T) {
5858
},
5959
)
6060
}
61+
62+
func TestGetBlockSkipTxIndex(t *testing.T) {
63+
tx1 := signAndEncodeCosmosTx(bankSendMsg(mnemonic1), mnemonic1, 7, 0)
64+
tx2 := signAndEncodeTx(send(0), mnemonic1)
65+
SetupTestServer([][][]byte{{tx1, tx2}}, mnemonicInitializer(mnemonic1)).Run(
66+
func(port int) {
67+
res := sendRequestWithNamespace("eth", port, "getBlockByHash", common.HexToHash("0x2").Hex(), true)
68+
txs := res["result"].(map[string]any)["transactions"].([]any)
69+
require.Len(t, txs, 1)
70+
require.Equal(t, "0x0", txs[0].(map[string]any)["transactionIndex"].(string))
71+
},
72+
)
73+
}

evmrpc/tests/tx.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
1111
sdk "github.com/cosmos/cosmos-sdk/types"
12+
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
1213
"github.com/ethereum/go-ethereum/accounts/abi"
1314
"github.com/ethereum/go-ethereum/common"
1415
ethtypes "github.com/ethereum/go-ethereum/core/types"
@@ -107,3 +108,12 @@ func jsonExtractAsBytesFromArray(nonce uint64) ethtypes.TxData {
107108
ChainID: chainId,
108109
}
109110
}
111+
112+
func bankSendMsg(mnemonic string) sdk.Msg {
113+
recipient, _ := testkeeper.MockAddressPair()
114+
return &banktypes.MsgSend{
115+
FromAddress: getSeiAddrWithMnemonic(mnemonic).String(),
116+
ToAddress: recipient.String(),
117+
Amount: sdk.NewCoins(sdk.NewCoin("usei", sdk.OneInt())),
118+
}
119+
}

evmrpc/tests/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func signAndEncodeTx(txData ethtypes.TxData, mnemonic string) []byte {
204204
}
205205

206206
func signAndEncodeCosmosTx(msg sdk.Msg, mnemonic string, acctN uint64, seq uint64) []byte {
207-
tx := signCosmosTxWithMnemonic(msg, mnemonic1, acctN, seq)
207+
tx := signCosmosTxWithMnemonic(msg, mnemonic, acctN, seq)
208208
txBz, _ := testkeeper.EVMTestApp.GetTxConfig().TxEncoder()(tx)
209209
return txBz
210210
}

0 commit comments

Comments
 (0)