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

docs: show example migration from bdk to bdk_wallet #1648

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ValuedMammal
Copy link
Contributor

@ValuedMammal ValuedMammal commented Oct 15, 2024

I created a migration workflow in the form of a runnable example that does everything in one go. In the example we

  1. Open an existing sqlite db
  2. Get the last revealed addresses
  3. Create a new bdk_wallet::Wallet with new database
  4. Restore previously revealed addresses
  5. Sync with electrum

fixes #1606

Changelog notice

Checklists

All Submissions:

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

@ValuedMammal ValuedMammal added this to the 1.0.0-beta milestone Oct 15, 2024
@notmandatory notmandatory added the documentation Improvements or additions to documentation label Oct 17, 2024
@notmandatory
Copy link
Member

Looks good, simpler and safer than dealing directly with database or intermediate text files as I'd originally proposed. A few suggestions:

  1. would read better with more descriptive variable names, ie. old_wallet, new_wallet
  2. should add sanity check asserts after sync for things like balances, indexes; maybe also utxos and tx ids.
  3. need to update PR description with sqlite instead of sled
  4. make it clearer in code comments that you're opening an "old" (will be) 0.30 wallet and a "new" 1.0 wallet

@ValuedMammal ValuedMammal marked this pull request as draft October 22, 2024 20:04
@ValuedMammal
Copy link
Contributor Author

I tested this by first syncing a bdk wallet using electrum_backend.rs, adapted to use this tprv

tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L

and persisted with sqlite. Then I ran the example in this PR, initializing BDK_DB_PATH to the old db path.

should add sanity check asserts after sync for things like balances, indexes; maybe also utxos and tx ids

I added assertions for utxos, balance, and revealed addresses. Just to note: this assumes the old database is up to date and no new information is presented as a result of syncing the new wallet.

I'm wondering whether example-crates/ is the best place for this compared to maybe a page in Book of BDK - I guess either is fine

@notmandatory
Copy link
Member

Yes I agree it might make a better book section with companion code to take snippets from.

@notmandatory notmandatory removed this from the 1.0.0-beta milestone Nov 21, 2024
@notmandatory
Copy link
Member

Maintenance version v0.30.0 of bdk crate with upgrade to rusqlite 0.31 is now published:

https://crates.io/crates/bdk/0.30.0

let old_wallet = Wallet::new(EXTERNAL, Some(INTERNAL), NETWORK, db)?;

// Get last revealed addresses for each keychain
let addr = old_wallet.get_address(AddressIndex::LastUnused)?;
Copy link
Member

Choose a reason for hiding this comment

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

Should sync old_wallet to make sure we have an accurate AddressIndex::LastUnused or could use AddressIndex::New here which makes sure we are working with the last exposed (possibly not yet used) index from old_wallet.

// for a fully functioning wallet.

let client = BdkElectrumClient::new(electrum_client::Client::new(ELECTRUM_URL)?);
let request = new_wallet.start_full_scan().inspect({
Copy link
Member

Choose a reason for hiding this comment

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

Do we really need a full_scan() here? Since we already know the last exposed SPK we should be able to do a sync() instead which will be quicker and can still retrieve the previous txouts.

@notmandatory
Copy link
Member

Overall looks great, short and too the point. I have a couple suggestions but otherwise this looks ready to go.

@notmandatory
Copy link
Member

notmandatory commented Dec 10, 2024

As discussed on call today, this can be closed in favor of bitcoindevkit/book-of-bdk#81. That PR is already using sync instead of full_scan and I understand and agree it's best to use AddressIndex::LastUnused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

Create wallet data migration process
2 participants