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

wallet: optimize migration process, batch db transactions #28574

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

furszy
Copy link
Member

@furszy furszy commented Oct 3, 2023

Last step in a chain of PRs (#26836, #28894, #28987, #29403).

The initial benchmark conducted locally showed a ~65% processing time reduction, on a SSD.
Results, from the very first benchmark, can be found at the end of the description.

Detailed Description:

The current wallet migration process performs only individual db writes. Accessing disk to
delete all legacy records, clone and clean each address book entry for every created wallet,
create each new descriptor (with their corresponding master key, caches and key pool), and
also clone and delete each transaction that requires to be transferred to a different wallet.

This work consolidates all individual disk writes into two batch operations. One for the descriptors
creation from the legacy data and a second one for the execution of the migration process itself.
Efficiently dumping all the information to disk at once atomically at the end of each process.

This represent a speed up and also a consistency improvement. During migration, we either
want to succeed or fail. No other outcomes should be accepted. We should never leave a
partially migrated wallet on disk and request the user to manually restore the previous wallet from
a backup (at least not if we can avoid it).

Additionally, since db transactions relevant to the key pool generation were also batched, this work
also speeds up the regular wallet creation process.

Note for Testers:

The first commit introduces a benchmark for the migration process. This one can be
cherry-picked on top of master to compare results pre and post changes.

Please note that the benchmark setup may take some time (~70 seconds here) due to the absence
of a batching mechanism for the address generation process (GetNewDestination() calls). Which,
I plan to address in a separate PR, just to not continue expanding this PR.

————————————————————————

Benchmark on master (debug mode)

ns/op op/s err% total benchmark
65,093,884,958.00 0.02 0.0% 65.09 WalletMigration

Benchmark on this branch (debug mode)

ns/op op/s err% total benchmark
22,926,814,750.00 0.04 0.0% 22.93 WalletMigration

@DrahtBot
Copy link
Contributor

DrahtBot commented Oct 3, 2023

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage

For detailed information about the code coverage, see the test coverage report.

Reviews

See the guideline for information on the review process.

Type Reviewers
Concept ACK pablomartin4btc, theStack
Stale ACK josibake

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #30221 (wallet: Ensure best block matches wallet scan state by achow101)
  • #29256 (Improve new LogDebug/Trace/Info/Warning/Error Macros by ryanofsky)
  • #29129 (wallet, rpc: add BIP44 account in createwallet by brunoerg)
  • #29124 (wallet: Automatically repair corrupted metadata with doubled derivation path by achow101)
  • #28710 (Remove the legacy wallet and BDB dependency by achow101)
  • #28333 (wallet: Construct ScriptPubKeyMans with all data rather than loaded progressively by achow101)
  • #27865 (wallet: Track no-longer-spendable TXOs separately by achow101)
  • #27286 (wallet: Keep track of the wallet's own transaction outputs in memory by achow101)
  • #26596 (wallet: Migrate legacy wallets to descriptor wallets without requiring BDB by achow101)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

Copy link
Member

@pablomartin4btc pablomartin4btc 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'll also perform the benchmark as instructed clearly in the description and will review the code later.

@theStack
Copy link
Contributor

theStack commented Nov 9, 2023

Concept ACK

fanquake added a commit that referenced this pull request Dec 8, 2023
… single db txn

f053024 wallet: batch external signer descriptor import (Sjors Provoost)
1f65241 wallet: descriptors setup, batch db operations (furszy)
3eb769f wallet: batch legacy spkm TopUp (furszy)
075aa44 wallet: batch descriptor spkm TopUp (furszy)
bb4554c bench: add benchmark for wallet creation procedure (furszy)

Pull request description:

  Work decoupled from #28574.

  Instead of performing multiple single write operations per spkm
  setup call, this PR batches them all within a single atomic db txn.

  Speeding up the process and preventing the wallet from entering
  an inconsistent state if any of the intermediate transactions fail
  (which shouldn't happen but.. if it does, it is better to not store
  any spkm rather than storing them partially).

  To compare the changes, added benchmark in the first commit.

ACKs for top commit:
  Sjors:
    re-utACK f053024
  achow101:
    ACK f053024
  BrandonOdiwuor:
    ACK f053024
  theStack:
    Code-review ACK f053024

Tree-SHA512: aead8548473e17d4d53e8e7039bbaf5e8bf2fe83f33b33f81cdedefe8a31b7003ceb6d5379b1bad1ca2692e909492009a21284ec8338eede078df3d19046ab5a
@furszy
Copy link
Member Author

furszy commented Mar 29, 2024

rebased after #29130 merge.

@DrahtBot
Copy link
Contributor

DrahtBot commented May 1, 2024

🚧 At least one of the CI tasks failed. Make sure to run all tests locally, according to the
documentation.

Possibly this is due to a silent merge conflict (the changes in this pull request being
incompatible with the current code in the target branch). If so, make sure to rebase on the latest
commit of the target branch.

Leave a comment here, if you need help tracking down a confusing failure.

Debug: https://github.com/bitcoin/bitcoin/runs/23239600985

@DrahtBot DrahtBot marked this pull request as draft May 13, 2024 08:04
@DrahtBot
Copy link
Contributor

Turned into draft for now, due to failing CI. If this is no longer a WIP, you can move it out of draft.

@furszy furszy force-pushed the 2023_wallet_batch_migration branch from 4f9d447 to e898c85 Compare May 15, 2024 14:40
@furszy furszy marked this pull request as ready for review May 15, 2024 14:41
@DrahtBot
Copy link
Contributor

DrahtBot commented Jun 3, 2024

🚧 At least one of the CI tasks failed. Make sure to run all tests locally, according to the
documentation.

Possibly this is due to a silent merge conflict (the changes in this pull request being
incompatible with the current code in the target branch). If so, make sure to rebase on the latest
commit of the target branch.

Leave a comment here, if you need help tracking down a confusing failure.

Debug: https://github.com/bitcoin/bitcoin/runs/25006227127

furszy added 10 commits June 3, 2024 10:26
Grouping all db writes into a single atomic write operation.
Speeding up the flow and preventing inconsistent states.
This will be useful in the following-up commit to batch the entire
wallet migration process.
So it can be used within an external db txn context.
The wallet is isolated during migration and reloaded at the end
of the process. There is no benefit on connecting the signals
few lines before unloading the wallet.
Useful to ensure that the in-memory state is updated only
after successfully committing the data to disk.
Preparing it to be used within a broader db txn procedure.
@furszy furszy force-pushed the 2023_wallet_batch_migration branch from e898c85 to 4e72821 Compare June 3, 2024 13:33
Copy link
Member Author

@furszy furszy left a comment

Choose a reason for hiding this comment

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

Rebased due a hidden conflict with #26606.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Development

Successfully merging this pull request may close these issues.

None yet

7 participants