From c1c0d9f84b8d25e51c71c12cc2bfa1bb6ff2c004 Mon Sep 17 00:00:00 2001 From: Zsolt Felfoldi Date: Wed, 1 May 2019 14:01:57 +0200 Subject: [PATCH] internal/ethapi: always use backend to find transaction --- eth/api_backend.go | 6 ++++-- internal/ethapi/api.go | 30 +++++++++++++++--------------- internal/ethapi/backend.go | 2 +- les/api_backend.go | 2 +- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/eth/api_backend.go b/eth/api_backend.go index 821ca8ffa6..cd2cdb41c0 100644 --- a/eth/api_backend.go +++ b/eth/api_backend.go @@ -25,6 +25,7 @@ import ( "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/bloombits" + "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" @@ -174,8 +175,9 @@ func (b *EthAPIBackend) GetPoolTransaction(hash common.Hash) *types.Transaction return b.eth.txPool.Get(hash) } -func (b *EthAPIBackend) GetTransactionWithOdr(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) { - return nil, common.Hash{}, 0, 0, nil +func (b *EthAPIBackend) GetCanonicalTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) { + tx, blockHash, blockNumber, index := rawdb.ReadTransaction(b.eth.ChainDb(), txHash) + return tx, blockHash, blockNumber, index, nil } func (b *EthAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) { diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index cb0da8636c..5f4da51672 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -1152,35 +1152,35 @@ func (s *PublicTransactionPoolAPI) GetTransactionCount(ctx context.Context, addr } // GetTransactionByHash returns the transaction for the given hash -func (s *PublicTransactionPoolAPI) GetTransactionByHash(ctx context.Context, hash common.Hash) *RPCTransaction { +func (s *PublicTransactionPoolAPI) GetTransactionByHash(ctx context.Context, hash common.Hash) (*RPCTransaction, error) { // Try to return an already finalized transaction - if tx, blockHash, blockNumber, index := rawdb.ReadTransaction(s.b.ChainDb(), hash); tx != nil { - return newRPCTransaction(tx, blockHash, blockNumber, index) + tx, blockHash, blockNumber, index, err := s.b.GetCanonicalTransaction(ctx, hash) + if err != nil { + return nil, err } - if tx, blockHash, blockNumber, index, err := s.b.GetTransactionWithOdr(ctx, hash); tx != nil && err == nil { - return newRPCTransaction(tx, blockHash, blockNumber, index) + if tx != nil { + return newRPCTransaction(tx, blockHash, blockNumber, index), nil } // No finalized transaction, try to retrieve it from the pool if tx := s.b.GetPoolTransaction(hash); tx != nil { - return newRPCPendingTransaction(tx) + return newRPCPendingTransaction(tx), nil } // Transaction unknown, return as such - return nil + return nil, nil } // GetRawTransactionByHash returns the bytes of the transaction for the given hash. func (s *PublicTransactionPoolAPI) GetRawTransactionByHash(ctx context.Context, hash common.Hash) (hexutil.Bytes, error) { - var tx *types.Transaction - // Retrieve a finalized transaction, or a pooled otherwise - if tx, _, _, _ = rawdb.ReadTransaction(s.b.ChainDb(), hash); tx == nil { + tx, _, _, _, err := s.b.GetCanonicalTransaction(ctx, hash) + if err != nil { + return nil, err + } + if tx == nil { if tx = s.b.GetPoolTransaction(hash); tx == nil { - var err error - if tx, _, _, _, err = s.b.GetTransactionWithOdr(ctx, hash); tx == nil || err != nil { - // Transaction not found anywhere, abort - return nil, err - } + // Transaction not found anywhere, abort + return nil, nil } } // Serialize to RLP and return diff --git a/internal/ethapi/backend.go b/internal/ethapi/backend.go index c0e4051e09..e2e364cf00 100644 --- a/internal/ethapi/backend.go +++ b/internal/ethapi/backend.go @@ -62,7 +62,7 @@ type Backend interface { // TxPool API SendTx(ctx context.Context, signedTx *types.Transaction) error - GetTransactionWithOdr(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) + GetCanonicalTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) GetPoolTransactions() (types.Transactions, error) GetPoolTransaction(txHash common.Hash) *types.Transaction GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) diff --git a/les/api_backend.go b/les/api_backend.go index db9d4383b4..a18009e2fb 100644 --- a/les/api_backend.go +++ b/les/api_backend.go @@ -128,7 +128,7 @@ func (b *LesApiBackend) GetPoolTransaction(txHash common.Hash) *types.Transactio return b.eth.txPool.GetTransaction(txHash) } -func (b *LesApiBackend) GetTransactionWithOdr(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) { +func (b *LesApiBackend) GetCanonicalTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) { return light.GetTransaction(ctx, b.eth.odr, txHash) }