internal/ethapi/api: refactor for txLookups

This commit is contained in:
Luke Williams 2019-02-23 01:53:56 +01:00
parent 763d4f52b1
commit d2c30c6ad2

View file

@ -17,7 +17,6 @@
package ethapi package ethapi
import ( import (
"bytes"
"context" "context"
"errors" "errors"
"fmt" "fmt"
@ -35,7 +34,6 @@ import (
"github.com/ubiq/go-ubiq/core/types" "github.com/ubiq/go-ubiq/core/types"
"github.com/ubiq/go-ubiq/core/vm" "github.com/ubiq/go-ubiq/core/vm"
"github.com/ubiq/go-ubiq/crypto" "github.com/ubiq/go-ubiq/crypto"
"github.com/ubiq/go-ubiq/ethdb"
"github.com/ubiq/go-ubiq/log" "github.com/ubiq/go-ubiq/log"
"github.com/ubiq/go-ubiq/p2p" "github.com/ubiq/go-ubiq/p2p"
"github.com/ubiq/go-ubiq/params" "github.com/ubiq/go-ubiq/params"
@ -949,42 +947,29 @@ func (s *PublicTransactionPoolAPI) GetTransactionCount(ctx context.Context, addr
} }
// GetTransactionByHash returns the transaction for the given hash // GetTransactionByHash returns the transaction for the given hash
func (s *PublicTransactionPoolAPI) GetTransactionByHash(ctx context.Context, hash common.Hash) (*RPCTransaction, error) { func (s *PublicTransactionPoolAPI) GetTransactionByHash(ctx context.Context, hash common.Hash) *RPCTransaction {
var tx *types.Transaction // Try to return an already finalized transaction
var isPending bool if tx, blockHash, blockNumber, index := core.GetTransaction(s.b.ChainDb(), hash); tx != nil {
var err error return newRPCTransaction(tx, blockHash, blockNumber, index)
if tx, isPending, err = getTransaction(s.b.ChainDb(), s.b, hash); err != nil {
log.Debug("Failed to retrieve transaction", "hash", hash, "err", err)
return nil, nil
} else if tx == nil {
return nil, nil
} }
if isPending { // No finalized transaction, try to retrieve it from the pool
return newRPCPendingTransaction(tx), nil if tx := s.b.GetPoolTransaction(hash); tx != nil {
return newRPCPendingTransaction(tx)
} }
// Transaction unknown, return as such
blockHash, _, _, err := getTransactionBlockData(s.b.ChainDb(), hash) return nil
if err != nil {
log.Debug("Failed to retrieve transaction block", "hash", hash, "err", err)
return nil, nil
}
if block, _ := s.b.GetBlock(ctx, blockHash); block != nil {
return newRPCTransaction(block, hash)
}
return nil, nil
} }
// GetRawTransactionByHash returns the bytes of the transaction for the given hash. // GetRawTransactionByHash returns the bytes of the transaction for the given hash.
func (s *PublicTransactionPoolAPI) GetRawTransactionByHash(ctx context.Context, hash common.Hash) (hexutil.Bytes, error) { func (s *PublicTransactionPoolAPI) GetRawTransactionByHash(ctx context.Context, hash common.Hash) (hexutil.Bytes, error) {
var tx *types.Transaction var tx *types.Transaction
if tx, _, err = getTransaction(s.b.ChainDb(), s.b, hash); err != nil { // Retrieve a finalized transaction, or a pooled otherwise
log.Debug("Failed to retrieve transaction", "hash", hash, "err", err) if tx, _, _, _ = core.GetTransaction(s.b.ChainDb(), hash); tx == nil {
return nil, nil if tx = s.b.GetPoolTransaction(hash); tx == nil {
} else if tx == nil { // Transaction not found anywhere, abort
return nil, nil return nil, nil
}
} }
// Serialize to RLP and return // Serialize to RLP and return
return rlp.EncodeToBytes(tx) return rlp.EncodeToBytes(tx)
@ -992,21 +977,8 @@ func (s *PublicTransactionPoolAPI) GetRawTransactionByHash(ctx context.Context,
// GetTransactionReceipt returns the transaction receipt for the given transaction hash. // GetTransactionReceipt returns the transaction receipt for the given transaction hash.
func (s *PublicTransactionPoolAPI) GetTransactionReceipt(hash common.Hash) (map[string]interface{}, error) { func (s *PublicTransactionPoolAPI) GetTransactionReceipt(hash common.Hash) (map[string]interface{}, error) {
receipt := core.GetReceipt(s.b.ChainDb(), hash) tx, blockHash, blockNumber, index := core.GetTransaction(s.b.ChainDb(), hash)
if receipt == nil { if tx == nil {
log.Debug("Receipt not found for transaction", "hash", hash)
return nil, nil
}
tx, _, err := getTransaction(s.b.ChainDb(), s.b, hash)
if err != nil {
log.Debug("Failed to retrieve transaction", "hash", hash, "err", err)
return nil, nil
}
txBlock, blockIndex, index, err := getTransactionBlockData(s.b.ChainDb(), hash)
if err != nil {
log.Debug("Failed to retrieve transaction block", "hash", hash, "err", err)
return nil, nil return nil, nil
} }
receipt, _, _, _ := core.GetReceipt(s.b.ChainDb(), hash) // Old receipts don't have the lookup data available receipt, _, _, _ := core.GetReceipt(s.b.ChainDb(), hash) // Old receipts don't have the lookup data available
@ -1019,8 +991,8 @@ func (s *PublicTransactionPoolAPI) GetTransactionReceipt(hash common.Hash) (map[
fields := map[string]interface{}{ fields := map[string]interface{}{
"root": hexutil.Bytes(receipt.PostState), "root": hexutil.Bytes(receipt.PostState),
"blockHash": txBlock, "blockHash": blockHash,
"blockNumber": hexutil.Uint64(blockIndex), "blockNumber": hexutil.Uint64(blockNumber),
"transactionHash": hash, "transactionHash": hash,
"transactionIndex": hexutil.Uint64(index), "transactionIndex": hexutil.Uint64(index),
"from": from, "from": from,