@@ -192,8 +192,6 @@ def run_test(self):
192
192
193
193
# Unload wallets and copy to older nodes:
194
194
node_master_wallets_dir = node_master .wallets_path
195
- node_v17_wallets_dir = node_v17 .wallets_path
196
- node_v16_wallets_dir = node_v16 .wallets_path
197
195
node_master .unloadwallet ("w1" )
198
196
node_master .unloadwallet ("w2" )
199
197
node_master .unloadwallet ("w3" )
@@ -293,79 +291,81 @@ def run_test(self):
293
291
self .log .info ("Test that 0.21 cannot open wallet containing tr() descriptors" )
294
292
assert_raises_rpc_error (- 1 , "map::at" , node_v21 .loadwallet , "w1" )
295
293
296
- # Create upgrade wallet in v0.16
297
- self .restart_node (node_v16 .index , extra_args = ["-wallet=u1_v16" ])
298
- wallet = node_v16 .get_wallet_rpc ("u1_v16" )
299
- v16_addr = wallet .getnewaddress ('' , "bech32" )
300
- v16_info = wallet .validateaddress (v16_addr )
301
- v16_pubkey = v16_info ['pubkey' ]
302
- self .stop_node (node_v16 .index )
294
+ self .log .info ("Test that a wallet can upgrade to and downgrade from master, from:" )
295
+ for node in descriptors_nodes if self .options .descriptors else legacy_nodes :
296
+ self .log .info (f"- { node .version } " )
297
+ wallet_name = f"up_{ node .version } "
298
+ if self .major_version_less_than (node , 17 ):
299
+ # createwallet is only available in 0.17+
300
+ self .restart_node (node .index , extra_args = [f"-wallet={ wallet_name } " ])
301
+ wallet_prev = node .get_wallet_rpc (wallet_name )
302
+ address = wallet_prev .getnewaddress ('' , "bech32" )
303
+ addr_info = wallet_prev .validateaddress (address )
304
+ else :
305
+ if self .major_version_at_least (node , 21 ):
306
+ node .rpc .createwallet (wallet_name = wallet_name , descriptors = self .options .descriptors )
307
+ else :
308
+ node .rpc .createwallet (wallet_name = wallet_name )
309
+ wallet_prev = node .get_wallet_rpc (wallet_name )
310
+ address = wallet_prev .getnewaddress ('' , "bech32" )
311
+ addr_info = wallet_prev .getaddressinfo (address )
312
+
313
+ hdkeypath = addr_info ["hdkeypath" ].replace ("'" , "h" )
314
+ pubkey = addr_info ["pubkey" ]
315
+
316
+ # Make a backup of the wallet file
317
+ backup_path = os .path .join (self .options .tmpdir , f"{ wallet_name } .dat" )
318
+ wallet_prev .backupwallet (backup_path )
319
+
320
+ # Remove the wallet from old node
321
+ if self .major_version_at_least (node , 17 ):
322
+ wallet_prev .unloadwallet ()
323
+ else :
324
+ self .stop_node (node .index )
325
+
326
+ # Restore the wallet to master
327
+ load_res = node_master .restorewallet (wallet_name , backup_path )
303
328
304
- self .log .info ("Test wallet upgrade path..." )
305
- # u1: regular wallet, created with v0.17
306
- node_v17 .rpc .createwallet (wallet_name = "u1_v17" )
307
- wallet = node_v17 .get_wallet_rpc ("u1_v17" )
308
- address = wallet .getnewaddress ("bech32" )
309
- v17_info = wallet .getaddressinfo (address )
310
- hdkeypath = v17_info ["hdkeypath" ].replace ("'" , "h" )
311
- pubkey = v17_info ["pubkey" ]
312
-
313
- if self .is_bdb_compiled ():
314
- # Old wallets are BDB and will only work if BDB is compiled
315
- # Copy the 0.16 wallet to the last Bitcoin Core version and open it:
316
- shutil .copyfile (
317
- os .path .join (node_v16_wallets_dir , "u1_v16" ),
318
- os .path .join (node_master_wallets_dir , "u1_v16" )
319
- )
320
- load_res = node_master .loadwallet ("u1_v16" )
321
329
# Make sure this wallet opens with only the migration warning. See https://github.com/bitcoin/bitcoin/pull/19054
322
- if int (node_master .getnetworkinfo ()["version" ]) >= 249900 :
323
- # loadwallet#warnings (added in v25) -- only present if there is a warning
330
+ if not self .options .descriptors :
324
331
# Legacy wallets will have only a deprecation warning
325
332
assert_equal (load_res ["warnings" ], ["Wallet loaded successfully. The legacy wallet type is being deprecated and support for creating and opening legacy wallets will be removed in the future. Legacy wallets can be migrated to a descriptor wallet with migratewallet." ])
326
333
else :
327
- # loadwallet#warning (deprecated in v25) -- always present, but empty string if no warning
328
- assert_equal (load_res ["warning" ], '' )
329
- wallet = node_master .get_wallet_rpc ("u1_v16" )
330
- info = wallet .getaddressinfo (v16_addr )
331
- descriptor = f"wpkh([{ info ['hdmasterfingerprint' ]} { hdkeypath [1 :]} ]{ v16_pubkey } )"
332
- assert_equal (info ["desc" ], descsum_create (descriptor ))
334
+ assert "warnings" not in load_res
333
335
334
- # Now copy that same wallet back to 0.16 to make sure no automatic upgrade breaks it
335
- node_master .unloadwallet ("u1_v16" )
336
- os .remove (os .path .join (node_v16_wallets_dir , "u1_v16" ))
337
- shutil .copyfile (
338
- os .path .join (node_master_wallets_dir , "u1_v16" ),
339
- os .path .join (node_v16_wallets_dir , "u1_v16" )
340
- )
341
- self .start_node (node_v16 .index , extra_args = ["-wallet=u1_v16" ])
342
- wallet = node_v16 .get_wallet_rpc ("u1_v16" )
343
- info = wallet .validateaddress (v16_addr )
344
- assert_equal (info , v16_info )
345
-
346
- # Copy the 0.17 wallet to the last Bitcoin Core version and open it:
347
- node_v17 .unloadwallet ("u1_v17" )
348
- shutil .copytree (
349
- os .path .join (node_v17_wallets_dir , "u1_v17" ),
350
- os .path .join (node_master_wallets_dir , "u1_v17" )
351
- )
352
- node_master .loadwallet ("u1_v17" )
353
- wallet = node_master .get_wallet_rpc ("u1_v17" )
336
+ wallet = node_master .get_wallet_rpc (wallet_name )
354
337
info = wallet .getaddressinfo (address )
355
338
descriptor = f"wpkh([{ info ['hdmasterfingerprint' ]} { hdkeypath [1 :]} ]{ pubkey } )"
356
339
assert_equal (info ["desc" ], descsum_create (descriptor ))
357
340
358
- # Now copy that same wallet back to 0.17 to make sure no automatic upgrade breaks it
359
- node_master .unloadwallet ("u1_v17" )
360
- shutil .rmtree (os .path .join (node_v17_wallets_dir , "u1_v17" ))
361
- shutil .copytree (
362
- os .path .join (node_master_wallets_dir , "u1_v17" ),
363
- os .path .join (node_v17_wallets_dir , "u1_v17" )
364
- )
365
- node_v17 .loadwallet ("u1_v17" )
366
- wallet = node_v17 .get_wallet_rpc ("u1_v17" )
367
- info = wallet .getaddressinfo (address )
368
- assert_equal (info , v17_info )
341
+ # Make backup so the wallet can be copied back to old node
342
+ down_wallet_name = f"re_down_{ node .version } "
343
+ down_backup_path = os .path .join (self .options .tmpdir , f"{ down_wallet_name } .dat" )
344
+ wallet .backupwallet (down_backup_path )
345
+ wallet .unloadwallet ()
346
+
347
+ # Check that no automatic upgrade broke the downgrading the wallet
348
+ if self .major_version_less_than (node , 17 ):
349
+ # loadwallet is only available in 0.17+
350
+ shutil .copyfile (
351
+ down_backup_path ,
352
+ node .wallets_path / down_wallet_name
353
+ )
354
+ self .start_node (node .index , extra_args = [f"-wallet={ down_wallet_name } " ])
355
+ wallet_res = node .get_wallet_rpc (down_wallet_name )
356
+ info = wallet_res .validateaddress (address )
357
+ assert_equal (info , addr_info )
358
+ else :
359
+ target_dir = node .wallets_path / down_wallet_name
360
+ os .makedirs (target_dir , exist_ok = True )
361
+ shutil .copyfile (
362
+ down_backup_path ,
363
+ target_dir / "wallet.dat"
364
+ )
365
+ node .loadwallet (down_wallet_name )
366
+ wallet_res = node .get_wallet_rpc (down_wallet_name )
367
+ info = wallet_res .getaddressinfo (address )
368
+ assert_equal (info , addr_info )
369
369
370
370
if __name__ == '__main__' :
371
371
BackwardsCompatibilityTest ().main ()
0 commit comments