Skip to content

Commit 03d3aad

Browse files
mmsqealjo242vladjdk
authored
fix: wrong TransactionIndex in receipt (#723)
* fix: avoid 0 TransactionIndex in receipt * add test --------- Co-authored-by: Alex | Cosmos Labs <[email protected]> Co-authored-by: Vlad J <[email protected]>
1 parent 43b70ee commit 03d3aad

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- [\#687](https://github.com/cosmos/evm/pull/687) Avoid blocking node shutdown when evm indexer is enabled, log startup failures instead of using errgroup.
2323
- [\#689](https://github.com/cosmos/evm/pull/689) Align debug addr for hex address.
2424
- [\#668](https://github.com/cosmos/evm/pull/668) Fix panic in legacy mempool when Reset() was called with a skipped header between old and new block.
25+
- [\#723](https://github.com/cosmos/evm/pull/723) Fix TransactionIndex in receipt generation to use actual EthTxIndex instead of loop index.
2526
- [\#729](https://github.com/cosmos/evm/pull/729) Remove non-deterministic state mutation from EVM pre-blocker.
2627
- [\#725](https://github.com/cosmos/evm/pull/725) Fix inconsistent block hash in json-rpc.
2728
- [\#727](https://github.com/cosmos/evm/pull/727) Avoid nil pointer for `tx evm raw` due to uninitialized EVM coin info.

rpc/backend/comet_to_eth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ func (b *Backend) ReceiptsFromCometBlock(
296296
// transaction corresponding to this receipt.
297297
BlockHash: blockHash,
298298
BlockNumber: big.NewInt(resBlock.Block.Height),
299-
TransactionIndex: uint(i), // #nosec G115 -- checked for int overflow already
299+
TransactionIndex: uint(txResult.EthTxIndex), // #nosec G115 -- checked for int overflow already
300300
}
301301

302302
receipts[i] = receipt

rpc/backend/tx_info_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ import (
2323
"github.com/cosmos/evm/indexer"
2424
"github.com/cosmos/evm/rpc/backend/mocks"
2525
rpctypes "github.com/cosmos/evm/rpc/types"
26+
servertypes "github.com/cosmos/evm/server/types"
2627
"github.com/cosmos/evm/testutil/constants"
2728
utiltx "github.com/cosmos/evm/testutil/tx"
2829
evmtypes "github.com/cosmos/evm/x/vm/types"
2930

3031
"github.com/cosmos/cosmos-sdk/client"
32+
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
3133
"github.com/cosmos/cosmos-sdk/crypto/keyring"
3234
"github.com/cosmos/cosmos-sdk/server"
3335
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -404,3 +406,100 @@ func TestCreateAccessList(t *testing.T) {
404406
})
405407
}
406408
}
409+
410+
func buildMsgEthereumTx(t *testing.T) *evmtypes.MsgEthereumTx {
411+
t.Helper()
412+
from, _ := utiltx.NewAddrKey()
413+
ethTxParams := evmtypes.EvmTxArgs{
414+
ChainID: new(big.Int).SetUint64(constants.ExampleChainID.EVMChainID),
415+
Nonce: uint64(0),
416+
To: &common.Address{},
417+
Amount: big.NewInt(0),
418+
GasLimit: 100000,
419+
GasPrice: big.NewInt(1),
420+
}
421+
msgEthereumTx := evmtypes.NewTx(&ethTxParams)
422+
msgEthereumTx.From = from.Bytes()
423+
return msgEthereumTx
424+
}
425+
426+
type MockIndexer struct {
427+
txResults map[common.Hash]*servertypes.TxResult
428+
}
429+
430+
func (m *MockIndexer) LastIndexedBlock() (int64, error) {
431+
return 0, nil
432+
}
433+
434+
func (m *MockIndexer) IndexBlock(block *tmtypes.Block, txResults []*abcitypes.ExecTxResult) error {
435+
return nil
436+
}
437+
438+
func (m *MockIndexer) GetByTxHash(hash common.Hash) (*servertypes.TxResult, error) {
439+
if result, exists := m.txResults[hash]; exists {
440+
return result, nil
441+
}
442+
return nil, nil
443+
}
444+
445+
func (m *MockIndexer) GetByBlockAndIndex(blockNumber int64, txIndex int32) (*servertypes.TxResult, error) {
446+
return nil, nil
447+
}
448+
449+
func TestReceiptsFromCometBlock(t *testing.T) {
450+
backend := setupMockBackend(t)
451+
height := int64(100)
452+
resBlock := &tmrpctypes.ResultBlock{
453+
Block: &tmtypes.Block{
454+
Header: tmtypes.Header{
455+
Height: height,
456+
},
457+
},
458+
}
459+
anyData := codectypes.UnsafePackAny(&evmtypes.MsgEthereumTxResponse{Hash: "hash"})
460+
txMsgData := &sdk.TxMsgData{MsgResponses: []*codectypes.Any{anyData}}
461+
encodingConfig := encoding.MakeConfig(constants.ExampleChainID.EVMChainID)
462+
encodedData, err := encodingConfig.Codec.Marshal(txMsgData)
463+
require.NoError(t, err)
464+
blockRes := &tmrpctypes.ResultBlockResults{
465+
Height: height,
466+
TxsResults: []*abcitypes.ExecTxResult{{Code: 0, Data: encodedData}},
467+
}
468+
tcs := []struct {
469+
name string
470+
ethTxIndex int32
471+
}{
472+
{"tx_with_index_5", 5},
473+
{"tx_with_index_10", 10},
474+
}
475+
for _, tc := range tcs {
476+
t.Run(tc.name, func(t *testing.T) {
477+
msgs := []*evmtypes.MsgEthereumTx{
478+
buildMsgEthereumTx(t),
479+
}
480+
expectedTxResult := &servertypes.TxResult{
481+
Height: height,
482+
TxIndex: 0,
483+
EthTxIndex: tc.ethTxIndex,
484+
MsgIndex: 0,
485+
}
486+
mockIndexer := &MockIndexer{
487+
txResults: map[common.Hash]*servertypes.TxResult{
488+
msgs[0].Hash(): expectedTxResult,
489+
},
490+
}
491+
backend.Indexer = mockIndexer
492+
mockEVMQueryClient := backend.QueryClient.QueryClient.(*mocks.EVMQueryClient)
493+
mockEVMQueryClient.On("BaseFee", mock.Anything, mock.Anything).Return(&evmtypes.QueryBaseFeeResponse{}, nil)
494+
receipts, err := backend.ReceiptsFromCometBlock(resBlock, blockRes, msgs)
495+
require.NoError(t, err)
496+
require.Len(t, receipts, 1)
497+
actualTxIndex := receipts[0].TransactionIndex
498+
require.NotEqual(t, uint(0), actualTxIndex)
499+
require.Equal(t, uint(tc.ethTxIndex), actualTxIndex) // #nosec G115
500+
require.Equal(t, msgs[0].Hash(), receipts[0].TxHash)
501+
require.Equal(t, big.NewInt(height), receipts[0].BlockNumber)
502+
require.Equal(t, ethtypes.ReceiptStatusSuccessful, receipts[0].Status)
503+
})
504+
}
505+
}

0 commit comments

Comments
 (0)