refactor: Lazy load interfaces.

2024-05-20_merge
tecnovert 1 year ago
parent 7fd60b3e82
commit 68ff57ebdc
No known key found for this signature in database
GPG Key ID: 8ED6D8750C4E3F93
  1. 16
      basicswap/basicswap.py
  2. 17
      basicswap/config.py
  3. 19
      tests/basicswap/extended/test_dash.py
  4. 22
      tests/basicswap/extended/test_firo.py
  5. 15
      tests/basicswap/extended/test_pivx.py
  6. 2
      tests/basicswap/test_xmr.py

@ -32,14 +32,6 @@ from sqlalchemy.orm.session import close_all_sessions
from .interface import Curves from .interface import Curves
from .interface.part import PARTInterface, PARTInterfaceAnon, PARTInterfaceBlind from .interface.part import PARTInterface, PARTInterfaceAnon, PARTInterfaceBlind
from .interface.btc import BTCInterface
from .interface.ltc import LTCInterface
from .interface.nmc import NMCInterface
from .interface.xmr import XMRInterface
from .interface.pivx import PIVXInterface
from .interface.dash import DASHInterface
from .interface.firo import FIROInterface
from .interface.passthrough_btc import PassthroughBTCInterface
from . import __version__ from . import __version__
from .rpc_xmr import make_xmr_rpc2_func from .rpc_xmr import make_xmr_rpc2_func
@ -578,27 +570,35 @@ class BasicSwap(BaseApp):
if coin == Coins.PART: if coin == Coins.PART:
return PARTInterface(self.coin_clients[coin], self.chain, self) return PARTInterface(self.coin_clients[coin], self.chain, self)
elif coin == Coins.BTC: elif coin == Coins.BTC:
from .interface.btc import BTCInterface
return BTCInterface(self.coin_clients[coin], self.chain, self) return BTCInterface(self.coin_clients[coin], self.chain, self)
elif coin == Coins.LTC: elif coin == Coins.LTC:
from .interface.ltc import LTCInterface
return LTCInterface(self.coin_clients[coin], self.chain, self) return LTCInterface(self.coin_clients[coin], self.chain, self)
elif coin == Coins.NMC: elif coin == Coins.NMC:
from .interface.nmc import NMCInterface
return NMCInterface(self.coin_clients[coin], self.chain, self) return NMCInterface(self.coin_clients[coin], self.chain, self)
elif coin == Coins.XMR: elif coin == Coins.XMR:
from .interface.xmr import XMRInterface
xmr_i = XMRInterface(self.coin_clients[coin], self.chain, self) xmr_i = XMRInterface(self.coin_clients[coin], self.chain, self)
chain_client_settings = self.getChainClientSettings(coin) chain_client_settings = self.getChainClientSettings(coin)
xmr_i.setWalletFilename(chain_client_settings['walletfile']) xmr_i.setWalletFilename(chain_client_settings['walletfile'])
return xmr_i return xmr_i
elif coin == Coins.PIVX: elif coin == Coins.PIVX:
from .interface.pivx import PIVXInterface
return PIVXInterface(self.coin_clients[coin], self.chain, self) return PIVXInterface(self.coin_clients[coin], self.chain, self)
elif coin == Coins.DASH: elif coin == Coins.DASH:
from .interface.dash import DASHInterface
return DASHInterface(self.coin_clients[coin], self.chain, self) return DASHInterface(self.coin_clients[coin], self.chain, self)
elif coin == Coins.FIRO: elif coin == Coins.FIRO:
from .interface.firo import FIROInterface
return FIROInterface(self.coin_clients[coin], self.chain, self) return FIROInterface(self.coin_clients[coin], self.chain, self)
else: else:
raise ValueError('Unknown coin type') raise ValueError('Unknown coin type')
def createPassthroughInterface(self, coin): def createPassthroughInterface(self, coin):
if coin == Coins.BTC: if coin == Coins.BTC:
from .interface.passthrough_btc import PassthroughBTCInterface
return PassthroughBTCInterface(self.coin_clients[coin], self.chain) return PassthroughBTCInterface(self.coin_clients[coin], self.chain)
else: else:
raise ValueError('Unknown coin type') raise ValueError('Unknown coin type')

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright (c) 2019-2022 tecnovert # Copyright (c) 2019-2023 tecnovert
# Distributed under the MIT software license, see the accompanying # Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php. # file LICENSE or http://www.opensource.org/licenses/mit-license.php.
@ -36,18 +36,3 @@ NAMECOIN_TX = os.getenv('NAMECOIN_TX', 'namecoin-tx' + bin_suffix)
XMR_BINDIR = os.path.expanduser(os.getenv('XMR_BINDIR', os.path.join(DEFAULT_TEST_BINDIR, 'monero'))) XMR_BINDIR = os.path.expanduser(os.getenv('XMR_BINDIR', os.path.join(DEFAULT_TEST_BINDIR, 'monero')))
XMRD = os.getenv('XMRD', 'monerod' + bin_suffix) XMRD = os.getenv('XMRD', 'monerod' + bin_suffix)
XMR_WALLET_RPC = os.getenv('XMR_WALLET_RPC', 'monero-wallet-rpc' + bin_suffix) XMR_WALLET_RPC = os.getenv('XMR_WALLET_RPC', 'monero-wallet-rpc' + bin_suffix)
PIVX_BINDIR = os.path.expanduser(os.getenv('PIVX_BINDIR', os.path.join(DEFAULT_TEST_BINDIR, 'pivx')))
PIVXD = os.getenv('PIVXD', 'pivxd' + bin_suffix)
PIVX_CLI = os.getenv('PIVX_CLI', 'pivx-cli' + bin_suffix)
PIVX_TX = os.getenv('PIVX_TX', 'pivx-tx' + bin_suffix)
DASH_BINDIR = os.path.expanduser(os.getenv('DASH_BINDIR', os.path.join(DEFAULT_TEST_BINDIR, 'dash')))
DASHD = os.getenv('DASHD', 'dashd' + bin_suffix)
DASH_CLI = os.getenv('DASH_CLI', 'dash-cli' + bin_suffix)
DASH_TX = os.getenv('DASH_TX', 'dash-tx' + bin_suffix)
FIRO_BINDIR = os.path.expanduser(os.getenv('FIRO_BINDIR', os.path.join(DEFAULT_TEST_BINDIR, 'firo')))
FIROD = os.getenv('FIROD', 'firod' + bin_suffix)
FIRO_CLI = os.getenv('FIRO_CLI', 'firo-cli' + bin_suffix)
FIRO_TX = os.getenv('FIRO_TX', 'firo-tx' + bin_suffix)

@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright (c) 2022 tecnovert # Copyright (c) 2022-2023 tecnovert
# Distributed under the MIT software license, see the accompanying # Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php. # file LICENSE or http://www.opensource.org/licenses/mit-license.php.
@ -83,6 +83,11 @@ BTC_NODE = 4
delay_event = threading.Event() delay_event = threading.Event()
stop_test = False stop_test = False
DASH_BINDIR = os.path.expanduser(os.getenv('DASH_BINDIR', os.path.join(cfg.DEFAULT_TEST_BINDIR, 'dash')))
DASHD = os.getenv('DASHD', 'dashd' + cfg.bin_suffix)
DASH_CLI = os.getenv('DASH_CLI', 'dash-cli' + cfg.bin_suffix)
DASH_TX = os.getenv('DASH_TX', 'dash-tx' + cfg.bin_suffix)
def prepareOtherDir(datadir, nodeId, conf_file='dash.conf'): def prepareOtherDir(datadir, nodeId, conf_file='dash.conf'):
node_dir = os.path.join(datadir, str(nodeId)) node_dir = os.path.join(datadir, str(nodeId))
@ -180,7 +185,7 @@ def prepareDir(datadir, nodeId, network_key, network_pubkey):
'manage_daemon': False, 'manage_daemon': False,
'rpcport': BASE_RPC_PORT + DASH_NODE, 'rpcport': BASE_RPC_PORT + DASH_NODE,
'datadir': dashdatadir, 'datadir': dashdatadir,
'bindir': cfg.DASH_BINDIR, 'bindir': DASH_BINDIR,
'use_csv': True, 'use_csv': True,
'use_segwit': False, 'use_segwit': False,
}, },
@ -219,7 +224,7 @@ def btcRpc(cmd):
def dashRpc(cmd, wallet=None): def dashRpc(cmd, wallet=None):
return callrpc_cli(cfg.DASH_BINDIR, os.path.join(cfg.TEST_DATADIRS, str(DASH_NODE)), 'regtest', cmd, cfg.DASH_CLI, wallet=wallet) return callrpc_cli(DASH_BINDIR, os.path.join(cfg.TEST_DATADIRS, str(DASH_NODE)), 'regtest', cmd, DASH_CLI, wallet=wallet)
def signal_handler(sig, frame): def signal_handler(sig, frame):
@ -299,12 +304,12 @@ class Test(unittest.TestCase):
''' '''
dash-wallet does not seem to create valid wallet files. dash-wallet does not seem to create valid wallet files.
if os.path.exists(os.path.join(cfg.DASH_BINDIR, 'dash-wallet')): if os.path.exists(os.path.join(DASH_BINDIR, 'dash-wallet')):
logging.info('Creating DASH wallet.') logging.info('Creating DASH wallet.')
callrpc_cli(cfg.DASH_BINDIR, dash_data_dir, 'regtest', '-wallet=wallet.dat create', 'dash-wallet') callrpc_cli(DASH_BINDIR, dash_data_dir, 'regtest', '-wallet=wallet.dat create', 'dash-wallet')
''' '''
cls.daemons.append(startDaemon(dash_data_dir, cfg.DASH_BINDIR, cfg.DASHD)) cls.daemons.append(startDaemon(dash_data_dir, DASH_BINDIR, DASHD))
logging.info('Started %s %d', cfg.DASHD, cls.daemons[-1].pid) logging.info('Started %s %d', DASHD, cls.daemons[-1].pid)
for i in range(NUM_NODES): for i in range(NUM_NODES):
data_dir = os.path.join(cfg.TEST_DATADIRS, str(i)) data_dir = os.path.join(cfg.TEST_DATADIRS, str(i))

@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright (c) 2022 tecnovert # Copyright (c) 2022-2023 tecnovert
# Distributed under the MIT software license, see the accompanying # Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php. # file LICENSE or http://www.opensource.org/licenses/mit-license.php.
@ -54,13 +54,18 @@ from tests.basicswap.test_xmr import BaseTest, test_delay_event, callnoderpc
logger = logging.getLogger() logger = logging.getLogger()
FIRO_BINDIR = os.path.expanduser(os.getenv('FIRO_BINDIR', os.path.join(cfg.DEFAULT_TEST_BINDIR, 'firo')))
FIROD = os.getenv('FIROD', 'firod' + cfg.bin_suffix)
FIRO_CLI = os.getenv('FIRO_CLI', 'firo-cli' + cfg.bin_suffix)
FIRO_TX = os.getenv('FIRO_TX', 'firo-tx' + cfg.bin_suffix)
FIRO_BASE_PORT = 34832 FIRO_BASE_PORT = 34832
FIRO_BASE_RPC_PORT = 35832 FIRO_BASE_RPC_PORT = 35832
FIRO_BASE_ZMQ_PORT = 36832 FIRO_BASE_ZMQ_PORT = 36832
def firoCli(cmd, node_id=0): def firoCli(cmd, node_id=0):
return callrpc_cli(cfg.FIRO_BINDIR, os.path.join(cfg.TEST_DATADIRS, 'firo_' + str(node_id)), 'regtest', cmd, cfg.FIRO_CLI) return callrpc_cli(FIRO_BINDIR, os.path.join(cfg.TEST_DATADIRS, 'firo_' + str(node_id)), 'regtest', cmd, FIRO_CLI)
def prepareDataDir(datadir, node_id, conf_file, dir_prefix, base_p2p_port, base_rpc_port, num_nodes=3): def prepareDataDir(datadir, node_id, conf_file, dir_prefix, base_p2p_port, base_rpc_port, num_nodes=3):
@ -126,15 +131,16 @@ class Test(BaseTest):
@classmethod @classmethod
def prepareExtraDataDir(cls, i): def prepareExtraDataDir(cls, i):
extra_opts = []
if not cls.restore_instance: if not cls.restore_instance:
seed_hex = cls.firo_seeds[i] seed_hex = cls.firo_seeds[i]
extra_opts = [f'-hdseed={seed_hex}', ] extra_opts.append(f'-hdseed={seed_hex}')
data_dir = prepareDataDir(cfg.TEST_DATADIRS, i, 'firo.conf', 'firo_', base_p2p_port=FIRO_BASE_PORT, base_rpc_port=FIRO_BASE_RPC_PORT) data_dir = prepareDataDir(cfg.TEST_DATADIRS, i, 'firo.conf', 'firo_', base_p2p_port=FIRO_BASE_PORT, base_rpc_port=FIRO_BASE_RPC_PORT)
if os.path.exists(os.path.join(cfg.FIRO_BINDIR, 'firo-wallet')): if os.path.exists(os.path.join(FIRO_BINDIR, 'firo-wallet')):
callrpc_cli(cfg.FIRO_BINDIR, data_dir, 'regtest', '-wallet=wallet.dat create', 'firo-wallet') callrpc_cli(FIRO_BINDIR, data_dir, 'regtest', '-wallet=wallet.dat create', 'firo-wallet')
cls.firo_daemons.append(startDaemon(os.path.join(cfg.TEST_DATADIRS, 'firo_' + str(i)), cfg.FIRO_BINDIR, cfg.FIROD, opts=extra_opts)) cls.firo_daemons.append(startDaemon(os.path.join(cfg.TEST_DATADIRS, 'firo_' + str(i)), FIRO_BINDIR, FIROD, opts=extra_opts))
logging.info('Started %s %d', cfg.FIROD, cls.firo_daemons[-1].pid) logging.info('Started %s %d', FIROD, cls.firo_daemons[-1].pid)
waitForRPC(make_rpc_func(i, base_rpc_port=FIRO_BASE_RPC_PORT)) waitForRPC(make_rpc_func(i, base_rpc_port=FIRO_BASE_RPC_PORT))
@ -182,7 +188,7 @@ class Test(BaseTest):
'rpcuser': 'test' + str(node_id), 'rpcuser': 'test' + str(node_id),
'rpcpassword': 'test_pass' + str(node_id), 'rpcpassword': 'test_pass' + str(node_id),
'datadir': os.path.join(datadir, 'firo_' + str(node_id)), 'datadir': os.path.join(datadir, 'firo_' + str(node_id)),
'bindir': cfg.FIRO_BINDIR, 'bindir': FIRO_BINDIR,
'use_csv': True, 'use_csv': True,
'use_segwit': False, 'use_segwit': False,
} }

@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright (c) 2022 tecnovert # Copyright (c) 2022-2023 tecnovert
# Distributed under the MIT software license, see the accompanying # Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php. # file LICENSE or http://www.opensource.org/licenses/mit-license.php.
@ -84,6 +84,11 @@ BTC_NODE = 4
delay_event = threading.Event() delay_event = threading.Event()
stop_test = False stop_test = False
PIVX_BINDIR = os.path.expanduser(os.getenv('PIVX_BINDIR', os.path.join(cfg.DEFAULT_TEST_BINDIR, 'pivx')))
PIVXD = os.getenv('PIVXD', 'pivxd' + cfg.bin_suffix)
PIVX_CLI = os.getenv('PIVX_CLI', 'pivx-cli' + cfg.bin_suffix)
PIVX_TX = os.getenv('PIVX_TX', 'pivx-tx' + cfg.bin_suffix)
def prepareOtherDir(datadir, nodeId, conf_file='pivx.conf'): def prepareOtherDir(datadir, nodeId, conf_file='pivx.conf'):
node_dir = os.path.join(datadir, str(nodeId)) node_dir = os.path.join(datadir, str(nodeId))
@ -186,7 +191,7 @@ def prepareDir(datadir, nodeId, network_key, network_pubkey):
'manage_daemon': False, 'manage_daemon': False,
'rpcport': BASE_RPC_PORT + PIVX_NODE, 'rpcport': BASE_RPC_PORT + PIVX_NODE,
'datadir': pivxdatadir, 'datadir': pivxdatadir,
'bindir': cfg.PIVX_BINDIR, 'bindir': PIVX_BINDIR,
'use_csv': False, 'use_csv': False,
'use_segwit': False, 'use_segwit': False,
}, },
@ -225,7 +230,7 @@ def btcRpc(cmd):
def pivxRpc(cmd): def pivxRpc(cmd):
return callrpc_cli(cfg.PIVX_BINDIR, os.path.join(cfg.TEST_DATADIRS, str(PIVX_NODE)), 'regtest', cmd, cfg.PIVX_CLI) return callrpc_cli(PIVX_BINDIR, os.path.join(cfg.TEST_DATADIRS, str(PIVX_NODE)), 'regtest', cmd, PIVX_CLI)
def signal_handler(sig, frame): def signal_handler(sig, frame):
@ -306,8 +311,8 @@ class Test(unittest.TestCase):
callrpc_cli(cfg.BITCOIN_BINDIR, btc_data_dir, 'regtest', '-wallet=wallet.dat create', 'bitcoin-wallet') callrpc_cli(cfg.BITCOIN_BINDIR, btc_data_dir, 'regtest', '-wallet=wallet.dat create', 'bitcoin-wallet')
cls.daemons.append(startDaemon(btc_data_dir, cfg.BITCOIN_BINDIR, cfg.BITCOIND)) cls.daemons.append(startDaemon(btc_data_dir, cfg.BITCOIN_BINDIR, cfg.BITCOIND))
logging.info('Started %s %d', cfg.BITCOIND, cls.daemons[-1].pid) logging.info('Started %s %d', cfg.BITCOIND, cls.daemons[-1].pid)
cls.daemons.append(startDaemon(os.path.join(cfg.TEST_DATADIRS, str(PIVX_NODE)), cfg.PIVX_BINDIR, cfg.PIVXD)) cls.daemons.append(startDaemon(os.path.join(cfg.TEST_DATADIRS, str(PIVX_NODE)), PIVX_BINDIR, PIVXD))
logging.info('Started %s %d', cfg.PIVXD, cls.daemons[-1].pid) logging.info('Started %s %d', PIVXD, cls.daemons[-1].pid)
for i in range(NUM_NODES): for i in range(NUM_NODES):
data_dir = os.path.join(cfg.TEST_DATADIRS, str(i)) data_dir = os.path.join(cfg.TEST_DATADIRS, str(i))

@ -346,6 +346,8 @@ class BaseTest(unittest.TestCase):
stream_stdout.setFormatter(formatter) stream_stdout.setFormatter(formatter)
logger.addHandler(stream_stdout) logger.addHandler(stream_stdout)
logging.info('Setting up tests for class: ' + cls.__name__)
diagrams_dir = 'doc/protocols/sequence_diagrams' diagrams_dir = 'doc/protocols/sequence_diagrams'
cls.states_bidder = extract_states_from_xu_file(os.path.join(diagrams_dir, 'ads.bidder.alt.xu'), 'B') cls.states_bidder = extract_states_from_xu_file(os.path.join(diagrams_dir, 'ads.bidder.alt.xu'), 'B')
cls.states_offerer = extract_states_from_xu_file(os.path.join(diagrams_dir, 'ads.offerer.alt.xu'), 'O') cls.states_offerer = extract_states_from_xu_file(os.path.join(diagrams_dir, 'ads.offerer.alt.xu'), 'O')

Loading…
Cancel
Save