Skip to content

Commit ee22ca5

Browse files
committed
Merge bitcoin/bitcoin#26740: wallet: Migrate wallets that are not in a wallet dir
a1e6538 test: Add test for migrating default wallet and plain file wallet (Andrew Chow) bdbe3fd wallet: Generated migrated wallet's path from walletdir and name (Andrew Chow) Pull request description: This PR fixes an assertion error that is hit during the setup of the new database during migration of a wallet that was not contained in a wallet dir. Also added a test for this case as well as one for migrating the default wallet. ACKs for top commit: ryanofsky: Code review ACK a1e6538 furszy: ACK a1e6538 Tree-SHA512: 96b218c0de8567d8650ec96e1bf58b0f8ca4c4726f5efc6362453979b56b9d569baea0bb09befb3a5aed8d16d29bf75ed5cd8ffc432bbd4cbcad3ac5574bc479
2 parents e4bbfb2 + a1e6538 commit ee22ca5

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/wallet/wallet.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -3856,16 +3856,19 @@ bool CWallet::MigrateToSQLite(bilingual_str& error)
38563856

38573857
// Close this database and delete the file
38583858
fs::path db_path = fs::PathFromString(m_database->Filename());
3859-
fs::path db_dir = db_path.parent_path();
38603859
m_database->Close();
38613860
fs::remove(db_path);
38623861

3862+
// Generate the path for the location of the migrated wallet
3863+
// Wallets that are plain files rather than wallet directories will be migrated to be wallet directories.
3864+
const fs::path wallet_path = fsbridge::AbsPathJoin(GetWalletDir(), fs::PathFromString(m_name));
3865+
38633866
// Make new DB
38643867
DatabaseOptions opts;
38653868
opts.require_create = true;
38663869
opts.require_format = DatabaseFormat::SQLITE;
38673870
DatabaseStatus db_status;
3868-
std::unique_ptr<WalletDatabase> new_db = MakeDatabase(db_dir, opts, db_status, error);
3871+
std::unique_ptr<WalletDatabase> new_db = MakeDatabase(wallet_path, opts, db_status, error);
38693872
assert(new_db); // This is to prevent doing anything further with this wallet. The original file was deleted, but a backup exists.
38703873
m_database.reset();
38713874
m_database = std::move(new_db);

test/functional/wallet_migration.py

+37
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import os
88
import random
9+
import shutil
910
from test_framework.descriptors import descsum_create
1011
from test_framework.test_framework import BitcoinTestFramework
1112
from test_framework.util import (
@@ -470,6 +471,40 @@ def test_unloaded_by_path(self):
470471

471472
assert_equal(bals, wallet.getbalances())
472473

474+
def test_default_wallet(self):
475+
self.log.info("Test migration of the wallet named as the empty string")
476+
wallet = self.create_legacy_wallet("")
477+
478+
wallet.migratewallet()
479+
info = wallet.getwalletinfo()
480+
assert_equal(info["descriptors"], True)
481+
assert_equal(info["format"], "sqlite")
482+
483+
def test_direct_file(self):
484+
self.log.info("Test migration of a wallet that is not in a wallet directory")
485+
wallet = self.create_legacy_wallet("plainfile")
486+
wallet.unloadwallet()
487+
488+
wallets_dir = os.path.join(self.nodes[0].datadir, "regtest", "wallets")
489+
wallet_path = os.path.join(wallets_dir, "plainfile")
490+
wallet_dat_path = os.path.join(wallet_path, "wallet.dat")
491+
shutil.copyfile(wallet_dat_path, os.path.join(wallets_dir, "plainfile.bak"))
492+
shutil.rmtree(wallet_path)
493+
shutil.move(os.path.join(wallets_dir, "plainfile.bak"), wallet_path)
494+
495+
self.nodes[0].loadwallet("plainfile")
496+
info = wallet.getwalletinfo()
497+
assert_equal(info["descriptors"], False)
498+
assert_equal(info["format"], "bdb")
499+
500+
wallet.migratewallet()
501+
info = wallet.getwalletinfo()
502+
assert_equal(info["descriptors"], True)
503+
assert_equal(info["format"], "sqlite")
504+
505+
assert os.path.isdir(wallet_path)
506+
assert os.path.isfile(wallet_dat_path)
507+
473508
def run_test(self):
474509
self.generate(self.nodes[0], 101)
475510

@@ -482,6 +517,8 @@ def run_test(self):
482517
self.test_encrypted()
483518
self.test_unloaded()
484519
self.test_unloaded_by_path()
520+
self.test_default_wallet()
521+
self.test_direct_file()
485522

486523
if __name__ == '__main__':
487524
WalletMigrationTest().main()

0 commit comments

Comments
 (0)