Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
rpc: fix get transaction by hash (#428)
Browse files Browse the repository at this point in the history
* fix get transaction by hash

* add test case
  • Loading branch information
thomas-nguy authored Aug 11, 2021
1 parent bc79006 commit 52f42b5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
28 changes: 16 additions & 12 deletions ethereum/rpc/namespaces/eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,11 +497,10 @@ func (e *PublicAPI) GetTransactionByHash(hash common.Hash) (*rpctypes.RPCTransac

res, err := e.GetTxByEthHash(hash)
if err != nil {
e.logger.Debug("tx not found", "hash", hash.Hex(), "error", err.Error())

// try to find tx in mempool
txs, err := e.backend.PendingTransactions()
if err != nil {
e.logger.Debug("tx not found", "hash", hash.Hex(), "error", err.Error())
return nil, nil
}

Expand All @@ -512,18 +511,23 @@ func (e *PublicAPI) GetTransactionByHash(hash common.Hash) (*rpctypes.RPCTransac
continue
}

rpctx, err := rpctypes.NewTransactionFromMsg(
msg,
common.Hash{},
uint64(0),
uint64(0),
e.chainIDEpoch,
)
if err != nil {
return nil, err
if msg.Hash == hash.Hex() {
rpctx, err := rpctypes.NewTransactionFromMsg(
msg,
common.Hash{},
uint64(0),
uint64(0),
e.chainIDEpoch,
)
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)
Expand Down
7 changes: 6 additions & 1 deletion tests/rpc/rpc_pending_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ func TestEth_Pending_GetTransactionByBlockNumberAndIndex(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"
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
Expand All @@ -277,7 +282,7 @@ func TestEth_Pending_GetTransactionByHash(t *testing.T) {
err := txHash.UnmarshalJSON(txRes.Result)
require.NoError(t, err)

rpcRes := Call(t, "eth_getTransactionByHash", []interface{}{txHash})
rpcRes = Call(t, "eth_getTransactionByHash", []interface{}{txHash})
var pendingBlockTx map[string]interface{}
err = json.Unmarshal(rpcRes.Result, &pendingBlockTx)
require.NoError(t, err)
Expand Down

0 comments on commit 52f42b5

Please sign in to comment.