refactor: Use read_json_api in more tests.

This commit is contained in:
tecnovert 2022-07-04 00:47:30 +02:00
parent 0c620ea388
commit 02bd90053a
No known key found for this signature in database
GPG key ID: 8ED6D8750C4E3F93
12 changed files with 118 additions and 115 deletions

View file

@ -1,3 +1,3 @@
name = "basicswap"
__version__ = "0.0.34"
__version__ = "0.0.35"

View file

@ -196,12 +196,19 @@ def wait_for_in_progress(delay_event, swap_client, bid_id, sent=False):
raise ValueError('wait_for_in_progress timed out.')
def read_json_api(port, path=None):
url = f'http://127.0.0.1:{port}/json'
if path is not None:
url += '/' + path
return json.loads(urlopen(url).read())
def wait_for_none_active(delay_event, port, wait_for=30):
for i in range(wait_for):
if delay_event.is_set():
raise ValueError('Test stopped.')
delay_event.wait(1)
js = json.loads(urlopen('http://127.0.0.1:{}/json'.format(port)).read())
js = read_json_api(port)
if js['num_swapping'] == 0 and js['num_watched_outputs'] == 0:
return
raise ValueError('wait_for_none_active timed out.')
@ -213,7 +220,7 @@ def waitForServer(delay_event, port, wait_for=20):
raise ValueError('Test stopped.')
try:
delay_event.wait(1)
summary = json.loads(urlopen('http://127.0.0.1:{}/json'.format(port)).read())
summary = read_json_api(port)
return
except Exception as e:
print('waitForServer, error:', str(e))
@ -224,7 +231,7 @@ def waitForNumOffers(delay_event, port, offers, wait_for=20):
for i in range(wait_for):
if delay_event.is_set():
raise ValueError('Test stopped.')
summary = json.loads(urlopen('http://127.0.0.1:{}/json'.format(port)).read())
summary = read_json_api(port)
if summary['num_network_offers'] >= offers:
return
delay_event.wait(1)
@ -235,7 +242,7 @@ def waitForNumBids(delay_event, port, bids, wait_for=20):
for i in range(wait_for):
if delay_event.is_set():
raise ValueError('Test stopped.')
summary = json.loads(urlopen('http://127.0.0.1:{}/json'.format(port)).read())
summary = read_json_api(port)
if summary['num_recv_bids'] >= bids:
return
delay_event.wait(1)
@ -246,7 +253,7 @@ def waitForNumSwapping(delay_event, port, bids, wait_for=60):
for i in range(wait_for):
if delay_event.is_set():
raise ValueError('Test stopped.')
summary = json.loads(urlopen('http://127.0.0.1:{}/json'.format(port)).read())
summary = read_json_api(port)
if summary['num_swapping'] >= bids:
return
delay_event.wait(1)
@ -368,10 +375,3 @@ def compare_bid_states(states, expect_states):
raise ValueError(f'Expected state {expect_states[i]}, found {s[1]}')
assert(s[1] == expect_states[i])
return True
def read_json_api(port, path=None):
url = f'http://127.0.0.1:{port}/json'
if path is not None:
url += '/' + path
return json.loads(urlopen(url).read())

View file

@ -14,7 +14,6 @@ import logging
import unittest
import traceback
import threading
from urllib.request import urlopen
import basicswap.config as cfg
from basicswap.basicswap import (
@ -46,6 +45,7 @@ from tests.basicswap.common import (
checkForks,
stopDaemons,
delay_for,
read_json_api,
TEST_HTTP_HOST,
TEST_HTTP_PORT,
BASE_P2P_PORT,
@ -305,7 +305,7 @@ class Test(unittest.TestCase):
for i in range(wait_for):
if delay_event.is_set():
raise ValueError('Test stopped.')
js = json.loads(urlopen('http://127.0.0.1:{}/json/network'.format(port)).read())
js = read_json_api(port, 'network')
num_nodes = 0
for p in js['peers']:
if p['ready'] is True:
@ -320,7 +320,7 @@ class Test(unittest.TestCase):
logging.info('---------- Test Network')
swap_clients = self.swap_clients
js_1 = json.loads(urlopen('http://127.0.0.1:1801/json/wallets').read())
js_1 = read_json_api(1801, 'wallets')
offer_id = swap_clients[0].postOffer(Coins.PART, Coins.BTC, 100 * COIN, 0.1 * COIN, 100 * COIN, SwapTypes.SELLER_FIRST)
@ -329,7 +329,7 @@ class Test(unittest.TestCase):
self.wait_for_num_nodes(1800, 2)
js_n0 = json.loads(urlopen('http://127.0.0.1:1800/json/network').read())
js_n0 = read_json_api(1800, 'network')
print(dumpj(js_n0))
path = [swap_clients[0]._network._network_pubkey, swap_clients[2]._network._network_pubkey]

View file

@ -19,7 +19,6 @@ import signal
import logging
import unittest
import threading
from urllib.request import urlopen
import basicswap.config as cfg
from basicswap.basicswap import (
@ -54,6 +53,7 @@ from tests.basicswap.common import (
wait_for_bid,
wait_for_bid_tx_state,
wait_for_in_progress,
read_json_api,
TEST_HTTP_HOST,
TEST_HTTP_PORT,
BASE_PORT,
@ -383,8 +383,8 @@ class Test(unittest.TestCase):
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 = json.loads(urlopen('http://127.0.0.1:1800/json').read())
js_1 = json.loads(urlopen('http://127.0.0.1:1801/json').read())
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)
@ -408,8 +408,8 @@ class Test(unittest.TestCase):
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 = json.loads(urlopen('http://127.0.0.1:1800/json').read())
js_1 = json.loads(urlopen('http://127.0.0.1:1801/json').read())
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)
@ -433,10 +433,10 @@ class Test(unittest.TestCase):
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 = json.loads(urlopen('http://127.0.0.1:1800/json/bids/{}'.format(bid_id.hex())).read())
js_0bid = read_json_api(1800, 'bids/{}'.format(bid_id.hex()))
js_0 = json.loads(urlopen('http://127.0.0.1:1800/json').read())
js_1 = json.loads(urlopen('http://127.0.0.1:1801/json').read())
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)
@ -462,8 +462,8 @@ class Test(unittest.TestCase):
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 = json.loads(urlopen('http://127.0.0.1:1800/json').read())
js_1 = json.loads(urlopen('http://127.0.0.1:1801/json').read())
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)
@ -471,7 +471,7 @@ class Test(unittest.TestCase):
logging.info('---------- Test same client, BTC to NMC')
swap_clients = self.swap_clients
js_0_before = json.loads(urlopen('http://127.0.0.1:1800/json').read())
js_0_before = read_json_api(1800)
offer_id = swap_clients[0].postOffer(Coins.NMC, Coins.BTC, 10 * COIN, 10 * COIN, 10 * COIN, SwapTypes.SELLER_FIRST, ABS_LOCK_TIME)
@ -487,7 +487,7 @@ class Test(unittest.TestCase):
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 = json.loads(urlopen('http://127.0.0.1:1800/json').read())
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)
@ -495,7 +495,7 @@ class Test(unittest.TestCase):
logging.info('---------- Test error, BTC to NMC, set fee above bid value')
swap_clients = self.swap_clients
js_0_before = json.loads(urlopen('http://127.0.0.1:1800/json').read())
js_0_before = read_json_api(1800)
offer_id = swap_clients[0].postOffer(Coins.NMC, Coins.BTC, 0.001 * COIN, 1.0 * COIN, 0.001 * COIN, SwapTypes.SELLER_FIRST, ABS_LOCK_TIME)

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2020-2021 tecnovert
# Copyright (c) 2020-2022 tecnovert
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
@ -19,19 +19,20 @@ python tests/basicswap/extended/test_wallet_init.py
import os
import sys
import json
import time
import shutil
import logging
import unittest
import traceback
import multiprocessing
from urllib.request import urlopen
from unittest.mock import patch
from tests.basicswap.mnemonics import mnemonics
import basicswap.config as cfg
from tests.basicswap.common import (
read_json_api,
)
import bin.basicswap_prepare as prepareSystem
import bin.basicswap_run as runSystem
@ -55,7 +56,7 @@ def waitForServer(port):
for i in range(20):
try:
time.sleep(1)
summary = json.loads(urlopen('http://127.0.0.1:{}/json'.format(port)).read())
summary = read_json_api(port)
break
except Exception as e:
print('waitForServer, error:', str(e))
@ -149,12 +150,12 @@ class Test(unittest.TestCase):
try:
waitForServer(12700)
wallets_0 = json.loads(urlopen('http://127.0.0.1:12700/json/wallets').read())
wallets_0 = read_json_api(12700, 'wallets')
assert(wallets_0['1']['expected_seed'] is True)
assert(wallets_0['6']['expected_seed'] is True)
waitForServer(12701)
wallets_1 = json.loads(urlopen('http://127.0.0.1:12701/json/wallets').read())
wallets_1 = read_json_api(12701, 'wallets')
assert(wallets_0['1']['expected_seed'] is True)
assert(wallets_1['6']['expected_seed'] is True)

View file

@ -29,7 +29,6 @@ import logging
import unittest
import threading
import multiprocessing
from urllib.request import urlopen
from unittest.mock import patch
from basicswap.rpc_xmr import (
@ -40,6 +39,7 @@ from basicswap.rpc import (
)
from tests.basicswap.mnemonics import mnemonics as test_mnemonics
from tests.basicswap.common import (
read_json_api,
waitForServer,
)
from basicswap.contrib.rpcauth import generate_salt, password_to_hmac
@ -268,7 +268,7 @@ class Test(unittest.TestCase):
for i in range(NUM_NODES):
waitForServer(self.delay_event, UI_PORT + i)
wallets = json.loads(urlopen('http://127.0.0.1:{}/json/wallets'.format(UI_PORT + 1)).read())
wallets = read_json_api(UI_PORT + 1, 'wallets')
self.xmr_addr = wallets['6']['main_address']
num_blocks = 100

View file

@ -1,15 +1,13 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2021 tecnovert
# Copyright (c) 2021-2022 tecnovert
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
import json
import random
import logging
import unittest
from urllib.request import urlopen
from basicswap.basicswap import (
Coins,
@ -26,6 +24,7 @@ from basicswap.util import (
)
from tests.basicswap.common import (
wait_for_bid,
read_json_api,
wait_for_offer,
wait_for_none_active,
)
@ -61,14 +60,14 @@ class Test(BaseTest):
logging.info('---------- Test {} to XMR'.format(str(self.test_coin_from)))
swap_clients = self.swap_clients
js_0 = json.loads(urlopen('http://127.0.0.1:1800/json/wallets').read())
js_0 = read_json_api(1800, 'wallets')
node0_from_before = self.getBalance(js_0)
js_1 = json.loads(urlopen('http://127.0.0.1:1801/json/wallets').read())
js_1 = read_json_api(1801, 'wallets')
node1_from_before = self.getBalance(js_1)
js_0_xmr = json.loads(urlopen('http://127.0.0.1:1800/json/wallets/xmr').read())
js_1_xmr = json.loads(urlopen('http://127.0.0.1:1801/json/wallets/xmr').read())
js_0_xmr = read_json_api(1800, 'wallets/xmr')
js_1_xmr = read_json_api(1801, 'wallets/xmr')
amt_swap = make_int(random.uniform(0.1, 2.0), scale=8, r=1)
rate_swap = make_int(random.uniform(0.2, 20.0), scale=12, r=1)
@ -87,17 +86,17 @@ class Test(BaseTest):
wait_for_bid(test_delay_event, swap_clients[1], bid_id, BidStates.SWAP_COMPLETED, sent=True)
amount_from = float(format_amount(amt_swap, 8))
js_1 = json.loads(urlopen('http://127.0.0.1:1801/json/wallets').read())
js_1 = read_json_api(1801, 'wallets')
node1_from_after = self.getBalance(js_1)
assert(node1_from_after > node1_from_before + (amount_from - 0.05))
js_0 = json.loads(urlopen('http://127.0.0.1:1800/json/wallets').read())
js_0 = read_json_api(1800, 'wallets')
node0_from_after = self.getBalance(js_0)
# TODO: Discard block rewards
# assert(node0_from_after < node0_from_before - amount_from)
js_0_xmr_after = json.loads(urlopen('http://127.0.0.1:1800/json/wallets/xmr').read())
js_1_xmr_after = json.loads(urlopen('http://127.0.0.1:1801/json/wallets/xmr').read())
js_0_xmr_after = read_json_api(1800, 'wallets/xmr')
js_1_xmr_after = read_json_api(1801, 'wallets/xmr')
scale_from = 8
amount_to = int((amt_swap * rate_swap) // (10 ** scale_from))
@ -110,7 +109,7 @@ class Test(BaseTest):
logging.info('---------- Test {} to XMR leader recovers coin a lock tx'.format(str(self.test_coin_from)))
swap_clients = self.swap_clients
js_w0_before = json.loads(urlopen('http://127.0.0.1:1800/json/wallets').read())
js_w0_before = read_json_api(1800, 'wallets')
node0_from_before = self.getBalance(js_w0_before)
amt_swap = make_int(random.uniform(0.1, 2.0), scale=8, r=1)
@ -135,7 +134,7 @@ class Test(BaseTest):
wait_for_bid(test_delay_event, swap_clients[0], bid_id, BidStates.XMR_SWAP_FAILED_REFUNDED, wait_for=180)
wait_for_bid(test_delay_event, swap_clients[1], bid_id, BidStates.XMR_SWAP_FAILED_REFUNDED, sent=True)
js_w0_after = json.loads(urlopen('http://127.0.0.1:1800/json/wallets').read())
js_w0_after = read_json_api(1800, 'wallets')
node0_from_after = self.getBalance(js_w0_after)
# TODO: Discard block rewards
@ -145,8 +144,8 @@ class Test(BaseTest):
logging.info('---------- Test {} to XMR follower recovers coin a lock tx'.format(str(self.test_coin_from)))
swap_clients = self.swap_clients
js_w0_before = json.loads(urlopen('http://127.0.0.1:1800/json/wallets').read())
js_w1_before = json.loads(urlopen('http://127.0.0.1:1801/json/wallets').read())
js_w0_before = read_json_api(1800, 'wallets')
js_w1_before = read_json_api(1801, 'wallets')
amt_swap = make_int(random.uniform(0.1, 2.0), scale=8, r=1)
rate_swap = make_int(random.uniform(0.2, 20.0), scale=12, r=1)
@ -171,7 +170,7 @@ class Test(BaseTest):
wait_for_bid(test_delay_event, swap_clients[0], bid_id, BidStates.BID_STALLED_FOR_TEST, wait_for=180)
wait_for_bid(test_delay_event, swap_clients[1], bid_id, BidStates.XMR_SWAP_FAILED_SWIPED, wait_for=80, sent=True)
js_w1_after = json.loads(urlopen('http://127.0.0.1:1801/json/wallets').read())
js_w1_after = read_json_api(1801, 'wallets')
node1_from_before = self.getBalance(js_w1_before)
node1_from_after = self.getBalance(js_w1_after)
@ -187,8 +186,8 @@ class Test(BaseTest):
swap_clients = self.swap_clients
js_w0_before = json.loads(urlopen('http://127.0.0.1:1800/json/wallets').read())
js_w1_before = json.loads(urlopen('http://127.0.0.1:1801/json/wallets').read())
js_w0_before = read_json_api(1800, 'wallets')
js_w1_before = read_json_api(1801, 'wallets')
amt_swap = make_int(random.uniform(0.1, 2.0), scale=8, r=1)
rate_swap = make_int(random.uniform(0.2, 20.0), scale=12, r=1)
@ -211,8 +210,8 @@ class Test(BaseTest):
wait_for_bid(test_delay_event, swap_clients[0], bid_id, BidStates.XMR_SWAP_FAILED_REFUNDED, wait_for=180)
wait_for_bid(test_delay_event, swap_clients[1], bid_id, BidStates.XMR_SWAP_FAILED_REFUNDED, sent=True)
js_w0_after = json.loads(urlopen('http://127.0.0.1:1800/json/wallets').read())
js_w1_after = json.loads(urlopen('http://127.0.0.1:1801/json/wallets').read())
js_w0_after = read_json_api(1800, 'wallets')
js_w1_after = read_json_api(1801, 'wallets')
node0_from_before = self.getBalance(js_w0_before)
node0_from_after = self.getBalance(js_w0_after)

View file

@ -1,15 +1,13 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2021 tecnovert
# Copyright (c) 2021-2022 tecnovert
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
import json
import random
import logging
import unittest
from urllib.request import urlopen
from basicswap.basicswap import (
Coins,
@ -26,6 +24,7 @@ from basicswap.util import (
)
from tests.basicswap.common import (
wait_for_bid,
read_json_api,
wait_for_offer,
wait_for_none_active,
)
@ -82,14 +81,14 @@ class Test(BaseTest):
logging.info('---------- Test {} to XMR'.format(str(self.test_coin_from)))
swap_clients = self.swap_clients
js_0 = json.loads(urlopen('http://127.0.0.1:1800/json/wallets').read())
js_0 = read_json_api(1800, 'wallets')
node0_from_before = self.getBalance(js_0)
js_1 = json.loads(urlopen('http://127.0.0.1:1801/json/wallets').read())
js_1 = read_json_api(1801, 'wallets')
node1_from_before = self.getBalance(js_1)
js_0_xmr = json.loads(urlopen('http://127.0.0.1:1800/json/wallets/xmr').read())
js_1_xmr = json.loads(urlopen('http://127.0.0.1:1801/json/wallets/xmr').read())
js_0_xmr = read_json_api(1800, 'wallets/xmr')
js_1_xmr = read_json_api(1801, 'wallets/xmr')
amt_swap = make_int(random.uniform(0.1, 2.0), scale=8, r=1)
rate_swap = make_int(random.uniform(0.2, 20.0), scale=12, r=1)
@ -108,17 +107,17 @@ class Test(BaseTest):
wait_for_bid(test_delay_event, swap_clients[1], bid_id, BidStates.SWAP_COMPLETED, sent=True)
amount_from = float(format_amount(amt_swap, 8))
js_1 = json.loads(urlopen('http://127.0.0.1:1801/json/wallets').read())
js_1 = read_json_api(1801, 'wallets')
node1_from_after = self.getBalance(js_1)
assert(node1_from_after > node1_from_before + (amount_from - 0.05))
js_0 = json.loads(urlopen('http://127.0.0.1:1800/json/wallets').read())
js_0 = read_json_api(1800, 'wallets')
node0_from_after = self.getBalance(js_0)
# TODO: Discard block rewards
# assert(node0_from_after < node0_from_before - amount_from)
js_0_xmr_after = json.loads(urlopen('http://127.0.0.1:1800/json/wallets/xmr').read())
js_1_xmr_after = json.loads(urlopen('http://127.0.0.1:1801/json/wallets/xmr').read())
js_0_xmr_after = read_json_api(1800, 'wallets/xmr')
js_1_xmr_after = read_json_api(1801, 'wallets/xmr')
scale_from = 8
amount_to = int((amt_swap * rate_swap) // (10 ** scale_from))
@ -131,7 +130,7 @@ class Test(BaseTest):
logging.info('---------- Test {} to XMR leader recovers coin a lock tx'.format(str(self.test_coin_from)))
swap_clients = self.swap_clients
js_w0_before = json.loads(urlopen('http://127.0.0.1:1800/json/wallets').read())
js_w0_before = read_json_api(1800, 'wallets')
node0_from_before = self.getBalance(js_w0_before)
amt_swap = make_int(random.uniform(0.1, 2.0), scale=8, r=1)
@ -156,7 +155,7 @@ class Test(BaseTest):
wait_for_bid(test_delay_event, swap_clients[0], bid_id, BidStates.XMR_SWAP_FAILED_REFUNDED, wait_for=180)
wait_for_bid(test_delay_event, swap_clients[1], bid_id, BidStates.XMR_SWAP_FAILED_REFUNDED, sent=True)
js_w0_after = json.loads(urlopen('http://127.0.0.1:1800/json/wallets').read())
js_w0_after = read_json_api(1800, 'wallets')
node0_from_after = self.getBalance(js_w0_after)
# TODO: Discard block rewards
@ -166,8 +165,8 @@ class Test(BaseTest):
logging.info('---------- Test {} to XMR follower recovers coin a lock tx'.format(str(self.test_coin_from)))
swap_clients = self.swap_clients
js_w0_before = json.loads(urlopen('http://127.0.0.1:1800/json/wallets').read())
js_w1_before = json.loads(urlopen('http://127.0.0.1:1801/json/wallets').read())
js_w0_before = read_json_api(1800, 'wallets')
js_w1_before = read_json_api(1801, 'wallets')
amt_swap = make_int(random.uniform(0.1, 2.0), scale=8, r=1)
rate_swap = make_int(random.uniform(0.2, 20.0), scale=12, r=1)
@ -192,7 +191,7 @@ class Test(BaseTest):
wait_for_bid(test_delay_event, swap_clients[0], bid_id, BidStates.BID_STALLED_FOR_TEST, wait_for=180)
wait_for_bid(test_delay_event, swap_clients[1], bid_id, BidStates.XMR_SWAP_FAILED_SWIPED, wait_for=80, sent=True)
js_w1_after = json.loads(urlopen('http://127.0.0.1:1801/json/wallets').read())
js_w1_after = read_json_api(1801, 'wallets')
node1_from_before = self.getBalance(js_w1_before)
node1_from_after = self.getBalance(js_w1_after)
amount_from = float(format_amount(amt_swap, 8))
@ -207,8 +206,8 @@ class Test(BaseTest):
swap_clients = self.swap_clients
js_w0_before = json.loads(urlopen('http://127.0.0.1:1800/json/wallets').read())
js_w1_before = json.loads(urlopen('http://127.0.0.1:1801/json/wallets').read())
js_w0_before = read_json_api(1800, 'wallets')
js_w1_before = read_json_api(1801, 'wallets')
amt_swap = make_int(random.uniform(0.1, 2.0), scale=8, r=1)
rate_swap = make_int(random.uniform(0.2, 20.0), scale=12, r=1)
@ -231,8 +230,8 @@ class Test(BaseTest):
wait_for_bid(test_delay_event, swap_clients[0], bid_id, BidStates.XMR_SWAP_FAILED_REFUNDED, wait_for=180)
wait_for_bid(test_delay_event, swap_clients[1], bid_id, BidStates.XMR_SWAP_FAILED_REFUNDED, sent=True)
js_w0_after = json.loads(urlopen('http://127.0.0.1:1800/json/wallets').read())
js_w1_after = json.loads(urlopen('http://127.0.0.1:1801/json/wallets').read())
js_w0_after = read_json_api(1800, 'wallets')
js_w1_after = read_json_api(1801, 'wallets')
node0_from_before = self.getBalance(js_w0_before)
node0_from_after = self.getBalance(js_w0_after)

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2021 tecnovert
# Copyright (c) 2021-2022 tecnovert
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
@ -27,6 +27,7 @@ from basicswap.util import (
)
from tests.basicswap.common import (
wait_for_bid,
read_json_api,
wait_for_offer,
wait_for_none_active,
wait_for_balance,
@ -45,7 +46,7 @@ class Test(BaseTest):
def setUpClass(cls):
super(Test, cls).setUpClass()
js_0 = json.loads(urlopen('http://127.0.0.1:1800/json/wallets/part').read())
js_0 = read_json_api(1800, 'wallets/part')
node0_blind_before = js_0['blind_balance'] + js_0['blind_unconfirmed']
post_json = {
@ -59,7 +60,7 @@ class Test(BaseTest):
logging.info('Waiting for blind balance')
wait_for_balance(test_delay_event, 'http://127.0.0.1:1800/json/wallets/part', 'blind_balance', 100.0 + node0_blind_before)
js_0 = json.loads(urlopen('http://127.0.0.1:1800/json/wallets/part').read())
js_0 = read_json_api(1800, 'wallets/part')
node0_blind_before = js_0['blind_balance'] + js_0['blind_unconfirmed']
def getBalance(self, js_wallets):
@ -72,15 +73,15 @@ class Test(BaseTest):
logging.info('---------- Test PARTct to XMR')
swap_clients = self.swap_clients
js_0 = json.loads(urlopen('http://127.0.0.1:1800/json/wallets/part').read())
js_0 = read_json_api(1800, 'wallets/part')
assert(float(js_0['blind_balance']) > 10.0)
node0_blind_before = js_0['blind_balance'] + js_0['blind_unconfirmed']
js_1 = json.loads(urlopen('http://127.0.0.1:1801/json/wallets/part').read())
js_1 = read_json_api(1801, 'wallets/part')
node1_blind_before = js_1['blind_balance'] + js_1['blind_unconfirmed']
js_0_xmr = json.loads(urlopen('http://127.0.0.1:1800/json/wallets/xmr').read())
js_1_xmr = json.loads(urlopen('http://127.0.0.1:1801/json/wallets/xmr').read())
js_0_xmr = read_json_api(1800, 'wallets/xmr')
js_1_xmr = read_json_api(1801, 'wallets/xmr')
amt_swap = make_int(random.uniform(0.1, 2.0), scale=8, r=1)
rate_swap = make_int(random.uniform(0.2, 20.0), scale=12, r=1)
@ -99,16 +100,16 @@ class Test(BaseTest):
wait_for_bid(test_delay_event, swap_clients[1], bid_id, BidStates.SWAP_COMPLETED, sent=True)
amount_from = float(format_amount(amt_swap, 8))
js_1 = json.loads(urlopen('http://127.0.0.1:1801/json/wallets/part').read())
js_1 = read_json_api(1801, 'wallets/part')
node1_blind_after = js_1['blind_balance'] + js_1['blind_unconfirmed']
assert(node1_blind_after > node1_blind_before + (amount_from - 0.05))
js_0 = json.loads(urlopen('http://127.0.0.1:1800/json/wallets/part').read())
js_0 = read_json_api(1800, 'wallets/part')
node0_blind_after = js_0['blind_balance'] + js_0['blind_unconfirmed']
assert(node0_blind_after < node0_blind_before - amount_from)
js_0_xmr_after = json.loads(urlopen('http://127.0.0.1:1800/json/wallets/xmr').read())
js_1_xmr_after = json.loads(urlopen('http://127.0.0.1:1801/json/wallets/xmr').read())
js_0_xmr_after = read_json_api(1800, 'wallets/xmr')
js_1_xmr_after = read_json_api(1801, 'wallets/xmr')
scale_from = 8
amount_to = int((amt_swap * rate_swap) // (10 ** scale_from))
@ -121,7 +122,7 @@ class Test(BaseTest):
logging.info('---------- Test PARTct to XMR leader recovers coin a lock tx')
swap_clients = self.swap_clients
js_w0_before = json.loads(urlopen('http://127.0.0.1:1800/json/wallets').read())
js_w0_before = read_json_api(1800, 'wallets')
node0_blind_before = self.getBalance(js_w0_before)
amt_swap = make_int(random.uniform(0.1, 2.0), scale=8, r=1)
@ -146,7 +147,7 @@ class Test(BaseTest):
wait_for_bid(test_delay_event, swap_clients[0], bid_id, BidStates.XMR_SWAP_FAILED_REFUNDED, wait_for=180)
wait_for_bid(test_delay_event, swap_clients[1], bid_id, BidStates.XMR_SWAP_FAILED_REFUNDED, sent=True)
js_w0_after = json.loads(urlopen('http://127.0.0.1:1800/json/wallets').read())
js_w0_after = read_json_api(1800, 'wallets')
node0_blind_after = self.getBalance(js_w0_after)
assert(node0_blind_before - node0_blind_after < 0.02)
@ -154,8 +155,8 @@ class Test(BaseTest):
logging.info('---------- Test PARTct to XMR follower recovers coin a lock tx')
swap_clients = self.swap_clients
js_w0_before = json.loads(urlopen('http://127.0.0.1:1800/json/wallets').read())
js_w1_before = json.loads(urlopen('http://127.0.0.1:1801/json/wallets').read())
js_w0_before = read_json_api(1800, 'wallets')
js_w1_before = read_json_api(1801, 'wallets')
amt_swap = make_int(random.uniform(0.1, 2.0), scale=8, r=1)
rate_swap = make_int(random.uniform(0.2, 20.0), scale=12, r=1)
@ -180,7 +181,7 @@ class Test(BaseTest):
wait_for_bid(test_delay_event, swap_clients[0], bid_id, BidStates.BID_STALLED_FOR_TEST, wait_for=180)
wait_for_bid(test_delay_event, swap_clients[1], bid_id, BidStates.XMR_SWAP_FAILED_SWIPED, wait_for=80, sent=True)
js_w1_after = json.loads(urlopen('http://127.0.0.1:1801/json/wallets').read())
js_w1_after = read_json_api(1801, 'wallets')
node1_blind_before = self.getBalance(js_w1_before)
node1_blind_after = self.getBalance(js_w1_after)
@ -207,8 +208,8 @@ class Test(BaseTest):
swap_clients = self.swap_clients
js_w0_before = json.loads(urlopen('http://127.0.0.1:1800/json/wallets').read())
js_w1_before = json.loads(urlopen('http://127.0.0.1:1801/json/wallets').read())
js_w0_before = read_json_api(1800, 'wallets')
js_w1_before = read_json_api(1801, 'wallets')
amt_swap = make_int(random.uniform(0.1, 2.0), scale=8, r=1)
rate_swap = make_int(random.uniform(0.2, 20.0), scale=12, r=1)
@ -232,8 +233,8 @@ class Test(BaseTest):
wait_for_bid(test_delay_event, swap_clients[0], bid_id, BidStates.XMR_SWAP_FAILED_REFUNDED, wait_for=180)
wait_for_bid(test_delay_event, swap_clients[1], bid_id, BidStates.XMR_SWAP_FAILED_REFUNDED, sent=True)
js_w0_after = json.loads(urlopen('http://127.0.0.1:1800/json/wallets').read())
js_w1_after = json.loads(urlopen('http://127.0.0.1:1801/json/wallets').read())
js_w0_after = read_json_api(1800, 'wallets')
js_w1_after = read_json_api(1801, 'wallets')
node0_blind_before = self.getBalance(js_w0_before)
node0_blind_after = self.getBalance(js_w0_after)

View file

@ -33,6 +33,7 @@ from basicswap.rpc import (
)
from tests.basicswap.mnemonics import mnemonics
from tests.basicswap.common import (
read_json_api,
waitForServer,
waitForNumOffers,
waitForNumBids,
@ -167,7 +168,7 @@ class Test(unittest.TestCase):
'lockhrs': '24'}).encode()
offer_id = json.loads(urlopen('http://127.0.0.1:12700/json/offers/new', data=data).read())
summary = json.loads(urlopen('http://127.0.0.1:12700/json').read())
summary = read_json_api(12700)
assert(summary['num_sent_offers'] == 1)
except Exception:
traceback.print_exc()
@ -175,7 +176,7 @@ class Test(unittest.TestCase):
logger.info('Waiting for offer:')
waitForNumOffers(delay_event, 12701, 1)
offers = json.loads(urlopen('http://127.0.0.1:12701/json/offers').read())
offers = read_json_api(12701, 'offers')
offer = offers[0]
data = parse.urlencode({
@ -186,7 +187,7 @@ class Test(unittest.TestCase):
waitForNumBids(delay_event, 12700, 1)
bids = json.loads(urlopen('http://127.0.0.1:12700/json/bids').read())
bids = read_json_api(12700, 'bids')
bid = bids[0]
data = parse.urlencode({
@ -205,7 +206,7 @@ class Test(unittest.TestCase):
processes[1].start()
waitForServer(delay_event, 12701)
rv = json.loads(urlopen('http://127.0.0.1:12701/json').read())
rv = read_json_api(12701)
assert(rv['num_swapping'] == 1)
update_thread = threading.Thread(target=updateThread)
@ -215,7 +216,7 @@ class Test(unittest.TestCase):
for i in range(240):
delay_event.wait(5)
rv = json.loads(urlopen('http://127.0.0.1:12700/json/bids/{}'.format(bid['bid_id'])).read())
rv = read_json_api(12700, 'bids/{}'.format(bid['bid_id']))
print(rv)
if rv['bid_state'] == 'Completed':
break

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2021 tecnovert
# Copyright (c) 2021-2022 tecnovert
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
@ -25,6 +25,7 @@ from urllib import parse
from urllib.request import urlopen
from tests.basicswap.common import (
read_json_api,
waitForServer,
waitForNumOffers,
waitForNumBids,
@ -49,7 +50,7 @@ class Test(XmrTestBase):
waitForServer(self.delay_event, 12700)
waitForServer(self.delay_event, 12701)
wallets1 = json.loads(urlopen('http://127.0.0.1:12701/json/wallets').read())
wallets1 = read_json_api(12701, 'wallets')
assert(float(wallets1['6']['balance']) > 0.0)
offer_data = {
@ -67,7 +68,7 @@ class Test(XmrTestBase):
rv = json.loads(urlopen('http://127.0.0.1:12700/json/offers/new', data=parse.urlencode(offer_data).encode()).read())
offer1_id = rv['offer_id']
summary = json.loads(urlopen('http://127.0.0.1:12700/json').read())
summary = read_json_api(12700)
assert(summary['num_sent_offers'] > 1)
logger.info('Waiting for offer')
@ -141,8 +142,8 @@ class Test(XmrTestBase):
raise ValueError('Test stopped.')
self.delay_event.wait(4)
rv0 = json.loads(urlopen('http://127.0.0.1:12700/json/bids/{}'.format(bid0_id)).read())
rv1 = json.loads(urlopen('http://127.0.0.1:12700/json/bids/{}'.format(bid1_id)).read())
rv0 = read_json_api(12700, 'bids/{}'.format(bid0_id))
rv1 = read_json_api(12700, 'bids/{}'.format(bid1_id))
if rv0['bid_state'] == 'Completed' and rv1['bid_state'] == 'Completed':
break
assert(rv0['bid_state'] == 'Completed')

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2020-2021 tecnovert
# Copyright (c) 2020-2022 tecnovert
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
@ -25,6 +25,7 @@ from urllib import parse
from urllib.request import urlopen
from tests.basicswap.common import (
read_json_api,
waitForServer,
waitForNumOffers,
waitForNumBids,
@ -47,7 +48,7 @@ class Test(XmrTestBase):
waitForServer(self.delay_event, 12700)
waitForServer(self.delay_event, 12701)
wallets1 = json.loads(urlopen('http://127.0.0.1:12701/json/wallets').read())
wallets1 = read_json_api(12701, 'wallets')
assert(float(wallets1['6']['balance']) > 0.0)
data = parse.urlencode({
@ -59,13 +60,13 @@ class Test(XmrTestBase):
'lockhrs': '24'}).encode()
offer_id = json.loads(urlopen('http://127.0.0.1:12700/json/offers/new', data=data).read())['offer_id']
summary = json.loads(urlopen('http://127.0.0.1:12700/json').read())
summary = read_json_api(12700)
assert(summary['num_sent_offers'] == 1)
logger.info('Waiting for offer')
waitForNumOffers(self.delay_event, 12701, 1)
offers = json.loads(urlopen('http://127.0.0.1:12701/json/offers').read())
offers = read_json_api(12701, 'offers')
offer = offers[0]
data = {
@ -95,7 +96,7 @@ class Test(XmrTestBase):
waitForNumBids(self.delay_event, 12700, 1)
for i in range(10):
bids = json.loads(urlopen('http://127.0.0.1:12700/json/bids').read())
bids = read_json_api(12700, 'bids')
bid = bids[0]
if bid['bid_state'] == 'Received':
break
@ -118,7 +119,7 @@ class Test(XmrTestBase):
self.processes[1].start()
waitForServer(self.delay_event, 12701)
rv = json.loads(urlopen('http://127.0.0.1:12701/json').read())
rv = read_json_api(12701)
assert(rv['num_swapping'] == 1)
rv = json.loads(urlopen('http://127.0.0.1:12700/json/revokeoffer/{}'.format(offer_id)).read())
@ -136,7 +137,7 @@ class Test(XmrTestBase):
assert(rv['bid_state'] == 'Completed')
# Ensure offer was revoked
summary = json.loads(urlopen('http://127.0.0.1:12700/json').read())
summary = read_json_api(12700)
assert(summary['num_network_offers'] == 0)
# Wait for bid to be removed from in-progress