From 6d31900e52efa2c7c7a220d8c8ad6353c412a2aa Mon Sep 17 00:00:00 2001 From: furszy Date: Tue, 17 Jan 2023 11:58:23 -0300 Subject: [PATCH] wallet: migrate wallet, exit early if no legacy data exist otherwise the process will create a backup file then return an error when notices that the db is already running sqlite. --- src/wallet/wallet.cpp | 10 ++++++---- test/functional/wallet_migration.py | 4 ++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 6158ff033ca..876f32f5f32 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -3839,10 +3839,7 @@ std::optional CWallet::GetDescriptorsForLegacy(bilingual_str& err AssertLockHeld(cs_wallet); LegacyScriptPubKeyMan* legacy_spkm = GetLegacyScriptPubKeyMan(); - if (!legacy_spkm) { - error = _("Error: This wallet is already a descriptor wallet"); - return std::nullopt; - } + assert(legacy_spkm); std::optional res = legacy_spkm->MigrateToDescriptor(); if (res == std::nullopt) { @@ -4138,6 +4135,11 @@ bool DoMigration(CWallet& wallet, WalletContext& context, bilingual_str& error, util::Result MigrateLegacyToDescriptor(std::shared_ptr&& wallet, WalletContext& context) { + // Before anything else, check if there is something to migrate. + if (!wallet->GetLegacyScriptPubKeyMan()) { + return util::Error{_("Error: This wallet is already a descriptor wallet")}; + } + MigrationResult res; bilingual_str error; std::vector warnings; diff --git a/test/functional/wallet_migration.py b/test/functional/wallet_migration.py index 688ac986171..72c5fe7b840 100755 --- a/test/functional/wallet_migration.py +++ b/test/functional/wallet_migration.py @@ -163,6 +163,10 @@ class WalletMigrationTest(BitcoinTestFramework): assert_equal(basic2.getbalance(), basic2_balance) self.assert_list_txs_equal(basic2.listtransactions(), basic2_txs) + # Now test migration on a descriptor wallet + self.log.info("Test \"nothing to migrate\" when the user tries to migrate a wallet with no legacy data") + assert_raises_rpc_error(-4, "Error: This wallet is already a descriptor wallet", basic2.migratewallet) + def test_multisig(self): default = self.nodes[0].get_wallet_rpc(self.default_wallet_name)