2020-09-14 06:59:07 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"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/core/types"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/log"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/rpc"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/turbo/adapter/ethapi"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetUncleByBlockNumberAndIndex returns the uncle block for the given block hash and index. When fullTx is true
|
|
|
|
// all transactions in the block are returned in full detail, otherwise only the transaction hash is returned.
|
|
|
|
func (api *APIImpl) GetUncleByBlockNumberAndIndex(ctx context.Context, number rpc.BlockNumber, index hexutil.Uint) (map[string]interface{}, error) {
|
2020-10-10 12:24:56 +00:00
|
|
|
tx, err := api.dbReader.Begin(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()
|
|
|
|
|
|
|
|
blockNum, err := getBlockNumber(number, tx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
block := rawdb.ReadBlockByNumber(tx, blockNum)
|
2020-09-14 06:59:07 +00:00
|
|
|
if block == nil {
|
|
|
|
return nil, fmt.Errorf("block not found: %d", blockNum)
|
|
|
|
}
|
|
|
|
hash := block.Hash()
|
|
|
|
additionalFields := make(map[string]interface{})
|
2020-10-10 12:24:56 +00:00
|
|
|
additionalFields["totalDifficulty"] = (*hexutil.Big)(rawdb.ReadTd(tx, block.Hash(), blockNum))
|
2020-09-14 06:59:07 +00:00
|
|
|
|
|
|
|
uncles := block.Uncles()
|
|
|
|
if index >= hexutil.Uint(len(uncles)) {
|
|
|
|
log.Debug("Requested uncle not found", "number", block.Number(), "hash", hash, "index", index)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
uncle := types.NewBlockWithHeader(uncles[index])
|
|
|
|
return ethapi.RPCMarshalBlock(uncle, false, false, additionalFields)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUncleByBlockHashAndIndex returns the uncle block for the given block hash and index. When fullTx is true
|
|
|
|
// all transactions in the block are returned in full detail, otherwise only the transaction hash is returned.
|
|
|
|
func (api *APIImpl) GetUncleByBlockHashAndIndex(ctx context.Context, hash common.Hash, index hexutil.Uint) (map[string]interface{}, error) {
|
2020-10-10 12:24:56 +00:00
|
|
|
tx, err := api.dbReader.Begin(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
block := rawdb.ReadBlockByHash(tx, hash)
|
2020-09-14 06:59:07 +00:00
|
|
|
if block == nil {
|
|
|
|
return nil, fmt.Errorf("block not found: %x", hash)
|
|
|
|
}
|
|
|
|
number := block.NumberU64()
|
|
|
|
additionalFields := make(map[string]interface{})
|
2020-10-10 12:24:56 +00:00
|
|
|
additionalFields["totalDifficulty"] = (*hexutil.Big)(rawdb.ReadTd(tx, hash, number))
|
2020-09-14 06:59:07 +00:00
|
|
|
|
|
|
|
uncles := block.Uncles()
|
|
|
|
if index >= hexutil.Uint(len(uncles)) {
|
|
|
|
log.Debug("Requested uncle not found", "number", block.Number(), "hash", hash, "index", index)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
uncle := types.NewBlockWithHeader(uncles[index])
|
|
|
|
|
|
|
|
return ethapi.RPCMarshalBlock(uncle, false, false, additionalFields)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUncleCountByBlockNumber returns number of uncles in the block for the given block number
|
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
|
|
|
|
|
|
|
tx, err := api.dbReader.Begin(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return &n, err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
blockNum, err := getBlockNumber(number, tx)
|
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-10 12:24:56 +00:00
|
|
|
block := rawdb.ReadBlockByNumber(tx, blockNum)
|
2020-09-14 06:59:07 +00:00
|
|
|
if block != nil {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// GetUncleCountByBlockHash returns number of uncles in the block for the given block hash
|
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)
|
2020-10-10 12:24:56 +00:00
|
|
|
tx, err := api.dbReader.Begin(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return &n, err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
block := rawdb.ReadBlockByHash(tx, hash)
|
2020-09-14 06:59:07 +00:00
|
|
|
if block != nil {
|
|
|
|
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
|
|
|
}
|