Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose the SubscribeHeadersSingle function #5

Merged
merged 1 commit into from
Feb 6, 2024

Conversation

lukasz-zimnoch
Copy link
Member

Refs: keep-network/keep-core#3739

The SubscribeHeadersSingle function subscribes to receive the header of the current blockchain tip. Unlike SubscribeHeaders, this function only returns the tip and does not listen for new block headers.

Worth noting that this action still creates a new subscription in the Electrum server. The protocol does neither support a single-shot request for the current blockchain tip nor subscription cancellation. Although this limitation causes a slight resource overhead on the client, it does not cause a memory leak like the SubscribeHeaders method which spawns a goroutine that may hang on the channel if the caller is no longer pulling from it.

@lukasz-zimnoch lukasz-zimnoch self-assigned this Feb 6, 2024
@lukasz-zimnoch lukasz-zimnoch marked this pull request as draft February 6, 2024 16:48
@lukasz-zimnoch lukasz-zimnoch marked this pull request as ready for review February 6, 2024 16:49
The `SubscribeHeadersSingle` function subscribes to receive the header of the
current blockchain tip. Unlike `SubscribeHeaders`, this function only returns
the tip and does not listen for new block headers.

Worth noting that this action still creates a new subscription in the Electrum
server. The protocol does neither support a single-shot request for the
current blockchain tip nor subscription cancellation. Although this limitation
causes a slight resource overhead on the client, it does not cause a memory
leak like the `SubscribeHeaders` method which spawns a goroutine that may hang
on the channel if the caller is no longer pulling from it.
@tomaszslabon tomaszslabon merged commit 6038cb5 into master Feb 6, 2024
@tomaszslabon tomaszslabon deleted the expose-subscribe-headers-single branch February 6, 2024 17:09
lukasz-zimnoch added a commit to keep-network/keep-core that referenced this pull request Feb 7, 2024
This is done in order to pull changes introduced in
keep-network/go-electrum#5
lukasz-zimnoch added a commit to keep-network/keep-core that referenced this pull request Feb 7, 2024
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.
tomaszslabon added a commit to keep-network/keep-core that referenced this pull request Feb 8, 2024
Closes: #3739
Depends on: keep-network/go-electrum#5
Depends on: #3774

So far, the `GetLatestBlockHeight` function of the Electrum client was
using `go-electrum`'s `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 with one item inside.

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

See keep-network/go-electrum#5 for further
reference.
lukasz-zimnoch added a commit to keep-network/keep-core that referenced this pull request Feb 12, 2024
This is done in order to pull changes introduced in
keep-network/go-electrum#5

(cherry picked from commit b3b800c)
lukasz-zimnoch added a commit to keep-network/keep-core that referenced this pull request Feb 12, 2024
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.

(cherry picked from commit fb91c33)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants