def test_imported_wallet_sync(setup_wallets, fast): used_count = [0, 0, 0, 0, 0] wallet = create_wallet_for_sync(used_count, ['test_imported_wallet_sync']) source_wallet = create_wallet_for_sync(used_count, ['test_imported_wallet_sync_origin']) address = source_wallet.get_new_addr(0, 1) wallet.import_private_key(0, source_wallet.get_wif(0, 1, 0)) txid = binascii.unhexlify(jm_single().bc_interface.grab_coins(address, 1)) sync_test_wallet(fast, wallet) assert wallet._utxos.have_utxo(txid, 0) == 0
def test_multigap_used_wallet_sync(setup_wallets, fast): start_index = 5 used_count = [start_index, 0, 0, 0, 0] wallet = create_wallet_for_sync(used_count, ['test_multigap_used_wallet_sync']) wallet.gap_limit = 5 mixdepth = 0 for w in range(5): for x in range(int(wallet.gap_limit * 0.6)): assert x <= wallet.gap_limit, "test broken" # create some unused addresses wallet.get_new_script(mixdepth, True) wallet.get_new_script(mixdepth, False) used_count[mixdepth] += x + 2 jm_single().bc_interface.grab_coins( wallet.get_new_addr(mixdepth, True), 1) jm_single().bc_interface.grab_coins( wallet.get_new_addr(mixdepth, False), 1) # reset indices to simulate completely unsynced wallet for md in range(wallet.max_mixdepth + 1): wallet.set_next_index(md, True, 0) wallet.set_next_index(md, False, 0) sync_test_wallet(fast, wallet) assert used_count[mixdepth] - start_index == wallet.get_next_unused_index( mixdepth, True) assert used_count[mixdepth] == wallet.get_next_unused_index( mixdepth, False)
def test_gap_used_wallet_sync(setup_wallets, fast): used_count = [1, 3, 6, 2, 23] wallet = create_wallet_for_sync(used_count, ['test_gap_used_wallet_sync']) wallet.gap_limit = 20 for md in range(len(used_count)): x = -1 for x in range(md): assert x <= wallet.gap_limit, "test broken" # create some unused addresses wallet.get_new_script(md, True) wallet.get_new_script(md, False) used_count[md] += x + 2 jm_single().bc_interface.grab_coins(wallet.get_new_addr(md, True), 1) jm_single().bc_interface.grab_coins(wallet.get_new_addr(md, False), 1) # reset indices to simulate completely unsynced wallet for md in range(wallet.max_mixdepth + 1): wallet.set_next_index(md, True, 0) wallet.set_next_index(md, False, 0) sync_test_wallet(fast, wallet) broken = True for md in range(len(used_count)): broken = False assert md + 1 == wallet.get_next_unused_index(md, True) assert used_count[md] == wallet.get_next_unused_index(md, False) assert not broken
def test_sync(setup_sync, internal, wallet_cls): used_count = [1, 3, 6, 2, 23] wallet_service = create_wallet_for_sync(used_count, ['test_sync'], populate_internal=internal, wallet_cls=wallet_cls) ##the gap limit should be not zero before sync assert wallet_service.gap_limit > 0 for md in range(len(used_count)): ##obtaining an address should be possible without error before sync wallet_service.get_new_script(md, internal) # TODO bci should probably not store this state globally, # in case syncing is needed for multiple wallets (as in this test): jm_single().bc_interface.import_addresses_call_count = 0 wallet_service.sync_wallet(fast=False) for md in range(len(used_count)): ##plus one to take into account the one new script obtained above assert used_count[md] + 1 == wallet_service.get_next_unused_index( md, internal) #gap limit is zero after sync assert wallet_service.gap_limit == 0 #obtaining an address leads to an error after sync with pytest.raises(RuntimeError) as e_info: wallet_service.get_new_script(0, internal)
def test_retain_unused_indices_wallet_sync(setup_wallets, fast): used_count = [0, 0, 0, 0, 0] wallet = create_wallet_for_sync(used_count, ['test_retain_unused_indices_wallet_sync']) for x in range(9): wallet.get_new_script(0, 1) sync_test_wallet(fast, wallet) assert wallet.get_next_unused_index(0, 1) == 9
def test_empty_wallet_sync(setup_wallets, fast): wallet = create_wallet_for_sync([0, 0, 0, 0, 0], ['test_empty_wallet_sync']) sync_test_wallet(fast, wallet) broken = True for md in range(wallet.max_mixdepth + 1): for internal in (True, False): broken = False assert 0 == wallet.get_next_unused_index(md, internal) assert not broken
def test_retain_unused_indices_wallet_sync(setup_wallets, fast): used_count = [0, 0, 0, 0, 0] wallet_service = create_wallet_for_sync( used_count, ['test_retain_unused_indices_wallet_sync']) for x in range(9): wallet_service.get_new_script(0, BaseWallet.ADDRESS_TYPE_INTERNAL) sync_test_wallet(fast, wallet_service) assert wallet_service.get_next_unused_index( 0, BaseWallet.ADDRESS_TYPE_INTERNAL) == 9
def test_empty_wallet_sync(setup_wallets, fast): wallet_service = create_wallet_for_sync([0, 0, 0, 0, 0], ['test_empty_wallet_sync']) sync_test_wallet(fast, wallet_service) broken = True for md in range(wallet_service.max_mixdepth + 1): for internal in (BaseWallet.ADDRESS_TYPE_INTERNAL, BaseWallet.ADDRESS_TYPE_EXTERNAL): broken = False assert 0 == wallet_service.get_next_unused_index(md, internal) assert not broken
def test_sequentially_used_wallet_sync(setup_wallets, fast, internal): used_count = [1, 3, 6, 2, 23] wallet = create_wallet_for_sync(used_count, ['test_sequentially_used_wallet_sync'], populate_internal=internal) sync_test_wallet(fast, wallet) broken = True for md in range(len(used_count)): broken = False assert used_count[md] == wallet.get_next_unused_index(md, internal) assert not broken
def test_gap_used_wallet_sync(setup_wallets, fast): """ After careful examination this test now only includes the Recovery sync. Note: pre-Aug 2019, because of a bug, this code was not in fact testing both Fast and Recovery sync, but only Recovery (twice). Also, the scenario set out in this test (where coins are funded to a wallet which has no index-cache, and initially no imports) is only appropriate for recovery-mode sync, not for fast-mode (the now default). """ used_count = [1, 3, 6, 2, 23] wallet_service = create_wallet_for_sync(used_count, ['test_gap_used_wallet_sync']) wallet_service.gap_limit = 20 for md in range(len(used_count)): x = -1 for x in range(md): assert x <= wallet_service.gap_limit, "test broken" # create some unused addresses wallet_service.get_new_script(md, BaseWallet.ADDRESS_TYPE_INTERNAL) wallet_service.get_new_script(md, BaseWallet.ADDRESS_TYPE_EXTERNAL) used_count[md] += x + 2 jm_single().bc_interface.grab_coins( wallet_service.get_new_addr(md, BaseWallet.ADDRESS_TYPE_INTERNAL), 1) jm_single().bc_interface.grab_coins( wallet_service.get_new_addr(md, BaseWallet.ADDRESS_TYPE_EXTERNAL), 1) # reset indices to simulate completely unsynced wallet for md in range(wallet_service.max_mixdepth + 1): wallet_service.set_next_index(md, BaseWallet.ADDRESS_TYPE_INTERNAL, 0) wallet_service.set_next_index(md, BaseWallet.ADDRESS_TYPE_EXTERNAL, 0) sync_test_wallet(fast, wallet_service) broken = True for md in range(len(used_count)): broken = False assert md + 1 == wallet_service.get_next_unused_index( md, BaseWallet.ADDRESS_TYPE_INTERNAL) assert used_count[md] == wallet_service.get_next_unused_index( md, BaseWallet.ADDRESS_TYPE_EXTERNAL) assert not broken
def test_query_utxo_set(setup_wallets): load_program_config() jm_single().bc_interface.tick_forward_chain_interval = 1 wallet = create_wallet_for_sync([2, 3, 0, 0, 0], ["wallet4utxo.json", "4utxo", [2, 3]]) sync_wallet(wallet, fast=True) txid = do_tx(wallet, 90000000) txid2 = do_tx(wallet, 20000000) print("Got txs: ", txid, txid2) res1 = jm_single().bc_interface.query_utxo_set(txid + ":0", includeunconf=True) res2 = jm_single().bc_interface.query_utxo_set( [txid + ":0", txid2 + ":1"], includeconf=True, includeunconf=True) assert len(res1) == 1 assert len(res2) == 2 assert all([x in res1[0] for x in ['script', 'address', 'value']]) assert not 'confirms' in res1[0] assert 'confirms' in res2[0] assert 'confirms' in res2[1] res3 = jm_single().bc_interface.query_utxo_set("ee" * 32 + ":25") assert res3 == [None]
def test_multigap_used_wallet_sync(setup_wallets, fast): """ See docstring for test_gap_used_wallet_sync; exactly the same applies here. """ start_index = 5 used_count = [start_index, 0, 0, 0, 0] wallet_service = create_wallet_for_sync(used_count, ['test_multigap_used_wallet_sync']) wallet_service.gap_limit = 5 mixdepth = 0 for w in range(5): for x in range(int(wallet_service.gap_limit * 0.6)): assert x <= wallet_service.gap_limit, "test broken" # create some unused addresses wallet_service.get_new_script(mixdepth, BaseWallet.ADDRESS_TYPE_INTERNAL) wallet_service.get_new_script(mixdepth, BaseWallet.ADDRESS_TYPE_EXTERNAL) used_count[mixdepth] += x + 2 jm_single().bc_interface.grab_coins( wallet_service.get_new_addr(mixdepth, BaseWallet.ADDRESS_TYPE_INTERNAL), 1) jm_single().bc_interface.grab_coins( wallet_service.get_new_addr(mixdepth, BaseWallet.ADDRESS_TYPE_EXTERNAL), 1) # reset indices to simulate completely unsynced wallet for md in range(wallet_service.max_mixdepth + 1): wallet_service.set_next_index(md, BaseWallet.ADDRESS_TYPE_INTERNAL, 0) wallet_service.set_next_index(md, BaseWallet.ADDRESS_TYPE_EXTERNAL, 0) sync_test_wallet(fast, wallet_service) assert used_count[mixdepth] - start_index == \ wallet_service.get_next_unused_index(mixdepth, BaseWallet.ADDRESS_TYPE_INTERNAL) assert used_count[mixdepth] == wallet_service.get_next_unused_index( mixdepth, BaseWallet.ADDRESS_TYPE_EXTERNAL)
def test_query_utxo_set(setup_wallets): load_test_config() jm_single().bc_interface.tick_forward_chain_interval = 1 wallet_service = create_wallet_for_sync( [2, 3, 0, 0, 0], ["wallet4utxo.json", "4utxo", [2, 3]]) wallet_service.sync_wallet(fast=True) txid = do_tx(wallet_service, 90000000) txid2 = do_tx(wallet_service, 20000000) print("Got txs: ", txid, txid2) res1 = jm_single().bc_interface.query_utxo_set((txid, 0), includeunconf=True) res2 = jm_single().bc_interface.query_utxo_set([(txid, 0), (txid2, 1)], includeconf=True, includeunconf=True) assert len(res1) == 1 assert len(res2) == 2 assert all([x in res1[0] for x in ['script', 'value']]) assert not 'confirms' in res1[0] assert 'confirms' in res2[0] assert 'confirms' in res2[1] res3 = jm_single().bc_interface.query_utxo_set((b"\xee" * 32, 25)) assert res3 == [None]
def test_sync(setup_sync, internal): load_program_config(bs="bitcoin-rpc-no-history") used_count = [1, 3, 6, 2, 23] wallet_service = create_wallet_for_sync(used_count, ['test_sync'], populate_internal=internal) ##the gap limit should be not zero before sync assert wallet_service.gap_limit > 0 for md in range(len(used_count)): ##obtaining an address should be possible without error before sync wallet_service.get_new_script(md, internal) wallet_service.sync_wallet(fast=False) for md in range(len(used_count)): ##plus one to take into account the one new script obtained above assert used_count[md] + 1 == wallet_service.get_next_unused_index(md, internal) #gap limit is zero after sync assert wallet_service.gap_limit == 0 #obtaining an address leads to an error after sync with pytest.raises(RuntimeError) as e_info: wallet_service.get_new_script(0, internal)
def test_fast_sync_unavailable(setup_sync): load_program_config(bs="bitcoin-rpc-no-history") wallet_service = create_wallet_for_sync([0, 0, 0, 0, 0], ['test_fast_sync_unavailable']) with pytest.raises(RuntimeError) as e_info: wallet_service.sync_wallet(fast=True)
def test_fast_sync_unavailable(setup_sync): wallet_service = create_wallet_for_sync([0, 0, 0, 0, 0], ['test_fast_sync_unavailable']) with pytest.raises(RuntimeError) as e_info: wallet_service.sync_wallet(fast=True)