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

Remove electrum tip query on start #708

Merged
merged 7 commits into from
Feb 3, 2025
Merged

Remove electrum tip query on start #708

merged 7 commits into from
Feb 3, 2025

Conversation

roeierez
Copy link
Member

@roeierez roeierez commented Feb 2, 2025

ElecturmClient needs to be initialized with the current tip which forced us to query right on start which caused up to several seconds of sdk initialization.
Since we don't really need lwk ElectrumClient to use electrum this PR shifts to use the underline Client directly and saves this startup time and reduced it to less than a millisecond.

Edit: It turns out that also Electurm client initialization has some overhead as it tries to connect. I changed the code to use lazy initialization of electrum which causes the startup flow to complete instantly now.

Copy link
Collaborator

@dangeross dangeross left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to reuse the block_headers_subscribe_raw() instead of caching the tip.

There are a couple of println's that need removing

Comment on lines 101 to 102
println!("Fetching block headers none");
// https://github.com/bitcoindevkit/rusprintln!("Fetching block headers");t-electrum-client/issues/124
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
println!("Fetching block headers none");
// https://github.com/bitcoindevkit/rusprintln!("Fetching block headers");t-electrum-client/issues/124
// https://github.com/bitcoindevkit/rust-electrum-client/issues/124

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@roeierez roeierez requested a review from dangeross February 3, 2025 09:43
let new_tip: Option<u32> = match maybe_popped_header {
Some(popped_header) => Some(popped_header.height.try_into()?),
None => {
// https://github.com/bitcoindevkit/rusprintln!("Fetching block headers");t-electrum-client/issues/124
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// https://github.com/bitcoindevkit/rusprintln!("Fetching block headers");t-electrum-client/issues/124
// https://github.com/bitcoindevkit/rust-electrum-client/issues/124

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh... how did it got there.
Done

@roeierez roeierez requested a review from dangeross February 3, 2025 10:54
Copy link
Collaborator

@dangeross dangeross left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@danielgranhao danielgranhao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Only a couple nits

}

Ok(tip.clone())
let tip = new_tip.ok_or_else(|| anyhow!("Failed to get tip"))?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the new_tip name no longer makes sense. Maybe rename to maybe_tip? We can even get rid of it and return the tip directly within the match.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 97 to 112
let new_tip: Option<u32> = match maybe_popped_header {
Some(popped_header) => Some(popped_header.height.try_into()?),
None => {
// https://github.com/bitcoindevkit/rust-electrum-client/issues/124
// It might be that the client has reconnected and subscriptions don't persist
// across connections. Calling `client.ping()` won't help here because the
// successful retry will prevent us knowing about the reconnect.
if let Ok(header) = client.block_headers_subscribe_raw() {
Some(header.height.try_into()?)
} else {
None
}
}
};

let tip: u32 = new_tip.ok_or_else(|| anyhow!("Failed to get tip"))?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Same as above. The name new_tip doesn't fit. We can return within the match.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@roeierez roeierez merged commit c42bebb into main Feb 3, 2025
9 checks passed
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.

3 participants