From 5ee28d0aa39e0221e4c1306dd1e3464fe890e4b0 Mon Sep 17 00:00:00 2001 From: tecnovert Date: Sat, 2 Nov 2024 11:17:00 +0200 Subject: [PATCH] Set Bitcoincash .conf file and port from basicswap.json. --- .github/workflows/lint.yml | 2 +- basicswap/bin/prepare.py | 61 +++++++++++++++++++++-------------- basicswap/bin/run.py | 54 ++++++++++++++++++++++--------- basicswap/chainparams.py | 3 +- basicswap/interface/bch.py | 2 ++ tests/basicswap/common_xmr.py | 3 +- 6 files changed, 82 insertions(+), 43 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index a862ce9..4f29fcc 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -20,7 +20,7 @@ jobs: pip install flake8 codespell - name: Running flake8 run: | - PYTHONWARNINGS="ignore" flake8 --ignore=E501,F841,W503 --exclude=basicswap/contrib,basicswap/interface/contrib,messages_pb2.py,.eggs,.tox,bin/install_certifi.py + flake8 --ignore=E501,F841,W503 --per-file-ignores="basicswap/interface/bch.py:E131,E702" --exclude=basicswap/contrib,basicswap/interface/contrib,messages_pb2.py,.eggs,.tox,bin/install_certifi.py - name: Running codespell run: | codespell --check-filenames --disable-colors --quiet-level=7 --ignore-words=tests/lint/spelling.ignore-words.txt -S .git,.eggs,.tox,pgp,*.pyc,*basicswap/contrib,*basicswap/interface/contrib,*mnemonics.py,bin/install_certifi.py,*basicswap/static diff --git a/basicswap/bin/prepare.py b/basicswap/bin/prepare.py index 1abe373..a9b7729 100644 --- a/basicswap/bin/prepare.py +++ b/basicswap/bin/prepare.py @@ -37,7 +37,7 @@ from basicswap.ui.util import getCoinName from basicswap.util import toBool from basicswap.util.network import urlretrieve, make_reporthook from basicswap.util.rfc2440 import rfc2440_hash_password -from basicswap.bin.run import startDaemon, startXmrWalletDaemon +from basicswap.bin.run import startDaemon, startXmrWalletDaemon, getCoreBinName, getCoreBinArgs, getWalletBinName PARTICL_VERSION = os.getenv('PARTICL_VERSION', '23.2.7.0') PARTICL_VERSION_TAG = os.getenv('PARTICL_VERSION_TAG', '') @@ -140,7 +140,8 @@ BIN_ARCH = os.getenv('BIN_ARCH', BIN_ARCH) FILE_EXT = os.getenv('FILE_EXT', FILE_EXT) logger = logging.getLogger() -logger.level = logging.INFO +LOG_LEVEL = logging.DEBUG +logger.level = LOG_LEVEL if not len(logger.handlers): logger.addHandler(logging.StreamHandler(sys.stdout)) @@ -193,7 +194,8 @@ BTC_RPC_PWD = os.getenv('BTC_RPC_PWD', '') BCH_RPC_HOST = os.getenv('BCH_RPC_HOST', '127.0.0.1') BCH_RPC_PORT = int(os.getenv('BCH_RPC_PORT', 19997)) -BCH_ONION_PORT = int(os.getenv('BCH_ONION_PORT', 8334)) +BCH_ONION_PORT = int(os.getenv('BCH_ONION_PORT', 8335)) +BCH_PORT = int(os.getenv('BCH_PORT', 19798)) BCH_RPC_USER = os.getenv('BCH_RPC_USER', '') BCH_RPC_PWD = os.getenv('BCH_RPC_PWD', '') @@ -319,6 +321,7 @@ def setConnectionParameters(timeout: int = 5, allow_set_tor: bool = True): # Set low timeout for urlretrieve connections socket.setdefaulttimeout(timeout) + logger.level = logging.INFO def popConnectionParameters() -> None: @@ -326,6 +329,7 @@ def popConnectionParameters() -> None: socket.socket = default_socket socket.getaddrinfo = default_socket_getaddrinfo socket.setdefaulttimeout(default_socket_timeout) + logger.level = LOG_LEVEL def getRemoteFileLength(url: str) -> (int, bool): @@ -893,7 +897,8 @@ def prepareDataDir(coin, settings, chain, particl_mnemonic, extra_opts={}): os.makedirs(data_dir) if coin in ('wownero', 'monero'): - core_conf_path = os.path.join(data_dir, coin + 'd.conf') + conf_filename: str = core_settings.get('config_filename', coin + 'd.conf') + core_conf_path = os.path.join(data_dir, conf_filename) if os.path.exists(core_conf_path): exitWithError('{} exists'.format(core_conf_path)) with open(core_conf_path, 'w') as fp: @@ -934,9 +939,8 @@ def prepareDataDir(coin, settings, chain, particl_mnemonic, extra_opts={}): if not os.path.exists(wallets_dir): os.makedirs(wallets_dir) - wallet_conf_path = os.path.join(wallets_dir, coin + '-wallet-rpc.conf') - if coin == 'monero': - wallet_conf_path = os.path.join(wallets_dir, 'monero_wallet.conf') + wallet_conf_filename: str = core_settings.get('wallet_config_filename', 'monero_wallet.conf' if coin == 'monero' else (coin + '-wallet-rpc.conf')) + wallet_conf_path = os.path.join(wallets_dir, wallet_conf_filename) if os.path.exists(wallet_conf_path): exitWithError('{} exists'.format(wallet_conf_path)) with open(wallet_conf_path, 'w') as fp: @@ -966,7 +970,8 @@ def prepareDataDir(coin, settings, chain, particl_mnemonic, extra_opts={}): if coin == 'decred': chainname = 'simnet' if chain == 'regtest' else chain - core_conf_path = os.path.join(data_dir, 'dcrd.conf') + conf_filename: str = core_settings.get('config_filename', 'dcrd.conf') + core_conf_path = os.path.join(data_dir, conf_filename) if os.path.exists(core_conf_path): exitWithError('{} exists'.format(core_conf_path)) with open(core_conf_path, 'w') as fp: @@ -983,7 +988,8 @@ def prepareDataDir(coin, settings, chain, particl_mnemonic, extra_opts={}): if tor_control_password is not None: writeTorSettings(fp, coin, core_settings, tor_control_password) - wallet_conf_path = os.path.join(data_dir, 'dcrwallet.conf') + wallet_conf_filename: str = core_settings.get('wallet_config_filename', 'dcrwallet.conf') + wallet_conf_path = os.path.join(data_dir, wallet_conf_filename) if os.path.exists(wallet_conf_path): exitWithError('{} exists'.format(wallet_conf_path)) with open(wallet_conf_path, 'w') as fp: @@ -1001,8 +1007,8 @@ def prepareDataDir(coin, settings, chain, particl_mnemonic, extra_opts={}): return - core_conf_name: str = 'bitcoin.conf' if coin == 'bitcoincash' else coin + '.conf' - core_conf_path: str = os.path.join(data_dir, core_conf_name) + core_conf_name: str = core_settings.get('config_filename', coin + '.conf') + core_conf_path = os.path.join(data_dir, core_conf_name) if os.path.exists(core_conf_path): exitWithError('{} exists'.format(core_conf_path)) with open(core_conf_path, 'w') as fp: @@ -1136,14 +1142,14 @@ def modify_tor_config(settings, coin, tor_control_password=None, enable=False, e data_dir = coin_settings['datadir'] if coin in ('monero', 'wownero'): - core_conf_path = os.path.join(data_dir, coin + 'd.conf') + core_conf_name: str = coin_settings.get('config_filename', coin + 'd.conf') + core_conf_path = os.path.join(data_dir, core_conf_name) if not os.path.exists(core_conf_path): exitWithError('{} does not exist'.format(core_conf_path)) wallets_dir = coin_settings.get('walletsdir', data_dir) - wallet_conf_path = os.path.join(wallets_dir, coin + '-wallet-rpc.conf') - if coin == 'monero': - wallet_conf_path = os.path.join(wallets_dir, 'monero_wallet.conf') + wallet_conf_filename: str = coin_settings.get('wallet_config_filename', 'monero_wallet.conf' if coin == 'monero' else (coin + '-wallet-rpc.conf')) + wallet_conf_path = os.path.join(wallets_dir, wallet_conf_filename) if not os.path.exists(wallet_conf_path): exitWithError('{} does not exist'.format(wallet_conf_path)) @@ -1198,10 +1204,8 @@ def modify_tor_config(settings, coin, tor_control_password=None, enable=False, e coin_settings['trusted_daemon'] = extra_opts.get('trust_remote_node', 'auto') return - if coin == 'decred': - config_path = os.path.join(data_dir, 'dcrd.conf') - else: - config_path = os.path.join(data_dir, coin + '.conf') + core_conf_name: str = coin_settings.get('config_filename', 'dcrd.conf' if coin == 'decred' else (coin + '.conf')) + config_path = os.path.join(data_dir, core_conf_name) if not os.path.exists(config_path): exitWithError('{} does not exist'.format(config_path)) @@ -1322,8 +1326,9 @@ def test_particl_encryption(data_dir, settings, chain, use_tor_proxy): c = Coins.PART coin_name = 'particl' coin_settings = settings['chainclients'][coin_name] + daemon_args += getCoreBinArgs(c, coin_settings) if coin_settings['manage_daemon']: - filename = coin_name + 'd' + ('.exe' if os.name == 'nt' else '') + filename: str = getCoreBinName(c, coin_settings, coin_name + 'd') daemons.append(startDaemon(coin_settings['datadir'], coin_settings['bindir'], filename, daemon_args)) swap_client.setDaemonPID(c, daemons[-1].handle.pid) swap_client.setCoinRunParams(c) @@ -1374,17 +1379,19 @@ def initialise_wallets(particl_wallet_mnemonic, with_coins, data_dir, settings, if c == Coins.XMR: if coin_settings['manage_wallet_daemon']: filename = coin_name + '-wallet-rpc' + ('.exe' if os.name == 'nt' else '') + filename: str = getWalletBinName(c, coin_settings, coin_name + '-wallet-rpc') daemons.append(startXmrWalletDaemon(coin_settings['datadir'], coin_settings['bindir'], filename)) elif c == Coins.WOW: if coin_settings['manage_wallet_daemon']: - filename = coin_name + '-wallet-rpc' + ('.exe' if os.name == 'nt' else '') + filename: str = getWalletBinName(c, coin_settings, coin_name + '-wallet-rpc') daemons.append(startXmrWalletDaemon(coin_settings['datadir'], coin_settings['bindir'], filename)) elif c == Coins.DCR: pass else: if coin_settings['manage_daemon']: - filename = (coin_name if not coin_name == "bitcoincash" else "bitcoin") + 'd' + ('.exe' if os.name == 'nt' else '') + filename: str = getCoreBinName(c, coin_settings, coin_name + 'd') coin_args = ['-nofindpeers', '-nostaking'] if c == Coins.PART else [] + coin_args += getCoreBinArgs(c, coin_settings) if c == Coins.FIRO: coin_args += ['-hdseed={}'.format(swap_client.getWalletKey(Coins.FIRO, 1).hex())] @@ -1405,7 +1412,7 @@ def initialise_wallets(particl_wallet_mnemonic, with_coins, data_dir, settings, '--pass={}'.format(dcr_password), ] - filename = 'dcrwallet' + ('.exe' if os.name == 'nt' else '') + filename: str = getWalletBinName(c, coin_settings, 'dcrwallet') args = [os.path.join(coin_settings['bindir'], filename), '--create'] + extra_opts hex_seed = swap_client.getWalletKey(Coins.DCR, 1).hex() createDCRWallet(args, hex_seed, logger, threading.Event()) @@ -1786,13 +1793,15 @@ def main(): 'core_version_group': 22, }, 'bitcoincash': { - 'connection_type': 'rpc' if 'bitcoincash' in with_coins else 'none', - 'manage_daemon': True if ('bitcoincash' in with_coins and BCH_RPC_HOST == '127.0.0.1') else False, + 'connection_type': 'rpc', + 'manage_daemon': shouldManageDaemon('BCH'), 'rpchost': BCH_RPC_HOST, 'rpcport': BCH_RPC_PORT + port_offset, 'onionport': BCH_ONION_PORT + port_offset, 'datadir': os.getenv('BCH_DATA_DIR', os.path.join(data_dir, 'bitcoincash')), 'bindir': os.path.join(bin_dir, 'bitcoincash'), + 'port': BCH_PORT + port_offset, + 'config_filename': 'bitcoin.conf', 'use_segwit': False, 'blocks_confirmed': 1, 'conf_target': 2, @@ -1830,6 +1839,7 @@ def main(): 'blocks_confirmed': 2, 'conf_target': 2, 'core_type_group': 'dcr', + 'config_filename': 'dcrd.conf', 'min_relay_fee': 0.00001, }, 'namecoin': { @@ -1866,6 +1876,7 @@ def main(): 'rpctimeout': 60, 'walletrpctimeout': 120, 'walletrpctimeoutlong': 600, + 'wallet_config_filename': 'monero_wallet.conf', 'core_type_group': 'xmr', }, 'pivx': { diff --git a/basicswap/bin/run.py b/basicswap/bin/run.py index 8793240..9db29bb 100755 --- a/basicswap/bin/run.py +++ b/basicswap/bin/run.py @@ -69,7 +69,7 @@ def startDaemon(node_dir, bin_dir, daemon_bin, opts=[], extra_config={}): config_to_add.remove(line) if len(config_to_add) > 0: - logging.info('Rewriting litecoin.conf') + logger.info('Rewriting litecoin.conf') shutil.copyfile(ltc_conf_path, ltc_conf_path + '.last') with open(ltc_conf_path, 'a') as fp: for line in config_to_add: @@ -80,7 +80,8 @@ def startDaemon(node_dir, bin_dir, daemon_bin, opts=[], extra_config={}): if add_datadir: args.append('-datadir=' + datadir_path) args += opts - logging.info('Starting node ' + daemon_bin + ' ' + (('-datadir=' + node_dir) if add_datadir else '')) + logger.info('Starting node {}'.format(daemon_bin)) + logger.debug('Arguments {}'.format(' '.join(args))) opened_files = [] if extra_config.get('stdout_to_file', False): @@ -103,7 +104,8 @@ def startXmrDaemon(node_dir, bin_dir, daemon_bin, opts=[]): datadir_path = os.path.expanduser(node_dir) config_filename = 'wownerod.conf' if daemon_bin.startswith('wow') else 'monerod.conf' args = [daemon_path, '--non-interactive', '--config-file=' + os.path.join(datadir_path, config_filename)] + opts - logging.info('Starting node {} --data-dir={}'.format(daemon_path, node_dir)) + logger.info('Starting node {}'.format(daemon_bin)) + logger.debug('Arguments {}'.format(' '.join(args))) # return subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) file_stdout = open(os.path.join(datadir_path, 'core_stdout.log'), 'w') @@ -112,8 +114,8 @@ def startXmrDaemon(node_dir, bin_dir, daemon_bin, opts=[]): def startXmrWalletDaemon(node_dir, bin_dir, wallet_bin, opts=[]): - daemon_bin = os.path.expanduser(os.path.join(bin_dir, wallet_bin)) - args = [daemon_bin, '--non-interactive'] + daemon_path = os.path.expanduser(os.path.join(bin_dir, wallet_bin)) + args = [daemon_path, '--non-interactive'] needs_rewrite: bool = False config_to_remove = ['daemon-address=', 'untrusted-daemon=', 'trusted-daemon=', 'proxy='] @@ -127,19 +129,20 @@ def startXmrWalletDaemon(node_dir, bin_dir, wallet_bin, opts=[]): with open(config_path) as fp: for line in fp: if any(line.startswith(config_line) for config_line in config_to_remove): - logging.warning('Found old config in monero_wallet.conf: {}'.format(line.strip())) + logger.warning('Found old config in monero_wallet.conf: {}'.format(line.strip())) needs_rewrite = True args += opts if needs_rewrite: - logging.info('Rewriting wallet config') + logger.info('Rewriting wallet config') shutil.copyfile(config_path, config_path + '.last') with open(config_path + '.last') as fp_from, open(config_path, 'w') as fp_to: for line in fp_from: if not any(line.startswith(config_line) for config_line in config_to_remove): fp_to.write(line) - logging.info('Starting wallet daemon {} --wallet-dir={}'.format(daemon_bin, node_dir)) + logger.info('Starting wallet daemon {}'.format(wallet_bin)) + logger.debug('Arguments {}'.format(' '.join(args))) # TODO: return subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=data_dir) wallet_stdout = open(os.path.join(data_dir, 'wallet_stdout.log'), 'w') @@ -166,8 +169,25 @@ def ws_message_received(client, server, message): swap_client.log.debug(f'ws_message_received {client["id"]} {message}') +def getCoreBinName(coin_id: int, coin_settings, default_name: str) -> str: + return coin_settings.get('core_binname', chainparams[coin_id].get('core_binname', default_name)) + ('.exe' if os.name == 'nt' else '') + + +def getWalletBinName(coin_id: int, coin_settings, default_name: str) -> str: + return coin_settings.get('wallet_binname', chainparams[coin_id].get('wallet_binname', default_name)) + ('.exe' if os.name == 'nt' else '') + + +def getCoreBinArgs(coin_id: int, coin_settings): + extra_args = [] + if 'config_filename' in coin_settings: + extra_args.append('--conf=' + coin_settings['config_filename']) + if 'port' in coin_settings: + extra_args.append('--port=' + str(int(coin_settings['port']))) + return extra_args + + def runClient(fp, data_dir, chain, start_only_coins): - global swap_client + global swap_client, logger daemons = [] pids = [] threads = [] @@ -188,6 +208,7 @@ def runClient(fp, data_dir, chain, start_only_coins): settings = json.load(fs) swap_client = BasicSwap(fp, data_dir, settings, chain) + logger = swap_client.log if os.path.exists(pids_path): with open(pids_path) as fd: @@ -214,7 +235,8 @@ def runClient(fp, data_dir, chain, start_only_coins): if c in ('monero', 'wownero'): if v['manage_daemon'] is True: swap_client.log.info(f'Starting {display_name} daemon') - filename = (c if not c == "bitcoincash" else "bitcoin") + 'd' + ('.exe' if os.name == 'nt' else '') + filename: str = getCoreBinName(coin_id, v, c + 'd') + daemons.append(startXmrDaemon(v['datadir'], v['bindir'], filename)) pid = daemons[-1].handle.pid swap_client.log.info('Started {} {}'.format(filename, pid)) @@ -240,7 +262,8 @@ def runClient(fp, data_dir, chain, start_only_coins): opts.append(daemon_rpcuser + ':' + daemon_rpcpass) opts.append('--trusted-daemon' if trusted_daemon else '--untrusted-daemon') - filename = c + '-wallet-rpc' + ('.exe' if os.name == 'nt' else '') + filename: str = getWalletBinName(coin_id, v, c + '-wallet-rpc') + daemons.append(startXmrWalletDaemon(v['datadir'], v['bindir'], filename, opts)) pid = daemons[-1].handle.pid swap_client.log.info('Started {} {}'.format(filename, pid)) @@ -253,7 +276,7 @@ def runClient(fp, data_dir, chain, start_only_coins): use_shell: bool = True if os.name == 'nt' else False if v['manage_daemon'] is True: swap_client.log.info(f'Starting {display_name} daemon') - filename = 'dcrd' + ('.exe' if os.name == 'nt' else '') + filename: str = getCoreBinName(coin_id, v, 'dcrd') extra_config = {'add_datadir': False, 'stdout_to_file': True, 'stdout_filename': 'dcrd_stdout.log', 'use_shell': use_shell} daemons.append(startDaemon(appdata, v['bindir'], filename, opts=extra_opts, extra_config=extra_config)) @@ -262,7 +285,7 @@ def runClient(fp, data_dir, chain, start_only_coins): if v['manage_wallet_daemon'] is True: swap_client.log.info(f'Starting {display_name} wallet daemon') - filename = 'dcrwallet' + ('.exe' if os.name == 'nt' else '') + filename: str = getWalletBinName(coin_id, v, 'dcrwallet') wallet_pwd = v['wallet_pwd'] if wallet_pwd == '': @@ -280,8 +303,9 @@ def runClient(fp, data_dir, chain, start_only_coins): if v['manage_daemon'] is True: swap_client.log.info(f'Starting {display_name} daemon') - filename = (c if not c == "bitcoincash" else "bitcoin") + 'd' + ('.exe' if os.name == 'nt' else '') - daemons.append(startDaemon(v['datadir'], v['bindir'], filename)) + filename: str = getCoreBinName(coin_id, v, c + 'd') + extra_opts = getCoreBinArgs(coin_id, v) + daemons.append(startDaemon(v['datadir'], v['bindir'], filename, opts=extra_opts)) pid = daemons[-1].handle.pid pids.append((c, pid)) swap_client.setDaemonPID(c, pid) diff --git a/basicswap/chainparams.py b/basicswap/chainparams.py index 525736b..09d284d 100644 --- a/basicswap/chainparams.py +++ b/basicswap/chainparams.py @@ -443,7 +443,8 @@ chainparams = { 'has_cltv': True, 'has_csv': True, 'has_segwit': False, - 'cli_binname' : 'bitcoin-cli', + 'cli_binname': 'bitcoin-cli', + 'core_binname': 'bitcoind', 'mainnet': { 'rpcport': 8332, 'pubkey_address': 0, diff --git a/basicswap/interface/bch.py b/basicswap/interface/bch.py index 418d02a..df5eebc 100644 --- a/basicswap/interface/bch.py +++ b/basicswap/interface/bch.py @@ -239,6 +239,7 @@ class BCHInterface(BTCInterface): public_key: bytes = kwargs['public_key'] if 'public_key' in kwargs else Kal timelock: int = kwargs['timelock'] + # fmt: off return CScript([ # // v4.1.0-CashTokens-Optimized # // Based on swaplock.cash v4.1.0-CashTokens @@ -313,6 +314,7 @@ class BCHInterface(BTCInterface): # // BCH and/or CashTokens are simply forwarded to Refund contract. OP_ENDIF ]) + # fmt: on def pubkey_to_segwit_address(self, pk: bytes) -> str: raise NotImplementedError() diff --git a/tests/basicswap/common_xmr.py b/tests/basicswap/common_xmr.py index ab55d45..b4b6a56 100644 --- a/tests/basicswap/common_xmr.py +++ b/tests/basicswap/common_xmr.py @@ -105,6 +105,7 @@ def run_prepare(node_id, datadir_path, bins_path, with_coins, mnemonic_in=None, os.environ['BTC_RPC_PORT'] = str(BITCOIN_RPC_PORT_BASE) os.environ['LTC_RPC_PORT'] = str(LITECOIN_RPC_PORT_BASE) os.environ['DCR_RPC_PORT'] = str(DECRED_RPC_PORT_BASE) + os.environ['BCH_PORT'] = str(BCH_BASE_PORT) os.environ['BCH_RPC_PORT'] = str(BITCOINCASH_RPC_PORT_BASE) os.environ['FIRO_RPC_PORT'] = str(FIRO_RPC_PORT_BASE) @@ -329,7 +330,7 @@ def run_prepare(node_id, datadir_path, bins_path, with_coins, mnemonic_in=None, for line in lines: if not line.startswith('prune'): fp.write(line) - fp.write('port={}\n'.format(BCH_BASE_PORT + node_id + port_ofs)) + # NOTE: port is set (when starting daemon) from basicswap.json fp.write('bind=127.0.0.1\n') fp.write('dnsseed=0\n') fp.write('discover=0\n')