rpc: fix DecodeTx (#227)

* fix decodeTx method

* fix logic

* fix singleswitchstatement lint error

* refactor logic

* add log

* remove unecessary function

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
This commit is contained in:
Thomas Nguy 2021-07-08 17:23:59 +09:00 committed by GitHub
parent 735f00d4a3
commit 3a398d9237
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 61 deletions

View File

@ -145,31 +145,35 @@ func (e *EVMBackend) EthBlockFromTendermint(
ethRPCTxs := []interface{}{} ethRPCTxs := []interface{}{}
for i, txBz := range block.Txs { for i, txBz := range block.Txs {
tx, gas := types.DecodeTx(e.clientCtx, txBz) tx, err := e.clientCtx.TxConfig.TxDecoder()(txBz)
if err != nil {
gasUsed += gas e.logger.WithError(err).Warningln("failed to decode transaction in block at height ", block.Height)
msg, isEthTx := tx.(*evmtypes.MsgEthereumTx)
if !isEthTx {
// TODO: eventually support Cosmos txs in the block
continue continue
} }
hash := msg.AsTransaction().Hash() 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 { if !fullTx {
ethRPCTxs = append(ethRPCTxs, hash) ethRPCTxs = append(ethRPCTxs, hash)
continue continue
} }
// get full transaction from message data // get full transaction from message data
from, err := ethMsg.GetSender(e.chainID)
from, err := msg.GetSender(e.chainID)
if err != nil { if err != nil {
from = common.HexToAddress(msg.From) e.logger.WithError(err).Warningln("failed to get sender from already included transaction ", hash)
from = common.HexToAddress(ethMsg.From)
} }
txData, err := evmtypes.UnpackTxData(msg.Data) txData, err := evmtypes.UnpackTxData(ethMsg.Data)
if err != nil { if err != nil {
e.logger.WithError(err).Debugln("decoding failed") e.logger.WithError(err).Debugln("decoding failed")
return nil, fmt.Errorf("failed to unpack tx data: %w", err) return nil, fmt.Errorf("failed to unpack tx data: %w", err)
@ -183,14 +187,13 @@ func (e *EVMBackend) EthBlockFromTendermint(
uint64(block.Height), uint64(block.Height),
uint64(i), uint64(i),
) )
if err != nil { if err != nil {
e.logger.WithError(err).Debugln("NewTransactionFromData for receipt failed", "hash", hash.Hex) e.logger.WithError(err).Debugln("NewTransactionFromData for receipt failed", "hash", hash.Hex)
continue continue
} }
ethRPCTxs = append(ethRPCTxs, ethTx) ethRPCTxs = append(ethRPCTxs, ethTx)
} }
}
blockBloomResp, err := queryClient.BlockBloom(types.ContextWithHeight(block.Height), &evmtypes.QueryBlockBloomRequest{}) blockBloomResp, err := queryClient.BlockBloom(types.ContextWithHeight(block.Height), &evmtypes.QueryBlockBloomRequest{})
if err != nil { if err != nil {

View File

@ -10,7 +10,6 @@ import (
tmtypes "github.com/tendermint/tendermint/types" tmtypes "github.com/tendermint/tendermint/types"
"github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
evmtypes "github.com/tharsis/ethermint/x/evm/types" 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 { type DataError interface {
Error() string // returns the message Error() string // returns the message
ErrorData() interface{} // returns the error data ErrorData() interface{} // returns the error data