Skip to content

Commit ae3e5e9

Browse files
committed
tapgarden: add GetBlockHash to ChainBridge interface and implementations
GetBlockHash accepts a block height and, if successful, returns the corresponding block hash.
1 parent 9c8d7b1 commit ae3e5e9

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

chain_bridge.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ func (l *LndRpcChainBridge) GetBlock(ctx context.Context,
6767
return block, nil
6868
}
6969

70+
// GetBlockHash returns the hash of the block in the best blockchain at the
71+
// given height.
72+
func (l *LndRpcChainBridge) GetBlockHash(ctx context.Context,
73+
blockHeight int64) (chainhash.Hash, error) {
74+
75+
blockHash, err := l.lnd.ChainKit.GetBlockHash(ctx, blockHeight)
76+
if err != nil {
77+
return chainhash.Hash{}, fmt.Errorf("unable to retrieve "+
78+
"block hash: %w", err)
79+
}
80+
81+
return blockHash, nil
82+
}
83+
7084
// CurrentHeight return the current height of the main chain.
7185
func (l *LndRpcChainBridge) CurrentHeight(ctx context.Context) (uint32, error) {
7286
info, err := l.lnd.Client.GetInfo(ctx)

tapgarden/interface.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ type ChainBridge interface {
252252
// GetBlock returns a chain block given its hash.
253253
GetBlock(context.Context, chainhash.Hash) (*wire.MsgBlock, error)
254254

255+
// GetBlockHash returns the hash of the block in the best blockchain at
256+
// the given height.
257+
GetBlockHash(context.Context, int64) (chainhash.Hash, error)
258+
255259
// CurrentHeight return the current height of the main chain.
256260
CurrentHeight(context.Context) (uint32, error)
257261

tapgarden/mock.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,14 @@ func (m *MockChainBridge) GetBlock(ctx context.Context,
298298
return &wire.MsgBlock{}, nil
299299
}
300300

301+
// GetBlockHash returns the hash of the block in the best blockchain at the
302+
// given height.
303+
func (m *MockChainBridge) GetBlockHash(ctx context.Context,
304+
blockHeight int64) (chainhash.Hash, error) {
305+
306+
return chainhash.Hash{}, nil
307+
}
308+
301309
func (m *MockChainBridge) CurrentHeight(_ context.Context) (uint32, error) {
302310
return 0, nil
303311
}

0 commit comments

Comments
 (0)