erigon-pulse/cmd/rpcdaemon/commands/tg_block.go
Alex Sharov 77a4b3305d
rawdb to return errors: part 1 (#1286)
* ReadBlockByNumber and ReadChainConfig return error

* Let's make rawdb package to return error. But by small steps.
High-level methods need to move first:

ReadBlockByNumber
ReadChainConfig
ReadBlockByHash
ReadHeaderByHash
WriteChainConfig
WriteDatabaseVersion
WriteBlock
DeleteTd
WriteTd
ReadTd
2020-10-24 07:57:09 +01:00

47 lines
1.1 KiB
Go

package commands
import (
"context"
"fmt"
"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/core/rawdb"
"github.com/ledgerwatch/turbo-geth/core/types"
"github.com/ledgerwatch/turbo-geth/rpc"
)
// GetHeaderByNumber returns a block's header by number
func (api *TgImpl) GetHeaderByNumber(ctx context.Context, blockNumber rpc.BlockNumber) (*types.Header, error) {
tx, err := api.db.Begin(ctx, nil, false)
if err != nil {
return nil, err
}
defer tx.Rollback()
header := rawdb.ReadHeaderByNumber(tx, uint64(blockNumber.Int64()))
if header == nil {
return nil, fmt.Errorf("block header not found: %d", blockNumber.Int64())
}
return header, nil
}
// GetHeaderByHash returns a block's header by hash
func (api *TgImpl) GetHeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
tx, err := api.db.Begin(ctx, nil, false)
if err != nil {
return nil, err
}
defer tx.Rollback()
header, err := rawdb.ReadHeaderByHash(tx, hash)
if err != nil {
return nil, err
}
if header == nil {
return nil, fmt.Errorf("block header not found: %s", hash.String())
}
return header, nil
}