[F-2026-17754] TraceTransaction mixes Cosmos and Ethereum index domains #27
Open
AryaLanjewar3005 wants to merge 3 commits into
Open
[F-2026-17754] TraceTransaction mixes Cosmos and Ethereum index domains #27AryaLanjewar3005 wants to merge 3 commits into
AryaLanjewar3005 wants to merge 3 commits into
Conversation
…ugh event-based log parsing
…additional fields on KV hit
…ndex instead of TxIndex (F-2026-17754)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
fix(rpc): TraceTransaction predecessor assembly uses Ethereum tx index (F-2026-17754)
Summary
debug_traceTransactioncontained six distinct bugs in its predecessor-assembly loop, all stemming from the same root cause: the Cosmos transaction slot index (TxIndex) and the Ethereum execution counter (EthTxIndex) were used interchangeably, even though they diverge whenever a Cosmos tx holds multiple EVM messages, holds no EVM messages, or derived txs shift the Ethereum counter.This finding was originally patched in the
audit-fixesbranch (0.2.0). This PR ports and adapts that fix to the 0.5.0 codebase (audit/evm-merge-inconsistent-logs), resolving all six bugs with appropriate architectural adaptations, and adds a comprehensive unit and integration test suite that did not exist in the original patch.Root Cause
TxIndexis the ordinal position of a Cosmos tx envelope inblock.Txs[].EthTxIndexis the monotonically-increasing Ethereum execution counter that increments once per EVM execution (including derived txs) across the block. These counters are not interchangeable.The original code used
transaction.TxIndexas the outer predecessor-loop bound whileGetTxByTxIndexresolved by Ethereum index. They only coincide when every Cosmos tx holds exactly oneMsgEthereumTxand no derived txs exist.Six Bugs Fixed
Bug 1 — Loop bound used wrong index domain
Bug 2 — Same-Cosmos-tx entries were double-counted
When the target is a later message in a multi-message Cosmos tx, the outer loop would fetch the same Cosmos slot and add early messages that the after-loop also adds. A guard now skips same-slot entries:
Bug 3 — Derived tx event-scan loop was doubly wrong
The old approach scanned
parsedTxs.Txsfor derived txs beforetxAdditional.Hash. It both (a) skipped the tx attxAdditional.Hashitself — so the last derived tx in a chain was always missed — and (b) double-counted earlier derived txs already added by their own outer-loop iterations. Each outer-loop iteration corresponds to exactly one Ethereum execution, so the direct add is correct and complete:Bug 4 — Normal tx decoded from wrong Cosmos slot
blk.Block.Txs[i]used the Ethereum index to index the block's Cosmos tx array. Wheni(Ethereum index) andpredecessorTx.TxIndex(Cosmos slot) differ, the wrong raw bytes were decoded:Bug 5 — Inner loop excluded message AT MsgIndex
for j := 0; j < MsgIndex; j++added messages beforeMsgIndexbut never the message atMsgIndex, silently dropping the last EVM message of any multi-message predecessor Cosmos tx from the predecessor set:Bug 6 — Missing nil guard on
blockResblockRescould be nil whenBlockResultssucceeds but returns nil; addedblockRes != nil &&guard before indexing intoblockRes.TxsResults.Tests
New unit tests (
rpc/backend/tracing_test.go) — 11 test functionsTestTraceTransactionTestTraceTransactionEthTxIndexTestTraceTransactionMultiMsgSameCosmosTargetTestTraceTransactionMultiMsgTargetIsThirdTestTraceTransactionMultiMsgCosmosAsPredecessorTestTraceTransactionThreeTxBlockTestTraceTransactionDerivedTxAsPredecessorTestTraceTransactionDerivedTxAsTargetIntegration test fixes (
tests/integration/rpc/backend/test_tracing.go)RegisterTxSearchEmptymock — after the KV miss on an empty block, the code correctly falls through to CometBFT TxSearch before returning the error.RegisterTraceTransactionWithPredecessorstoRegisterTraceTransaction— the target hasEthTxIndex=0so zero predecessors is the correct expectation.What Was Different from the 0.2.0 Application
The original patch was written against Ethermint/Evmos 0.2.0 (
audit-fixesbranch). Applying the same fix to 0.5.0 required the following adaptations:1. Exported vs Unexported Backend Fields
All Backend fields are exported in 0.5.0. Every field reference was updated:
b.rpcClientb.RPCClientb.ctxb.Ctxb.clientCtxb.ClientCtxb.indexerb.Indexerb.loggerb.Loggerb.chainIDb.EvmChainIDb.queryClientb.QueryClient2.
MsgEthereumTx.FromType ChangeFromchanged fromstringin 0.2.0 to[]bytein 0.5.0. All test helpers updated accordingly (msg.From = from.Bytes()notfrom.String(), multi-msg reset usesnilnot"").3. Test Infrastructure Required Complete Rebuild
The original 0.2.0 patch included a test file, but 0.5.0 had no
BackendTestSuiteor mock helper infrastructure inrpc/backend/. Three new files were created from scratch:rpc/backend/backend_suite_test.go: ThesetupMockBackendhelper (fromtx_info_test.go) was reused rather than duplicating manual backend construction from 0.2.0. All field references updated for exported names. ATestMainwas added (not present in 0.2.0) becauseGetEthChainConfig()panics atNewBackendwithout prior chain config initialization — this was not an issue in the 0.2.0 test setup.rpc/backend/client_test.go: Mock helpers adapted from 0.2.0. TheChainIDvariable changed from aChainIDConfigstruct to a plainstring. TheRegisterTraceTransactionWithPredecessorsfunction usesmock.Anythingfor the TraceTx request argument instead ofmock.MatchedBywith a custom proto comparison function — the 0.5.0MsgEthereumTx.Hashfield is afunctype which cannot be compared with!=, causing a compile error in the custom matcher.rpc/backend/tracing_test.go: 11 unit test functions ported and adapted from 0.2.0.4. Pre-existing Bug Fixed:
encoding/config.goMissing EVM Type RegistrationDiscovered during test development:
encoding.MakeConfig()did not callevmtypes.RegisterInterfaces(interfaceRegistry), so theTxDecodercould not resolve the/cosmos.evm.vm.v1.MsgEthereumTxproto type URL. This caused the KV indexer to silently skip every EVM tx duringIndexBlock. This was a pre-existing 0.5.0 bug (not present in 0.2.0 which used a different encoding setup). Fixed by adding one line toMakeConfig:5. Integration Test Corrections (Two Pre-existing Bugs Exposed)
The integration test at
tests/integration/rpc/backend/test_tracing.gohad two bugs that were only exposed once the chain config initialization and KV indexer registration issues were fixed:"fail - tx not found": The
registerMockwasfunc() {}(empty). After our fix, a KV miss on an empty block now correctly falls through to CometBFT TxSearch. Without a registered TxSearch mock, testify panics. AddedRegisterTxSearchEmpty(client, query)."pass - transaction found in a block with multiple transactions": The mock expected
msgEthereumTxto be its own predecessor (RegisterTraceTransactionWithPredecessors(..., []*evmtypes.MsgEthereumTx{msgEthereumTx})). However, the traced tx is atEthTxIndex=0— it is the first tx in the block and has zero predecessors. Changed toRegisterTraceTransaction(QueryClient, msgEthereumTx). This was a pre-existing wrong expectation in the test.Files Changed
rpc/backend/tracing.goencoding/config.goevmtypes.RegisterInterfaces(pre-existing bug fix)rpc/backend/backend_suite_test.gorpc/backend/client_test.gorpc/backend/tracing_test.gotests/integration/rpc/backend/test_tracing.goAuthor Checklist
All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.
I have...
mainbranch