rpc: fix eth_getTransactionByHash tx in mempool (#261)

Closes #260

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
This commit is contained in:
yihuang 2021-07-12 18:34:47 +08:00 committed by GitHub
parent c6dae31231
commit 8f73d556c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 1 deletions

View File

@ -604,9 +604,56 @@ func (e *PublicAPI) GetTransactionByHash(hash common.Hash) (*rpctypes.RPCTransac
res, err := e.GetTxByEthHash(hash) res, err := e.GetTxByEthHash(hash)
if err != nil { if err != nil {
e.logger.WithError(err).Debugln("tx not found", "hash", hash.Hex()) e.logger.WithError(err).Debugln("tx not found", "hash", hash.Hex())
// try to find tx in mempool
txs, err := e.backend.PendingTransactions()
if err != nil {
return nil, nil return nil, nil
} }
for _, tx := range txs {
if tx == nil {
return nil, fmt.Errorf("invalid tx in mempool")
}
if len((*tx).GetMsgs()) != 1 {
continue
}
msg, ok := (*tx).GetMsgs()[0].(*evmtypes.MsgEthereumTx)
if !ok {
continue
}
txhash := msg.AsTransaction().Hash()
if txhash != hash {
continue
}
from, err := msg.GetSender(e.chainIDEpoch)
if err != nil {
return nil, err
}
data, err := evmtypes.UnpackTxData(msg.Data)
if err != nil {
return nil, fmt.Errorf("failed to unpack tx data: %w", err)
}
rpctx, err := rpctypes.NewTransactionFromData(
data,
from,
hash,
common.Hash{},
uint64(0),
uint64(0),
)
if err != nil {
return nil, err
}
return rpctx, nil
}
}
resBlock, err := e.clientCtx.Client.Block(e.ctx, &res.Height) resBlock, err := e.clientCtx.Client.Block(e.ctx, &res.Height)
if err != nil { if err != nil {
e.logger.WithError(err).Debugln("block not found", "height", res.Height) e.logger.WithError(err).Debugln("block not found", "height", res.Height)

View File

@ -38,6 +38,7 @@ import (
func TestEth_Pending_GetBalance(t *testing.T) { func TestEth_Pending_GetBalance(t *testing.T) {
var res hexutil.Big var res hexutil.Big
var resTxHash common.Hash
rpcRes := Call(t, "eth_getBalance", []string{addrA, "latest"}) rpcRes := Call(t, "eth_getBalance", []string{addrA, "latest"})
err := res.UnmarshalJSON(rpcRes.Result) err := res.UnmarshalJSON(rpcRes.Result)
require.NoError(t, err) require.NoError(t, err)
@ -65,6 +66,12 @@ func TestEth_Pending_GetBalance(t *testing.T) {
rpcRes = Call(t, "eth_sendTransaction", param) rpcRes = Call(t, "eth_sendTransaction", param)
require.Nil(t, rpcRes.Error) require.Nil(t, rpcRes.Error)
err = resTxHash.UnmarshalJSON(rpcRes.Result)
require.NoError(t, err)
rpcRes = Call(t, "eth_getTransactionByHash", []string{resTxHash.Hex()})
require.Nil(t, rpcRes.Error)
rpcRes = Call(t, "eth_getBalance", []string{addrA, "pending"}) rpcRes = Call(t, "eth_getBalance", []string{addrA, "pending"})
err = res.UnmarshalJSON(rpcRes.Result) err = res.UnmarshalJSON(rpcRes.Result)
require.NoError(t, err) require.NoError(t, err)