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.
This commit is contained in:
pablomartin4btc 2025-03-26 19:13:44 -03:00
parent 42c13141b5
commit 0f602c5693
2 changed files with 12 additions and 3 deletions

View file

@ -4534,13 +4534,13 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(std::shared_ptr<CWallet>
// First change to using SQLite // First change to using SQLite
if (!local_wallet->MigrateToSQLite(error)) return util::Error{error}; 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 // Do the migration of keys and scripts for non-empty wallets, and cleanup if it fails
success = local_wallet->IsWalletFlagSet(WALLET_FLAG_BLANK_WALLET); if (HasLegacyRecords(*local_wallet)) {
if (!success) {
success = DoMigration(*local_wallet, context, error, res); success = DoMigration(*local_wallet, context, error, res);
} else { } else {
// Make sure that descriptors flag is actually set // Make sure that descriptors flag is actually set
local_wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS); local_wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
success = true;
} }
} }

View file

@ -445,6 +445,15 @@ class WalletMigrationTest(BitcoinTestFramework):
# After migrating, the "keypool" is empty # After migrating, the "keypool" is empty
assert_raises_rpc_error(-4, "Error: This wallet has no available keys", watchonly1.getnewaddress) 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): def test_pk_coinbases(self):
self.log.info("Test migration of a wallet using old pk() coinbases") self.log.info("Test migration of a wallet using old pk() coinbases")
wallet = self.create_legacy_wallet("pkcb") wallet = self.create_legacy_wallet("pkcb")