2019-07-27 17:26:06 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2023-01-08 22:53:59 +00:00
|
|
|
# Copyright (c) 2019-2023 tecnovert
|
2019-07-27 17:26:06 +00:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
2020-10-30 08:55:45 +00:00
|
|
|
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
|
2019-07-27 17:26:06 +00:00
|
|
|
|
|
|
|
"""
|
2022-07-13 21:28:38 +00:00
|
|
|
export TEST_PATH=/tmp/test_basicswap
|
|
|
|
mkdir -p ${TEST_PATH}/bin
|
|
|
|
cp -r ~/tmp/basicswap_bin/* ${TEST_PATH}/bin
|
2019-09-30 20:21:25 +00:00
|
|
|
export PYTHONPATH=$(pwd)
|
|
|
|
python tests/basicswap/test_reload.py
|
2019-07-27 17:26:06 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
2020-12-03 23:46:01 +00:00
|
|
|
import logging
|
|
|
|
import unittest
|
2019-08-15 19:14:28 +00:00
|
|
|
import traceback
|
2019-10-02 20:34:03 +00:00
|
|
|
import threading
|
2020-12-03 23:46:01 +00:00
|
|
|
import multiprocessing
|
|
|
|
from unittest.mock import patch
|
2019-08-15 19:14:28 +00:00
|
|
|
|
2020-02-01 23:18:29 +00:00
|
|
|
from basicswap.rpc import (
|
2019-09-30 20:21:25 +00:00
|
|
|
callrpc_cli,
|
|
|
|
)
|
2022-11-17 22:58:14 +00:00
|
|
|
from tests.basicswap.util import (
|
2022-07-03 22:47:30 +00:00
|
|
|
read_json_api,
|
2022-07-15 14:38:05 +00:00
|
|
|
post_json_api,
|
2021-01-11 22:15:06 +00:00
|
|
|
waitForServer,
|
2022-11-17 22:58:14 +00:00
|
|
|
)
|
|
|
|
from tests.basicswap.common import (
|
2021-01-11 22:15:06 +00:00
|
|
|
waitForNumOffers,
|
|
|
|
waitForNumBids,
|
|
|
|
waitForNumSwapping,
|
|
|
|
)
|
2022-07-13 21:28:38 +00:00
|
|
|
from tests.basicswap.common_xmr import (
|
|
|
|
prepare_nodes,
|
|
|
|
)
|
2019-07-27 17:26:06 +00:00
|
|
|
import bin.basicswap_run as runSystem
|
2019-08-15 22:31:39 +00:00
|
|
|
|
2022-07-13 21:28:38 +00:00
|
|
|
TEST_PATH = os.path.expanduser(os.getenv('TEST_PATH', '~/test_basicswap1'))
|
2021-01-11 22:15:06 +00:00
|
|
|
delay_event = threading.Event()
|
2019-07-27 17:26:06 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger()
|
|
|
|
logger.level = logging.DEBUG
|
|
|
|
if not len(logger.handlers):
|
|
|
|
logger.addHandler(logging.StreamHandler(sys.stdout))
|
|
|
|
|
|
|
|
|
2019-09-30 20:21:25 +00:00
|
|
|
def btcRpc(client_no, cmd):
|
2022-07-13 21:28:38 +00:00
|
|
|
bin_path = os.path.join(TEST_PATH, 'bin', 'bitcoin')
|
|
|
|
data_path = os.path.join(TEST_PATH, 'client{}'.format(client_no), 'bitcoin')
|
2019-09-30 20:21:25 +00:00
|
|
|
return callrpc_cli(bin_path, data_path, 'regtest', cmd, 'bitcoin-cli')
|
|
|
|
|
|
|
|
|
2022-11-21 13:36:36 +00:00
|
|
|
def partRpc(client_no, cmd):
|
|
|
|
bin_path = os.path.join(TEST_PATH, 'bin', 'particl')
|
|
|
|
data_path = os.path.join(TEST_PATH, 'client{}'.format(client_no), 'particl')
|
|
|
|
return callrpc_cli(bin_path, data_path, 'regtest', cmd, 'particl-cli')
|
|
|
|
|
|
|
|
|
2019-10-02 20:34:03 +00:00
|
|
|
def updateThread():
|
|
|
|
btc_addr = btcRpc(0, 'getnewaddress mining_addr bech32')
|
|
|
|
|
2021-01-11 22:15:06 +00:00
|
|
|
while not delay_event.is_set():
|
2019-10-02 20:34:03 +00:00
|
|
|
btcRpc(0, 'generatetoaddress {} {}'.format(1, btc_addr))
|
2021-01-11 22:15:06 +00:00
|
|
|
delay_event.wait(5)
|
2019-10-02 20:34:03 +00:00
|
|
|
|
|
|
|
|
2019-07-27 17:26:06 +00:00
|
|
|
class Test(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
super(Test, cls).setUpClass()
|
|
|
|
|
2022-07-13 21:28:38 +00:00
|
|
|
prepare_nodes(3, 'bitcoin')
|
2019-08-15 19:14:28 +00:00
|
|
|
|
|
|
|
def run_thread(self, client_id):
|
2022-07-13 21:28:38 +00:00
|
|
|
client_path = os.path.join(TEST_PATH, 'client{}'.format(client_id))
|
2019-08-15 22:31:39 +00:00
|
|
|
testargs = ['basicswap-run', '-datadir=' + client_path, '-regtest']
|
2019-07-27 17:26:06 +00:00
|
|
|
with patch.object(sys, 'argv', testargs):
|
|
|
|
runSystem.main()
|
|
|
|
|
2022-11-21 13:36:36 +00:00
|
|
|
def wait_for_node_height(self, port=12701, wallet_ticker='part', wait_for_blocks=3):
|
|
|
|
# Wait for height, or sequencelock is thrown off by genesis blocktime
|
|
|
|
logging.info(f'Waiting for {wallet_ticker} chain height {wait_for_blocks} at port {port}', )
|
|
|
|
for i in range(60):
|
|
|
|
if delay_event.is_set():
|
|
|
|
raise ValueError('Test stopped.')
|
|
|
|
try:
|
|
|
|
wallet = read_json_api(port, f'wallets/{wallet_ticker}')
|
|
|
|
node_blocks = wallet['blocks']
|
|
|
|
print(f'{wallet_ticker} node_blocks {node_blocks}')
|
|
|
|
if node_blocks >= wait_for_blocks:
|
|
|
|
return
|
|
|
|
except Exception as e:
|
|
|
|
print('Error reading wallets', str(e))
|
|
|
|
delay_event.wait(1)
|
|
|
|
raise ValueError(f'wait_for_node_height timed out, {wallet_ticker}, {wait_for_blocks}, {port}')
|
|
|
|
|
2019-07-27 17:26:06 +00:00
|
|
|
def test_reload(self):
|
2019-10-04 18:23:33 +00:00
|
|
|
global stop_test
|
2019-08-15 22:31:39 +00:00
|
|
|
processes = []
|
2019-07-27 17:26:06 +00:00
|
|
|
|
2019-08-15 22:31:39 +00:00
|
|
|
for i in range(3):
|
|
|
|
processes.append(multiprocessing.Process(target=self.run_thread, args=(i,)))
|
|
|
|
processes[-1].start()
|
2019-08-15 19:14:28 +00:00
|
|
|
|
|
|
|
try:
|
2021-01-11 22:15:06 +00:00
|
|
|
waitForServer(delay_event, 12700)
|
2022-11-21 13:36:36 +00:00
|
|
|
partRpc(0, 'reservebalance false') # WakeThreadStakeMiner
|
|
|
|
self.wait_for_node_height()
|
2019-08-18 18:49:47 +00:00
|
|
|
|
2019-09-30 20:21:25 +00:00
|
|
|
num_blocks = 500
|
|
|
|
btc_addr = btcRpc(1, 'getnewaddress mining_addr bech32')
|
2020-11-21 13:16:27 +00:00
|
|
|
logging.info('Mining %d Bitcoin blocks to %s', num_blocks, btc_addr)
|
2019-09-30 20:21:25 +00:00
|
|
|
btcRpc(1, 'generatetoaddress {} {}'.format(num_blocks, btc_addr))
|
2022-11-21 13:36:36 +00:00
|
|
|
self.wait_for_node_height(12700, 'btc', num_blocks)
|
2019-09-30 20:21:25 +00:00
|
|
|
|
2022-07-15 14:38:05 +00:00
|
|
|
data = {
|
2019-08-15 19:14:28 +00:00
|
|
|
'addr_from': '-1',
|
2022-07-25 20:56:14 +00:00
|
|
|
'coin_from': 'PART',
|
2019-08-15 19:14:28 +00:00
|
|
|
'coin_to': '2',
|
|
|
|
'amt_from': '1',
|
|
|
|
'amt_to': '1',
|
2022-07-15 14:38:05 +00:00
|
|
|
'lockhrs': '24'}
|
2019-08-15 19:14:28 +00:00
|
|
|
|
2023-01-08 22:53:59 +00:00
|
|
|
offer_id = post_json_api(12700, 'offers/new', data)['offer_id']
|
2022-07-03 22:47:30 +00:00
|
|
|
summary = read_json_api(12700)
|
2022-07-31 18:01:49 +00:00
|
|
|
assert (summary['num_sent_offers'] == 1)
|
2019-08-15 19:14:28 +00:00
|
|
|
except Exception:
|
|
|
|
traceback.print_exc()
|
2019-07-27 17:26:06 +00:00
|
|
|
|
2023-01-08 22:53:59 +00:00
|
|
|
sentoffers = read_json_api(12700, 'sentoffers', {'active': True})
|
|
|
|
assert sentoffers[0]['offer_id'] == offer_id
|
|
|
|
|
2019-09-30 20:21:25 +00:00
|
|
|
logger.info('Waiting for offer:')
|
2021-01-11 22:15:06 +00:00
|
|
|
waitForNumOffers(delay_event, 12701, 1)
|
2019-09-30 20:21:25 +00:00
|
|
|
|
2022-07-03 22:47:30 +00:00
|
|
|
offers = read_json_api(12701, 'offers')
|
2019-09-30 20:21:25 +00:00
|
|
|
offer = offers[0]
|
|
|
|
|
2022-07-15 14:38:05 +00:00
|
|
|
data = {
|
2019-09-30 20:21:25 +00:00
|
|
|
'offer_id': offer['offer_id'],
|
2022-07-15 14:38:05 +00:00
|
|
|
'amount_from': offer['amount_from']}
|
2019-09-30 20:21:25 +00:00
|
|
|
|
2022-07-15 14:38:05 +00:00
|
|
|
bid_id = post_json_api(12701, 'bids/new', data)
|
2019-09-30 20:21:25 +00:00
|
|
|
|
2021-01-11 22:15:06 +00:00
|
|
|
waitForNumBids(delay_event, 12700, 1)
|
2019-09-30 20:21:25 +00:00
|
|
|
|
2022-07-03 22:47:30 +00:00
|
|
|
bids = read_json_api(12700, 'bids')
|
2019-10-02 20:34:03 +00:00
|
|
|
bid = bids[0]
|
2019-09-30 20:21:25 +00:00
|
|
|
|
2022-07-15 14:38:05 +00:00
|
|
|
data = {
|
2019-10-02 20:34:03 +00:00
|
|
|
'accept': True
|
2022-07-15 14:38:05 +00:00
|
|
|
}
|
|
|
|
rv = post_json_api(12700, 'bids/{}'.format(bid['bid_id']), data)
|
2022-07-31 18:01:49 +00:00
|
|
|
assert (rv['bid_state'] == 'Accepted')
|
2019-10-02 20:34:03 +00:00
|
|
|
|
2021-01-11 22:15:06 +00:00
|
|
|
waitForNumSwapping(delay_event, 12701, 1)
|
2019-10-02 20:34:03 +00:00
|
|
|
|
|
|
|
logger.info('Restarting client:')
|
|
|
|
c1 = processes[1]
|
|
|
|
c1.terminate()
|
|
|
|
c1.join()
|
|
|
|
processes[1] = multiprocessing.Process(target=self.run_thread, args=(1,))
|
|
|
|
processes[1].start()
|
|
|
|
|
2021-01-11 22:15:06 +00:00
|
|
|
waitForServer(delay_event, 12701)
|
2022-07-03 22:47:30 +00:00
|
|
|
rv = read_json_api(12701)
|
2022-07-31 18:01:49 +00:00
|
|
|
assert (rv['num_swapping'] == 1)
|
2019-10-02 20:34:03 +00:00
|
|
|
|
|
|
|
update_thread = threading.Thread(target=updateThread)
|
|
|
|
update_thread.start()
|
|
|
|
|
|
|
|
logger.info('Completing swap:')
|
|
|
|
for i in range(240):
|
2021-01-11 22:15:06 +00:00
|
|
|
delay_event.wait(5)
|
2019-10-02 20:34:03 +00:00
|
|
|
|
2022-07-03 22:47:30 +00:00
|
|
|
rv = read_json_api(12700, 'bids/{}'.format(bid['bid_id']))
|
2019-10-02 20:34:03 +00:00
|
|
|
if rv['bid_state'] == 'Completed':
|
|
|
|
break
|
2022-07-31 18:01:49 +00:00
|
|
|
assert (rv['bid_state'] == 'Completed')
|
2019-10-02 20:34:03 +00:00
|
|
|
|
2021-01-11 22:15:06 +00:00
|
|
|
delay_event.set()
|
2019-10-02 20:34:03 +00:00
|
|
|
update_thread.join()
|
2019-08-15 22:31:39 +00:00
|
|
|
for p in processes:
|
|
|
|
p.terminate()
|
|
|
|
for p in processes:
|
|
|
|
p.join()
|
2019-07-27 17:26:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|