2020-10-12 14:17:34 +00:00
|
|
|
package commands
|
|
|
|
|
2020-10-14 15:59:42 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-10-12 14:17:34 +00:00
|
|
|
|
2020-10-14 15:59:42 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common/hexutil"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/rawdb"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/rpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetTransactionByHash returns the transaction for the given hash
|
|
|
|
func (api *APIImpl) GetTransactionByHash(ctx context.Context, hash common.Hash) (*RPCTransaction, error) {
|
|
|
|
tx, err := api.dbReader.Begin(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
// https://infura.io/docs/ethereum/json-rpc/eth-getTransactionByHash
|
|
|
|
txn, blockHash, blockNumber, txIndex := rawdb.ReadTransaction(tx, hash)
|
|
|
|
if txn == nil {
|
|
|
|
return nil, fmt.Errorf("transaction %#x not found", hash)
|
|
|
|
}
|
|
|
|
return newRPCTransaction(txn, blockHash, blockNumber, txIndex), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransactionByBlockHashAndIndex returns the transaction for the given block hash and index.
|
|
|
|
func (api *APIImpl) GetTransactionByBlockHashAndIndex(ctx context.Context, blockHash common.Hash, txIndex hexutil.Uint64) (*RPCTransaction, error) {
|
|
|
|
tx, err := api.dbReader.Begin(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
// https://infura.io/docs/ethereum/json-rpc/eth-getTransactionByBlockHashAndIndex
|
|
|
|
block := rawdb.ReadBlockByHash(tx, blockHash)
|
|
|
|
if block == nil {
|
|
|
|
return nil, fmt.Errorf("block %#x not found", blockHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
txs := block.Transactions()
|
|
|
|
if uint64(txIndex) >= uint64(len(txs)) {
|
|
|
|
return nil, fmt.Errorf("txIndex (%d) out of range (nTxs: %d)", uint64(txIndex), uint64(len(txs)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return newRPCTransaction(txs[txIndex], block.Hash(), block.NumberU64(), uint64(txIndex)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransactionByBlockNumberAndIndex returns the transaction for the given block number and index.
|
|
|
|
func (api *APIImpl) GetTransactionByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, txIndex hexutil.Uint) (*RPCTransaction, error) {
|
|
|
|
tx, err := api.dbReader.Begin(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
// https://infura.io/docs/ethereum/json-rpc/eth-getTransactionByBlockNumberAndIndex
|
|
|
|
blockNum, err := getBlockNumber(blockNr, tx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
block := rawdb.ReadBlockByNumber(tx, blockNum)
|
|
|
|
if block == nil {
|
|
|
|
return nil, fmt.Errorf("block %d not found", blockNum)
|
|
|
|
}
|
|
|
|
|
|
|
|
txs := block.Transactions()
|
|
|
|
if uint64(txIndex) >= uint64(len(txs)) {
|
|
|
|
return nil, fmt.Errorf("txIndex (%d) out of range (nTxs: %d)", uint64(txIndex), uint64(len(txs)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return newRPCTransaction(txs[txIndex], block.Hash(), block.NumberU64(), uint64(txIndex)), nil
|
|
|
|
}
|