From 1bfb271b8728de717f7abc5970543a5e908f4c86 Mon Sep 17 00:00:00 2001 From: tecnovert Date: Mon, 5 Feb 2024 17:17:44 +0200 Subject: [PATCH] Add settings for Monero rpc timeouts. --- basicswap/basicswap.py | 13 ++++++++++--- basicswap/interface/xmr.py | 24 ++++++++++++++---------- basicswap/rpc_xmr.py | 10 ++++++---- bin/basicswap_prepare.py | 8 ++++++-- 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py index 1d1cd59..98290b0 100644 --- a/basicswap/basicswap.py +++ b/basicswap/basicswap.py @@ -278,6 +278,8 @@ class BasicSwap(BaseApp): self.min_sequence_lock_seconds = self.settings.get('min_sequence_lock_seconds', 60 if self.debug else (1 * 60 * 60)) self.max_sequence_lock_seconds = self.settings.get('max_sequence_lock_seconds', 96 * 60 * 60) + self._wallet_update_timeout = self.settings.get('wallet_update_timeout', 10) + self._restrict_unknown_seed_wallets = self.settings.get('restrict_unknown_seed_wallets', True) self._bid_expired_leeway = 5 @@ -492,6 +494,10 @@ class BasicSwap(BaseApp): if self.coin_clients[coin]['connection_type'] == 'rpc': if coin == Coins.XMR: + self.coin_clients[coin]['rpctimeout'] = chain_client_settings.get('rpctimeout', 60) + self.coin_clients[coin]['walletrpctimeout'] = chain_client_settings.get('walletrpctimeout', 120) + self.coin_clients[coin]['walletrpctimeoutlong'] = chain_client_settings.get('walletrpctimeoutlong', 600) + if chain_client_settings.get('automatically_select_daemon', False): self.selectXMRRemoteDaemon(coin) @@ -513,6 +519,7 @@ class BasicSwap(BaseApp): coin_settings = self.coin_clients[coin] rpchost: str = coin_settings['rpchost'] rpcport: int = coin_settings['rpcport'] + timeout: int = coin_settings['rpctimeout'] proxy_host: str = self.tor_proxy_host if self.use_tor_proxy else None proxy_port: int = self.tor_proxy_port if self.use_tor_proxy else None @@ -527,7 +534,7 @@ class BasicSwap(BaseApp): self.log.info(f'Trying last used url {rpchost}:{rpcport}.') try: rpc2 = make_xmr_rpc2_func(rpcport, daemon_login, rpchost, proxy_host=proxy_host, proxy_port=proxy_port) - test = rpc2('get_height', timeout=20)['height'] + test = rpc2('get_height', timeout=timeout)['height'] return True except Exception as e: self.log.warning(f'Failed to set XMR remote daemon to {rpchost}:{rpcport}, {e}') @@ -537,7 +544,7 @@ class BasicSwap(BaseApp): try: rpchost, rpcport = url.rsplit(':', 1) rpc2 = make_xmr_rpc2_func(rpcport, daemon_login, rpchost, proxy_host=proxy_host, proxy_port=proxy_port) - test = rpc2('get_height', timeout=20)['height'] + test = rpc2('get_height', timeout=timeout)['height'] coin_settings['rpchost'] = rpchost coin_settings['rpcport'] = rpcport data = { @@ -6622,7 +6629,7 @@ class BasicSwap(BaseApp): handle = self.thread_pool.submit(self.updateWalletInfo, c) if wait_for_complete: try: - handle.result(timeout=10) + handle.result(timeout=self._wallet_update_timeout) except Exception as e: self.log.error(f'updateWalletInfo {e}') diff --git a/basicswap/interface/xmr.py b/basicswap/interface/xmr.py index 524b649..0b0a8ec 100644 --- a/basicswap/interface/xmr.py +++ b/basicswap/interface/xmr.py @@ -110,9 +110,13 @@ class XMRInterface(CoinInterface): elif manage_daemon is False: self._log.info(f'Connecting to remote {self.coin_name()} daemon at {rpchost}.') - self.rpc = make_xmr_rpc_func(coin_settings['rpcport'], daemon_login, host=rpchost, proxy_host=proxy_host, proxy_port=proxy_port) - self.rpc2 = make_xmr_rpc2_func(coin_settings['rpcport'], daemon_login, host=rpchost, proxy_host=proxy_host, proxy_port=proxy_port) # non-json endpoint - self.rpc_wallet = make_xmr_rpc_func(coin_settings['walletrpcport'], coin_settings['walletrpcauth'], host=coin_settings.get('walletrpchost', '127.0.0.1')) + self._rpctimeout = coin_settings.get('rpctimeout', 60) + self._walletrpctimeout = coin_settings.get('walletrpctimeout', 120) + self._walletrpctimeoutlong = coin_settings.get('walletrpctimeoutlong', 600) + + self.rpc = make_xmr_rpc_func(coin_settings['rpcport'], daemon_login, host=rpchost, proxy_host=proxy_host, proxy_port=proxy_port, default_timeout=self._rpctimeout) + self.rpc2 = make_xmr_rpc2_func(coin_settings['rpcport'], daemon_login, host=rpchost, proxy_host=proxy_host, proxy_port=proxy_port, default_timeout=self._rpctimeout) # non-json endpoint + self.rpc_wallet = make_xmr_rpc_func(coin_settings['walletrpcport'], coin_settings['walletrpcauth'], host=coin_settings.get('walletrpchost', '127.0.0.1'), default_timeout=self._walletrpctimeout) def checkWallets(self) -> int: return 1 @@ -176,7 +180,7 @@ class XMRInterface(CoinInterface): return self.rpc_wallet('get_version')['version'] def getBlockchainInfo(self): - get_height = self.rpc2('get_height', timeout=30) + get_height = self.rpc2('get_height', timeout=self._rpctimeout) rv = { 'blocks': get_height['height'], 'verificationprogress': 0.0, @@ -187,7 +191,7 @@ class XMRInterface(CoinInterface): # get_block_count returns "Internal error" if bootstrap-daemon is active if get_height['untrusted'] is True: rv['bootstrapping'] = True - get_info = self.rpc2('get_info', timeout=30) + get_info = self.rpc2('get_info', timeout=self._rpctimeout) if 'height_without_bootstrap' in get_info: rv['blocks'] = get_info['height_without_bootstrap'] @@ -195,7 +199,7 @@ class XMRInterface(CoinInterface): if rv['known_block_count'] > rv['blocks']: rv['verificationprogress'] = rv['blocks'] / rv['known_block_count'] else: - rv['known_block_count'] = self.rpc('get_block_count', timeout=30)['count'] + rv['known_block_count'] = self.rpc('get_block_count', timeout=self._rpctimeout)['count'] rv['verificationprogress'] = rv['blocks'] / rv['known_block_count'] except Exception as e: self._log.warning('XMR get_block_count failed with: %s', str(e)) @@ -204,7 +208,7 @@ class XMRInterface(CoinInterface): return rv def getChainHeight(self): - return self.rpc2('get_height', timeout=30)['height'] + return self.rpc2('get_height', timeout=self._rpctimeout)['height'] def getWalletInfo(self): with self._mx_wallet: @@ -347,7 +351,7 @@ class XMRInterface(CoinInterface): self.createWallet(params) self.openWallet(address_b58) - self.rpc_wallet('refresh', timeout=600) + self.rpc_wallet('refresh', timeout=self._walletrpctimeoutlong) ''' # Debug @@ -382,10 +386,10 @@ class XMRInterface(CoinInterface): def findTxnByHash(self, txid): with self._mx_wallet: self.openWallet(self._wallet_filename) - self.rpc_wallet('refresh', timeout=600) + self.rpc_wallet('refresh', timeout=self._walletrpctimeoutlong) try: - current_height = self.rpc2('get_height', timeout=30)['height'] + current_height = self.rpc2('get_height', timeout=self._rpctimeout)['height'] self._log.info('findTxnByHash XMR current_height %d\nhash: %s', current_height, txid) except Exception as e: self._log.info('rpc failed %s', str(e)) diff --git a/basicswap/rpc_xmr.py b/basicswap/rpc_xmr.py index eb5ab26..94bc936 100644 --- a/basicswap/rpc_xmr.py +++ b/basicswap/rpc_xmr.py @@ -222,33 +222,35 @@ def callrpc_xmr2(rpc_port: int, method: str, params=None, auth=None, rpc_host='1 return r -def make_xmr_rpc2_func(port, auth, host='127.0.0.1', proxy_host=None, proxy_port=None): +def make_xmr_rpc2_func(port, auth, host='127.0.0.1', proxy_host=None, proxy_port=None, default_timeout=120): port = port auth = auth host = host transport = None + default_timeout = default_timeout if proxy_host: transport = SocksTransport() transport.set_proxy(proxy_host, proxy_port) - def rpc_func(method, params=None, wallet=None, timeout=120): + def rpc_func(method, params=None, wallet=None, timeout=default_timeout): nonlocal port, auth, host, transport return callrpc_xmr2(port, method, params, auth=auth, rpc_host=host, timeout=timeout, transport=transport) return rpc_func -def make_xmr_rpc_func(port, auth, host='127.0.0.1', proxy_host=None, proxy_port=None): +def make_xmr_rpc_func(port, auth, host='127.0.0.1', proxy_host=None, proxy_port=None, default_timeout=120): port = port auth = auth host = host transport = None + default_timeout = default_timeout if proxy_host: transport = SocksTransport() transport.set_proxy(proxy_host, proxy_port) - def rpc_func(method, params=None, wallet=None, timeout=120): + def rpc_func(method, params=None, wallet=None, timeout=default_timeout): nonlocal port, auth, host, transport return callrpc_xmr(port, method, params, rpc_host=host, auth=auth, timeout=timeout, transport=transport) return rpc_func diff --git a/bin/basicswap_prepare.py b/bin/basicswap_prepare.py index d492d01..96756ad 100755 --- a/bin/basicswap_prepare.py +++ b/bin/basicswap_prepare.py @@ -1456,7 +1456,7 @@ def main(): logger.warning('Not automatically setting --usetorproxy as --notorproxy is set') else: use_tor_proxy = True - logger.info(f'Automatically setting --usetorproxy') + logger.info('Automatically setting --usetorproxy') setConnectionParameters(allow_set_tor=False) @@ -1572,6 +1572,9 @@ def main(): 'bindir': os.path.join(bin_dir, 'monero'), 'restore_height': xmr_restore_height, 'blocks_confirmed': 3, + 'rpctimeout': 60, + 'walletrpctimeout': 120, + 'walletrpctimeoutlong': 600, }, 'pivx': { 'connection_type': 'rpc' if 'pivx' in with_coins else 'none', @@ -1800,7 +1803,8 @@ def main(): 'max_delay_event': 50, # Max delay in seconds before reacting to an event 'check_progress_seconds': 60, 'check_watched_seconds': 60, - 'check_expired_seconds': 60 + 'check_expired_seconds': 60, + 'wallet_update_timeout': 10, # Seconds to wait for wallet page update } if wshost != 'none':