erigon-pulse/cmd/rpcdaemon/commands/eth_uncles.go
Alex Sharov 331dcd45eb
Store receipts separately - one record per tx (#1271)
* squash

* add --database flag to integration

* clean

* split to 2 buckets

* split to 2 buckets

* split to 2 buckets

* split to 2 buckets

* split to 2 buckets

* save progress

* save progress

* improve test

* improve test

* save progress

* change app logic

* change app logic

* return err from rawdb package

* don't clean automatically

* don't clean automatically

* clean

* clean

* clean

* don't rely on `make clean`

* improve cbor code

* clean

* clean

* clean

* fix tests

* rebase master

* stop on error: headers stage

* make TxDb walk and multiwalk safe

* Fix panics

Co-authored-by: Alexey Akhunov <akhounov@gmail.com>
2020-10-25 08:38:55 +00:00

129 lines
3.8 KiB
Go

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/ethdb"
"github.com/ledgerwatch/turbo-geth/log"
"github.com/ledgerwatch/turbo-geth/rpc"
"github.com/ledgerwatch/turbo-geth/turbo/adapter/ethapi"
)
// GetUncleByBlockNumberAndIndex implements eth_getUncleByBlockNumberAndIndex. Returns information about an uncle given a block's number and the index of the uncle.
func (api *APIImpl) GetUncleByBlockNumberAndIndex(ctx context.Context, number rpc.BlockNumber, index hexutil.Uint) (map[string]interface{}, error) {
tx, err := api.dbReader.Begin(ctx, ethdb.RO)
if err != nil {
return nil, err
}
defer tx.Rollback()
blockNum, err := getBlockNumber(number, tx)
if err != nil {
return nil, err
}
block, err := rawdb.ReadBlockByNumber(tx, blockNum)
if err != nil {
return nil, err
}
if block == nil {
return nil, fmt.Errorf("block not found: %d", blockNum)
}
hash := block.Hash()
additionalFields := make(map[string]interface{})
td, err := rawdb.ReadTd(tx, block.Hash(), blockNum)
if err != nil {
return nil, err
}
additionalFields["totalDifficulty"] = (*hexutil.Big)(td)
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 implements eth_getUncleByBlockHashAndIndex. Returns information about an uncle given a block's hash and the index of the uncle.
func (api *APIImpl) GetUncleByBlockHashAndIndex(ctx context.Context, hash common.Hash, index hexutil.Uint) (map[string]interface{}, error) {
tx, err := api.dbReader.Begin(ctx, ethdb.RO)
if err != nil {
return nil, err
}
defer tx.Rollback()
block, err := rawdb.ReadBlockByHash(tx, hash)
if err != nil {
return nil, err
}
if block == nil {
return nil, fmt.Errorf("block not found: %x", hash)
}
number := block.NumberU64()
additionalFields := make(map[string]interface{})
td, err := rawdb.ReadTd(tx, hash, number)
if err != nil {
return nil, err
}
additionalFields["totalDifficulty"] = (*hexutil.Big)(td)
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 implements eth_getUncleCountByBlockNumber. Returns the number of uncles in the block, if any.
func (api *APIImpl) GetUncleCountByBlockNumber(ctx context.Context, number rpc.BlockNumber) (*hexutil.Uint, error) {
n := hexutil.Uint(0)
tx, err := api.dbReader.Begin(ctx, ethdb.RO)
if err != nil {
return &n, err
}
defer tx.Rollback()
blockNum, err := getBlockNumber(number, tx)
if err != nil {
return &n, err
}
block, err := rawdb.ReadBlockByNumber(tx, blockNum)
if err != nil {
return nil, err
}
if block != nil {
n = hexutil.Uint(len(block.Uncles()))
}
return &n, nil
}
// GetUncleCountByBlockHash implements eth_getUncleCountByBlockHash. Returns the number of uncles in the block, if any.
func (api *APIImpl) GetUncleCountByBlockHash(ctx context.Context, hash common.Hash) (*hexutil.Uint, error) {
n := hexutil.Uint(0)
tx, err := api.dbReader.Begin(ctx, ethdb.RO)
if err != nil {
return &n, err
}
defer tx.Rollback()
block, err := rawdb.ReadBlockByHash(tx, hash)
if err != nil {
return &n, err
}
if block != nil {
n = hexutil.Uint(len(block.Uncles()))
}
return &n, nil
}