Skip to content

Fix: Wallet sync may decrement address index#653

Merged
afilini merged 1 commit intobitcoindevkit:release/0.20.0from
evanlinjin:fix-electrum-sync-index-decrement
Jul 11, 2022
Merged

Fix: Wallet sync may decrement address index#653
afilini merged 1 commit intobitcoindevkit:release/0.20.0from
evanlinjin:fix-electrum-sync-index-decrement

Conversation

@evanlinjin
Copy link
Copy Markdown
Member

@evanlinjin evanlinjin commented Jul 3, 2022

Description

Fixes #649

It is critical to ensure Wallet::get_address with AddressIndex::new always returns a new and unused address.

This bug seems to be Electrum-specific. The fix is to check address index updates to ensure that newly suggested indexes are not smaller than indexes already in database.

Notes to the reviewers

I have written new tests in /testutils/blockchain_tests.rs that tests all Blockchain implementations.

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 force-pushed the fix-electrum-sync-index-decrement branch 2 times, most recently from 49cae10 to f7ec271 Compare July 3, 2022 06:53
@evanlinjin evanlinjin marked this pull request as draft July 3, 2022 07:38
@evanlinjin evanlinjin force-pushed the fix-electrum-sync-index-decrement branch 2 times, most recently from 07fa29e to 06c76a5 Compare July 3, 2022 11:18
@evanlinjin evanlinjin marked this pull request as ready for review July 3, 2022 12:07
@afilini afilini added this to the Release 0.20.0 Feature Freeze milestone Jul 4, 2022
@evanlinjin evanlinjin force-pushed the fix-electrum-sync-index-decrement branch 2 times, most recently from 1123954 to f4975d1 Compare July 4, 2022 16:26
@notmandatory notmandatory added the bug Something isn't working label Jul 4, 2022
@evanlinjin evanlinjin force-pushed the fix-electrum-sync-index-decrement branch 4 times, most recently from d09efa9 to a8f470f Compare July 5, 2022 11:28
Comment thread src/blockchain/script_sync.rs Outdated
@evanlinjin evanlinjin force-pushed the fix-electrum-sync-index-decrement branch 2 times, most recently from dcddd61 to 6f6d923 Compare July 6, 2022 19:21
@evanlinjin evanlinjin requested a review from afilini July 6, 2022 19:24
Comment thread src/testutils/blockchain_tests.rs Outdated
@evanlinjin evanlinjin force-pushed the fix-electrum-sync-index-decrement branch 3 times, most recently from b4b2ad9 to edf5ab7 Compare July 6, 2022 19:51
Copy link
Copy Markdown
Contributor

@rajarshimaitra rajarshimaitra 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..

I verified that the test is only failing for Electrum, but not for RPC or Esplora.. Maybe worth exploring further..

I have a code suggestion for the same checking logic.. It seems we can get the check done in much sorter codes.

Comment thread src/blockchain/script_sync.rs Outdated
Copy link
Copy Markdown
Contributor

@rajarshimaitra rajarshimaitra left a comment

Choose a reason for hiding this comment

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

tACK ac5f0af

Thanks for the quick update..

@evanlinjin evanlinjin changed the base branch from master to release/0.20.0 July 11, 2022 09:33
@evanlinjin evanlinjin force-pushed the fix-electrum-sync-index-decrement branch from ac5f0af to 6f0ed1b Compare July 11, 2022 09:37
This bug seems to be Electrum-specific. The fix is to check the
proposed changes against the current state of the database. Ensure
newly suggested indexes are not smaller than indexes already in
database.

Changes:
* Check index updates before they are applied to database during
  Electrum Blockchain sync (Thank you @rajarshimaitra for providing
  an elegant solution).

Tests added:
* bdk_blockchain_tests!::test_sync_address_index_should_not_decrement
* bdk_blockchain_tests!::test_sync_address_index_should_increment

These tests ensure there will be no unexpected address reuse when
grabbing a new address via `Wallet::get_address` with `AddressIndex::New`.

Other changes:
* Tweak `rpc.rs` so that clippy is happy.
@evanlinjin evanlinjin force-pushed the fix-electrum-sync-index-decrement branch from 6f0ed1b to af6bde3 Compare July 11, 2022 09:52
@afilini
Copy link
Copy Markdown
Member

afilini commented Jul 11, 2022

ACK af6bde3

I'll wait for the CI to finish and then I'll merge this.

@afilini afilini merged commit 5561057 into bitcoindevkit:release/0.20.0 Jul 11, 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

None yet

Development

Successfully merging this pull request may close these issues.

Syncing causes get_address index to decrement

4 participants