def test_wallet_mixdepth_decrease(setup_wallet): wallet = get_populated_wallet(num=1) # setup max_mixdepth = wallet.max_mixdepth assert max_mixdepth >= 1, "bad default value for mixdepth for this test" utxo = fund_wallet_addr(wallet, wallet.get_internal_addr(max_mixdepth), 1) bci = jm_single().bc_interface unspent_list = bci.listunspent(0) # filter on label, but note (a) in certain circumstances (in- # wallet transfer) it is possible for the utxo to be labeled # with the external label, and (b) the wallet will know if it # belongs or not anyway (is_known_addr): our_unspent_list = [x for x in unspent_list if ( bci.is_address_labeled(x, wallet.get_wallet_name()))] assert wallet.get_balance_by_mixdepth()[max_mixdepth] == 10**8 wallet.close() storage_data = wallet._storage.file_data # actual test new_mixdepth = max_mixdepth - 1 new_wallet = type(wallet)( VolatileStorage(data=storage_data), mixdepth=new_mixdepth) assert new_wallet.max_mixdepth == max_mixdepth assert new_wallet.mixdepth == new_mixdepth sync_test_wallet(True, WalletService(new_wallet)) assert max_mixdepth not in new_wallet.get_balance_by_mixdepth() assert max_mixdepth not in new_wallet.get_utxos_by_mixdepth() # wallet.select_utxos will still return utxos from higher mixdepths # because we explicitly ask for a specific mixdepth assert utxo in new_wallet.select_utxos(max_mixdepth, 10**7)
def test_wallet_mixdepth_decrease(setup_wallet): wallet = get_populated_wallet(num=1) # setup max_mixdepth = wallet.max_mixdepth assert max_mixdepth >= 1, "bad default value for mixdepth for this test" utxo = fund_wallet_addr(wallet, wallet.get_internal_addr(max_mixdepth), 1) assert wallet.get_balance_by_mixdepth()[max_mixdepth] == 10**8 wallet.close() storage_data = wallet._storage.file_data # actual test new_mixdepth = max_mixdepth - 1 new_wallet = type(wallet)(VolatileStorage(data=storage_data), mixdepth=new_mixdepth) assert new_wallet.max_mixdepth == max_mixdepth assert new_wallet.mixdepth == new_mixdepth sync_test_wallet(True, new_wallet) assert max_mixdepth not in new_wallet.get_balance_by_mixdepth() assert max_mixdepth not in new_wallet.get_utxos_by_mixdepth() # wallet.select_utxos will still return utxos from higher mixdepths # because we explicitly ask for a specific mixdepth assert utxo in new_wallet.select_utxos_(max_mixdepth, 10**7)
def test_address_reuse_freezing(setup_walletservice): """ Creates a WalletService on a pre-populated wallet, and sets different values of the config var 'max_sats_freeze_reuse' then adds utxos to different already used addresses to check that they are frozen or not as appropriate. Note that to avoid a twisted main loop the WalletService is not actually started, but the transaction_monitor is triggered manually (which executes the same code). A custom test version of the reuse warning callback is added and to check correct function, we check that this callback is called, and that the balance available in the mixdepth correctly reflects the usage pattern and freeze policy. """ cb_called = 0 def reuse_callback(utxostr): nonlocal cb_called print("Address reuse freezing callback on utxo: ", utxostr) cb_called += 1 # we must fund after initial sync (for imports), hence # "populated" with no coins wallet = get_populated_wallet(num=0) wallet_service = WalletService(wallet) wallet_service.set_autofreeze_warning_cb(reuse_callback) sync_test_wallet(True, wallet_service) for i in range(3): fund_wallet_addr(wallet_service.wallet, wallet_service.get_addr(0, 1, i)) # manually force the wallet service to see the new utxos: wallet_service.transaction_monitor() # check that with default status any reuse is blocked: try_address_reuse(wallet_service, 0, 1, -1, 3 * 10**8) assert cb_called == 1, "Failed to trigger freeze callback" # check that above the threshold is allowed (1 sat less than funding) try_address_reuse(wallet_service, 1, 1, 99999999, 4 * 10**8) assert cb_called == 1, "Incorrectly triggered freeze callback" # check that below the threshold on the same address is not allowed: try_address_reuse(wallet_service, 1, 0.99999998, 99999999, 4 * 10**8) # note can be more than 1 extra call here, somewhat suboptimal: assert cb_called > 1, "Failed to trigger freeze callback"