-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge bitcoin/bitcoin#28574: wallet: optimize migration process, batc…
…h db transactions c98fc36 wallet: migration, consolidate external wallets db writes (furszy) 7c9076a wallet: migration, consolidate main wallet db writes (furszy) 9ef20e8 wallet: provide WalletBatch to 'SetupDescriptorScriptPubKeyMans' (furszy) 34bf079 wallet: refactor ApplyMigrationData to return util::Result<void> (furszy) aacaaaa wallet: provide WalletBatch to 'RemoveTxs' (furszy) 57249ff wallet: introduce active db txn listeners (furszy) 91e065e wallet: remove post-migration signals connection (furszy) 055c053 wallet: provide WalletBatch to 'DeleteRecords' (furszy) 122d103 wallet: introduce 'SetWalletFlagWithDB' (furszy) 6052c78 wallet: decouple default descriptors creation from external signer setup (furszy) f2541d0 wallet: batch MigrateToDescriptor() db transactions (furszy) 66c9936 bench: add coverage for wallet migration process (furszy) Pull request description: Last step in a chain of PRs (#26836, #28894, #28987, #29403). #### 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). Since the speedup depends on the storage device, benchmark results can vary significantly. Locally, I have seen a 15% speedup on a USB 3.2 pendrive. #### 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). ACKs for top commit: achow101: ACK c98fc36 theStack: re-ACK c98fc36 pablomartin4btc: re-ACK c98fc36 Tree-SHA512: a52d5f2eef27811045d613637c0a9d0b7e180256ddc1c893749d98ba2882b570c45f28cc7263cadd4710f2c10db1bea33d88051f29c6b789bc6180c85b5fd8f6
- Loading branch information
Showing
14 changed files
with
259 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (c) 2024 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or https://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <bitcoin-build-config.h> // IWYU pragma: keep | ||
|
||
#include <bench/bench.h> | ||
#include <interfaces/chain.h> | ||
#include <node/context.h> | ||
#include <test/util/mining.h> | ||
#include <test/util/setup_common.h> | ||
#include <wallet/test/util.h> | ||
#include <wallet/context.h> | ||
#include <wallet/receive.h> | ||
#include <wallet/wallet.h> | ||
|
||
#include <optional> | ||
|
||
#if defined(USE_BDB) && defined(USE_SQLITE) // only enable benchmark when bdb and sqlite are enabled | ||
|
||
namespace wallet{ | ||
|
||
static void WalletMigration(benchmark::Bench& bench) | ||
{ | ||
const auto test_setup = MakeNoLogFileContext<TestingSetup>(); | ||
|
||
WalletContext context; | ||
context.args = &test_setup->m_args; | ||
context.chain = test_setup->m_node.chain.get(); | ||
|
||
// Number of imported watch only addresses | ||
int NUM_WATCH_ONLY_ADDR = 20; | ||
|
||
// Setup legacy wallet | ||
DatabaseOptions options; | ||
options.use_unsafe_sync = true; | ||
options.verify = false; | ||
DatabaseStatus status; | ||
bilingual_str error; | ||
auto database = MakeWalletDatabase(fs::PathToString(test_setup->m_path_root / "legacy"), options, status, error); | ||
uint64_t create_flags = 0; | ||
auto wallet = TestLoadWallet(std::move(database), context, create_flags); | ||
|
||
// Add watch-only addresses | ||
std::vector<CScript> scripts_watch_only; | ||
for (int w = 0; w < NUM_WATCH_ONLY_ADDR; ++w) { | ||
CKey key = GenerateRandomKey(); | ||
LOCK(wallet->cs_wallet); | ||
const CScript& script = scripts_watch_only.emplace_back(GetScriptForDestination(GetDestinationForKey(key.GetPubKey(), OutputType::LEGACY))); | ||
bool res = wallet->ImportScriptPubKeys(strprintf("watch_%d", w), {script}, | ||
/*have_solving_data=*/false, /*apply_label=*/true, /*timestamp=*/1); | ||
assert(res); | ||
} | ||
|
||
// Generate transactions and local addresses | ||
for (int j = 0; j < 400; ++j) { | ||
CMutableTransaction mtx; | ||
mtx.vout.emplace_back(COIN, GetScriptForDestination(*Assert(wallet->GetNewDestination(OutputType::BECH32, strprintf("bench_%d", j))))); | ||
mtx.vout.emplace_back(COIN, GetScriptForDestination(*Assert(wallet->GetNewDestination(OutputType::LEGACY, strprintf("legacy_%d", j))))); | ||
mtx.vout.emplace_back(COIN, scripts_watch_only.at(j % NUM_WATCH_ONLY_ADDR)); | ||
mtx.vin.resize(2); | ||
wallet->AddToWallet(MakeTransactionRef(mtx), TxStateInactive{}, /*update_wtx=*/nullptr, /*fFlushOnClose=*/false, /*rescanning_old_block=*/true); | ||
} | ||
|
||
// Unload so the migration process loads it | ||
TestUnloadWallet(std::move(wallet)); | ||
|
||
bench.epochs(/*numEpochs=*/1).run([&] { | ||
util::Result<MigrationResult> res = MigrateLegacyToDescriptor(fs::PathToString(test_setup->m_path_root / "legacy"), "", context); | ||
assert(res); | ||
assert(res->wallet); | ||
assert(res->watchonly_wallet); | ||
}); | ||
} | ||
|
||
BENCHMARK(WalletMigration, benchmark::PriorityLevel::LOW); | ||
|
||
} // namespace wallet | ||
|
||
#endif // end USE_SQLITE && USE_BDB |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.