Skip to content

Commit

Permalink
tapgarden: add GetBlockHash to ChainBridge interface and implementations
Browse files Browse the repository at this point in the history
GetBlockHash accepts a block height and, if successful, returns the
corresponding block hash.
  • Loading branch information
ffranr committed Jul 13, 2023
1 parent 9c8d7b1 commit ae3e5e9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
14 changes: 14 additions & 0 deletions chain_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ func (l *LndRpcChainBridge) GetBlock(ctx context.Context,
return block, nil
}

// GetBlockHash returns the hash of the block in the best blockchain at the
// given height.
func (l *LndRpcChainBridge) GetBlockHash(ctx context.Context,
blockHeight int64) (chainhash.Hash, error) {

blockHash, err := l.lnd.ChainKit.GetBlockHash(ctx, blockHeight)
if err != nil {
return chainhash.Hash{}, fmt.Errorf("unable to retrieve "+
"block hash: %w", err)
}

return blockHash, nil
}

// CurrentHeight return the current height of the main chain.
func (l *LndRpcChainBridge) CurrentHeight(ctx context.Context) (uint32, error) {
info, err := l.lnd.Client.GetInfo(ctx)
Expand Down
4 changes: 4 additions & 0 deletions tapgarden/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ type ChainBridge interface {
// GetBlock returns a chain block given its hash.
GetBlock(context.Context, chainhash.Hash) (*wire.MsgBlock, error)

// GetBlockHash returns the hash of the block in the best blockchain at
// the given height.
GetBlockHash(context.Context, int64) (chainhash.Hash, error)

// CurrentHeight return the current height of the main chain.
CurrentHeight(context.Context) (uint32, error)

Expand Down
8 changes: 8 additions & 0 deletions tapgarden/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,14 @@ func (m *MockChainBridge) GetBlock(ctx context.Context,
return &wire.MsgBlock{}, nil
}

// GetBlockHash returns the hash of the block in the best blockchain at the
// given height.
func (m *MockChainBridge) GetBlockHash(ctx context.Context,
blockHeight int64) (chainhash.Hash, error) {

return chainhash.Hash{}, nil
}

func (m *MockChainBridge) CurrentHeight(_ context.Context) (uint32, error) {
return 0, nil
}
Expand Down

0 comments on commit ae3e5e9

Please sign in to comment.