rpc: fix get transaction by hash (#428)

* fix get transaction by hash

* add test case
This commit is contained in:
Thomas Nguy 2021-08-11 20:15:11 +09:00 committed by GitHub
parent bc790066c4
commit 52f42b5ff4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 13 deletions

View File

@ -497,11 +497,10 @@ 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.Debug("tx not found", "hash", hash.Hex(), "error", err.Error())
// try to find tx in mempool // try to find tx in mempool
txs, err := e.backend.PendingTransactions() txs, err := e.backend.PendingTransactions()
if err != nil { if err != nil {
e.logger.Debug("tx not found", "hash", hash.Hex(), "error", err.Error())
return nil, nil return nil, nil
} }
@ -512,18 +511,23 @@ func (e *PublicAPI) GetTransactionByHash(hash common.Hash) (*rpctypes.RPCTransac
continue continue
} }
rpctx, err := rpctypes.NewTransactionFromMsg( if msg.Hash == hash.Hex() {
msg, rpctx, err := rpctypes.NewTransactionFromMsg(
common.Hash{}, msg,
uint64(0), common.Hash{},
uint64(0), uint64(0),
e.chainIDEpoch, uint64(0),
) e.chainIDEpoch,
if err != nil { )
return nil, err if err != nil {
return nil, err
}
return rpctx, nil
} }
return rpctx, nil
} }
e.logger.Debug("tx not found", "hash", hash.Hex())
return nil, nil
} }
resBlock, err := e.clientCtx.Client.Block(e.ctx, &res.Height) resBlock, err := e.clientCtx.Client.Block(e.ctx, &res.Height)

View File

@ -259,6 +259,11 @@ func TestEth_Pending_GetTransactionByBlockNumberAndIndex(t *testing.T) {
} }
func TestEth_Pending_GetTransactionByHash(t *testing.T) { func TestEth_Pending_GetTransactionByHash(t *testing.T) {
// negative case, check that it returns empty.
rpcRes := Call(t, "eth_getTransactionByHash", []interface{}{"0xec5fa15e1368d6ac314f9f64118c5794f076f63c02e66f97ea5fe1de761a8973"})
require.Nil(t, rpcRes.Result)
// create a transaction.
data := "0x608060405234801561001057600080fd5b5061011e806100206000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c806302eb691b14602d575b600080fd5b603360ab565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101560715780820151818401526020810190506058565b50505050905090810190601f168015609d5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b60606040518060400160405280600d81526020017f617261736b61776173686572650000000000000000000000000000000000000081525090509056fea264697066735822122060917c5c2fab8c058a17afa6d3c1d23a7883b918ea3c7157131ea5b396e1aa7564736f6c63430007050033" data := "0x608060405234801561001057600080fd5b5061011e806100206000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c806302eb691b14602d575b600080fd5b603360ab565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101560715780820151818401526020810190506058565b50505050905090810190601f168015609d5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b60606040518060400160405280600d81526020017f617261736b61776173686572650000000000000000000000000000000000000081525090509056fea264697066735822122060917c5c2fab8c058a17afa6d3c1d23a7883b918ea3c7157131ea5b396e1aa7564736f6c63430007050033"
param := make([]map[string]string, 1) param := make([]map[string]string, 1)
param[0] = make(map[string]string) param[0] = make(map[string]string)
@ -277,7 +282,7 @@ func TestEth_Pending_GetTransactionByHash(t *testing.T) {
err := txHash.UnmarshalJSON(txRes.Result) err := txHash.UnmarshalJSON(txRes.Result)
require.NoError(t, err) require.NoError(t, err)
rpcRes := Call(t, "eth_getTransactionByHash", []interface{}{txHash}) rpcRes = Call(t, "eth_getTransactionByHash", []interface{}{txHash})
var pendingBlockTx map[string]interface{} var pendingBlockTx map[string]interface{}
err = json.Unmarshal(rpcRes.Result, &pendingBlockTx) err = json.Unmarshal(rpcRes.Result, &pendingBlockTx)
require.NoError(t, err) require.NoError(t, err)