@@ -675,6 +675,38 @@ def test_default_wallet(self):
675675 # migrate_and_get_rpc already checks for backup file existence
676676 assert os .path .basename (res ["backup_path" ]).startswith ("default_wallet" )
677677
678+ wallet .unloadwallet ()
679+
680+ def test_default_wallet_failure (self ):
681+ self .log .info ("Test failure during unnamed (default) wallet migration" )
682+ master_wallet = self .master_node .get_wallet_rpc (self .default_wallet_name )
683+ wallet = self .create_legacy_wallet ("" , blank = True )
684+ wallet .importaddress (master_wallet .getnewaddress (address_type = "legacy" ))
685+
686+ # Create wallet directory with the watch-only name and a wallet file.
687+ # Because the wallet dir exists, this will cause migration to fail.
688+ watch_only_dir = self .master_node .wallets_path / "_watchonly"
689+ os .mkdir (watch_only_dir )
690+ shutil .copyfile (self .old_node .wallets_path / "wallet.dat" , watch_only_dir / "wallet.dat" )
691+
692+ mocked_time = int (time .time ())
693+ self .master_node .setmocktime (mocked_time )
694+ assert_raises_rpc_error (- 4 , "Failed to create database" , self .migrate_and_get_rpc , "" )
695+ self .master_node .setmocktime (0 )
696+
697+ # Verify the /wallets/ path exists
698+ assert self .master_node .wallets_path .exists ()
699+ # Check backup file exists. Because the wallet has no name, the backup is prefixed with 'default_wallet'
700+ backup_path = self .master_node .wallets_path / f"default_wallet_{ mocked_time } .legacy.bak"
701+ assert backup_path .exists ()
702+ # Verify the original unnamed wallet was restored
703+ assert (self .master_node .wallets_path / "wallet.dat" ).exists ()
704+ # And verify it is still a BDB wallet
705+ self .assert_is_bdb ("" )
706+
707+ # Test cleanup: clear default wallet for next test
708+ os .remove (self .old_node .wallets_path / "wallet.dat" )
709+
678710 def test_direct_file (self ):
679711 self .log .info ("Test migration of a wallet that is not in a wallet directory" )
680712 wallet = self .create_legacy_wallet ("plainfile" )
@@ -1560,6 +1592,7 @@ def run_test(self):
15601592 self .test_wallet_with_relative_path ()
15611593 self .test_wallet_with_path ("path/to/mywallet/" )
15621594 self .test_wallet_with_path ("path/that/ends/in/.." )
1595+ self .test_default_wallet_failure ()
15631596 self .test_default_wallet ()
15641597 self .test_direct_file ()
15651598 self .test_addressbook ()
0 commit comments