Skip to content

Commit

Permalink
chainkit: add GetBlockHeader to ChainKit
Browse files Browse the repository at this point in the history
  • Loading branch information
jharveyb committed Nov 6, 2023
1 parent dd23e97 commit b93f98a
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions chainkit_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ type ChainKitClient interface {
GetBlock(ctx context.Context, hash chainhash.Hash) (*wire.MsgBlock,
error)

// GetBlockHeader returns a block header given the corresponding block
// hash.
GetBlockHeader(ctx context.Context,
hash chainhash.Hash) (*wire.BlockHeader, error)

// GetBestBlock returns the latest block hash and current height of the
// valid most-work chain.
GetBestBlock(ctx context.Context) (chainhash.Hash, int32, error)
Expand Down Expand Up @@ -77,6 +82,33 @@ func (s *chainKitClient) GetBlock(ctxParent context.Context,
return msgBlock, nil
}

// GetBlockHeader returns a block header given the corresponding block hash.
func (s *chainKitClient) GetBlockHeader(ctxParent context.Context,
hash chainhash.Hash) (*wire.BlockHeader, error) {

ctx, cancel := context.WithTimeout(ctxParent, s.timeout)
defer cancel()

macaroonAuth := s.chainMac.WithMacaroonAuth(ctx)
req := &chainrpc.GetBlockHeaderRequest{
BlockHash: hash[:],
}
resp, err := s.client.GetBlockHeader(macaroonAuth, req)
if err != nil {
return nil, err
}

// Convert raw block header bytes into wire.BlockHeader.
blockHeader := &wire.BlockHeader{}
blockReader := bytes.NewReader(resp.RawBlockHeader)
err = blockHeader.Deserialize(blockReader)
if err != nil {
return nil, err
}

return blockHeader, nil
}

// GetBestBlock returns the block hash and current height from the valid
// most-work chain.
func (s *chainKitClient) GetBestBlock(ctxParent context.Context) (chainhash.Hash,
Expand Down

0 comments on commit b93f98a

Please sign in to comment.