tests: Move BCH code to BCH test file.

This commit is contained in:
tecnovert 2024-10-31 13:47:03 +02:00
parent c0b94b3d7b
commit 8e00753f97
No known key found for this signature in database
GPG key ID: 8ED6D8750C4E3F93
6 changed files with 126 additions and 136 deletions

View file

@ -55,10 +55,10 @@ def signal_handler(sig, frame):
def startDaemon(node_dir, bin_dir, daemon_bin, opts=[], extra_config={}):
daemon_bin = os.path.expanduser(os.path.join(bin_dir, daemon_bin))
datadir_path = os.path.expanduser(node_dir)
# Rewrite litecoin.conf for 0.21.3
# TODO: Remove
ltc_conf_path = os.path.join(datadir_path, 'litecoin.conf')
if os.path.exists(ltc_conf_path):
config_to_add = ['blockfilterindex=0', 'peerblockfilters=0']

View file

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2019-2023 tecnovert
# Copyright (c) 2019-2024 The Basicswap developers
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
@ -37,7 +37,4 @@ XMR_BINDIR = os.path.expanduser(os.getenv('XMR_BINDIR', os.path.join(DEFAULT_TES
XMRD = os.getenv('XMRD', 'monerod' + bin_suffix)
XMR_WALLET_RPC = os.getenv('XMR_WALLET_RPC', 'monero-wallet-rpc' + bin_suffix)
BITCOINCASH_BINDIR = os.path.expanduser(os.getenv('BITCOINCASH_BINDIR', os.path.join(DEFAULT_TEST_BINDIR, 'bitcoincash')))
BITCOINCASHD = os.getenv('BITCOINCASHD', 'bitcoind' + bin_suffix)
BITCOINCASH_CLI = os.getenv('BITCOINCASH_CLI', 'bitcoin-cli' + bin_suffix)
BITCOINCASH_TX = os.getenv('BITCOINCASH_TX', 'bitcoin-tx' + bin_suffix)
# NOTE: Adding coin definitions here is deprecated. Please add in coin test file.

View file

@ -31,19 +31,10 @@ BTC_BASE_RPC_PORT = 32792
BTC_BASE_ZMQ_PORT = 33792
BTC_BASE_TOR_PORT = 33732
BCH_BASE_PORT = 41792
BCH_BASE_RPC_PORT = 42792
BCH_BASE_ZMQ_PORT = 43792
BCH_BASE_TOR_PORT = 43732
LTC_BASE_PORT = 34792
LTC_BASE_RPC_PORT = 35792
LTC_BASE_ZMQ_PORT = 36792
DCR_BASE_PORT = 18555
DCR_BASE_RPC_PORT = 9110
PIVX_BASE_PORT = 34892
PIVX_BASE_RPC_PORT = 35892
PIVX_BASE_ZMQ_PORT = 36892
@ -51,7 +42,7 @@ PIVX_BASE_ZMQ_PORT = 36892
PREFIX_SECRET_KEY_REGTEST = 0x2e
def prepareDataDir(datadir, node_id, conf_file, dir_prefix, base_p2p_port=BASE_PORT, base_rpc_port=BASE_RPC_PORT, num_nodes=3):
def prepareDataDir(datadir, node_id, conf_file, dir_prefix, base_p2p_port=BASE_PORT, base_rpc_port=BASE_RPC_PORT, num_nodes=3, extra_opts=[]):
node_dir = os.path.join(datadir, dir_prefix + str(node_id))
if not os.path.exists(node_dir):
os.makedirs(node_dir)
@ -80,9 +71,12 @@ def prepareDataDir(datadir, node_id, conf_file, dir_prefix, base_p2p_port=BASE_P
fp.write('acceptnonstdtxn=0\n')
fp.write('txindex=1\n')
fp.write('wallet=wallet.dat\n')
if not base_p2p_port == BCH_BASE_PORT:
fp.write('findpeers=0\n')
for opt in extra_opts:
fp.write(opt + '\n')
if base_p2p_port == BTC_BASE_PORT:
fp.write('deprecatedrpc=create_bdb\n')
elif base_p2p_port == BASE_PORT: # Particl

View file

@ -29,9 +29,12 @@ from tests.basicswap.common import (
BASE_PORT, BASE_RPC_PORT,
BTC_BASE_PORT, BTC_BASE_RPC_PORT, BTC_BASE_TOR_PORT,
LTC_BASE_PORT, LTC_BASE_RPC_PORT,
DCR_BASE_PORT, DCR_BASE_RPC_PORT,
PIVX_BASE_PORT,
)
from tests.basicswap.extended.test_dcr import (
DCR_BASE_PORT, DCR_BASE_RPC_PORT,
)
from basicswap.contrib.rpcauth import generate_salt, password_to_hmac
import basicswap.config as cfg

View file

@ -5,25 +5,25 @@
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
import logging
import os
import random
import unittest
from basicswap.chainparams import XMR_COIN
import basicswap.config as cfg
from basicswap.basicswap import (
BidStates,
Coins,
SwapTypes,
)
from basicswap.util import (
COIN,
)
from basicswap.bin.run import startDaemon
from basicswap.util.crypto import sha256
from tests.basicswap.test_btc_xmr import BasicSwapTest
from tests.basicswap.common import (
BCH_BASE_RPC_PORT,
wait_for_bid,
wait_for_offer,
make_rpc_func,
prepareDataDir,
stopDaemons,
waitForRPC,
)
from basicswap.contrib.test_framework.messages import (
ToHex,
@ -38,8 +38,11 @@ from basicswap.contrib.test_framework.script import (
OP_CHECKSEQUENCEVERIFY,
)
from basicswap.interface.bch import BCHInterface
from basicswap.rpc import (
callrpc_cli,
)
from basicswap.util import ensure
from .test_xmr import BaseTest, test_delay_event, callnoderpc
from .test_xmr import test_delay_event, callnoderpc
from coincurve.ecdsaotves import (
ecdsaotves_enc_sign,
@ -47,6 +50,16 @@ from coincurve.ecdsaotves import (
ecdsaotves_dec_sig
)
BITCOINCASH_BINDIR = os.path.expanduser(os.getenv('BITCOINCASH_BINDIR', os.path.join(cfg.DEFAULT_TEST_BINDIR, 'bitcoincash')))
BITCOINCASHD = os.getenv('BITCOINCASHD', 'bitcoind' + cfg.bin_suffix)
BITCOINCASH_CLI = os.getenv('BITCOINCASH_CLI', 'bitcoin-cli' + cfg.bin_suffix)
BITCOINCASH_TX = os.getenv('BITCOINCASH_TX', 'bitcoin-tx' + cfg.bin_suffix)
BCH_BASE_PORT = 41792
BCH_BASE_RPC_PORT = 42792
BCH_BASE_ZMQ_PORT = 43792
BCH_BASE_TOR_PORT = 43732
logger = logging.getLogger()
@ -74,50 +87,85 @@ class TestXmrBchSwapInterface(unittest.TestCase):
ensure(signature is None, 'signature present')
class TestFunctions(BaseTest):
__test__ = False
start_bch_nodes = True
base_rpc_port = BCH_BASE_RPC_PORT
extra_wait_time = 0
test_coin_from = Coins.BCH
def callnoderpc(self, method, params=[], wallet=None, node_id=0):
return callnoderpc(node_id, method, params, wallet, self.base_rpc_port)
def mineBlock(self, num_blocks=1):
self.callnoderpc('generatetoaddress', [num_blocks, self.bch_addr])
def test_05_bch_xmr(self):
logging.info('---------- Test BCH to XMR')
swap_clients = self.swap_clients
offer_id = swap_clients[0].postOffer(Coins.BCH, Coins.XMR, 10 * COIN, 100 * XMR_COIN, 10 * COIN, SwapTypes.XMR_SWAP)
wait_for_offer(test_delay_event, swap_clients[1], offer_id)
offers = swap_clients[1].listOffers(filters={'offer_id': offer_id})
offer = offers[0]
swap_clients[1].ci(Coins.XMR).setFeePriority(3)
bid_id = swap_clients[1].postXmrBid(offer_id, offer.amount_from)
wait_for_bid(test_delay_event, swap_clients[0], bid_id, BidStates.BID_RECEIVED)
bid, xmr_swap = swap_clients[0].getXmrBid(bid_id)
assert (xmr_swap)
swap_clients[0].acceptXmrBid(bid_id)
wait_for_bid(test_delay_event, swap_clients[0], bid_id, BidStates.SWAP_COMPLETED, wait_for=180)
wait_for_bid(test_delay_event, swap_clients[1], bid_id, BidStates.SWAP_COMPLETED, sent=True)
swap_clients[1].ci(Coins.XMR).setFeePriority(0)
class TestBCH(BasicSwapTest):
__test__ = True
test_coin = Coins.BCH
test_coin_from = Coins.BCH
start_bch_nodes = True
base_rpc_port = BCH_BASE_RPC_PORT
bch_daemons = []
start_ltc_nodes = False
bch_addr = None
@classmethod
def prepareExtraDataDir(cls, i):
if not cls.restore_instance:
data_dir = prepareDataDir(cfg.TEST_DATADIRS, i, 'bitcoin.conf', 'bch_', base_p2p_port=BCH_BASE_PORT, base_rpc_port=BCH_BASE_RPC_PORT)
# Rewrite conf file
config_filename: str = os.path.join(cfg.TEST_DATADIRS, 'bch_' + str(i), 'bitcoin.conf')
with open(config_filename, 'r') as fp:
lines = fp.readlines()
with open(config_filename, 'w') as fp:
for line in lines:
if not line.startswith('findpeers'):
fp.write(line)
if os.path.exists(os.path.join(BITCOINCASH_BINDIR, 'bitcoin-wallet')):
try:
callrpc_cli(BITCOINCASH_BINDIR, data_dir, 'regtest', '-wallet=wallet.dat create', 'bitcoin-wallet')
except Exception as e:
logging.warning('bch: bitcoin-wallet create failed')
raise e
cls.bch_daemons.append(startDaemon(os.path.join(cfg.TEST_DATADIRS, 'bch_' + str(i)), BITCOINCASH_BINDIR, BITCOINCASHD))
logging.info('BCH: Started %s %d', BITCOINCASHD, cls.bch_daemons[-1].handle.pid)
waitForRPC(make_rpc_func(i, base_rpc_port=BCH_BASE_RPC_PORT), test_delay_event)
@classmethod
def addPIDInfo(cls, sc, i):
sc.setDaemonPID(Coins.BCH, cls.bch_daemons[i].handle.pid)
@classmethod
def prepareExtraCoins(cls):
cls.bch_addr = callnoderpc(0, 'getnewaddress', ['mining_addr'], base_rpc_port=BCH_BASE_RPC_PORT, wallet='wallet.dat')
if not cls.restore_instance:
num_blocks: int = 200
logging.info('Mining %d BitcoinCash blocks to %s', num_blocks, cls.bch_addr)
callnoderpc(0, 'generatetoaddress', [num_blocks, cls.bch_addr], base_rpc_port=BCH_BASE_RPC_PORT, wallet='wallet.dat')
@classmethod
def addCoinSettings(cls, settings, datadir, node_id):
settings['chainclients']['bitcoincash'] = {
'connection_type': 'rpc',
'manage_daemon': False,
'rpcport': BCH_BASE_RPC_PORT + node_id,
'rpcuser': 'test' + str(node_id),
'rpcpassword': 'test_pass' + str(node_id),
'datadir': os.path.join(datadir, 'bch_' + str(node_id)),
'bindir': BITCOINCASH_BINDIR,
'use_segwit': False,
}
@classmethod
def coins_loop(cls):
super(TestBCH, cls).coins_loop()
ci0 = cls.swap_clients[0].ci(cls.test_coin)
try:
if cls.bch_addr is not None:
ci0.rpc_wallet('generatetoaddress', [1, cls.bch_addr])
except Exception as e:
logging.warning('coins_loop generate {}'.format(e))
@classmethod
def tearDownClass(cls):
logging.info('Finalising Bitcoincash Test')
super(TestBCH, cls).tearDownClass()
stopDaemons(cls.bch_daemons)
cls.bch_daemons.clear()
def mineBlock(self, num_blocks=1):
self.callnoderpc('generatetoaddress', [num_blocks, self.bch_addr])
@ -125,17 +173,17 @@ class TestBCH(BasicSwapTest):
return True
def test_001_nested_segwit(self):
logging.info('---------- Test {} p2sh nested segwit'.format(self.test_coin_from.name))
logging.info('---------- Test {} p2sh nested segwit'.format(self.test_coin.name))
logging.info('Skipped')
def test_002_native_segwit(self):
logging.info('---------- Test {} p2sh native segwit'.format(self.test_coin_from.name))
logging.info('---------- Test {} p2sh native segwit'.format(self.test_coin.name))
logging.info('Skipped')
def test_003_cltv(self):
logging.info('---------- Test {} cltv'.format(self.test_coin_from.name))
logging.info('---------- Test {} cltv'.format(self.test_coin.name))
ci = self.swap_clients[0].ci(self.test_coin_from)
ci = self.swap_clients[0].ci(self.test_coin)
self.check_softfork_active('bip65')
@ -196,10 +244,10 @@ class TestBCH(BasicSwapTest):
assert (len(tx_wallet['blockhash']) == 64)
def test_004_csv(self):
logging.info('---------- Test {} csv'.format(self.test_coin_from.name))
logging.info('---------- Test {} csv'.format(self.test_coin.name))
swap_clients = self.swap_clients
ci = self.swap_clients[0].ci(self.test_coin_from)
ci = self.swap_clients[0].ci(self.test_coin)
self.check_softfork_active('csv')
@ -252,9 +300,9 @@ class TestBCH(BasicSwapTest):
assert (len(tx_wallet['blockhash']) == 64)
def test_005_watchonly(self):
logging.info('---------- Test {} watchonly'.format(self.test_coin_from.name))
ci = self.swap_clients[0].ci(self.test_coin_from)
ci1 = self.swap_clients[1].ci(self.test_coin_from)
logging.info('---------- Test {} watchonly'.format(self.test_coin.name))
ci = self.swap_clients[0].ci(self.test_coin)
ci1 = self.swap_clients[1].ci(self.test_coin)
addr = ci.rpc_wallet('getnewaddress', ['watchonly test'])
ro = ci1.rpc_wallet('importaddress', [addr, '', False])
@ -268,10 +316,10 @@ class TestBCH(BasicSwapTest):
super().test_006_getblock_verbosity()
def test_007_hdwallet(self):
logging.info('---------- Test {} hdwallet'.format(self.test_coin_from.name))
logging.info('---------- Test {} hdwallet'.format(self.test_coin.name))
test_seed = '8e54a313e6df8918df6d758fafdbf127a115175fdd2238d0e908dd8093c9ac3b'
test_wif = self.swap_clients[0].ci(self.test_coin_from).encodeKey(bytes.fromhex(test_seed))
test_wif = self.swap_clients[0].ci(self.test_coin).encodeKey(bytes.fromhex(test_seed))
new_wallet_name = random.randbytes(10).hex()
self.callnoderpc('createwallet', [new_wallet_name])
self.callnoderpc('sethdseed', [True, test_wif], wallet=new_wallet_name)
@ -402,10 +450,10 @@ class TestBCH(BasicSwapTest):
def test_011_p2sh(self):
# Not used in bsx for native-segwit coins
logging.info('---------- Test {} p2sh'.format(self.test_coin_from.name))
logging.info('---------- Test {} p2sh'.format(self.test_coin.name))
swap_clients = self.swap_clients
ci = self.swap_clients[0].ci(self.test_coin_from)
ci = self.swap_clients[0].ci(self.test_coin)
script = CScript([2, 2, OP_EQUAL, ])
@ -449,10 +497,10 @@ class TestBCH(BasicSwapTest):
def test_011_p2sh32(self):
# Not used in bsx for native-segwit coins
logging.info('---------- Test {} p2sh32'.format(self.test_coin_from.name))
logging.info('---------- Test {} p2sh32'.format(self.test_coin.name))
swap_clients = self.swap_clients
ci = self.swap_clients[0].ci(self.test_coin_from)
ci = self.swap_clients[0].ci(self.test_coin)
script = CScript([2, 2, OP_EQUAL, ])
@ -495,7 +543,7 @@ class TestBCH(BasicSwapTest):
assert (len(tx_wallet['blockhash']) == 64)
def test_012_p2sh_p2wsh(self):
logging.info('---------- Test {} p2sh-p2wsh'.format(self.test_coin_from.name))
logging.info('---------- Test {} p2sh-p2wsh'.format(self.test_coin.name))
logging.info('Skipped')
def test_01_a_full_swap(self):
@ -560,7 +608,7 @@ class TestBCH(BasicSwapTest):
super().test_05_self_bid_rev()
def test_06_preselect_inputs(self):
tla_from = self.test_coin_from.name
tla_from = self.test_coin.name
logging.info('---------- Test {} Preselected inputs'.format(tla_from))
logging.info('Skipped')

View file

@ -84,8 +84,6 @@ from tests.basicswap.common import (
BASE_ZMQ_PORT,
BTC_BASE_PORT,
BTC_BASE_RPC_PORT,
BCH_BASE_PORT,
BCH_BASE_RPC_PORT,
LTC_BASE_PORT,
LTC_BASE_RPC_PORT,
PREFIX_SECRET_KEY_REGTEST,
@ -101,7 +99,6 @@ logger = logging.getLogger()
NUM_NODES = 3
NUM_XMR_NODES = 3
NUM_BTC_NODES = 3
NUM_BCH_NODES = 3
NUM_LTC_NODES = 3
TEST_DIR = cfg.TEST_DATADIRS
@ -219,18 +216,6 @@ def prepare_swapclient_dir(datadir, node_id, network_key, network_pubkey, with_c
'use_segwit': True,
}
if Coins.BCH in with_coins:
settings['chainclients']['bitcoincash'] = {
'connection_type': 'rpc',
'manage_daemon': False,
'rpcport': BCH_BASE_RPC_PORT + node_id,
'rpcuser': 'test' + str(node_id),
'rpcpassword': 'test_pass' + str(node_id),
'datadir': os.path.join(datadir, 'bch_' + str(node_id)),
'bindir': cfg.BITCOINCASH_BINDIR,
'use_segwit': False,
}
if cls:
cls.addCoinSettings(settings, datadir, node_id)
@ -242,10 +227,6 @@ def btcCli(cmd, node_id=0):
return callrpc_cli(cfg.BITCOIN_BINDIR, os.path.join(TEST_DIR, 'btc_' + str(node_id)), 'regtest', cmd, cfg.BITCOIN_CLI)
def bchCli(cmd, node_id=0):
return callrpc_cli(cfg.BITCOINCASH_BINDIR, os.path.join(TEST_DIR, 'bch_' + str(node_id)), 'regtest', cmd, cfg.BITCOINCASH_CLI)
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)
@ -313,20 +294,17 @@ class BaseTest(unittest.TestCase):
swap_clients = []
part_daemons = []
btc_daemons = []
bch_daemons = []
ltc_daemons = []
xmr_daemons = []
xmr_wallet_auth = []
restore_instance = False
start_bch_nodes = False
start_ltc_nodes = False
start_xmr_nodes = True
has_segwit = True
xmr_addr = None
btc_addr = None
bch_addr = None
ltc_addr = None
@classmethod
@ -427,20 +405,6 @@ class BaseTest(unittest.TestCase):
waitForRPC(make_rpc_func(i, base_rpc_port=BTC_BASE_RPC_PORT), test_delay_event)
for i in range(NUM_BCH_NODES):
if not cls.restore_instance:
data_dir = prepareDataDir(TEST_DIR, i, 'bitcoin.conf', 'bch_', base_p2p_port=BCH_BASE_PORT, base_rpc_port=BCH_BASE_RPC_PORT)
if os.path.exists(os.path.join(cfg.BITCOINCASH_BINDIR, 'bitcoin-wallet')):
try:
callrpc_cli(cfg.BITCOINCASH_BINDIR, data_dir, 'regtest', '-wallet=wallet.dat create', 'bitcoin-wallet')
except Exception as e:
logging.warning('bch: bitcoin-wallet create failed')
raise e
cls.bch_daemons.append(startDaemon(os.path.join(TEST_DIR, 'bch_' + str(i)), cfg.BITCOINCASH_BINDIR, cfg.BITCOINCASHD))
logging.info('Bch: Started %s %d', cfg.BITCOINCASHD, cls.part_daemons[-1].handle.pid)
waitForRPC(make_rpc_func(i, base_rpc_port=BCH_BASE_RPC_PORT), test_delay_event)
if cls.start_ltc_nodes:
for i in range(NUM_LTC_NODES):
if not cls.restore_instance:
@ -502,8 +466,6 @@ class BaseTest(unittest.TestCase):
start_nodes.add(Coins.LTC)
if cls.start_xmr_nodes:
start_nodes.add(Coins.XMR)
if cls.start_bch_nodes:
start_nodes.add(Coins.BCH)
if not cls.restore_instance:
prepare_swapclient_dir(TEST_DIR, i, cls.network_key, cls.network_pubkey, start_nodes, cls)
basicswap_dir = os.path.join(os.path.join(TEST_DIR, 'basicswap_' + str(i)))
@ -521,8 +483,6 @@ class BaseTest(unittest.TestCase):
if cls.start_ltc_nodes:
sc.setDaemonPID(Coins.LTC, cls.ltc_daemons[i].handle.pid)
if cls.start_bch_nodes:
sc.setDaemonPID(Coins.BCH, cls.bch_daemons[i].handle.pid)
cls.addPIDInfo(sc, i)
sc.start()
@ -542,8 +502,6 @@ class BaseTest(unittest.TestCase):
cls.ltc_addr = cls.swap_clients[0].ci(Coins.LTC).pubkey_to_address(void_block_rewards_pubkey)
if cls.start_xmr_nodes:
cls.xmr_addr = cls.callxmrnodewallet(cls, 1, 'get_address')['address']
if cls.start_bch_nodes:
cls.bch_addr = cls.swap_clients[0].ci(Coins.BCH).pubkey_to_address(void_block_rewards_pubkey)
else:
cls.btc_addr = callnoderpc(0, 'getnewaddress', ['mining_addr', 'bech32'], base_rpc_port=BTC_BASE_RPC_PORT)
num_blocks = 400 # Mine enough to activate segwit
@ -592,12 +550,6 @@ class BaseTest(unittest.TestCase):
checkForks(callnoderpc(0, 'getblockchaininfo', base_rpc_port=LTC_BASE_RPC_PORT, wallet='wallet.dat'))
if cls.start_bch_nodes:
num_blocks = 200
cls.bch_addr = callnoderpc(0, 'getnewaddress', ['mining_addr'], base_rpc_port=BCH_BASE_RPC_PORT, wallet='wallet.dat')
logging.info('Mining %d BitcoinCash blocks to %s', num_blocks, cls.bch_addr)
callnoderpc(0, 'generatetoaddress', [num_blocks, cls.bch_addr], base_rpc_port=BCH_BASE_RPC_PORT, wallet='wallet.dat')
num_blocks = 100
if cls.start_xmr_nodes:
cls.xmr_addr = cls.callxmrnodewallet(cls, 1, 'get_address')['address']
@ -660,14 +612,12 @@ class BaseTest(unittest.TestCase):
stopDaemons(cls.xmr_daemons)
stopDaemons(cls.part_daemons)
stopDaemons(cls.btc_daemons)
stopDaemons(cls.bch_daemons)
stopDaemons(cls.ltc_daemons)
cls.http_threads.clear()
cls.swap_clients.clear()
cls.part_daemons.clear()
cls.btc_daemons.clear()
cls.bch_daemons.clear()
cls.ltc_daemons.clear()
cls.xmr_daemons.clear()
@ -693,8 +643,6 @@ class BaseTest(unittest.TestCase):
def coins_loop(cls):
if cls.btc_addr is not None:
btcCli('generatetoaddress 1 {}'.format(cls.btc_addr))
if cls.bch_addr is not None:
bchCli('generatetoaddress 1 {}'.format(cls.bch_addr))
if cls.ltc_addr is not None:
ltcCli('generatetoaddress 1 {}'.format(cls.ltc_addr))
if cls.xmr_addr is not None: