From 0f602c5693ef5d0c63b1e5b7dc0990dced3655d6 Mon Sep 17 00:00:00 2001 From: pablomartin4btc Date: Wed, 26 Mar 2025 19:13:44 -0300 Subject: [PATCH] wallet, migration: Fix crash on empty wallet Same as with a blank wallet, wallets with no legacy records (i.e. empty, non-blank, watch-only wallet) do not require to be migrated. --- src/wallet/wallet.cpp | 6 +++--- test/functional/wallet_migration.py | 9 +++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 40d52b90f00..9ae0fb5329e 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -4534,13 +4534,13 @@ util::Result MigrateLegacyToDescriptor(std::shared_ptr // First change to using SQLite if (!local_wallet->MigrateToSQLite(error)) return util::Error{error}; - // Do the migration of keys and scripts for non-blank wallets, and cleanup if it fails - success = local_wallet->IsWalletFlagSet(WALLET_FLAG_BLANK_WALLET); - if (!success) { + // Do the migration of keys and scripts for non-empty wallets, and cleanup if it fails + if (HasLegacyRecords(*local_wallet)) { success = DoMigration(*local_wallet, context, error, res); } else { // Make sure that descriptors flag is actually set local_wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS); + success = true; } } diff --git a/test/functional/wallet_migration.py b/test/functional/wallet_migration.py index ce8dc19460d..c467d6ad36d 100755 --- a/test/functional/wallet_migration.py +++ b/test/functional/wallet_migration.py @@ -445,6 +445,15 @@ class WalletMigrationTest(BitcoinTestFramework): # After migrating, the "keypool" is empty assert_raises_rpc_error(-4, "Error: This wallet has no available keys", watchonly1.getnewaddress) + self.log.info("Test migration of a watch-only empty wallet") + for idx, is_blank in enumerate([True, False], start=1): + wallet_name = f"watchonly_empty{idx}" + self.create_legacy_wallet(wallet_name, disable_private_keys=True, blank=is_blank) + _, watchonly_empty = self.migrate_and_get_rpc(wallet_name) + info = watchonly_empty.getwalletinfo() + assert_equal(info["private_keys_enabled"], False) + assert_equal(info["blank"], is_blank) + def test_pk_coinbases(self): self.log.info("Test migration of a wallet using old pk() coinbases") wallet = self.create_legacy_wallet("pkcb")