mirror of
https://github.com/basicswap/basicswap.git
synced 2025-11-05 10:28:10 +01:00
coins: Add PIVX
No CSV or segwit. sethdseed requires a fully synced chain, manual intervention required to set a key derived from the master mnemonic. Requires a pivxd version with a backported scantxoutset command.
This commit is contained in:
@@ -14,6 +14,7 @@ from urllib.request import urlopen
|
||||
|
||||
from basicswap.rpc import callrpc
|
||||
from basicswap.contrib.rpcauth import generate_salt, password_to_hmac
|
||||
from bin.basicswap_prepare import downloadPIVXParams
|
||||
|
||||
|
||||
TEST_HTTP_HOST = os.getenv('TEST_HTTP_HOST', '127.0.0.1') # Set to 0.0.0.0 when used in docker
|
||||
@@ -33,6 +34,10 @@ LTC_BASE_PORT = 34792
|
||||
LTC_BASE_RPC_PORT = 35792
|
||||
LTC_BASE_ZMQ_PORT = 36792
|
||||
|
||||
PIVX_BASE_PORT = 34892
|
||||
PIVX_BASE_RPC_PORT = 35892
|
||||
PIVX_BASE_ZMQ_PORT = 36892
|
||||
|
||||
PREFIX_SECRET_KEY_REGTEST = 0x2e
|
||||
|
||||
|
||||
@@ -73,6 +78,11 @@ def prepareDataDir(datadir, node_id, conf_file, dir_prefix, base_p2p_port=BASE_P
|
||||
fp.write('stakethreadconddelayms=1000\n')
|
||||
fp.write('smsgsregtestadjust=0\n')
|
||||
|
||||
if conf_file == 'pivx.conf':
|
||||
params_dir = os.path.join(datadir, 'pivx-params')
|
||||
downloadPIVXParams(params_dir)
|
||||
fp.write(f'paramsdir={params_dir}\n')
|
||||
|
||||
for i in range(0, num_nodes):
|
||||
if node_id == i:
|
||||
continue
|
||||
|
||||
@@ -27,6 +27,7 @@ from tests.basicswap.common import (
|
||||
BASE_PORT, BASE_RPC_PORT,
|
||||
BTC_BASE_PORT, BTC_BASE_RPC_PORT,
|
||||
LTC_BASE_PORT,
|
||||
PIVX_BASE_PORT,
|
||||
)
|
||||
from basicswap.contrib.rpcauth import generate_salt, password_to_hmac
|
||||
|
||||
@@ -45,9 +46,6 @@ XMR_BASE_P2P_PORT = 17792
|
||||
XMR_BASE_RPC_PORT = 29798
|
||||
XMR_BASE_WALLET_RPC_PORT = 29998
|
||||
|
||||
LTC_BASE_RPC_PORT = 35792
|
||||
LTC_BASE_ZMQ_PORT = 36792
|
||||
|
||||
EXTRA_CONFIG_JSON = json.loads(os.getenv('EXTRA_CONFIG_JSON', '{}'))
|
||||
|
||||
|
||||
@@ -192,6 +190,33 @@ def run_prepare(node_id, datadir_path, bins_path, with_coins, mnemonic_in=None,
|
||||
for opt in EXTRA_CONFIG_JSON.get('ltc{}'.format(node_id), []):
|
||||
fp.write(opt + '\n')
|
||||
|
||||
if 'pivx' in coins_array:
|
||||
# Pruned nodes don't provide blocks
|
||||
with open(os.path.join(datadir_path, 'pivx', 'pivx.conf'), 'r') as fp:
|
||||
lines = fp.readlines()
|
||||
with open(os.path.join(datadir_path, 'pivx', 'pivx.conf'), 'w') as fp:
|
||||
for line in lines:
|
||||
if not line.startswith('prune'):
|
||||
fp.write(line)
|
||||
fp.write('port={}\n'.format(PIVX_BASE_PORT + node_id + port_ofs))
|
||||
fp.write('bind=127.0.0.1\n')
|
||||
fp.write('dnsseed=0\n')
|
||||
fp.write('discover=0\n')
|
||||
fp.write('listenonion=0\n')
|
||||
fp.write('upnp=0\n')
|
||||
if use_rpcauth:
|
||||
salt = generate_salt(16)
|
||||
rpc_user = 'test_pivx_' + str(node_id)
|
||||
rpc_pass = 'test_pivx_pwd_' + str(node_id)
|
||||
fp.write('rpcauth={}:{}${}\n'.format(rpc_user, salt, password_to_hmac(salt, rpc_pass)))
|
||||
settings['chainclients']['pivx']['rpcuser'] = rpc_user
|
||||
settings['chainclients']['pivx']['rpcpassword'] = rpc_pass
|
||||
for ip in range(num_nodes):
|
||||
if ip != node_id:
|
||||
fp.write('connect=127.0.0.1:{}\n'.format(PIVX_BASE_PORT + ip + port_ofs))
|
||||
for opt in EXTRA_CONFIG_JSON.get('pivx{}'.format(node_id), []):
|
||||
fp.write(opt + '\n')
|
||||
|
||||
if 'monero' in coins_array:
|
||||
with open(os.path.join(datadir_path, 'monero', 'monerod.conf'), 'a') as fp:
|
||||
fp.write('p2p-bind-ip=127.0.0.1\n')
|
||||
|
||||
552
tests/basicswap/extended/test_pivx.py
Normal file
552
tests/basicswap/extended/test_pivx.py
Normal file
@@ -0,0 +1,552 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright (c) 2022 tecnovert
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
"""
|
||||
basicswap]$ python tests/basicswap/extended/test_pivx.py
|
||||
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import time
|
||||
import shutil
|
||||
import signal
|
||||
import logging
|
||||
import unittest
|
||||
import threading
|
||||
|
||||
import basicswap.config as cfg
|
||||
from basicswap.basicswap import (
|
||||
BasicSwap,
|
||||
Coins,
|
||||
SwapTypes,
|
||||
BidStates,
|
||||
TxStates,
|
||||
)
|
||||
from basicswap.util import (
|
||||
COIN,
|
||||
)
|
||||
from basicswap.basicswap_util import (
|
||||
TxLockTypes,
|
||||
)
|
||||
from basicswap.util.address import (
|
||||
toWIF,
|
||||
)
|
||||
from basicswap.rpc import (
|
||||
callrpc_cli,
|
||||
waitForRPC,
|
||||
)
|
||||
from basicswap.contrib.key import (
|
||||
ECKey,
|
||||
)
|
||||
from basicswap.http_server import (
|
||||
HttpThread,
|
||||
)
|
||||
from tests.basicswap.common import (
|
||||
checkForks,
|
||||
stopDaemons,
|
||||
wait_for_offer,
|
||||
wait_for_bid,
|
||||
wait_for_bid_tx_state,
|
||||
wait_for_in_progress,
|
||||
read_json_api,
|
||||
TEST_HTTP_HOST,
|
||||
TEST_HTTP_PORT,
|
||||
BASE_PORT,
|
||||
BASE_RPC_PORT,
|
||||
BASE_ZMQ_PORT,
|
||||
PREFIX_SECRET_KEY_REGTEST,
|
||||
)
|
||||
from bin.basicswap_run import startDaemon
|
||||
from bin.basicswap_prepare import downloadPIVXParams
|
||||
|
||||
|
||||
logger = logging.getLogger()
|
||||
logger.level = logging.DEBUG
|
||||
if not len(logger.handlers):
|
||||
logger.addHandler(logging.StreamHandler(sys.stdout))
|
||||
|
||||
NUM_NODES = 3
|
||||
PIVX_NODE = 3
|
||||
BTC_NODE = 4
|
||||
|
||||
delay_event = threading.Event()
|
||||
stop_test = False
|
||||
|
||||
|
||||
def prepareOtherDir(datadir, nodeId, conf_file='pivx.conf'):
|
||||
node_dir = os.path.join(datadir, str(nodeId))
|
||||
if not os.path.exists(node_dir):
|
||||
os.makedirs(node_dir)
|
||||
filePath = os.path.join(node_dir, conf_file)
|
||||
|
||||
with open(filePath, 'w+') as fp:
|
||||
fp.write('regtest=1\n')
|
||||
fp.write('[regtest]\n')
|
||||
fp.write('port=' + str(BASE_PORT + nodeId) + '\n')
|
||||
fp.write('rpcport=' + str(BASE_RPC_PORT + nodeId) + '\n')
|
||||
|
||||
fp.write('daemon=0\n')
|
||||
fp.write('printtoconsole=0\n')
|
||||
fp.write('server=1\n')
|
||||
fp.write('discover=0\n')
|
||||
fp.write('listenonion=0\n')
|
||||
fp.write('bind=127.0.0.1\n')
|
||||
fp.write('findpeers=0\n')
|
||||
fp.write('debug=1\n')
|
||||
fp.write('debugexclude=libevent\n')
|
||||
|
||||
fp.write('fallbackfee=0.01\n')
|
||||
fp.write('acceptnonstdtxn=0\n')
|
||||
|
||||
if conf_file == 'pivx.conf':
|
||||
params_dir = os.path.join(datadir, 'pivx-params')
|
||||
downloadPIVXParams(params_dir)
|
||||
fp.write(f'paramsdir={params_dir}\n')
|
||||
|
||||
if conf_file == 'bitcoin.conf':
|
||||
fp.write('wallet=wallet.dat\n')
|
||||
|
||||
|
||||
def prepareDir(datadir, nodeId, network_key, network_pubkey):
|
||||
node_dir = os.path.join(datadir, str(nodeId))
|
||||
if not os.path.exists(node_dir):
|
||||
os.makedirs(node_dir)
|
||||
filePath = os.path.join(node_dir, 'particl.conf')
|
||||
|
||||
with open(filePath, 'w+') as fp:
|
||||
fp.write('regtest=1\n')
|
||||
fp.write('[regtest]\n')
|
||||
fp.write('port=' + str(BASE_PORT + nodeId) + '\n')
|
||||
fp.write('rpcport=' + str(BASE_RPC_PORT + nodeId) + '\n')
|
||||
|
||||
fp.write('daemon=0\n')
|
||||
fp.write('printtoconsole=0\n')
|
||||
fp.write('server=1\n')
|
||||
fp.write('discover=0\n')
|
||||
fp.write('listenonion=0\n')
|
||||
fp.write('bind=127.0.0.1\n')
|
||||
fp.write('findpeers=0\n')
|
||||
fp.write('debug=1\n')
|
||||
fp.write('debugexclude=libevent\n')
|
||||
fp.write('zmqpubsmsg=tcp://127.0.0.1:' + str(BASE_ZMQ_PORT + nodeId) + '\n')
|
||||
fp.write('wallet=wallet.dat\n')
|
||||
fp.write('fallbackfee=0.01\n')
|
||||
|
||||
fp.write('acceptnonstdtxn=0\n')
|
||||
fp.write('minstakeinterval=5\n')
|
||||
fp.write('smsgsregtestadjust=0\n')
|
||||
|
||||
for i in range(0, NUM_NODES):
|
||||
if nodeId == i:
|
||||
continue
|
||||
fp.write('addnode=127.0.0.1:%d\n' % (BASE_PORT + i))
|
||||
|
||||
if nodeId < 2:
|
||||
fp.write('spentindex=1\n')
|
||||
fp.write('txindex=1\n')
|
||||
|
||||
basicswap_dir = os.path.join(datadir, str(nodeId), 'basicswap')
|
||||
if not os.path.exists(basicswap_dir):
|
||||
os.makedirs(basicswap_dir)
|
||||
|
||||
pivxdatadir = os.path.join(datadir, str(PIVX_NODE))
|
||||
btcdatadir = os.path.join(datadir, str(BTC_NODE))
|
||||
settings_path = os.path.join(basicswap_dir, cfg.CONFIG_FILENAME)
|
||||
settings = {
|
||||
'debug': True,
|
||||
'zmqhost': 'tcp://127.0.0.1',
|
||||
'zmqport': BASE_ZMQ_PORT + nodeId,
|
||||
'htmlhost': '127.0.0.1',
|
||||
'htmlport': 12700 + nodeId,
|
||||
'network_key': network_key,
|
||||
'network_pubkey': network_pubkey,
|
||||
'chainclients': {
|
||||
'particl': {
|
||||
'connection_type': 'rpc',
|
||||
'manage_daemon': False,
|
||||
'rpcport': BASE_RPC_PORT + nodeId,
|
||||
'datadir': node_dir,
|
||||
'bindir': cfg.PARTICL_BINDIR,
|
||||
'blocks_confirmed': 2, # Faster testing
|
||||
},
|
||||
'pivx': {
|
||||
'connection_type': 'rpc',
|
||||
'manage_daemon': False,
|
||||
'rpcport': BASE_RPC_PORT + PIVX_NODE,
|
||||
'datadir': pivxdatadir,
|
||||
'bindir': cfg.PIVX_BINDIR,
|
||||
'use_csv': False,
|
||||
'use_segwit': False,
|
||||
},
|
||||
'bitcoin': {
|
||||
'connection_type': 'rpc',
|
||||
'manage_daemon': False,
|
||||
'rpcport': BASE_RPC_PORT + BTC_NODE,
|
||||
'datadir': btcdatadir,
|
||||
'bindir': cfg.BITCOIN_BINDIR,
|
||||
'use_segwit': True,
|
||||
}
|
||||
},
|
||||
'check_progress_seconds': 2,
|
||||
'check_watched_seconds': 4,
|
||||
'check_expired_seconds': 60
|
||||
}
|
||||
with open(settings_path, 'w') as fp:
|
||||
json.dump(settings, fp, indent=4)
|
||||
|
||||
|
||||
def partRpc(cmd, node_id=0):
|
||||
return callrpc_cli(cfg.PARTICL_BINDIR, os.path.join(cfg.TEST_DATADIRS, str(node_id)), 'regtest', cmd, cfg.PARTICL_CLI)
|
||||
|
||||
|
||||
def btcRpc(cmd):
|
||||
return callrpc_cli(cfg.BITCOIN_BINDIR, os.path.join(cfg.TEST_DATADIRS, str(BTC_NODE)), 'regtest', cmd, cfg.BITCOIN_CLI)
|
||||
|
||||
|
||||
def pivxRpc(cmd):
|
||||
return callrpc_cli(cfg.PIVX_BINDIR, os.path.join(cfg.TEST_DATADIRS, str(PIVX_NODE)), 'regtest', cmd, cfg.PIVX_CLI)
|
||||
|
||||
|
||||
def signal_handler(sig, frame):
|
||||
global stop_test
|
||||
print('signal {} detected.'.format(sig))
|
||||
stop_test = True
|
||||
delay_event.set()
|
||||
|
||||
|
||||
def run_coins_loop(cls):
|
||||
while not stop_test:
|
||||
try:
|
||||
pivxRpc('generatetoaddress 1 {}'.format(cls.pivx_addr))
|
||||
btcRpc('generatetoaddress 1 {}'.format(cls.btc_addr))
|
||||
except Exception as e:
|
||||
logging.warning('run_coins_loop ' + str(e))
|
||||
time.sleep(1.0)
|
||||
|
||||
|
||||
def run_loop(self):
|
||||
while not stop_test:
|
||||
for c in self.swap_clients:
|
||||
c.update()
|
||||
time.sleep(1)
|
||||
|
||||
|
||||
def make_part_cli_rpc_func(node_id):
|
||||
node_id = node_id
|
||||
|
||||
def rpc_func(method, params=None, wallet=None):
|
||||
nonlocal node_id
|
||||
cmd = method
|
||||
if params:
|
||||
for p in params:
|
||||
cmd += ' "' + p + '"'
|
||||
return partRpc(cmd, node_id)
|
||||
return rpc_func
|
||||
|
||||
|
||||
class Test(unittest.TestCase):
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super(Test, cls).setUpClass()
|
||||
|
||||
eckey = ECKey()
|
||||
eckey.generate()
|
||||
cls.network_key = toWIF(PREFIX_SECRET_KEY_REGTEST, eckey.get_bytes())
|
||||
cls.network_pubkey = eckey.get_pubkey().get_bytes().hex()
|
||||
|
||||
if os.path.isdir(cfg.TEST_DATADIRS):
|
||||
logging.info('Removing ' + cfg.TEST_DATADIRS)
|
||||
for name in os.listdir(cfg.TEST_DATADIRS):
|
||||
if name == 'pivx-params':
|
||||
continue
|
||||
fullpath = os.path.join(cfg.TEST_DATADIRS, name)
|
||||
if os.path.isdir(fullpath):
|
||||
shutil.rmtree(fullpath)
|
||||
else:
|
||||
os.remove(fullpath)
|
||||
|
||||
for i in range(NUM_NODES):
|
||||
prepareDir(cfg.TEST_DATADIRS, i, cls.network_key, cls.network_pubkey)
|
||||
|
||||
prepareOtherDir(cfg.TEST_DATADIRS, PIVX_NODE)
|
||||
prepareOtherDir(cfg.TEST_DATADIRS, BTC_NODE, 'bitcoin.conf')
|
||||
|
||||
cls.daemons = []
|
||||
cls.swap_clients = []
|
||||
cls.http_threads = []
|
||||
|
||||
btc_data_dir = os.path.join(cfg.TEST_DATADIRS, str(BTC_NODE))
|
||||
if os.path.exists(os.path.join(cfg.BITCOIN_BINDIR, '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))
|
||||
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))
|
||||
logging.info('Started %s %d', cfg.PIVXD, cls.daemons[-1].pid)
|
||||
|
||||
for i in range(NUM_NODES):
|
||||
data_dir = os.path.join(cfg.TEST_DATADIRS, str(i))
|
||||
if os.path.exists(os.path.join(cfg.PARTICL_BINDIR, 'particl-wallet')):
|
||||
callrpc_cli(cfg.PARTICL_BINDIR, data_dir, 'regtest', '-wallet=wallet.dat create', 'particl-wallet')
|
||||
cls.daemons.append(startDaemon(data_dir, cfg.PARTICL_BINDIR, cfg.PARTICLD))
|
||||
logging.info('Started %s %d', cfg.PARTICLD, cls.daemons[-1].pid)
|
||||
|
||||
for i in range(NUM_NODES):
|
||||
rpc = make_part_cli_rpc_func(i)
|
||||
waitForRPC(rpc)
|
||||
if i == 0:
|
||||
rpc('extkeyimportmaster', ['abandon baby cabbage dad eager fabric gadget habit ice kangaroo lab absorb'])
|
||||
elif i == 1:
|
||||
rpc('extkeyimportmaster', ['pact mammal barrel matrix local final lecture chunk wasp survey bid various book strong spread fall ozone daring like topple door fatigue limb olympic', '', 'true'])
|
||||
rpc('getnewextaddress', ['lblExtTest'])
|
||||
rpc('rescanblockchain')
|
||||
else:
|
||||
rpc('extkeyimportmaster', [rpc('mnemonic', ['new'])['master']])
|
||||
|
||||
basicswap_dir = os.path.join(os.path.join(cfg.TEST_DATADIRS, str(i)), 'basicswap')
|
||||
settings_path = os.path.join(basicswap_dir, cfg.CONFIG_FILENAME)
|
||||
with open(settings_path) as fs:
|
||||
settings = json.load(fs)
|
||||
fp = open(os.path.join(basicswap_dir, 'basicswap.log'), 'w')
|
||||
cls.swap_clients.append(BasicSwap(fp, basicswap_dir, settings, 'regtest', log_name='BasicSwap{}'.format(i)))
|
||||
cls.swap_clients[-1].setDaemonPID(Coins.BTC, cls.daemons[0].pid)
|
||||
cls.swap_clients[-1].setDaemonPID(Coins.PIVX, cls.daemons[1].pid)
|
||||
cls.swap_clients[-1].setDaemonPID(Coins.PART, cls.daemons[2 + i].pid)
|
||||
cls.swap_clients[-1].start()
|
||||
|
||||
t = HttpThread(cls.swap_clients[i].fp, TEST_HTTP_HOST, TEST_HTTP_PORT + i, False, cls.swap_clients[i])
|
||||
cls.http_threads.append(t)
|
||||
t.start()
|
||||
|
||||
waitForRPC(pivxRpc)
|
||||
num_blocks = 1352 # CHECKLOCKTIMEVERIFY soft-fork activates at (regtest) block height 1351.
|
||||
logging.info('Mining %d pivx blocks', num_blocks)
|
||||
cls.pivx_addr = pivxRpc('getnewaddress mining_addr')
|
||||
pivxRpc('generatetoaddress {} {}'.format(num_blocks, cls.pivx_addr))
|
||||
|
||||
ro = pivxRpc('getblockchaininfo')
|
||||
try:
|
||||
assert (ro['bip9_softforks']['csv']['status'] == 'active')
|
||||
except Exception:
|
||||
logging.info('pivx: csv is not active')
|
||||
try:
|
||||
assert (ro['bip9_softforks']['segwit']['status'] == 'active')
|
||||
except Exception:
|
||||
logging.info('pivx: segwit is not active')
|
||||
|
||||
waitForRPC(btcRpc)
|
||||
cls.btc_addr = btcRpc('getnewaddress mining_addr bech32')
|
||||
logging.info('Mining %d Bitcoin blocks to %s', num_blocks, cls.btc_addr)
|
||||
btcRpc('generatetoaddress {} {}'.format(num_blocks, cls.btc_addr))
|
||||
|
||||
ro = btcRpc('getblockchaininfo')
|
||||
checkForks(ro)
|
||||
|
||||
ro = pivxRpc('getwalletinfo')
|
||||
print('pivxRpc', ro)
|
||||
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
cls.update_thread = threading.Thread(target=run_loop, args=(cls,))
|
||||
cls.update_thread.start()
|
||||
|
||||
cls.coins_update_thread = threading.Thread(target=run_coins_loop, args=(cls,))
|
||||
cls.coins_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):
|
||||
particl_blocks = cls.swap_clients[0].callrpc('getblockchaininfo')['blocks']
|
||||
print('particl_blocks', particl_blocks)
|
||||
if particl_blocks >= num_blocks:
|
||||
break
|
||||
delay_event.wait(1)
|
||||
assert (particl_blocks >= num_blocks)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
global stop_test
|
||||
logging.info('Finalising')
|
||||
stop_test = True
|
||||
cls.update_thread.join()
|
||||
cls.coins_update_thread.join()
|
||||
for t in cls.http_threads:
|
||||
t.stop()
|
||||
t.join()
|
||||
for c in cls.swap_clients:
|
||||
c.finalise()
|
||||
c.fp.close()
|
||||
|
||||
stopDaemons(cls.daemons)
|
||||
|
||||
super(Test, cls).tearDownClass()
|
||||
|
||||
def test_02_part_pivx(self):
|
||||
logging.info('---------- Test PART to PIVX')
|
||||
swap_clients = self.swap_clients
|
||||
|
||||
offer_id = swap_clients[0].postOffer(Coins.PART, Coins.PIVX, 100 * COIN, 0.1 * COIN, 100 * COIN, SwapTypes.SELLER_FIRST, TxLockTypes.ABS_LOCK_TIME)
|
||||
|
||||
wait_for_offer(delay_event, swap_clients[1], offer_id)
|
||||
offers = swap_clients[1].listOffers()
|
||||
assert (len(offers) == 1)
|
||||
for offer in offers:
|
||||
if offer.offer_id == offer_id:
|
||||
bid_id = swap_clients[1].postBid(offer_id, offer.amount_from)
|
||||
|
||||
wait_for_bid(delay_event, swap_clients[0], bid_id)
|
||||
|
||||
swap_clients[0].acceptBid(bid_id)
|
||||
|
||||
wait_for_in_progress(delay_event, swap_clients[1], bid_id, sent=True)
|
||||
|
||||
wait_for_bid(delay_event, swap_clients[0], bid_id, BidStates.SWAP_COMPLETED, wait_for=60)
|
||||
wait_for_bid(delay_event, swap_clients[1], bid_id, BidStates.SWAP_COMPLETED, sent=True, wait_for=60)
|
||||
|
||||
js_0 = read_json_api(1800)
|
||||
js_1 = read_json_api(1801)
|
||||
assert (js_0['num_swapping'] == 0 and js_0['num_watched_outputs'] == 0)
|
||||
assert (js_1['num_swapping'] == 0 and js_1['num_watched_outputs'] == 0)
|
||||
|
||||
def test_03_pivx_part(self):
|
||||
logging.info('---------- Test PIVX to PART')
|
||||
swap_clients = self.swap_clients
|
||||
|
||||
offer_id = swap_clients[1].postOffer(Coins.PIVX, Coins.PART, 10 * COIN, 9.0 * COIN, 10 * COIN, SwapTypes.SELLER_FIRST, TxLockTypes.ABS_LOCK_TIME)
|
||||
|
||||
wait_for_offer(delay_event, swap_clients[0], offer_id)
|
||||
offers = swap_clients[0].listOffers()
|
||||
for offer in offers:
|
||||
if offer.offer_id == offer_id:
|
||||
bid_id = swap_clients[0].postBid(offer_id, offer.amount_from)
|
||||
|
||||
wait_for_bid(delay_event, swap_clients[1], bid_id)
|
||||
swap_clients[1].acceptBid(bid_id)
|
||||
|
||||
wait_for_in_progress(delay_event, swap_clients[0], bid_id, sent=True)
|
||||
|
||||
wait_for_bid(delay_event, swap_clients[0], bid_id, BidStates.SWAP_COMPLETED, sent=True, wait_for=60)
|
||||
wait_for_bid(delay_event, swap_clients[1], bid_id, BidStates.SWAP_COMPLETED, wait_for=60)
|
||||
|
||||
js_0 = read_json_api(1800)
|
||||
js_1 = read_json_api(1801)
|
||||
assert (js_0['num_swapping'] == 0 and js_0['num_watched_outputs'] == 0)
|
||||
assert (js_1['num_swapping'] == 0 and js_1['num_watched_outputs'] == 0)
|
||||
|
||||
def test_04_pivx_btc(self):
|
||||
logging.info('---------- Test PIVX to BTC')
|
||||
swap_clients = self.swap_clients
|
||||
|
||||
offer_id = swap_clients[0].postOffer(Coins.PIVX, Coins.BTC, 10 * COIN, 0.1 * COIN, 10 * COIN, SwapTypes.SELLER_FIRST, TxLockTypes.ABS_LOCK_TIME)
|
||||
|
||||
wait_for_offer(delay_event, swap_clients[1], offer_id)
|
||||
offers = swap_clients[1].listOffers()
|
||||
for offer in offers:
|
||||
if offer.offer_id == offer_id:
|
||||
bid_id = swap_clients[1].postBid(offer_id, offer.amount_from)
|
||||
|
||||
wait_for_bid(delay_event, swap_clients[0], bid_id)
|
||||
swap_clients[0].acceptBid(bid_id)
|
||||
|
||||
wait_for_in_progress(delay_event, swap_clients[1], bid_id, sent=True)
|
||||
|
||||
wait_for_bid(delay_event, swap_clients[0], bid_id, BidStates.SWAP_COMPLETED, wait_for=60)
|
||||
wait_for_bid(delay_event, swap_clients[1], bid_id, BidStates.SWAP_COMPLETED, sent=True, wait_for=60)
|
||||
|
||||
js_0bid = read_json_api(1800, 'bids/{}'.format(bid_id.hex()))
|
||||
|
||||
js_0 = read_json_api(1800)
|
||||
js_1 = read_json_api(1801)
|
||||
|
||||
assert (js_0['num_swapping'] == 0 and js_0['num_watched_outputs'] == 0)
|
||||
assert (js_1['num_swapping'] == 0 and js_1['num_watched_outputs'] == 0)
|
||||
|
||||
def test_05_refund(self):
|
||||
# Seller submits initiate txn, buyer doesn't respond
|
||||
logging.info('---------- Test refund, PIVX to BTC')
|
||||
swap_clients = self.swap_clients
|
||||
|
||||
offer_id = swap_clients[0].postOffer(Coins.PIVX, Coins.BTC, 10 * COIN, 0.1 * COIN, 10 * COIN, SwapTypes.SELLER_FIRST,
|
||||
TxLockTypes.ABS_LOCK_BLOCKS, 10)
|
||||
|
||||
wait_for_offer(delay_event, swap_clients[1], offer_id)
|
||||
offers = swap_clients[1].listOffers()
|
||||
for offer in offers:
|
||||
if offer.offer_id == offer_id:
|
||||
bid_id = swap_clients[1].postBid(offer_id, offer.amount_from)
|
||||
|
||||
wait_for_bid(delay_event, swap_clients[0], bid_id)
|
||||
swap_clients[1].abandonBid(bid_id)
|
||||
swap_clients[0].acceptBid(bid_id)
|
||||
|
||||
wait_for_bid(delay_event, swap_clients[0], bid_id, BidStates.SWAP_COMPLETED, wait_for=60)
|
||||
wait_for_bid(delay_event, swap_clients[1], bid_id, BidStates.BID_ABANDONED, sent=True, wait_for=60)
|
||||
|
||||
js_0 = read_json_api(1800)
|
||||
js_1 = read_json_api(1801)
|
||||
assert (js_0['num_swapping'] == 0 and js_0['num_watched_outputs'] == 0)
|
||||
assert (js_1['num_swapping'] == 0 and js_1['num_watched_outputs'] == 0)
|
||||
|
||||
def test_06_self_bid(self):
|
||||
logging.info('---------- Test same client, BTC to PIVX')
|
||||
swap_clients = self.swap_clients
|
||||
|
||||
js_0_before = read_json_api(1800)
|
||||
|
||||
offer_id = swap_clients[0].postOffer(Coins.PIVX, Coins.BTC, 10 * COIN, 10 * COIN, 10 * COIN, SwapTypes.SELLER_FIRST, TxLockTypes.ABS_LOCK_TIME)
|
||||
|
||||
wait_for_offer(delay_event, swap_clients[0], offer_id)
|
||||
offers = swap_clients[0].listOffers()
|
||||
for offer in offers:
|
||||
if offer.offer_id == offer_id:
|
||||
bid_id = swap_clients[0].postBid(offer_id, offer.amount_from)
|
||||
|
||||
wait_for_bid(delay_event, swap_clients[0], bid_id)
|
||||
swap_clients[0].acceptBid(bid_id)
|
||||
|
||||
wait_for_bid_tx_state(delay_event, swap_clients[0], bid_id, TxStates.TX_REDEEMED, TxStates.TX_REDEEMED, wait_for=60)
|
||||
wait_for_bid(delay_event, swap_clients[0], bid_id, BidStates.SWAP_COMPLETED, wait_for=60)
|
||||
|
||||
js_0 = read_json_api(1800)
|
||||
assert (js_0['num_swapping'] == 0 and js_0['num_watched_outputs'] == 0)
|
||||
assert (js_0['num_recv_bids'] == js_0_before['num_recv_bids'] + 1 and js_0['num_sent_bids'] == js_0_before['num_sent_bids'] + 1)
|
||||
|
||||
def test_07_error(self):
|
||||
logging.info('---------- Test error, BTC to PIVX, set fee above bid value')
|
||||
swap_clients = self.swap_clients
|
||||
|
||||
js_0_before = read_json_api(1800)
|
||||
|
||||
offer_id = swap_clients[0].postOffer(Coins.PIVX, Coins.BTC, 0.001 * COIN, 1.0 * COIN, 0.001 * COIN, SwapTypes.SELLER_FIRST, TxLockTypes.ABS_LOCK_TIME)
|
||||
|
||||
wait_for_offer(delay_event, swap_clients[0], offer_id)
|
||||
offers = swap_clients[0].listOffers()
|
||||
for offer in offers:
|
||||
if offer.offer_id == offer_id:
|
||||
bid_id = swap_clients[0].postBid(offer_id, offer.amount_from)
|
||||
|
||||
wait_for_bid(delay_event, swap_clients[0], bid_id)
|
||||
swap_clients[0].acceptBid(bid_id)
|
||||
swap_clients[0].getChainClientSettings(Coins.BTC)['override_feerate'] = 10.0
|
||||
swap_clients[0].getChainClientSettings(Coins.PIVX)['override_feerate'] = 10.0
|
||||
wait_for_bid(delay_event, swap_clients[0], bid_id, BidStates.BID_ERROR, wait_for=60)
|
||||
|
||||
def pass_99_delay(self):
|
||||
global stop_test
|
||||
logging.info('Delay')
|
||||
for i in range(60 * 5):
|
||||
if stop_test:
|
||||
break
|
||||
time.sleep(1)
|
||||
print('delay', i)
|
||||
stop_test = True
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -43,6 +43,8 @@ class Test(BaseTest):
|
||||
cls.test_coin_from = Coins.BTC
|
||||
if not hasattr(cls, 'start_ltc_nodes'):
|
||||
cls.start_ltc_nodes = False
|
||||
if not hasattr(cls, 'start_pivx_nodes'):
|
||||
cls.start_pivx_nodes = False
|
||||
super(Test, cls).setUpClass()
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -38,7 +38,7 @@ from basicswap.util import (
|
||||
|
||||
|
||||
class Test(unittest.TestCase):
|
||||
REQUIRED_SETTINGS = {'blocks_confirmed': 1, 'conf_target': 1, 'use_segwit': True}
|
||||
REQUIRED_SETTINGS = {'blocks_confirmed': 1, 'conf_target': 1, 'use_segwit': True, 'connection_type': 'rpc'}
|
||||
|
||||
def test_serialise_num(self):
|
||||
def test_case(v, nb=None):
|
||||
|
||||
@@ -64,6 +64,7 @@ class Test(BaseTest):
|
||||
def setUpClass(cls):
|
||||
cls.start_ltc_nodes = True
|
||||
cls.start_xmr_nodes = False
|
||||
cls.start_pivx_nodes = False
|
||||
super(Test, cls).setUpClass()
|
||||
|
||||
btc_addr1 = callnoderpc(1, 'getnewaddress', ['initial funds', 'bech32'], base_rpc_port=BTC_BASE_RPC_PORT)
|
||||
|
||||
@@ -80,6 +80,8 @@ from tests.basicswap.common import (
|
||||
BTC_BASE_RPC_PORT,
|
||||
LTC_BASE_PORT,
|
||||
LTC_BASE_RPC_PORT,
|
||||
PIVX_BASE_PORT,
|
||||
PIVX_BASE_RPC_PORT,
|
||||
PREFIX_SECRET_KEY_REGTEST,
|
||||
)
|
||||
from bin.basicswap_run import startDaemon, startXmrDaemon
|
||||
@@ -91,6 +93,7 @@ NUM_NODES = 3
|
||||
NUM_XMR_NODES = 3
|
||||
NUM_BTC_NODES = 3
|
||||
NUM_LTC_NODES = 3
|
||||
NUM_PIVX_NODES = 3
|
||||
TEST_DIR = cfg.TEST_DATADIRS
|
||||
|
||||
XMR_BASE_P2P_PORT = 17792
|
||||
@@ -150,7 +153,7 @@ def startXmrWalletRPC(node_dir, bin_dir, wallet_bin, node_id, opts=[]):
|
||||
return subprocess.Popen(args, stdin=subprocess.PIPE, stdout=wallet_stdout, stderr=wallet_stderr, cwd=data_dir)
|
||||
|
||||
|
||||
def prepare_swapclient_dir(datadir, node_id, network_key, network_pubkey, with_ltc=False, with_xmr=False):
|
||||
def prepare_swapclient_dir(datadir, node_id, network_key, network_pubkey, with_coins=set()):
|
||||
basicswap_dir = os.path.join(datadir, 'basicswap_' + str(node_id))
|
||||
if not os.path.exists(basicswap_dir):
|
||||
os.makedirs(basicswap_dir)
|
||||
@@ -201,7 +204,7 @@ def prepare_swapclient_dir(datadir, node_id, network_key, network_pubkey, with_l
|
||||
'debug_ui': True,
|
||||
}
|
||||
|
||||
if with_xmr:
|
||||
if Coins.XMR in with_coins:
|
||||
settings['chainclients']['monero'] = {
|
||||
'connection_type': 'rpc',
|
||||
'manage_daemon': False,
|
||||
@@ -214,7 +217,7 @@ def prepare_swapclient_dir(datadir, node_id, network_key, network_pubkey, with_l
|
||||
'bindir': cfg.XMR_BINDIR,
|
||||
}
|
||||
|
||||
if with_ltc:
|
||||
if Coins.LTC in with_coins:
|
||||
settings['chainclients']['litecoin'] = {
|
||||
'connection_type': 'rpc',
|
||||
'manage_daemon': False,
|
||||
@@ -226,6 +229,18 @@ def prepare_swapclient_dir(datadir, node_id, network_key, network_pubkey, with_l
|
||||
'use_segwit': True,
|
||||
}
|
||||
|
||||
if Coins.PIVX in with_coins:
|
||||
settings['chainclients']['pivx'] = {
|
||||
'connection_type': 'rpc',
|
||||
'manage_daemon': False,
|
||||
'rpcport': PIVX_BASE_RPC_PORT + node_id,
|
||||
'rpcuser': 'test' + str(node_id),
|
||||
'rpcpassword': 'test_pass' + str(node_id),
|
||||
'datadir': os.path.join(datadir, 'pivx_' + str(node_id)),
|
||||
'bindir': cfg.PIVX_BINDIR,
|
||||
'use_segwit': False,
|
||||
}
|
||||
|
||||
with open(settings_path, 'w') as fp:
|
||||
json.dump(settings, fp, indent=4)
|
||||
|
||||
@@ -238,6 +253,10 @@ def ltcCli(cmd, node_id=0):
|
||||
return callrpc_cli(cfg.LITECOIN_BINDIR, os.path.join(TEST_DIR, 'ltc_' + str(node_id)), 'regtest', cmd, cfg.LITECOIN_CLI)
|
||||
|
||||
|
||||
def pivxCli(cmd, node_id=0):
|
||||
return callrpc_cli(cfg.PIVX_BINDIR, os.path.join(TEST_DIR, 'pivx_' + str(node_id)), 'regtest', cmd, cfg.PIVX_CLI)
|
||||
|
||||
|
||||
def signal_handler(sig, frame):
|
||||
logging.info('signal {} detected.'.format(sig))
|
||||
test_delay_event.set()
|
||||
@@ -283,6 +302,8 @@ def run_coins_loop(cls):
|
||||
btcCli('generatetoaddress 1 {}'.format(cls.btc_addr))
|
||||
if cls.ltc_addr is not None:
|
||||
ltcCli('generatetoaddress 1 {}'.format(cls.ltc_addr))
|
||||
if cls.pivx_addr is not None:
|
||||
pivxCli('generatetoaddress 1 {}'.format(cls.pivx_addr))
|
||||
if cls.xmr_addr is not None:
|
||||
callrpc_xmr_na(XMR_BASE_RPC_PORT + 1, 'generateblocks', {'wallet_address': cls.xmr_addr, 'amount_of_blocks': 1})
|
||||
except Exception as e:
|
||||
@@ -304,6 +325,8 @@ class BaseTest(unittest.TestCase):
|
||||
def setUpClass(cls):
|
||||
if not hasattr(cls, 'start_ltc_nodes'):
|
||||
cls.start_ltc_nodes = False
|
||||
if not hasattr(cls, 'start_pivx_nodes'):
|
||||
cls.start_pivx_nodes = False
|
||||
if not hasattr(cls, 'start_xmr_nodes'):
|
||||
cls.start_xmr_nodes = True
|
||||
|
||||
@@ -316,12 +339,14 @@ class BaseTest(unittest.TestCase):
|
||||
cls.part_daemons = []
|
||||
cls.btc_daemons = []
|
||||
cls.ltc_daemons = []
|
||||
cls.pivx_daemons = []
|
||||
cls.xmr_daemons = []
|
||||
cls.xmr_wallet_auth = []
|
||||
|
||||
cls.xmr_addr = None
|
||||
cls.btc_addr = None
|
||||
cls.ltc_addr = None
|
||||
cls.pivx_addr = None
|
||||
|
||||
logger.propagate = False
|
||||
logger.handlers = []
|
||||
@@ -333,7 +358,14 @@ class BaseTest(unittest.TestCase):
|
||||
|
||||
if os.path.isdir(TEST_DIR):
|
||||
logging.info('Removing ' + TEST_DIR)
|
||||
shutil.rmtree(TEST_DIR)
|
||||
for name in os.listdir(TEST_DIR):
|
||||
if name == 'pivx-params':
|
||||
continue
|
||||
fullpath = os.path.join(TEST_DIR, name)
|
||||
if os.path.isdir(fullpath):
|
||||
shutil.rmtree(fullpath)
|
||||
else:
|
||||
os.remove(fullpath)
|
||||
if not os.path.exists(TEST_DIR):
|
||||
os.makedirs(TEST_DIR)
|
||||
|
||||
@@ -391,6 +423,17 @@ class BaseTest(unittest.TestCase):
|
||||
|
||||
waitForRPC(make_rpc_func(i, base_rpc_port=LTC_BASE_RPC_PORT))
|
||||
|
||||
if cls.start_pivx_nodes:
|
||||
for i in range(NUM_PIVX_NODES):
|
||||
data_dir = prepareDataDir(TEST_DIR, i, 'pivx.conf', 'pivx_', base_p2p_port=PIVX_BASE_PORT, base_rpc_port=PIVX_BASE_RPC_PORT)
|
||||
if os.path.exists(os.path.join(cfg.PIVX_BINDIR, 'pivx-wallet')):
|
||||
callrpc_cli(cfg.PIVX_BINDIR, data_dir, 'regtest', '-wallet=wallet.dat create', 'pivx-wallet')
|
||||
|
||||
cls.pivx_daemons.append(startDaemon(os.path.join(TEST_DIR, 'pivx_' + str(i)), cfg.PIVX_BINDIR, cfg.PIVXD))
|
||||
logging.info('Started %s %d', cfg.PIVXD, cls.part_daemons[-1].pid)
|
||||
|
||||
waitForRPC(make_rpc_func(i, base_rpc_port=PIVX_BASE_RPC_PORT))
|
||||
|
||||
if cls.start_xmr_nodes:
|
||||
for i in range(NUM_XMR_NODES):
|
||||
prepareXmrDataDir(TEST_DIR, i, 'monerod.conf')
|
||||
@@ -417,7 +460,14 @@ class BaseTest(unittest.TestCase):
|
||||
cls.network_pubkey = eckey.get_pubkey().get_bytes().hex()
|
||||
|
||||
for i in range(NUM_NODES):
|
||||
prepare_swapclient_dir(TEST_DIR, i, cls.network_key, cls.network_pubkey, cls.start_ltc_nodes, cls.start_xmr_nodes)
|
||||
start_nodes = set()
|
||||
if cls.start_ltc_nodes:
|
||||
start_nodes.add(Coins.LTC)
|
||||
if cls.start_xmr_nodes:
|
||||
start_nodes.add(Coins.XMR)
|
||||
if cls.start_pivx_nodes:
|
||||
start_nodes.add(Coins.PIVX)
|
||||
prepare_swapclient_dir(TEST_DIR, i, cls.network_key, cls.network_pubkey, start_nodes)
|
||||
basicswap_dir = os.path.join(os.path.join(TEST_DIR, 'basicswap_' + str(i)))
|
||||
settings_path = os.path.join(basicswap_dir, cfg.CONFIG_FILENAME)
|
||||
with open(settings_path) as fs:
|
||||
@@ -481,6 +531,18 @@ class BaseTest(unittest.TestCase):
|
||||
|
||||
checkForks(callnoderpc(0, 'getblockchaininfo', base_rpc_port=LTC_BASE_RPC_PORT))
|
||||
|
||||
if cls.start_pivx_nodes:
|
||||
num_blocks = 400
|
||||
cls.pivx_addr = callnoderpc(0, 'getnewaddress', ['mining_addr'], base_rpc_port=PIVX_BASE_RPC_PORT)
|
||||
logging.info('Mining %d PIVX blocks to %s', num_blocks, cls.pivx_addr)
|
||||
callnoderpc(0, 'generatetoaddress', [num_blocks, cls.pivx_addr], base_rpc_port=PIVX_BASE_RPC_PORT)
|
||||
|
||||
# Switch addresses so wallet amounts stay constant
|
||||
num_blocks = 100
|
||||
cls.pivx_addr = cls.swap_clients[0].ci(Coins.PIVX).pubkey_to_address(void_block_rewards_pubkey)
|
||||
logging.info('Mining %d PIVX blocks to %s', num_blocks, cls.pivx_addr)
|
||||
callnoderpc(0, 'generatetoaddress', [num_blocks, cls.pivx_addr], base_rpc_port=PIVX_BASE_RPC_PORT)
|
||||
|
||||
num_blocks = 100
|
||||
if cls.start_xmr_nodes:
|
||||
cls.xmr_addr = cls.callxmrnodewallet(cls, 1, 'get_address')['address']
|
||||
@@ -537,6 +599,7 @@ class BaseTest(unittest.TestCase):
|
||||
stopDaemons(cls.part_daemons)
|
||||
stopDaemons(cls.btc_daemons)
|
||||
stopDaemons(cls.ltc_daemons)
|
||||
stopDaemons(cls.pivx_daemons)
|
||||
|
||||
super(BaseTest, cls).tearDownClass()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user