Skip to content

Commit fb877f9

Browse files
committed
lndservices+tapgarden: add GetBlockHeaderByHeight to chain bridge
Extend the LND chain bridge service with a GetBlockHeaderByHeight method, which returns a block header for a given block height.
1 parent c2b0a37 commit fb877f9

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

lndservices/chain_bridge.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,50 @@ func (l *LndRpcChainBridge) GetBlockHeader(ctx context.Context,
151151
)
152152
}
153153

154+
// GetBlockHeaderByHeight returns a block header given the block height.
155+
func (l *LndRpcChainBridge) GetBlockHeaderByHeight(ctx context.Context,
156+
blockHeight int64) (*wire.BlockHeader, error) {
157+
158+
// First, we need to resolve the block hash at the given height.
159+
blockHash, err := fn.RetryFuncN(
160+
ctx, l.retryConfig, func() (chainhash.Hash, error) {
161+
var zero chainhash.Hash
162+
163+
blockHash, err := l.lnd.ChainKit.GetBlockHash(
164+
ctx, blockHeight,
165+
)
166+
if err != nil {
167+
return zero, fmt.Errorf(
168+
"unable to retrieve block hash: %w",
169+
err,
170+
)
171+
}
172+
173+
return blockHash, nil
174+
},
175+
)
176+
if err != nil {
177+
return nil, fmt.Errorf("unable to retrieve block hash: %w", err)
178+
}
179+
180+
// Now that we have the block hash, we can fetch the block header.
181+
return fn.RetryFuncN(
182+
ctx, l.retryConfig, func() (*wire.BlockHeader, error) {
183+
header, err := l.lnd.ChainKit.GetBlockHeader(
184+
ctx, blockHash,
185+
)
186+
if err != nil {
187+
return nil, fmt.Errorf(
188+
"unable to retrieve block header: %w",
189+
err,
190+
)
191+
}
192+
193+
return header, nil
194+
},
195+
)
196+
}
197+
154198
// GetBlockHash returns the hash of the block in the best blockchain at the
155199
// given height.
156200
func (l *LndRpcChainBridge) GetBlockHash(ctx context.Context,

tapgarden/interface.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,10 @@ type ChainBridge interface {
350350
// height.
351351
GetBlockTimestamp(context.Context, uint32) int64
352352

353+
// GetBlockHeaderByHeight returns a block header given the block height.
354+
GetBlockHeaderByHeight(ctx context.Context,
355+
blockHeight int64) (*wire.BlockHeader, error)
356+
353357
// PublishTransaction attempts to publish a new transaction to the
354358
// network.
355359
PublishTransaction(context.Context, *wire.MsgTx, string) error

tapgarden/mock.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,13 @@ func (m *MockChainBridge) GetBlock(ctx context.Context,
688688
return block, nil
689689
}
690690

691+
// GetBlockHeaderByHeight returns a block header given the block height.
692+
func (m *MockChainBridge) GetBlockHeaderByHeight(ctx context.Context,
693+
blockHeight int64) (*wire.BlockHeader, error) {
694+
695+
return &wire.BlockHeader{}, nil
696+
}
697+
691698
// GetBlockHash returns the hash of the block in the best blockchain at the
692699
// given height.
693700
func (m *MockChainBridge) GetBlockHash(ctx context.Context,

universe/supplycommit/mock.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,17 @@ func (m *mockChainBridge) GetBlock(ctx context.Context,
242242
return args.Get(0).(*wire.MsgBlock), args.Error(1)
243243
}
244244

245+
// GetBlockHeaderByHeight returns a block header given the block height.
246+
func (m *mockChainBridge) GetBlockHeaderByHeight(ctx context.Context,
247+
blockHeight int64) (*wire.BlockHeader, error) {
248+
249+
args := m.Called(ctx, blockHeight)
250+
if args.Get(0) == nil {
251+
return nil, args.Error(1)
252+
}
253+
return args.Get(0).(*wire.BlockHeader), args.Error(1)
254+
}
255+
245256
func (m *mockChainBridge) GetBlockHash(ctx context.Context,
246257
height int64) (chainhash.Hash, error) {
247258

0 commit comments

Comments
 (0)