2020-10-18 19:44:28 +00:00
|
|
|
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
|
2020-10-20 21:16:28 +00:00
|
|
|
func (api *TgImpl) GetHeaderByNumber(ctx context.Context, blockNumber rpc.BlockNumber) (*types.Header, error) {
|
2020-10-18 19:44:28 +00:00
|
|
|
tx, err := api.db.Begin(ctx, nil, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2020-10-20 21:16:28 +00:00
|
|
|
header := rawdb.ReadHeaderByNumber(tx, uint64(blockNumber.Int64()))
|
2020-10-18 19:44:28 +00:00
|
|
|
if header == nil {
|
2020-10-20 21:16:28 +00:00
|
|
|
return nil, fmt.Errorf("block header not found: %d", blockNumber.Int64())
|
2020-10-18 19:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 := rawdb.ReadHeaderByHash(tx, hash)
|
|
|
|
if header == nil {
|
|
|
|
return nil, fmt.Errorf("block header not found: %s", hash.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return header, nil
|
|
|
|
}
|