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"
2020-10-25 08:38:55 +00:00
"github.com/ledgerwatch/turbo-geth/ethdb"
2020-10-18 19:44:28 +00:00
"github.com/ledgerwatch/turbo-geth/rpc"
)
2020-10-24 17:03:52 +00:00
// GetHeaderByNumber implements tg_getHeaderByNumber. Returns a block's header given a block number ignoring the block's transaction and uncle list (may be faster).
2020-10-20 21:16:28 +00:00
func ( api * TgImpl ) GetHeaderByNumber ( ctx context . Context , blockNumber rpc . BlockNumber ) ( * types . Header , error ) {
2020-10-25 08:38:55 +00:00
tx , err := api . dbReader . Begin ( ctx , ethdb . RO )
2020-10-18 19:44:28 +00:00
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
}
2020-10-24 17:03:52 +00:00
// GetHeaderByHash implements tg_getHeaderByHash. Returns a block's header given a block's hash.
2020-10-18 19:44:28 +00:00
func ( api * TgImpl ) GetHeaderByHash ( ctx context . Context , hash common . Hash ) ( * types . Header , error ) {
2020-10-25 08:38:55 +00:00
tx , err := api . dbReader . Begin ( ctx , ethdb . RO )
2020-10-18 19:44:28 +00:00
if err != nil {
return nil , err
}
defer tx . Rollback ( )
2020-10-24 06:57:09 +00:00
header , err := rawdb . ReadHeaderByHash ( tx , hash )
if err != nil {
return nil , err
}
2020-10-18 19:44:28 +00:00
if header == nil {
return nil , fmt . Errorf ( "block header not found: %s" , hash . String ( ) )
}
return header , nil
}