From a8296542ee019c8754724e012c74229647166a06 Mon Sep 17 00:00:00 2001 From: Arya Lanjewar <102943033+AryaLanjewar3005@users.noreply.github.com> Date: Mon, 29 Sep 2025 13:54:33 +0530 Subject: [PATCH 1/4] fixed trace_transaction (yet to test) --- rpc/backend/blocks.go | 25 +++++++++++ rpc/backend/tracing.go | 95 +++++++++++++++++++++++++++++++++++------- 2 files changed, 104 insertions(+), 16 deletions(-) diff --git a/rpc/backend/blocks.go b/rpc/backend/blocks.go index d69babbd5..7e2c43b9f 100644 --- a/rpc/backend/blocks.go +++ b/rpc/backend/blocks.go @@ -335,6 +335,31 @@ func (b *Backend) parseDerivedTxFromAdditionalFields( ethMsg.From = additional.Sender.Hex() return ethMsg } +func (b *Backend) parseDerivedTxFromAdditionalFieldsForTrace( + additional *rpctypes.TxResultAdditionalFields, +) *evmtypes.MsgEthereumTx { + recipient := additional.Recipient + t := ethtypes.NewTx(ðtypes.LegacyTx{ + Nonce: additional.Nonce, + Data: additional.Data, + Gas: additional.GasUsed, + To: &recipient, + GasPrice: nil, + Value: additional.Value, + V: big.NewInt(27), + R: big.NewInt(1), + S: big.NewInt(1), + }) + ethMsg := &evmtypes.MsgEthereumTx{} + err := ethMsg.FromEthereumTx(t) + if err != nil { + b.logger.Error("can not create eth msg", err.Error()) + return nil + } + ethMsg.Hash = additional.Hash.Hex() + ethMsg.From = additional.Sender.Hex() + return ethMsg +} // HeaderByNumber returns the block header identified by height. func (b *Backend) HeaderByNumber(blockNum rpctypes.BlockNumber) (*ethtypes.Header, error) { diff --git a/rpc/backend/tracing.go b/rpc/backend/tracing.go index 3788ff88d..2432c2884 100644 --- a/rpc/backend/tracing.go +++ b/rpc/backend/tracing.go @@ -18,7 +18,7 @@ import ( // and returns them as a JSON object. func (b *Backend) TraceTransaction(hash common.Hash, config *evmtypes.TraceConfig) (interface{}, error) { // Get transaction by hash - transaction, _, err := b.GetTxByEthHash(hash) + transaction, additional, err := b.GetTxByEthHash(hash) if err != nil { b.logger.Debug("tx not found", "hash", hash) return nil, err @@ -46,19 +46,54 @@ func (b *Backend) TraceTransaction(hash common.Hash, config *evmtypes.TraceConfi } var predecessors []*evmtypes.MsgEthereumTx - for _, txBz := range blk.Block.Txs[:transaction.TxIndex] { - tx, err := b.clientCtx.TxConfig.TxDecoder()(txBz) + // for _, txBz := range blk.Block.Txs[:transaction.TxIndex] { // (block.Block.Txs[res.TxIndex] + // tx, err := b.clientCtx.TxConfig.TxDecoder()(txBz) + // if err != nil { + // b.logger.Debug("failed to decode transaction in block", "height", blk.Block.Height, "error", err.Error()) + // continue + // } + // for _, msg := range tx.GetMsgs() { + // ethMsg, ok := msg.(*evmtypes.MsgEthereumTx) + // if !ok { + // continue + // } + + // predecessors = append(predecessors, ethMsg) + // } + // } + for i := 0; i < int(transaction.TxIndex); i++ { + _, txAdditional, err := b.GetTxByTxIndex(blk.Block.Height, uint(i)) if err != nil { - b.logger.Debug("failed to decode transaction in block", "height", blk.Block.Height, "error", err.Error()) + b.logger.Debug("failed to get tx by index", + "height", blk.Block.Height, + "index", i, + "error", err.Error()) continue } - for _, msg := range tx.GetMsgs() { - ethMsg, ok := msg.(*evmtypes.MsgEthereumTx) - if !ok { - continue + + if txAdditional != nil { + // Handle synthetic EVM transaction + ethMsg := b.parseDerivedTxFromAdditionalFieldsForTrace(txAdditional) + if ethMsg != nil { + predecessors = append(predecessors, ethMsg) } + continue + } - predecessors = append(predecessors, ethMsg) + // Fallback: decode as normal Cosmos tx + tx, err := b.clientCtx.TxConfig.TxDecoder()(blk.Block.Txs[i]) + if err != nil { + b.logger.Debug("failed to decode transaction in block", + "height", blk.Block.Height, + "index", i, + "error", err.Error()) + continue + } + + for _, msg := range tx.GetMsgs() { + if ethMsg, ok := msg.(*evmtypes.MsgEthereumTx); ok { + predecessors = append(predecessors, ethMsg) + } } } @@ -70,18 +105,46 @@ func (b *Backend) TraceTransaction(hash common.Hash, config *evmtypes.TraceConfi // add predecessor messages in current cosmos tx index := int(transaction.MsgIndex) // #nosec G115 + for i := 0; i < index; i++ { - ethMsg, ok := tx.GetMsgs()[i].(*evmtypes.MsgEthereumTx) - if !ok { + msg := tx.GetMsgs()[i] + + // Check if it’s a normal Ethereum tx + if ethMsg, ok := msg.(*evmtypes.MsgEthereumTx); ok { + predecessors = append(predecessors, ethMsg) + continue + } + + // Check if it’s a synthetic tx (custom Msg type) + // We need to fetch additional info for synthetic tx reconstruction + _, txAdditional, err := b.GetTxByTxIndex(blk.Block.Height, uint(transaction.TxIndex)) + if err != nil { + b.logger.Debug("failed to get tx additional info", "error", err.Error()) continue } - predecessors = append(predecessors, ethMsg) + + if txAdditional != nil { + ethMsg := b.parseDerivedTxFromAdditionalFieldsForTrace(txAdditional) + if ethMsg != nil { + predecessors = append(predecessors, ethMsg) + } + } } + var ethMessage *evmtypes.MsgEthereumTx + var ok bool - ethMessage, ok := tx.GetMsgs()[transaction.MsgIndex].(*evmtypes.MsgEthereumTx) - if !ok { - b.logger.Debug("invalid transaction type", "type", fmt.Sprintf("%T", tx)) - return nil, fmt.Errorf("invalid transaction type %T", tx) + if additional == nil { + ethMessage, ok = tx.GetMsgs()[transaction.MsgIndex].(*evmtypes.MsgEthereumTx) + if !ok { + b.logger.Debug("invalid transaction type", "type", fmt.Sprintf("%T", tx.GetMsgs()[transaction.MsgIndex])) + return nil, fmt.Errorf("invalid transaction type %T", tx.GetMsgs()[transaction.MsgIndex]) + } + } else { + ethMessage = b.parseDerivedTxFromAdditionalFieldsForTrace(additional) + if ethMessage == nil { + b.logger.Error("failed to get derived eth msg from additional fields") + return nil, fmt.Errorf("failed to get derived eth msg from additional fields") + } } nc, ok := b.clientCtx.Client.(tmrpcclient.NetworkClient) From 23f8d3c46c2bf085751961b16fa1d38a1d7c9ab5 Mon Sep 17 00:00:00 2001 From: Arya Lanjewar <102943033+AryaLanjewar3005@users.noreply.github.com> Date: Tue, 7 Oct 2025 11:57:14 +0530 Subject: [PATCH 2/4] fixed predecessor issue --- rpc/backend/blocks.go | 2 +- rpc/backend/tracing.go | 19 ++++++++++++++++++- rpc/backend/tx_info.go | 20 ++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/rpc/backend/blocks.go b/rpc/backend/blocks.go index 7e2c43b9f..93df253ad 100644 --- a/rpc/backend/blocks.go +++ b/rpc/backend/blocks.go @@ -344,7 +344,7 @@ func (b *Backend) parseDerivedTxFromAdditionalFieldsForTrace( Data: additional.Data, Gas: additional.GasUsed, To: &recipient, - GasPrice: nil, + GasPrice: big.NewInt(2500000), Value: additional.Value, V: big.NewInt(27), R: big.NewInt(1), diff --git a/rpc/backend/tracing.go b/rpc/backend/tracing.go index 2432c2884..d174528cd 100644 --- a/rpc/backend/tracing.go +++ b/rpc/backend/tracing.go @@ -23,6 +23,8 @@ func (b *Backend) TraceTransaction(hash common.Hash, config *evmtypes.TraceConfi b.logger.Debug("tx not found", "hash", hash) return nil, err } + fmt.Println("stage 1 (additionals) : ", additional) + fmt.Println("stage 2 (transaction) : ", transaction) // check if block number is 0 if transaction.Height == 0 { @@ -34,6 +36,11 @@ func (b *Backend) TraceTransaction(hash common.Hash, config *evmtypes.TraceConfi b.logger.Debug("block not found", "height", transaction.Height) return nil, err } + fmt.Println("checking Block : ", blk) + fmt.Println("transaction : ", transaction) + fmt.Println("Transaction_index", transaction.TxIndex) + fmt.Println("Transaction_index", transaction.EthTxIndex) + fmt.Println("checking Blocks inside : ", blk.Block.Txs) // check tx index is not out of bound if len(blk.Block.Txs) > math.MaxUint32 { @@ -96,32 +103,38 @@ func (b *Backend) TraceTransaction(hash common.Hash, config *evmtypes.TraceConfi } } } + fmt.Println("stage 3 (block predecessors) : ", predecessors) tx, err := b.clientCtx.TxConfig.TxDecoder()(blk.Block.Txs[transaction.TxIndex]) if err != nil { b.logger.Debug("tx not found", "hash", hash) return nil, err } + fmt.Println("decoded transaction (target) : ", tx.GetMsgs()[transaction.MsgIndex]) // add predecessor messages in current cosmos tx index := int(transaction.MsgIndex) // #nosec G115 + fmt.Println("checking index", index) for i := 0; i < index; i++ { msg := tx.GetMsgs()[i] + fmt.Println("predecessor number", i) // Check if it’s a normal Ethereum tx if ethMsg, ok := msg.(*evmtypes.MsgEthereumTx); ok { + fmt.Println("what! it is actually evm type mesage") predecessors = append(predecessors, ethMsg) continue } // Check if it’s a synthetic tx (custom Msg type) // We need to fetch additional info for synthetic tx reconstruction - _, txAdditional, err := b.GetTxByTxIndex(blk.Block.Height, uint(transaction.TxIndex)) + _, txAdditional, err := b.GetTxByEthHashAndMsgIndex(hash, i) if err != nil { b.logger.Debug("failed to get tx additional info", "error", err.Error()) continue } + fmt.Println("subMsg additional : ", txAdditional) if txAdditional != nil { ethMsg := b.parseDerivedTxFromAdditionalFieldsForTrace(txAdditional) @@ -130,6 +143,7 @@ func (b *Backend) TraceTransaction(hash common.Hash, config *evmtypes.TraceConfi } } } + fmt.Println("stage 4 (block+cosmostx predecessors) : ", predecessors) var ethMessage *evmtypes.MsgEthereumTx var ok bool @@ -146,6 +160,7 @@ func (b *Backend) TraceTransaction(hash common.Hash, config *evmtypes.TraceConfi return nil, fmt.Errorf("failed to get derived eth msg from additional fields") } } + fmt.Println("actual_message after parseDervied", ethMessage) nc, ok := b.clientCtx.Client.(tmrpcclient.NetworkClient) if !ok { @@ -182,6 +197,7 @@ func (b *Backend) TraceTransaction(hash common.Hash, config *evmtypes.TraceConfi if err != nil { return nil, err } + // fmt.Println("stage 5 (trace result) : ", traceResult) // Response format is unknown due to custom tracer config param // More information can be found here https://geth.ethereum.org/docs/dapp/tracing-filtered @@ -190,6 +206,7 @@ func (b *Backend) TraceTransaction(hash common.Hash, config *evmtypes.TraceConfi if err != nil { return nil, err } + // fmt.Println("stage 6 (decoded trace result) : ", decodedResult) return decodedResult, nil } diff --git a/rpc/backend/tx_info.go b/rpc/backend/tx_info.go index 889922f4b..7c0635de4 100644 --- a/rpc/backend/tx_info.go +++ b/rpc/backend/tx_info.go @@ -424,6 +424,26 @@ func (b *Backend) GetTxByEthHash(hash common.Hash) (*types.TxResult, *rpctypes.T return txResult, txAdditional, nil } +func (b *Backend) GetTxByEthHashAndMsgIndex(hash common.Hash, index int) (*types.TxResult, *rpctypes.TxResultAdditionalFields, error) { + if b.indexer != nil { + txRes, err := b.indexer.GetByTxHash(hash) + if err != nil { + return nil, nil, err + } + return txRes, nil, nil + } + + // fallback to tendermint tx indexer + query := fmt.Sprintf("%s.%s='%s'", evmtypes.TypeMsgEthereumTx, evmtypes.AttributeKeyEthereumTxHash, hash.Hex()) + txResult, txAdditional, err := b.queryTendermintTxIndexer(query, func(txs *rpctypes.ParsedTxs) *rpctypes.ParsedTx { + return txs.GetTxByMsgIndex(index) + }) + if err != nil { + return nil, nil, errorsmod.Wrapf(err, "GetTxByEthHash %s", hash.Hex()) + } + return txResult, txAdditional, nil +} + // GetTxByTxIndex uses `/tx_query` to find transaction by tx index of valid ethereum txs func (b *Backend) GetTxByTxIndex(height int64, index uint) (*types.TxResult, *rpctypes.TxResultAdditionalFields, error) { int32Index := int32(index) //#nosec G115 -- checked for int overflow already From 421162eece9df2ba1738c417a1f9d9932edac74b Mon Sep 17 00:00:00 2001 From: Arya Lanjewar <102943033+AryaLanjewar3005@users.noreply.github.com> Date: Tue, 7 Oct 2025 15:23:10 +0530 Subject: [PATCH 3/4] Removed Debug logs --- rpc/backend/tracing.go | 37 +------------------------------------ 1 file changed, 1 insertion(+), 36 deletions(-) diff --git a/rpc/backend/tracing.go b/rpc/backend/tracing.go index d174528cd..2e068333a 100644 --- a/rpc/backend/tracing.go +++ b/rpc/backend/tracing.go @@ -23,8 +23,6 @@ func (b *Backend) TraceTransaction(hash common.Hash, config *evmtypes.TraceConfi b.logger.Debug("tx not found", "hash", hash) return nil, err } - fmt.Println("stage 1 (additionals) : ", additional) - fmt.Println("stage 2 (transaction) : ", transaction) // check if block number is 0 if transaction.Height == 0 { @@ -36,11 +34,6 @@ func (b *Backend) TraceTransaction(hash common.Hash, config *evmtypes.TraceConfi b.logger.Debug("block not found", "height", transaction.Height) return nil, err } - fmt.Println("checking Block : ", blk) - fmt.Println("transaction : ", transaction) - fmt.Println("Transaction_index", transaction.TxIndex) - fmt.Println("Transaction_index", transaction.EthTxIndex) - fmt.Println("checking Blocks inside : ", blk.Block.Txs) // check tx index is not out of bound if len(blk.Block.Txs) > math.MaxUint32 { @@ -53,21 +46,6 @@ func (b *Backend) TraceTransaction(hash common.Hash, config *evmtypes.TraceConfi } var predecessors []*evmtypes.MsgEthereumTx - // for _, txBz := range blk.Block.Txs[:transaction.TxIndex] { // (block.Block.Txs[res.TxIndex] - // tx, err := b.clientCtx.TxConfig.TxDecoder()(txBz) - // if err != nil { - // b.logger.Debug("failed to decode transaction in block", "height", blk.Block.Height, "error", err.Error()) - // continue - // } - // for _, msg := range tx.GetMsgs() { - // ethMsg, ok := msg.(*evmtypes.MsgEthereumTx) - // if !ok { - // continue - // } - - // predecessors = append(predecessors, ethMsg) - // } - // } for i := 0; i < int(transaction.TxIndex); i++ { _, txAdditional, err := b.GetTxByTxIndex(blk.Block.Height, uint(i)) if err != nil { @@ -103,38 +81,29 @@ func (b *Backend) TraceTransaction(hash common.Hash, config *evmtypes.TraceConfi } } } - fmt.Println("stage 3 (block predecessors) : ", predecessors) tx, err := b.clientCtx.TxConfig.TxDecoder()(blk.Block.Txs[transaction.TxIndex]) if err != nil { b.logger.Debug("tx not found", "hash", hash) return nil, err } - fmt.Println("decoded transaction (target) : ", tx.GetMsgs()[transaction.MsgIndex]) // add predecessor messages in current cosmos tx index := int(transaction.MsgIndex) // #nosec G115 - fmt.Println("checking index", index) for i := 0; i < index; i++ { msg := tx.GetMsgs()[i] - fmt.Println("predecessor number", i) - // Check if it’s a normal Ethereum tx if ethMsg, ok := msg.(*evmtypes.MsgEthereumTx); ok { - fmt.Println("what! it is actually evm type mesage") predecessors = append(predecessors, ethMsg) continue } - - // Check if it’s a synthetic tx (custom Msg type) - // We need to fetch additional info for synthetic tx reconstruction + // Fetch additional data for predecessors _, txAdditional, err := b.GetTxByEthHashAndMsgIndex(hash, i) if err != nil { b.logger.Debug("failed to get tx additional info", "error", err.Error()) continue } - fmt.Println("subMsg additional : ", txAdditional) if txAdditional != nil { ethMsg := b.parseDerivedTxFromAdditionalFieldsForTrace(txAdditional) @@ -143,7 +112,6 @@ func (b *Backend) TraceTransaction(hash common.Hash, config *evmtypes.TraceConfi } } } - fmt.Println("stage 4 (block+cosmostx predecessors) : ", predecessors) var ethMessage *evmtypes.MsgEthereumTx var ok bool @@ -160,7 +128,6 @@ func (b *Backend) TraceTransaction(hash common.Hash, config *evmtypes.TraceConfi return nil, fmt.Errorf("failed to get derived eth msg from additional fields") } } - fmt.Println("actual_message after parseDervied", ethMessage) nc, ok := b.clientCtx.Client.(tmrpcclient.NetworkClient) if !ok { @@ -197,7 +164,6 @@ func (b *Backend) TraceTransaction(hash common.Hash, config *evmtypes.TraceConfi if err != nil { return nil, err } - // fmt.Println("stage 5 (trace result) : ", traceResult) // Response format is unknown due to custom tracer config param // More information can be found here https://geth.ethereum.org/docs/dapp/tracing-filtered @@ -206,7 +172,6 @@ func (b *Backend) TraceTransaction(hash common.Hash, config *evmtypes.TraceConfi if err != nil { return nil, err } - // fmt.Println("stage 6 (decoded trace result) : ", decodedResult) return decodedResult, nil } From 79018a1306d521192e9decf507831aec7f074a11 Mon Sep 17 00:00:00 2001 From: Arya Lanjewar <102943033+AryaLanjewar3005@users.noreply.github.com> Date: Tue, 14 Oct 2025 14:40:41 +0530 Subject: [PATCH 4/4] removed hardcoded gasPrice from parseDerivedTxFromAdditionalFieldsForTrace --- rpc/backend/blocks.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rpc/backend/blocks.go b/rpc/backend/blocks.go index 93df253ad..7e2c43b9f 100644 --- a/rpc/backend/blocks.go +++ b/rpc/backend/blocks.go @@ -344,7 +344,7 @@ func (b *Backend) parseDerivedTxFromAdditionalFieldsForTrace( Data: additional.Data, Gas: additional.GasUsed, To: &recipient, - GasPrice: big.NewInt(2500000), + GasPrice: nil, Value: additional.Value, V: big.NewInt(27), R: big.NewInt(1),