2020-09-14 06:59:07 +00:00
package commands
import (
"context"
2021-05-20 18:25:53 +00:00
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/hexutil"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/rpc"
"github.com/ledgerwatch/erigon/turbo/adapter/ethapi"
2022-06-14 13:29:49 +00:00
"github.com/ledgerwatch/erigon/turbo/rpchelper"
2021-07-29 10:23:23 +00:00
"github.com/ledgerwatch/log/v3"
2020-09-14 06:59:07 +00:00
)
2020-10-24 17:03:52 +00:00
// GetUncleByBlockNumberAndIndex implements eth_getUncleByBlockNumberAndIndex. Returns information about an uncle given a block's number and the index of the uncle.
2020-09-14 06:59:07 +00:00
func ( api * APIImpl ) GetUncleByBlockNumberAndIndex ( ctx context . Context , number rpc . BlockNumber , index hexutil . Uint ) ( map [ string ] interface { } , error ) {
2021-04-03 06:26:00 +00:00
tx , err := api . db . BeginRo ( ctx )
2020-09-14 06:59:07 +00:00
if err != nil {
return nil , err
}
2020-10-10 12:24:56 +00:00
defer tx . Rollback ( )
2022-06-14 13:29:49 +00:00
blockNum , hash , _ , err := rpchelper . GetBlockNumber ( rpc . BlockNumberOrHashWithNumber ( number ) , tx , api . filters )
2020-10-10 12:24:56 +00:00
if err != nil {
return nil , err
}
2021-09-29 06:51:51 +00:00
block , err := api . blockByNumberWithSenders ( tx , blockNum )
2020-10-24 06:57:09 +00:00
if err != nil {
return nil , err
}
2020-09-14 06:59:07 +00:00
if block == nil {
2021-05-26 10:35:39 +00:00
return nil , nil // not error, see https://github.com/ledgerwatch/erigon/issues/1645
2020-09-14 06:59:07 +00:00
}
additionalFields := make ( map [ string ] interface { } )
2021-04-08 11:03:45 +00:00
td , err := rawdb . ReadTd ( tx , block . Hash ( ) , blockNum )
2020-10-24 06:57:09 +00:00
if err != nil {
return nil , err
}
additionalFields [ "totalDifficulty" ] = ( * hexutil . Big ) ( td )
2020-09-14 06:59:07 +00:00
uncles := block . Uncles ( )
if index >= hexutil . Uint ( len ( uncles ) ) {
2021-10-05 01:14:04 +00:00
log . Trace ( "Requested uncle not found" , "number" , block . Number ( ) , "hash" , hash , "index" , index )
2020-09-14 06:59:07 +00:00
return nil , nil
}
uncle := types . NewBlockWithHeader ( uncles [ index ] )
return ethapi . RPCMarshalBlock ( uncle , false , false , additionalFields )
}
2020-10-24 17:03:52 +00:00
// GetUncleByBlockHashAndIndex implements eth_getUncleByBlockHashAndIndex. Returns information about an uncle given a block's hash and the index of the uncle.
2020-09-14 06:59:07 +00:00
func ( api * APIImpl ) GetUncleByBlockHashAndIndex ( ctx context . Context , hash common . Hash , index hexutil . Uint ) ( map [ string ] interface { } , error ) {
2021-04-03 06:26:00 +00:00
tx , err := api . db . BeginRo ( ctx )
2020-10-10 12:24:56 +00:00
if err != nil {
return nil , err
}
defer tx . Rollback ( )
2021-09-29 06:51:51 +00:00
block , err := api . blockByHashWithSenders ( tx , hash )
2020-10-24 06:57:09 +00:00
if err != nil {
return nil , err
}
2020-09-14 06:59:07 +00:00
if block == nil {
2021-05-26 10:35:39 +00:00
return nil , nil // not error, see https://github.com/ledgerwatch/erigon/issues/1645
2020-09-14 06:59:07 +00:00
}
number := block . NumberU64 ( )
additionalFields := make ( map [ string ] interface { } )
2021-04-08 11:03:45 +00:00
td , err := rawdb . ReadTd ( tx , hash , number )
2020-10-24 06:57:09 +00:00
if err != nil {
return nil , err
}
additionalFields [ "totalDifficulty" ] = ( * hexutil . Big ) ( td )
2020-09-14 06:59:07 +00:00
uncles := block . Uncles ( )
if index >= hexutil . Uint ( len ( uncles ) ) {
2021-10-05 01:14:04 +00:00
log . Trace ( "Requested uncle not found" , "number" , block . Number ( ) , "hash" , hash , "index" , index )
2020-09-14 06:59:07 +00:00
return nil , nil
}
uncle := types . NewBlockWithHeader ( uncles [ index ] )
return ethapi . RPCMarshalBlock ( uncle , false , false , additionalFields )
}
2020-10-24 17:03:52 +00:00
// GetUncleCountByBlockNumber implements eth_getUncleCountByBlockNumber. Returns the number of uncles in the block, if any.
2020-10-10 12:24:56 +00:00
func ( api * APIImpl ) GetUncleCountByBlockNumber ( ctx context . Context , number rpc . BlockNumber ) ( * hexutil . Uint , error ) {
2020-09-14 06:59:07 +00:00
n := hexutil . Uint ( 0 )
2020-10-10 12:24:56 +00:00
2021-04-03 06:26:00 +00:00
tx , err := api . db . BeginRo ( ctx )
2020-10-10 12:24:56 +00:00
if err != nil {
return & n , err
}
defer tx . Rollback ( )
2022-06-14 13:29:49 +00:00
blockNum , _ , _ , err := rpchelper . GetBlockNumber ( rpc . BlockNumberOrHashWithNumber ( number ) , tx , api . filters )
2020-09-14 06:59:07 +00:00
if err != nil {
2020-10-10 12:24:56 +00:00
return & n , err
2020-09-14 06:59:07 +00:00
}
2020-10-24 17:03:52 +00:00
2021-09-29 06:51:51 +00:00
block , err := api . blockByNumberWithSenders ( tx , blockNum )
2020-10-24 06:57:09 +00:00
if err != nil {
return nil , err
}
2021-04-01 05:15:22 +00:00
if block == nil {
2021-05-26 10:35:39 +00:00
return nil , nil // not error, see https://github.com/ledgerwatch/erigon/issues/1645
2020-09-14 06:59:07 +00:00
}
2021-04-01 05:15:22 +00:00
n = hexutil . Uint ( len ( block . Uncles ( ) ) )
2020-10-10 12:24:56 +00:00
return & n , nil
2020-09-14 06:59:07 +00:00
}
2020-10-24 17:03:52 +00:00
// GetUncleCountByBlockHash implements eth_getUncleCountByBlockHash. Returns the number of uncles in the block, if any.
2020-10-10 12:24:56 +00:00
func ( api * APIImpl ) GetUncleCountByBlockHash ( ctx context . Context , hash common . Hash ) ( * hexutil . Uint , error ) {
2020-09-14 06:59:07 +00:00
n := hexutil . Uint ( 0 )
2021-04-03 06:26:00 +00:00
tx , err := api . db . BeginRo ( ctx )
2020-10-10 12:24:56 +00:00
if err != nil {
return & n , err
}
defer tx . Rollback ( )
2021-11-29 03:43:19 +00:00
number := rawdb . ReadHeaderNumber ( tx , hash )
if number == nil {
return nil , nil // not error, see https://github.com/ledgerwatch/erigon/issues/1645
}
block , err := api . blockWithSenders ( tx , hash , * number )
2020-10-24 06:57:09 +00:00
if err != nil {
2021-11-29 03:43:19 +00:00
return nil , err
2020-10-24 06:57:09 +00:00
}
2021-04-01 05:15:22 +00:00
if block == nil {
2021-05-26 10:35:39 +00:00
return nil , nil // not error, see https://github.com/ledgerwatch/erigon/issues/1645
2020-09-14 06:59:07 +00:00
}
2021-04-01 05:15:22 +00:00
n = hexutil . Uint ( len ( block . Uncles ( ) ) )
2020-10-10 12:24:56 +00:00
return & n , nil
2020-09-14 06:59:07 +00:00
}