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,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{})

View File

@ -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