Skip to content

Commit 9af87cf

Browse files
committed
test: Check that a failed wallet migration is cleaned up
1 parent 76d8957 commit 9af87cf

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

test/functional/wallet_migration.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66

77
import random
88
import shutil
9+
import struct
10+
911
from test_framework.address import (
1012
script_to_p2sh,
1113
key_to_p2pkh,
1214
key_to_p2wpkh,
1315
)
16+
from test_framework.bdb import BTREE_MAGIC
1417
from test_framework.descriptors import descsum_create
1518
from test_framework.key import ECPubKey
1619
from test_framework.test_framework import BitcoinTestFramework
@@ -20,6 +23,7 @@
2023
assert_equal,
2124
assert_raises_rpc_error,
2225
find_vout_for_address,
26+
sha256sum_file,
2327
)
2428
from test_framework.wallet_util import (
2529
get_generate_key,
@@ -827,6 +831,43 @@ def test_hybrid_pubkey(self):
827831

828832
wallet.unloadwallet()
829833

834+
def test_failed_migration_cleanup(self):
835+
self.log.info("Test that a failed migration is cleaned up")
836+
wallet = self.create_legacy_wallet("failed")
837+
838+
# Make a copy of the wallet with the solvables wallet name so that we are unable
839+
# to create the solvables wallet when migrating, thus failing to migrate
840+
wallet.unloadwallet()
841+
solvables_path = self.nodes[0].wallets_path / "failed_solvables"
842+
shutil.copytree(self.nodes[0].wallets_path / "failed", solvables_path)
843+
original_shasum = sha256sum_file(solvables_path / "wallet.dat")
844+
845+
self.nodes[0].loadwallet("failed")
846+
847+
# Add a multisig so that a solvables wallet is created
848+
wallet.addmultisigaddress(2, [wallet.getnewaddress(), get_generate_key().pubkey])
849+
wallet.importaddress(get_generate_key().p2pkh_addr)
850+
851+
assert_raises_rpc_error(-4, "Failed to create new watchonly wallet", wallet.migratewallet)
852+
853+
assert "failed" in self.nodes[0].listwallets()
854+
assert "failed_watchonly" not in self.nodes[0].listwallets()
855+
assert "failed_solvables" not in self.nodes[0].listwallets()
856+
857+
assert not (self.nodes[0].wallets_path / "failed_watchonly").exists()
858+
# Since the file in failed_solvables is one that we put there, migration shouldn't touch it
859+
assert solvables_path.exists()
860+
new_shasum = sha256sum_file(solvables_path / "wallet.dat")
861+
assert_equal(original_shasum, new_shasum)
862+
863+
wallet.unloadwallet()
864+
# Check the wallet we tried to migrate is still BDB
865+
with open(self.nodes[0].wallets_path / "failed" / "wallet.dat", "rb") as f:
866+
data = f.read(16)
867+
_, _, magic = struct.unpack("QII", data)
868+
assert_equal(magic, BTREE_MAGIC)
869+
870+
830871
def run_test(self):
831872
self.generate(self.nodes[0], 101)
832873

@@ -845,6 +886,7 @@ def run_test(self):
845886
self.test_migrate_raw_p2sh()
846887
self.test_conflict_txs()
847888
self.test_hybrid_pubkey()
889+
self.test_failed_migration_cleanup()
848890

849891
if __name__ == '__main__':
850892
WalletMigrationTest().main()

0 commit comments

Comments
 (0)