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

Fix: Hang when ElectrumBlockchainConfig::stop_gap == 0 #652

Merged
merged 2 commits into from
Jul 5, 2022

Conversation

evanlinjin
Copy link
Member

  • Ensure chunk_size is > 0 during wallet sync.

  • Slight refactoring for better readability.

  • Add test: test_electrum_blockchain_factory_sync_with_stop_gaps

Description

Wallet::sync hangs indefinitely when syncing with Electrum with stop_gap set as 0.

The culprit is having chunk_size set as stop_gap. A zero value results in syncing not being able to progress.

Fixes #651

Checklists

All Submissions:

  • I've signed all my commits
  • I followed the contribution guidelines
  • I ran cargo fmt and cargo clippy before committing

Bugfixes:

* [ ] This pull request breaks the existing API

  • I've added tests to reproduce the issue which are now passing
  • I'm linking the issue being fixed by this PR

@evanlinjin evanlinjin changed the title Fix hang when ElectrumBlockchainConfig::stop_gap == 0 Fix: hang when ElectrumBlockchainConfig::stop_gap == 0 Jul 3, 2022
@evanlinjin evanlinjin changed the title Fix: hang when ElectrumBlockchainConfig::stop_gap == 0 Fix: Hang when ElectrumBlockchainConfig::stop_gap == 0 Jul 3, 2022
@evanlinjin

This comment was marked as resolved.

src/blockchain/electrum.rs Outdated Show resolved Hide resolved
@LLFourn
Copy link
Contributor

LLFourn commented Jul 4, 2022

Interesting bug. I'm not sure about the semantics of stop_gap = 0? Maybe the correct thing is to do nothing. If I am going to stop after I find a gap of 0 then I should just stop right away no? (you start off with a gap of 0).

src/blockchain/electrum.rs Outdated Show resolved Hide resolved
@LLFourn
Copy link
Contributor

LLFourn commented Jul 4, 2022

ACK. See #652 (comment)

chunk_size was set wrongly before. Thanks!

@evanlinjin evanlinjin force-pushed the fix-electrum-stop-gap-0 branch from 629604f to 475b005 Compare July 4, 2022 06:46
Copy link
Member

@afilini afilini left a comment

Choose a reason for hiding this comment

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

Concept ACK, a couple of things:

  • did you check if this happens with esplora as well? The two blockchains are very similar in terms of architecture and code
  • once you are done fixing the various comments, could you squash the commits into the first one?

src/blockchain/electrum.rs Outdated Show resolved Hide resolved
// [1] actual_gap: Range size of address indexes without a balance
// [2] addrs_before: Range size of address indexes (before gap) which contains a balance
// [3] addrs_after: Range size of address indexes (after gap) which contains a balance
let test_vectors: Vec<[u64; 4]> = vec![
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't it be better to use usize here? I see that you have to cast these values in a few places

Copy link
Member Author

Choose a reason for hiding this comment

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

It seems that if we do this, other variables will need casting... Should we leave it as is?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah at a quick glance I thought using usize would be fine, but if you need to cast anyway there's no reason to change it

@evanlinjin
Copy link
Member Author

evanlinjin commented Jul 4, 2022

did you check if this happens with esplora as well? The two blockchains are very similar in terms of architecture and code

@afilini I've made a trait that can be reused for testing both esplora and electrum. I've also made it expandable, so in the future we can implement more tests in it. Let me know whether these changes make sense!

@evanlinjin evanlinjin force-pushed the fix-electrum-stop-gap-0 branch 3 times, most recently from 924b320 to 35a49fd Compare July 4, 2022 13:45
* Ensure chunk_size is > 0 during wallet sync.
* Slight refactoring for better readability.
* Add test: test_electrum_blockchain_factory_sync_with_stop_gaps
@evanlinjin evanlinjin force-pushed the fix-electrum-stop-gap-0 branch from 35a49fd to 3533afa Compare July 4, 2022 13:53
@evanlinjin
Copy link
Member Author

once you are done fixing the various comments, could you squash the commits into the first one?

@afilini I feel like it's better to keep it to two commits. One if for the actual fix, the other is introducing a new testing trait that can be shared between different blockchain implementations.

What do you think?

@evanlinjin evanlinjin requested review from LLFourn and afilini July 4, 2022 13:56
@notmandatory notmandatory added the bug Something isn't working label Jul 4, 2022
@evanlinjin evanlinjin force-pushed the fix-electrum-stop-gap-0 branch from 3533afa to a884a63 Compare July 4, 2022 23:43
This is a continuation of the bitcoindevkit#651 fix. We should also check whether the
same bug affects esplora as noted by @afilini. To achieve this, I've
introduced a `ConfigurableBlockchainTester` trait that can test multiple
blockchain implementations.

* Introduce `ConfigurableBlockchainTester` trait.
* Use the aforementioned trait to also test esplora.
* Change the electrum test to also use the new trait.
* Fix some complaints by clippy in ureq.rs file (why is CI not seeing
  this?).
* Refactor some code.
@evanlinjin evanlinjin force-pushed the fix-electrum-stop-gap-0 branch from a884a63 to 612da16 Compare July 4, 2022 23:53
@evanlinjin
Copy link
Member Author

@afilini Should this fix also be part of Release 0.20.0 Feature Freeze?

Ensuring that the Blockchain implementations respect the stop_gap parameter can potentially be important for avoiding address reuse.

The test I have included checks for this.

assert!(
wallet_balance >= min_balance,
"wallet balance is smaller than expected: {}",
details
);

@afilini
Copy link
Member

afilini commented Jul 5, 2022

@afilini I feel like it's better to keep it to two commits. One if for the actual fix, the other is introducing a new testing trait that can be shared between different blockchain implementations.

Yeah two commits works for me, as long as they are self-contained (they should both compile and run cargo test with no errors) and they have a good commit message

@afilini Should this fix also be part of Release 0.20.0 Feature Freeze?

I don't think this is as critical as that other one, but if this is ready in time yes, this is also important

Copy link
Member

@afilini afilini left a comment

Choose a reason for hiding this comment

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

ACK 612da16

@afilini afilini merged commit 0e92820 into bitcoindevkit:master Jul 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Setting electrum client's stop_gap as 0 results in hang
4 participants