mirror of
https://github.com/basicswap/basicswap.git
synced 2025-11-05 10:28:10 +01:00
Replace makeInt with make_int
This commit is contained in:
@@ -4,6 +4,7 @@ import tests.basicswap.test_other as test_other
|
||||
import tests.basicswap.test_prepare as test_prepare
|
||||
import tests.basicswap.test_run as test_run
|
||||
import tests.basicswap.test_reload as test_reload
|
||||
import tests.basicswap.test_xmr as test_xmr
|
||||
|
||||
|
||||
def test_suite():
|
||||
@@ -12,5 +13,6 @@ def test_suite():
|
||||
suite.addTests(loader.loadTestsFromModule(test_prepare))
|
||||
suite.addTests(loader.loadTestsFromModule(test_run))
|
||||
suite.addTests(loader.loadTestsFromModule(test_reload))
|
||||
suite.addTests(loader.loadTestsFromModule(test_xmr))
|
||||
|
||||
return suite
|
||||
|
||||
14
tests/basicswap/common.py
Normal file
14
tests/basicswap/common.py
Normal file
@@ -0,0 +1,14 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright (c) 2020 tecnovert
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
# file LICENSE.txt or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
def checkForks(ro):
|
||||
if 'bip9_softforks' in ro:
|
||||
assert(ro['bip9_softforks']['csv']['status'] == 'active')
|
||||
assert(ro['bip9_softforks']['segwit']['status'] == 'active')
|
||||
else:
|
||||
assert(ro['softforks']['csv']['active'])
|
||||
assert(ro['softforks']['segwit']['active'])
|
||||
@@ -9,7 +9,7 @@ import unittest
|
||||
from basicswap.util import (
|
||||
SerialiseNum,
|
||||
DeserialiseNum,
|
||||
makeInt,
|
||||
make_int,
|
||||
format8,
|
||||
)
|
||||
from basicswap.basicswap import (
|
||||
@@ -57,19 +57,28 @@ class Test(unittest.TestCase):
|
||||
decoded = decodeSequence(encoded)
|
||||
assert(decoded == blocks_val)
|
||||
|
||||
def test_makeInt(self):
|
||||
def test_make_int(self):
|
||||
def test_case(vs, vf, expect_int):
|
||||
assert(makeInt(vs) == expect_int)
|
||||
assert(makeInt(vf) == expect_int)
|
||||
vs_out = format8(makeInt(vs))
|
||||
i = make_int(vs)
|
||||
assert(i == expect_int and isinstance(i, int))
|
||||
i = make_int(vf)
|
||||
assert(i == expect_int and isinstance(i, int))
|
||||
vs_out = format_amount(i, 8)
|
||||
# Strip
|
||||
for i in range(7):
|
||||
if vs_out[-1] == '0':
|
||||
vs_out = vs_out[:-1]
|
||||
assert(vs_out == vs)
|
||||
if '.' in vs:
|
||||
assert(vs_out == vs)
|
||||
else:
|
||||
assert(vs_out[:-2] == vs)
|
||||
test_case('0', 0, 0)
|
||||
test_case('1', 1, 100000000)
|
||||
test_case('10', 10, 1000000000)
|
||||
test_case('0.00899999', 0.00899999, 899999)
|
||||
test_case('899999.0', 899999.0, 89999900000000)
|
||||
test_case('899999.00899999', 899999.00899999, 89999900899999)
|
||||
test_case('0.0', 0.0, 0)
|
||||
test_case('1.0', 1.0, 100000000)
|
||||
test_case('1.1', 1.1, 110000000)
|
||||
test_case('1.2', 1.2, 120000000)
|
||||
@@ -79,6 +88,52 @@ class Test(unittest.TestCase):
|
||||
test_case('0.123', 0.123, 12300000)
|
||||
test_case('123000.000123', 123000.000123, 12300000012300)
|
||||
|
||||
try:
|
||||
make_int('0.123456789')
|
||||
assert(False)
|
||||
except Exception as e:
|
||||
assert(str(e) == 'Mantissa too long')
|
||||
validate_amount('0.12345678')
|
||||
|
||||
# floor
|
||||
assert(make_int('0.123456789', r=-1) == 12345678)
|
||||
# Round up
|
||||
assert(make_int('0.123456789', r=1) == 12345679)
|
||||
|
||||
def test_make_int12(self):
|
||||
def test_case(vs, vf, expect_int):
|
||||
i = make_int(vs, 12)
|
||||
assert(i == expect_int and isinstance(i, int))
|
||||
i = make_int(vf, 12)
|
||||
assert(i == expect_int and isinstance(i, int))
|
||||
vs_out = format_amount(i, 12)
|
||||
# Strip
|
||||
for i in range(7):
|
||||
if vs_out[-1] == '0':
|
||||
vs_out = vs_out[:-1]
|
||||
if '.' in vs:
|
||||
assert(vs_out == vs)
|
||||
else:
|
||||
assert(vs_out[:-2] == vs)
|
||||
test_case('0.123456789', 0.123456789, 123456789000)
|
||||
test_case('0.123456789123', 0.123456789123, 123456789123)
|
||||
try:
|
||||
make_int('0.1234567891234', 12)
|
||||
assert(False)
|
||||
except Exception as e:
|
||||
assert(str(e) == 'Mantissa too long')
|
||||
validate_amount('0.123456789123', 12)
|
||||
try:
|
||||
validate_amount('0.1234567891234', 12)
|
||||
assert(False)
|
||||
except Exception as e:
|
||||
assert('Too many decimal places' in str(e))
|
||||
try:
|
||||
validate_amount(0.1234567891234, 12)
|
||||
assert(False)
|
||||
except Exception as e:
|
||||
assert('Too many decimal places' in str(e))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
@@ -48,6 +48,9 @@ from basicswap.contrib.key import (
|
||||
from basicswap.http_server import (
|
||||
HttpThread,
|
||||
)
|
||||
from tests.basicswap.common import (
|
||||
checkForks,
|
||||
)
|
||||
from bin.basicswap_run import startDaemon
|
||||
|
||||
logger = logging.getLogger()
|
||||
@@ -205,15 +208,6 @@ def run_loop(self):
|
||||
btcRpc('generatetoaddress 1 {}'.format(self.btc_addr))
|
||||
|
||||
|
||||
def checkForks(ro):
|
||||
if 'bip9_softforks' in ro:
|
||||
assert(ro['bip9_softforks']['csv']['status'] == 'active')
|
||||
assert(ro['bip9_softforks']['segwit']['status'] == 'active')
|
||||
else:
|
||||
assert(ro['softforks']['csv']['active'])
|
||||
assert(ro['softforks']['segwit']['active'])
|
||||
|
||||
|
||||
class Test(unittest.TestCase):
|
||||
|
||||
@classmethod
|
||||
|
||||
246
tests/basicswap/test_xmr.py
Normal file
246
tests/basicswap/test_xmr.py
Normal file
@@ -0,0 +1,246 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright (c) 2020 tecnovert
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
import json
|
||||
import logging
|
||||
import shutil
|
||||
import time
|
||||
import signal
|
||||
import threading
|
||||
from urllib.request import urlopen
|
||||
from coincurve.ecdsaotves import (
|
||||
ecdsaotves_enc_sign,
|
||||
ecdsaotves_enc_verify,
|
||||
ecdsaotves_dec_sig,
|
||||
ecdsaotves_rec_enc_key)
|
||||
from coincurve.dleag import (
|
||||
dleag_prove,
|
||||
dleag_verify)
|
||||
|
||||
import basicswap.config as cfg
|
||||
from basicswap.basicswap import (
|
||||
BasicSwap,
|
||||
Coins,
|
||||
SwapTypes,
|
||||
BidStates,
|
||||
TxStates,
|
||||
SEQUENCE_LOCK_BLOCKS,
|
||||
)
|
||||
from basicswap.util import (
|
||||
COIN,
|
||||
toWIF,
|
||||
dumpje,
|
||||
)
|
||||
from basicswap.rpc import (
|
||||
callrpc_cli,
|
||||
waitForRPC,
|
||||
)
|
||||
from basicswap.contrib.key import (
|
||||
ECKey,
|
||||
)
|
||||
from basicswap.http_server import (
|
||||
HttpThread,
|
||||
)
|
||||
from bin.basicswap_run import startDaemon
|
||||
|
||||
logger = logging.getLogger()
|
||||
logger.level = logging.DEBUG
|
||||
if not len(logger.handlers):
|
||||
logger.addHandler(logging.StreamHandler(sys.stdout))
|
||||
|
||||
NUM_NODES = 3
|
||||
BASE_PORT = 14792
|
||||
BASE_RPC_PORT = 19792
|
||||
BASE_ZMQ_PORT = 20792
|
||||
PREFIX_SECRET_KEY_REGTEST = 0x2e
|
||||
TEST_HTML_PORT = 1800
|
||||
stop_test = False
|
||||
|
||||
|
||||
|
||||
def prepareOtherDir(datadir, nodeId, conf_file='litecoin.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.0002\n')
|
||||
|
||||
fp.write('acceptnonstdtxn=0\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('acceptnonstdtxn=0\n')
|
||||
fp.write('minstakeinterval=5\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)
|
||||
|
||||
ltcdatadir = os.path.join(datadir, str(LTC_NODE))
|
||||
btcdatadir = os.path.join(datadir, str(BTC_NODE))
|
||||
settings_path = os.path.join(basicswap_dir, cfg.CONFIG_FILENAME)
|
||||
settings = {
|
||||
'zmqhost': 'tcp://127.0.0.1',
|
||||
'zmqport': BASE_ZMQ_PORT + nodeId,
|
||||
'htmlhost': 'localhost',
|
||||
'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
|
||||
},
|
||||
'litecoin': {
|
||||
'connection_type': 'rpc',
|
||||
'manage_daemon': False,
|
||||
'rpcport': BASE_RPC_PORT + LTC_NODE,
|
||||
'datadir': ltcdatadir,
|
||||
'bindir': cfg.LITECOIN_BINDIR,
|
||||
# 'use_segwit': True,
|
||||
},
|
||||
'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,
|
||||
'check_events_seconds': 1,
|
||||
'min_delay_auto_accept': 1,
|
||||
'max_delay_auto_accept': 5
|
||||
}
|
||||
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 signal_handler(sig, frame):
|
||||
global stop_test
|
||||
print('signal {} detected.'.format(sig))
|
||||
stop_test = True
|
||||
|
||||
|
||||
def run_loop(self):
|
||||
while not stop_test:
|
||||
time.sleep(1)
|
||||
for c in self.swap_clients:
|
||||
c.update()
|
||||
btcRpc('generatetoaddress 1 {}'.format(self.btc_addr))
|
||||
|
||||
|
||||
def checkForks(ro):
|
||||
if 'bip9_softforks' in ro:
|
||||
assert(ro['bip9_softforks']['csv']['status'] == 'active')
|
||||
assert(ro['bip9_softforks']['segwit']['status'] == 'active')
|
||||
else:
|
||||
assert(ro['softforks']['csv']['active'])
|
||||
assert(ro['softforks']['segwit']['active'])
|
||||
|
||||
|
||||
class Test(unittest.TestCase):
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super(Test, cls).setUpClass()
|
||||
|
||||
cls.swap_clients = []
|
||||
cls.xmr_daemons = []
|
||||
cls.xmr_wallet_auth = []
|
||||
|
||||
cls.part_stakelimit = 0
|
||||
cls.xmr_addr = None
|
||||
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
cls.update_thread = threading.Thread(target=run_loop, args=(cls,))
|
||||
cls.update_thread.start()
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
global stop_test
|
||||
logging.info('Finalising')
|
||||
stop_test = True
|
||||
cls.update_thread.join()
|
||||
|
||||
super(Test, cls).tearDownClass()
|
||||
|
||||
def test_01_part_xmr(self):
|
||||
logging.info('---------- Test PART to XMR')
|
||||
#swap_clients = self.swap_clients
|
||||
|
||||
#offer_id = swap_clients[0].postOffer(Coins.PART, Coins.XMR, 100 * COIN, 0.5 * COIN, 100 * COIN, SwapTypes.SELLER_FIRST)
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user