From fb91c33f3d1c552722da48b4f222cb83dd78ddff Mon Sep 17 00:00:00 2001 From: Lukasz Zimnoch Date: Tue, 6 Feb 2024 18:22:33 +0100 Subject: [PATCH] Rework `GetLatestBlockHeight` function of the Electrum client 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 https://github.com/keep-network/go-electrum/pull/5 for further reference. --- pkg/bitcoin/electrum/electrum.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/bitcoin/electrum/electrum.go b/pkg/bitcoin/electrum/electrum.go index 2a627f2039..b02eeaa7cd 100644 --- a/pkg/bitcoin/electrum/electrum.go +++ b/pkg/bitcoin/electrum/electrum.go @@ -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",