Skip to content

Commit

Permalink
Rework GetLatestBlockHeight function of the Electrum client
Browse files Browse the repository at this point in the history
So far, `GetLatestBlockHeight` function used `SubscribeHeaders` under the hood.
That caused a memory leak because `GetLatestBlockHeight` was not interested
in reading from the returned `headersChan` channel. Each call to
`GetLatestBlockHeight` produced a new dangling goroutine blocked on a buffered
channel holding one item

Here we replace `SubscribeHeaders` with `SubscribeHeadersSingle` which
does not create a goroutine supposed to handle future headers notifications.
The `SubscribeHeadersSingle` just return the current chain tip and ignores
further notifications coming from the Electrum server.

See keep-network/go-electrum#5 for further reference.
  • Loading branch information
lukasz-zimnoch committed Feb 7, 2024
1 parent b3b800c commit fb91c33
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/bitcoin/electrum/electrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,11 @@ func (c *Connection) GetLatestBlockHeight() (uint, error) {
blockHeight, err := requestWithRetry(
c,
func(ctx context.Context, client *electrum.Client) (int32, error) {
headersChan, err := client.SubscribeHeaders(ctx)
tip, err := client.SubscribeHeadersSingle(ctx)
if err != nil {
return 0, fmt.Errorf("failed to get the blocks tip height: [%w]", err)
}
tip := <-headersChan

return tip.Height, nil
},
"SubscribeHeaders",
Expand Down

0 comments on commit fb91c33

Please sign in to comment.