From c9e04332fc89ddf87417582316a0115a73ba2d64 Mon Sep 17 00:00:00 2001 From: tecnovert Date: Sun, 15 Jan 2023 10:56:12 +0200 Subject: [PATCH] tests: Update test_wallet_restore --- tests/basicswap/common_xmr.py | 41 ++++++++------ .../basicswap/extended/test_wallet_restore.py | 55 ++++++++++++------- 2 files changed, 57 insertions(+), 39 deletions(-) diff --git a/tests/basicswap/common_xmr.py b/tests/basicswap/common_xmr.py index 16b0063..57d0a8b 100644 --- a/tests/basicswap/common_xmr.py +++ b/tests/basicswap/common_xmr.py @@ -28,7 +28,7 @@ from tests.basicswap.util import ( from tests.basicswap.common import ( BASE_PORT, BASE_RPC_PORT, BTC_BASE_PORT, BTC_BASE_RPC_PORT, BTC_BASE_TOR_PORT, - LTC_BASE_PORT, + LTC_BASE_PORT, LTC_BASE_RPC_PORT, PIVX_BASE_PORT, ) from basicswap.contrib.rpcauth import generate_salt, password_to_hmac @@ -45,6 +45,8 @@ BITCOIN_PORT_BASE = int(os.getenv('BITCOIN_PORT_BASE', BTC_BASE_PORT)) BITCOIN_RPC_PORT_BASE = int(os.getenv('BITCOIN_RPC_PORT_BASE', BTC_BASE_RPC_PORT)) BITCOIN_TOR_PORT_BASE = int(os.getenv('BITCOIN_TOR_PORT_BASE', BTC_BASE_TOR_PORT)) +LITECOIN_RPC_PORT_BASE = int(os.getenv('LITECOIN_RPC_PORT_BASE', LTC_BASE_RPC_PORT)) + XMR_BASE_P2P_PORT = 17792 XMR_BASE_RPC_PORT = 29798 XMR_BASE_WALLET_RPC_PORT = 29998 @@ -85,6 +87,7 @@ def run_prepare(node_id, datadir_path, bins_path, with_coins, mnemonic_in=None, os.environ['PART_RPC_PORT'] = str(PARTICL_RPC_PORT_BASE) os.environ['BTC_RPC_PORT'] = str(BITCOIN_RPC_PORT_BASE) + os.environ['LTC_RPC_PORT'] = str(LITECOIN_RPC_PORT_BASE) os.environ['XMR_RPC_USER'] = 'xmr_user' os.environ['XMR_RPC_PWD'] = 'xmr_pwd' @@ -296,6 +299,24 @@ class TestBase(unittest.TestCase): if self.delay_event.is_set(): raise ValueError('Test stopped.') + def wait_for_particl_height(self, http_port, num_blocks=3): + # Wait for height, or sequencelock is thrown off by genesis blocktime + logging.info('Waiting for Particl chain height %d', num_blocks) + for i in range(60): + if self.delay_event.is_set(): + raise ValueError('Test stopped.') + try: + wallets = json.loads(urlopen(f'http://127.0.0.1:{http_port}/json/wallets').read()) + particl_blocks = wallets['PART']['blocks'] + print('particl_blocks', particl_blocks) + if particl_blocks >= num_blocks: + return + except Exception as e: + print('Error reading wallets', str(e)) + + self.delay_event.wait(1) + raise ValueError(f'wait_for_particl_height failed http_port: {http_port}') + class XmrTestBase(TestBase): @classmethod @@ -349,23 +370,7 @@ class XmrTestBase(TestBase): self.update_thread = threading.Thread(target=updateThread, args=(xmr_addr1, self.delay_event, xmr_auth)) self.update_thread.start() - # Wait for height, or sequencelock is thrown off by genesis blocktime - num_blocks = 3 - logging.info('Waiting for Particl chain height %d', num_blocks) - for i in range(60): - if self.delay_event.is_set(): - raise ValueError('Test stopped.') - try: - wallets = json.loads(urlopen('http://127.0.0.1:12701/json/wallets').read()) - particl_blocks = wallets['PART']['blocks'] - print('particl_blocks', particl_blocks) - if particl_blocks >= num_blocks: - break - except Exception as e: - print('Error reading wallets', str(e)) - - self.delay_event.wait(1) - assert particl_blocks >= num_blocks + self.wait_for_particl_height(12701, num_blocks=3) @classmethod def tearDownClass(cls): diff --git a/tests/basicswap/extended/test_wallet_restore.py b/tests/basicswap/extended/test_wallet_restore.py index b3f587b..de2fc1f 100644 --- a/tests/basicswap/extended/test_wallet_restore.py +++ b/tests/basicswap/extended/test_wallet_restore.py @@ -45,9 +45,15 @@ from basicswap.rpc import ( ) from tests.basicswap.mnemonics import mnemonics import bin.basicswap_run as runSystem -from bin.basicswap_prepare import LTC_RPC_PORT, BTC_RPC_PORT +from tests.basicswap.common import ( + BTC_BASE_RPC_PORT, + LTC_BASE_RPC_PORT, +) +PORT_OFS = int(os.getenv('PORT_OFS', 1)) TEST_PATH = os.path.expanduser(os.getenv('TEST_PATH', '~/test_basicswap1')) +LITECOIN_RPC_PORT_BASE = int(os.getenv('LITECOIN_RPC_PORT_BASE', LTC_BASE_RPC_PORT)) +BITCOIN_RPC_PORT_BASE = int(os.getenv('BITCOIN_RPC_PORT_BASE', BTC_BASE_RPC_PORT)) logger = logging.getLogger() logger.level = logging.DEBUG @@ -55,12 +61,12 @@ if not len(logger.handlers): logger.addHandler(logging.StreamHandler(sys.stdout)) -def callbtcnoderpc(node_id, method, params=[], wallet=None, base_rpc_port=BTC_RPC_PORT): +def callbtcnoderpc(node_id, method, params=[], wallet=None, base_rpc_port=BITCOIN_RPC_PORT_BASE): auth = 'test_btc_{0}:test_btc_pwd_{0}'.format(node_id) return callrpc(base_rpc_port + node_id, auth, method, params, wallet) -def callltcnoderpc(node_id, method, params=[], wallet=None, base_rpc_port=LTC_RPC_PORT): +def callltcnoderpc(node_id, method, params=[], wallet=None, base_rpc_port=LITECOIN_RPC_PORT_BASE): auth = 'test_ltc_{0}:test_ltc_pwd_{0}'.format(node_id) return callrpc(base_rpc_port + node_id, auth, method, params, wallet) @@ -99,10 +105,18 @@ class Test(TestBase): with patch.object(sys, 'argv', testargs): runSystem.main() + def finalise(self, processes): + self.delay_event.set() + if self.update_thread: + self.update_thread.join() + for p in processes: + p.terminate() + for p in processes: + p.join() + def test_wallet(self): processes = [] - self.wait_seconds(5) for i in range(3): processes.append(multiprocessing.Process(target=self.run_thread, args=(i,))) processes[-1].start() @@ -130,6 +144,8 @@ class Test(TestBase): self.update_thread = threading.Thread(target=updateThread, args=(self,)) self.update_thread.start() + self.wait_for_particl_height(12701, num_blocks=3) + data = { 'addr_from': '-1', 'coin_from': 'part', @@ -157,8 +173,8 @@ class Test(TestBase): waitForNumBids(self.delay_event, 12700, 1) - waitForBidState(self.delay_event, 12700, bid_id, 'Completed') - waitForBidState(self.delay_event, 12701, bid_id, 'Completed') + waitForBidState(self.delay_event, 12700, bid_id, 'Completed', wait_for=120) + waitForBidState(self.delay_event, 12701, bid_id, 'Completed', wait_for=120) logging.info('Starting a new node on the same mnemonic as the first') prepare_node(3, self.used_mnemonics[0]) @@ -170,32 +186,29 @@ class Test(TestBase): # TODO: Attempt to detect past swaps - ltc_original = read_json_api(12700, 'wallets/ltc') - ltc_restored = read_json_api(12703, 'wallets/ltc') - assert (float(ltc_original['balance']) + float(ltc_original['unconfirmed']) > 0.0) - assert (float(ltc_original['balance']) + float(ltc_original['unconfirmed']) == float(ltc_restored['balance']) + float(ltc_restored['unconfirmed'])) + for coin in ('part', 'btc', 'ltc'): + logging.info(f'Checking {coin} balance') + original = read_json_api(12700, f'wallets/{coin}') + restored = read_json_api(12703, f'wallets/{coin}') + assert (float(original['balance']) + float(original['unconfirmed']) == float(restored['balance']) + float(restored['unconfirmed'])) wallets_original = read_json_api(12700, 'wallets') # TODO: After restoring a new deposit address should be generated, should be automated # Swaps should use a new key path, not the external path - next_addr = read_json_api(12703, 'wallets/part/nextdepositaddr') + next_addr = read_json_api(12700, 'wallets/part/nextdepositaddr') next_addr = read_json_api(12703, 'wallets/part/nextdepositaddr') wallets_restored = read_json_api(12703, 'wallets') for k, w in wallets_original.items(): assert (w['deposit_address'] == wallets_restored[k]['deposit_address']) - logging.info('Test passed.') - - except Exception: + except Exception as e: traceback.print_exc() + self.finalise(processes) + logging.info('Test failed.') + raise e - self.delay_event.set() - if self.update_thread: - self.update_thread.join() - for p in processes: - p.terminate() - for p in processes: - p.join() + self.finalise(processes) + logging.info('Test passed.') if __name__ == '__main__':