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"
2020-10-25 08:38:55 +00:00
"github.com/ledgerwatch/turbo-geth/ethdb"
2020-10-14 15:59:42 +00:00
"github.com/ledgerwatch/turbo-geth/rpc"
)
2020-10-24 17:03:52 +00:00
// GetTransactionByHash implements eth_getTransactionByHash. Returns information about a transaction given the transaction's hash.
2020-10-14 15:59:42 +00:00
func ( api * APIImpl ) GetTransactionByHash ( ctx context . Context , hash common . Hash ) ( * RPCTransaction , error ) {
2021-04-03 06:26:00 +00:00
tx , err := api . db . BeginRo ( ctx )
2020-10-14 15:59:42 +00:00
if err != nil {
return nil , err
}
defer tx . Rollback ( )
// https://infura.io/docs/ethereum/json-rpc/eth-getTransactionByHash
2021-03-30 09:53:54 +00:00
txn , blockHash , blockNumber , txIndex := rawdb . ReadTransaction ( ethdb . NewRoTxDb ( tx ) , hash )
2020-10-14 15:59:42 +00:00
if txn == nil {
2021-04-01 05:15:22 +00:00
return nil , nil // not error, see https://github.com/ledgerwatch/turbo-geth/issues/1645
2020-10-14 15:59:42 +00:00
}
return newRPCTransaction ( txn , blockHash , blockNumber , txIndex ) , nil
}
2020-10-24 17:03:52 +00:00
// GetTransactionByBlockHashAndIndex implements eth_getTransactionByBlockHashAndIndex. Returns information about a transaction given the block's hash and a transaction index.
2020-10-14 15:59:42 +00:00
func ( api * APIImpl ) GetTransactionByBlockHashAndIndex ( ctx context . Context , blockHash common . Hash , txIndex hexutil . Uint64 ) ( * RPCTransaction , error ) {
2021-04-03 06:26:00 +00:00
tx , err := api . db . BeginRo ( ctx )
2020-10-14 15:59:42 +00:00
if err != nil {
return nil , err
}
defer tx . Rollback ( )
// https://infura.io/docs/ethereum/json-rpc/eth-getTransactionByBlockHashAndIndex
2021-03-30 09:53:54 +00:00
block , err := rawdb . ReadBlockByHash ( ethdb . NewRoTxDb ( tx ) , blockHash )
2020-10-24 06:57:09 +00:00
if err != nil {
return nil , err
}
2020-10-14 15:59:42 +00:00
if block == nil {
2021-04-01 05:15:22 +00:00
return nil , nil // not error, see https://github.com/ledgerwatch/turbo-geth/issues/1645
2020-10-14 15:59:42 +00:00
}
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
}
2020-10-24 17:03:52 +00:00
// GetTransactionByBlockNumberAndIndex implements eth_getTransactionByBlockNumberAndIndex. Returns information about a transaction given a block number and transaction index.
2020-10-14 15:59:42 +00:00
func ( api * APIImpl ) GetTransactionByBlockNumberAndIndex ( ctx context . Context , blockNr rpc . BlockNumber , txIndex hexutil . Uint ) ( * RPCTransaction , error ) {
2021-04-03 06:26:00 +00:00
tx , err := api . db . BeginRo ( ctx )
2020-10-14 15:59:42 +00:00
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
}
2021-03-30 09:53:54 +00:00
block , err := rawdb . ReadBlockByNumber ( ethdb . NewRoTxDb ( tx ) , blockNum )
2020-10-24 06:57:09 +00:00
if err != nil {
return nil , err
}
2020-10-14 15:59:42 +00:00
if block == nil {
2021-04-01 05:15:22 +00:00
return nil , nil // not error, see https://github.com/ledgerwatch/turbo-geth/issues/1645
2020-10-14 15:59:42 +00:00
}
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
}