Skip to content

Commit

Permalink
Merge pull request #9720
Browse files Browse the repository at this point in the history
3b64d1f functional tests: fix race in restore wallet refresh (j-berman)
  • Loading branch information
tobtoht committed Jan 24, 2025
2 parents eaf9f98 + 3b64d1f commit d42db81
Showing 1 changed file with 23 additions and 40 deletions.
63 changes: 23 additions & 40 deletions tests/functional_tests/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ def diff_incoming_transfers(actual_transfers, expected_transfers):
# wallet2 m_transfers container is ordered and order should be the same across rescans
diff_transfers(actual_transfers, expected_transfers, ignore_order = False)

def restore_wallet(wallet, seed, restore_height = 0, filename = '', password = ''):
try: wallet.close_wallet()
except: pass
if filename != '':
util_resources.remove_wallet_files(filename)
wallet.auto_refresh(enable = False)
wallet.restore_deterministic_wallet(seed = seed, restore_height = restore_height, filename = filename, password = password)
assert wallet.get_transfers() == {}

class TransferTest():
def run_test(self):
self.reset()
Expand Down Expand Up @@ -888,13 +897,6 @@ def check_scan_tx(self):

print('Testing scan_tx')

def restore_wallet(wallet, seed, restore_height = 0):
try: wallet.close_wallet()
except: pass
wallet.restore_deterministic_wallet(seed = seed, restore_height = restore_height)
wallet.auto_refresh(enable = False)
assert wallet.get_transfers() == {}

# set up sender_wallet
sender_wallet = self.wallet[0]
restore_wallet(sender_wallet, seeds[0])
Expand Down Expand Up @@ -1186,14 +1188,6 @@ def open_with_wrong_password(wallet, filename, password):
except: invalid_password = True
assert invalid_password

def restore_wallet(wallet, seed, filename = '', password = ''):
wallet.close_wallet()
if filename != '':
util_resources.remove_wallet_files(filename)
wallet.restore_deterministic_wallet(seed = seed, filename = filename, password = password)
wallet.auto_refresh(enable = False)
assert wallet.get_transfers() == {}

def assert_correct_transfers(wallet, expected_transfers, expected_inc_transfers, expected_balance):
diff_transfers(wallet.get_transfers(), expected_transfers)
diff_incoming_transfers(wallet.incoming_transfers(transfer_type = 'all'), expected_inc_transfers)
Expand All @@ -1203,10 +1197,7 @@ def assert_correct_transfers(wallet, expected_transfers, expected_inc_transfers,
# We're testing a sweep because it makes sure background sync can
# properly pick up txs which do not have a change output back to sender.
sender_wallet = self.wallet[0]
try: sender_wallet.close_wallet()
except: pass
sender_wallet.restore_deterministic_wallet(seed = seeds[0])
sender_wallet.auto_refresh(enable = False)
restore_wallet(sender_wallet, seeds[0])
sender_wallet.refresh()
res = sender_wallet.incoming_transfers(transfer_type = 'available')
unlocked = [x for x in res.transfers if x.unlocked and x.amount > 0]
Expand All @@ -1225,10 +1216,7 @@ def assert_correct_transfers(wallet, expected_transfers, expected_inc_transfers,

# set up receiver_wallet
receiver_wallet = self.wallet[1]
try: receiver_wallet.close_wallet()
except: pass
receiver_wallet.restore_deterministic_wallet(seed = seeds[1])
receiver_wallet.auto_refresh(enable = False)
restore_wallet(receiver_wallet, seeds[1])
receiver_wallet.refresh()
res = receiver_wallet.get_transfers()
in_len = 0 if 'in' not in res else len(res['in'])
Expand Down Expand Up @@ -1299,7 +1287,7 @@ def assert_correct_transfers(wallet, expected_transfers, expected_inc_transfers,

# Check stopping a wallet with wallet files saved to disk
for background_sync_type in [reuse_password, custom_password]:
restore_wallet(sender_wallet, seeds[0], 'test1', 'test_password')
restore_wallet(sender_wallet, seeds[0], filename = 'test1', password = 'test_password')
background_cache_password = None if background_sync_type == reuse_password else 'background_password'
sender_wallet.setup_background_sync(background_sync_type = background_sync_type, wallet_password = 'test_password', background_cache_password = background_cache_password)
sender_wallet.start_background_sync()
Expand All @@ -1311,7 +1299,7 @@ def assert_correct_transfers(wallet, expected_transfers, expected_inc_transfers,

# Close wallet while background syncing, then reopen
for background_sync_type in [reuse_password, custom_password]:
restore_wallet(sender_wallet, seeds[0], 'test1', 'test_password')
restore_wallet(sender_wallet, seeds[0], filename = 'test1', password = 'test_password')
background_cache_password = None if background_sync_type == reuse_password else 'background_password'
sender_wallet.setup_background_sync(background_sync_type = background_sync_type, wallet_password = 'test_password', background_cache_password = background_cache_password)
sender_wallet.start_background_sync()
Expand All @@ -1325,7 +1313,7 @@ def assert_correct_transfers(wallet, expected_transfers, expected_inc_transfers,

# Close wallet while syncing normally, then reopen
for background_sync_type in [reuse_password, custom_password]:
restore_wallet(sender_wallet, seeds[0], 'test1', 'test_password')
restore_wallet(sender_wallet, seeds[0], filename = 'test1', password = 'test_password')
background_cache_password = None if background_sync_type == reuse_password else 'background_password'
sender_wallet.setup_background_sync(background_sync_type = background_sync_type, wallet_password = 'test_password', background_cache_password = background_cache_password)
sender_wallet.refresh()
Expand All @@ -1337,7 +1325,7 @@ def assert_correct_transfers(wallet, expected_transfers, expected_inc_transfers,

# Create background cache using custom password, then use it to sync, then reopen main wallet
for background_cache_password in ['background_password', '']:
restore_wallet(sender_wallet, seeds[0], 'test1', 'test_password')
restore_wallet(sender_wallet, seeds[0], filename = 'test1', password = 'test_password')
assert not util_resources.file_exists('test1.background')
assert not util_resources.file_exists('test1.background.keys')
sender_wallet.setup_background_sync(background_sync_type = custom_password, wallet_password = 'test_password', background_cache_password = background_cache_password)
Expand All @@ -1353,7 +1341,7 @@ def assert_correct_transfers(wallet, expected_transfers, expected_inc_transfers,
assert_correct_transfers(sender_wallet, transfers, incoming_transfers, expected_sender_balance)

# Check that main wallet keeps background cache encrypted with custom password in sync
restore_wallet(sender_wallet, seeds[0], 'test1', 'test_password')
restore_wallet(sender_wallet, seeds[0], filename = 'test1', password = 'test_password')
sender_wallet.setup_background_sync(background_sync_type = background_sync_type, wallet_password = 'test_password', background_cache_password = 'background_password')
sender_wallet.refresh()
assert_correct_transfers(sender_wallet, transfers, incoming_transfers, expected_sender_balance)
Expand All @@ -1362,7 +1350,7 @@ def assert_correct_transfers(wallet, expected_transfers, expected_inc_transfers,
assert_correct_transfers(sender_wallet, transfers, incoming_transfers, expected_sender_balance)

# Try using wallet password as custom background password
restore_wallet(sender_wallet, seeds[0], 'test1', 'test_password')
restore_wallet(sender_wallet, seeds[0], filename = 'test1', password = 'test_password')
assert not util_resources.file_exists('test1.background')
assert not util_resources.file_exists('test1.background.keys')
same_password = False
Expand All @@ -1374,7 +1362,7 @@ def assert_correct_transfers(wallet, expected_transfers, expected_inc_transfers,

# Turn off background sync
for background_sync_type in [reuse_password, custom_password]:
restore_wallet(sender_wallet, seeds[0], 'test1', 'test_password')
restore_wallet(sender_wallet, seeds[0], filename = 'test1', password = 'test_password')
background_cache_password = None if background_sync_type == reuse_password else 'background_password'
sender_wallet.setup_background_sync(background_sync_type = background_sync_type, wallet_password = 'test_password', background_cache_password = background_cache_password)
if background_sync_type == custom_password:
Expand All @@ -1399,8 +1387,7 @@ def assert_correct_transfers(wallet, expected_transfers, expected_inc_transfers,
sender_wallet.open_wallet('test1', password = 'test_password')

# Sanity check against outgoing wallet restored at height 0
sender_wallet.close_wallet()
sender_wallet.restore_deterministic_wallet(seed = seeds[0], restore_height = 0)
restore_wallet(sender_wallet, seeds[0], restore_height = 0)
sender_wallet.refresh()
assert_correct_transfers(sender_wallet, transfers, incoming_transfers, expected_sender_balance)

Expand Down Expand Up @@ -1449,7 +1436,7 @@ def assert_correct_transfers(wallet, expected_transfers, expected_inc_transfers,
assert receiver_wallet.get_balance().balance == expected_receiver_balance

# Check a fresh incoming wallet with wallet files saved to disk and encrypted with password
restore_wallet(receiver_wallet, seeds[1], 'test2', 'test_password')
restore_wallet(receiver_wallet, seeds[1], filename = 'test2', password = 'test_password')
receiver_wallet.setup_background_sync(background_sync_type = reuse_password, wallet_password = 'test_password')
receiver_wallet.start_background_sync()
receiver_wallet.refresh()
Expand All @@ -1459,7 +1446,7 @@ def assert_correct_transfers(wallet, expected_transfers, expected_inc_transfers,
assert_correct_transfers(receiver_wallet, transfers, incoming_transfers, expected_receiver_balance)

# Close receiver's wallet while background sync is enabled then reopen
restore_wallet(receiver_wallet, seeds[1], 'test2', 'test_password')
restore_wallet(receiver_wallet, seeds[1], filename = 'test2', password = 'test_password')
receiver_wallet.setup_background_sync(background_sync_type = reuse_password, wallet_password = 'test_password')
receiver_wallet.start_background_sync()
receiver_wallet.refresh()
Expand All @@ -1472,8 +1459,7 @@ def assert_correct_transfers(wallet, expected_transfers, expected_inc_transfers,
assert_correct_transfers(receiver_wallet, transfers, incoming_transfers, expected_receiver_balance)

# Sanity check against incoming wallet restored at height 0
receiver_wallet.close_wallet()
receiver_wallet.restore_deterministic_wallet(seed = seeds[1], restore_height = 0)
restore_wallet(receiver_wallet, seeds[1], restore_height = 0)
receiver_wallet.refresh()
assert_correct_transfers(receiver_wallet, transfers, incoming_transfers, expected_receiver_balance)

Expand All @@ -1499,10 +1485,7 @@ def check_background_sync_reorg_recovery(self):

for background_sync_type in [reuse_password, custom_password]:
# Set up wallet saved to disk
sender_wallet.close_wallet()
util_resources.remove_wallet_files('test1')
sender_wallet.restore_deterministic_wallet(seed = seeds[0], filename = 'test1', password = '')
sender_wallet.auto_refresh(enable = False)
restore_wallet(sender_wallet, seeds[0], filename = 'test1', password = '')
sender_wallet.refresh()
sender_starting_balance = sender_wallet.get_balance().balance

Expand Down

0 comments on commit d42db81

Please sign in to comment.