Skip to content

Commit a3fe8af

Browse files
committed
Implement GetLatestBlockHeight function
The function queries electrum server for a blockchain tip and returns its' block height.
1 parent b21ea8d commit a3fe8af

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

pkg/bitcoin/electrum/electrum.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,30 @@ func (c *Connection) BroadcastTransaction(
136136
panic("not implemented")
137137
}
138138

139+
// GetLatestBlockHeight gets the height of the latest block (tip). If the
140+
// latest block was not determined, this function returns an error.
139141
func (c *Connection) GetLatestBlockHeight() (uint, error) {
140-
// TODO: Implementation.
141-
panic("not implemented")
142+
var tip *electrum.SubscribeHeadersResult
143+
err := wrappers.DoWithDefaultRetry(c.requestRetryTimeout, func(ctx context.Context) error {
144+
headersChan, err := c.client.SubscribeHeaders(c.ctx)
145+
if err != nil {
146+
return fmt.Errorf("failed to get the blocks tip height: [%w]", err)
147+
}
148+
149+
tip = <-headersChan
150+
return nil
151+
})
152+
if err != nil {
153+
return 0, fmt.Errorf("failed to subscribe for headers: [%w]", err)
154+
}
155+
156+
blockHeight := tip.Height
157+
158+
if blockHeight > 0 {
159+
return uint(blockHeight), nil
160+
}
161+
162+
return 0, nil
142163
}
143164

144165
// GetBlockHeader gets the block header for the given block height. If the

0 commit comments

Comments
 (0)