diff --git a/ethereum/rpc/backend/backend.go b/ethereum/rpc/backend/backend.go index 75a8fbe4..ca667d1e 100644 --- a/ethereum/rpc/backend/backend.go +++ b/ethereum/rpc/backend/backend.go @@ -145,51 +145,54 @@ func (e *EVMBackend) EthBlockFromTendermint( ethRPCTxs := []interface{}{} for i, txBz := range block.Txs { - tx, gas := types.DecodeTx(e.clientCtx, txBz) - - gasUsed += gas - - msg, isEthTx := tx.(*evmtypes.MsgEthereumTx) - - if !isEthTx { - // TODO: eventually support Cosmos txs in the block + tx, err := e.clientCtx.TxConfig.TxDecoder()(txBz) + if err != nil { + e.logger.WithError(err).Warningln("failed to decode transaction in block at height ", block.Height) continue } - hash := msg.AsTransaction().Hash() - if !fullTx { - ethRPCTxs = append(ethRPCTxs, hash) - continue + for _, msg := range tx.GetMsgs() { + ethMsg, ok := msg.(*evmtypes.MsgEthereumTx) + + if !ok { + continue + } + + // Todo: gasUsed does not consider the refund gas so it is incorrect, we need to extract it from the result + gasUsed += ethMsg.GetGas() + hash := ethMsg.AsTransaction().Hash() + if !fullTx { + ethRPCTxs = append(ethRPCTxs, hash) + continue + } + + // get full transaction from message data + from, err := ethMsg.GetSender(e.chainID) + if err != nil { + e.logger.WithError(err).Warningln("failed to get sender from already included transaction ", hash) + from = common.HexToAddress(ethMsg.From) + } + + txData, err := evmtypes.UnpackTxData(ethMsg.Data) + if err != nil { + e.logger.WithError(err).Debugln("decoding failed") + return nil, fmt.Errorf("failed to unpack tx data: %w", err) + } + + ethTx, err := types.NewTransactionFromData( + txData, + from, + hash, + common.BytesToHash(block.Hash()), + uint64(block.Height), + uint64(i), + ) + if err != nil { + e.logger.WithError(err).Debugln("NewTransactionFromData for receipt failed", "hash", hash.Hex) + continue + } + ethRPCTxs = append(ethRPCTxs, ethTx) } - - // get full transaction from message data - - from, err := msg.GetSender(e.chainID) - if err != nil { - from = common.HexToAddress(msg.From) - } - - txData, err := evmtypes.UnpackTxData(msg.Data) - if err != nil { - e.logger.WithError(err).Debugln("decoding failed") - return nil, fmt.Errorf("failed to unpack tx data: %w", err) - } - - ethTx, err := types.NewTransactionFromData( - txData, - from, - hash, - common.BytesToHash(block.Hash()), - uint64(block.Height), - uint64(i), - ) - - if err != nil { - e.logger.WithError(err).Debugln("NewTransactionFromData for receipt failed", "hash", hash.Hex) - continue - } - - ethRPCTxs = append(ethRPCTxs, ethTx) } blockBloomResp, err := queryClient.BlockBloom(types.ContextWithHeight(block.Height), &evmtypes.QueryBlockBloomRequest{}) diff --git a/ethereum/rpc/types/utils.go b/ethereum/rpc/types/utils.go index c21b841f..7c96f027 100644 --- a/ethereum/rpc/types/utils.go +++ b/ethereum/rpc/types/utils.go @@ -10,7 +10,6 @@ import ( tmtypes "github.com/tendermint/tendermint/types" "github.com/cosmos/cosmos-sdk/client" - sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" evmtypes "github.com/tharsis/ethermint/x/evm/types" @@ -206,25 +205,6 @@ func FormatBlock( } } -func DecodeTx(clientCtx client.Context, txBz tmtypes.Tx) (sdk.Tx, uint64) { - var gasUsed uint64 - txDecoder := clientCtx.TxConfig.TxDecoder() - - tx, err := txDecoder(txBz) - if err != nil { - return nil, 0 - } - - switch tx := tx.(type) { - case *evmtypes.MsgEthereumTx: - gasUsed = tx.GetGas() // NOTE: this doesn't include the gas refunded - case sdk.FeeTx: - gasUsed = tx.GetGas() - } - - return tx, gasUsed -} - type DataError interface { Error() string // returns the message ErrorData() interface{} // returns the error data