diff --git a/tests/basicswap/extended/test_dash.py b/tests/basicswap/extended/test_dash.py index 2b12a4d..7039a2f 100644 --- a/tests/basicswap/extended/test_dash.py +++ b/tests/basicswap/extended/test_dash.py @@ -382,11 +382,8 @@ class Test(unittest.TestCase): offer_id = swap_clients[0].postOffer(Coins.PART, Coins.DASH, 100 * COIN, 0.1 * COIN, 100 * COIN, SwapTypes.SELLER_FIRST) 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) + offer = swap_clients[1].getOffer(offer_id) + bid_id = swap_clients[1].postBid(offer_id, offer.amount_from) wait_for_bid(delay_event, swap_clients[0], bid_id) @@ -409,10 +406,8 @@ class Test(unittest.TestCase): offer_id = swap_clients[1].postOffer(Coins.DASH, Coins.PART, 10 * COIN, 9.0 * COIN, 10 * COIN, SwapTypes.SELLER_FIRST) 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) + offer = swap_clients[0].getOffer(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) @@ -434,10 +429,8 @@ class Test(unittest.TestCase): offer_id = swap_clients[0].postOffer(Coins.DASH, Coins.BTC, 10 * COIN, 0.1 * COIN, 10 * COIN, SwapTypes.SELLER_FIRST) 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) + offer = swap_clients[1].getOffer(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) @@ -464,10 +457,8 @@ class Test(unittest.TestCase): TxLockTypes.SEQUENCE_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) + offer = swap_clients[1].getOffer(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) @@ -490,10 +481,8 @@ class Test(unittest.TestCase): offer_id = swap_clients[0].postOffer(Coins.DASH, Coins.BTC, 10 * COIN, 10 * COIN, 10 * COIN, SwapTypes.SELLER_FIRST) 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) + offer = swap_clients[0].getOffer(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) @@ -514,10 +503,8 @@ class Test(unittest.TestCase): offer_id = swap_clients[0].postOffer(Coins.DASH, Coins.BTC, 0.001 * COIN, 1.0 * COIN, 0.001 * COIN, SwapTypes.SELLER_FIRST) 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) + offer = swap_clients[0].getOffer(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) diff --git a/tests/basicswap/extended/test_firo.py b/tests/basicswap/extended/test_firo.py index 1e00ee7..c8e9112 100644 --- a/tests/basicswap/extended/test_firo.py +++ b/tests/basicswap/extended/test_firo.py @@ -273,11 +273,8 @@ class Test(BaseTest): offer_id = swap_clients[0].postOffer(Coins.PART, self.test_coin_from, 100 * COIN, 0.1 * COIN, 100 * COIN, SwapTypes.SELLER_FIRST) wait_for_offer(test_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) + offer = swap_clients[1].getOffer(offer_id) + bid_id = swap_clients[1].postBid(offer_id, offer.amount_from) wait_for_bid(test_delay_event, swap_clients[0], bid_id) swap_clients[0].acceptBid(bid_id) @@ -298,10 +295,8 @@ class Test(BaseTest): offer_id = swap_clients[1].postOffer(self.test_coin_from, Coins.PART, 10 * COIN, 9.0 * COIN, 10 * COIN, SwapTypes.SELLER_FIRST) wait_for_offer(test_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) + offer = swap_clients[0].getOffer(offer_id) + bid_id = swap_clients[0].postBid(offer_id, offer.amount_from) wait_for_bid(test_delay_event, swap_clients[1], bid_id) swap_clients[1].acceptBid(bid_id) @@ -323,10 +318,8 @@ class Test(BaseTest): offer_id = swap_clients[0].postOffer(self.test_coin_from, Coins.BTC, 10 * COIN, 0.1 * COIN, 10 * COIN, SwapTypes.SELLER_FIRST) wait_for_offer(test_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) + offer = swap_clients[1].getOffer(offer_id) + bid_id = swap_clients[1].postBid(offer_id, offer.amount_from) wait_for_bid(test_delay_event, swap_clients[0], bid_id) swap_clients[0].acceptBid(bid_id) @@ -353,10 +346,8 @@ class Test(BaseTest): TxLockTypes.SEQUENCE_LOCK_BLOCKS, 10) wait_for_offer(test_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) + offer = swap_clients[1].getOffer(offer_id) + bid_id = swap_clients[1].postBid(offer_id, offer.amount_from) wait_for_bid(test_delay_event, swap_clients[0], bid_id) swap_clients[1].abandonBid(bid_id) @@ -379,10 +370,8 @@ class Test(BaseTest): offer_id = swap_clients[0].postOffer(self.test_coin_from, Coins.BTC, 10 * COIN, 10 * COIN, 10 * COIN, SwapTypes.SELLER_FIRST) wait_for_offer(test_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) + offer = swap_clients[0].getOffer(offer_id) + bid_id = swap_clients[0].postBid(offer_id, offer.amount_from) wait_for_bid(test_delay_event, swap_clients[0], bid_id) swap_clients[0].acceptBid(bid_id) @@ -403,10 +392,8 @@ class Test(BaseTest): offer_id = swap_clients[0].postOffer(self.test_coin_from, Coins.BTC, 0.001 * COIN, 1.0 * COIN, 0.001 * COIN, SwapTypes.SELLER_FIRST) wait_for_offer(test_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) + offer = swap_clients[0].getOffer(offer_id) + bid_id = swap_clients[0].postBid(offer_id, offer.amount_from) wait_for_bid(test_delay_event, swap_clients[0], bid_id) swap_clients[0].acceptBid(bid_id) @@ -449,8 +436,7 @@ class Test(BaseTest): rate_swap = make_int(random.uniform(0.2, 20.0), scale=12, r=1) offer_id = swap_clients[0].postOffer(self.test_coin_from, Coins.XMR, amt_swap, rate_swap, amt_swap, SwapTypes.XMR_SWAP) wait_for_offer(test_delay_event, swap_clients[1], offer_id) - offers = swap_clients[0].listOffers(filters={'offer_id': offer_id}) - offer = offers[0] + offer = swap_clients[0].getOffer(offer_id) 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) diff --git a/tests/basicswap/extended/test_nmc.py b/tests/basicswap/extended/test_nmc.py index 98800b9..fbe97f3 100644 --- a/tests/basicswap/extended/test_nmc.py +++ b/tests/basicswap/extended/test_nmc.py @@ -381,11 +381,8 @@ class Test(unittest.TestCase): offer_id = swap_clients[0].postOffer(Coins.PART, Coins.NMC, 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) + offer = swap_clients[1].getOffer(offer_id) + bid_id = swap_clients[1].postBid(offer_id, offer.amount_from) wait_for_bid(delay_event, swap_clients[0], bid_id) @@ -408,10 +405,8 @@ class Test(unittest.TestCase): offer_id = swap_clients[1].postOffer(Coins.NMC, 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) + offer = swap_clients[0].getOffer(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) @@ -433,10 +428,8 @@ class Test(unittest.TestCase): offer_id = swap_clients[0].postOffer(Coins.NMC, 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) + offer = swap_clients[1].getOffer(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) @@ -463,10 +456,8 @@ class Test(unittest.TestCase): 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) + offer = swap_clients[1].getOffer(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) @@ -489,10 +480,8 @@ class Test(unittest.TestCase): offer_id = swap_clients[0].postOffer(Coins.NMC, 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) + offer = swap_clients[0].getOffer(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) @@ -513,10 +502,8 @@ class Test(unittest.TestCase): offer_id = swap_clients[0].postOffer(Coins.NMC, 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) + offer = swap_clients[0].getOffer(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) diff --git a/tests/basicswap/extended/test_pivx.py b/tests/basicswap/extended/test_pivx.py index 5ddb897..136b03e 100644 --- a/tests/basicswap/extended/test_pivx.py +++ b/tests/basicswap/extended/test_pivx.py @@ -392,11 +392,8 @@ class Test(unittest.TestCase): 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) + offer = swap_clients[1].getOffer(offer_id) + bid_id = swap_clients[1].postBid(offer_id, offer.amount_from) wait_for_bid(delay_event, swap_clients[0], bid_id) @@ -419,10 +416,8 @@ class Test(unittest.TestCase): 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) + offer = swap_clients[0].getOffer(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) @@ -444,10 +439,8 @@ class Test(unittest.TestCase): 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) + offer = swap_clients[1].getOffer(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) @@ -474,10 +467,8 @@ class Test(unittest.TestCase): 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) + offer = swap_clients[1].getOffer(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) @@ -500,10 +491,8 @@ class Test(unittest.TestCase): 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) + offer = swap_clients[0].getOffer(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) @@ -524,10 +513,8 @@ class Test(unittest.TestCase): 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) + offer = swap_clients[0].getOffer(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) diff --git a/tests/basicswap/test_btc_xmr.py b/tests/basicswap/test_btc_xmr.py index 7e7d964..bcb6e27 100644 --- a/tests/basicswap/test_btc_xmr.py +++ b/tests/basicswap/test_btc_xmr.py @@ -47,16 +47,17 @@ from .test_xmr import BaseTest, test_delay_event, callnoderpc logger = logging.getLogger() -class Test(BaseTest): - __test__ = True - test_coin_from = Coins.BTC - start_ltc_nodes = False +class BasicSwapTest(BaseTest): + base_rpc_port = None def getBalance(self, js_wallets): return float(js_wallets[self.test_coin_from.name]['balance']) + float(js_wallets[self.test_coin_from.name]['unconfirmed']) def callnoderpc(self, method, params=[], wallet=None, node_id=0): - return callnoderpc(node_id, method, params, wallet, base_rpc_port=BTC_BASE_RPC_PORT) + return callnoderpc(node_id, method, params, wallet, self.base_rpc_port) + + def mineBlock(self, num_blocks=1): + self.callnoderpc('generatetoaddress', [num_blocks, self.btc_addr]) def test_001_nested_segwit(self): logging.info('---------- Test {} p2sh nested segwit'.format(self.test_coin_from.name)) @@ -68,7 +69,7 @@ class Test(BaseTest): txid = self.callnoderpc('sendtoaddress', [addr_p2sh_segwit, 1.0]) assert len(txid) == 64 - self.callnoderpc('generatetoaddress', [1, self.btc_addr]) + self.mineBlock() ro = self.callnoderpc('scantxoutset', ['start', ['addr({})'.format(addr_p2sh_segwit)]]) assert (len(ro['unspents']) == 1) assert (ro['unspents'][0]['txid'] == txid) @@ -108,7 +109,7 @@ class Test(BaseTest): tx_wallet = self.callnoderpc('gettransaction', [txid, ])['hex'] tx = self.callnoderpc('decoderawtransaction', [tx_wallet, ]) - self.callnoderpc('generatetoaddress', [1, self.btc_addr]) + self.mineBlock() ro = self.callnoderpc('scantxoutset', ['start', ['addr({})'.format(addr_segwit)]]) assert (len(ro['unspents']) == 1) assert (ro['unspents'][0]['txid'] == txid) @@ -161,9 +162,9 @@ class Test(BaseTest): except Exception as e: assert ('non-final' in str(e)) - self.callnoderpc('generatetoaddress', [49, self.btc_addr]) + self.mineBlock(5) txid = self.callnoderpc('sendrawtransaction', [tx_spend_hex, ]) - self.callnoderpc('generatetoaddress', [1, self.btc_addr]) + self.mineBlock() ro = self.callnoderpc('listreceivedbyaddress', [0, ]) sum_addr = 0 for entry in ro: @@ -206,9 +207,9 @@ class Test(BaseTest): except Exception as e: assert ('non-BIP68-final' in str(e)) - self.callnoderpc('generatetoaddress', [3, self.btc_addr]) + self.mineBlock(3) txid = self.callnoderpc('sendrawtransaction', [tx_spend_hex, ]) - self.callnoderpc('generatetoaddress', [1, self.btc_addr]) + self.mineBlock(1) ro = self.callnoderpc('listreceivedbyaddress', [0, ]) sum_addr = 0 for entry in ro: @@ -418,5 +419,12 @@ class Test(BaseTest): assert (node1_xmr_before - node1_xmr_after < 0.02) +class TestBTC(BasicSwapTest): + __test__ = True + test_coin_from = Coins.BTC + start_ltc_nodes = False + base_rpc_port = BTC_BASE_RPC_PORT + + if __name__ == '__main__': unittest.main() diff --git a/tests/basicswap/test_ltc_xmr.py b/tests/basicswap/test_ltc_xmr.py index b1fc275..694e3d0 100644 --- a/tests/basicswap/test_ltc_xmr.py +++ b/tests/basicswap/test_ltc_xmr.py @@ -13,237 +13,98 @@ from basicswap.basicswap import ( Coins, SwapTypes, BidStates, - DebugTypes, -) -from basicswap.basicswap_util import ( - TxLockTypes, ) from basicswap.util import ( - make_int, - format_amount, + COIN, ) from tests.basicswap.common import ( wait_for_bid, read_json_api, wait_for_offer, - wait_for_none_active, + wait_for_in_progress, + LTC_BASE_RPC_PORT, ) - -''' -# Should work? - -from .test_btc_xmr import Test, test_delay_event +from .test_btc_xmr import BasicSwapTest, test_delay_event logger = logging.getLogger() -class TestLTC(Test): +class TestLTC(BasicSwapTest): __test__ = True + test_coin_from = Coins.LTC + start_ltc_nodes = True + base_rpc_port = LTC_BASE_RPC_PORT - @classmethod - def setUpClass(cls): - cls.test_coin_from = Coins.LTC - cls.start_ltc_nodes = True - super(TestLTC, cls).setUpClass() + def mineBlock(self, num_blocks=1): + self.callnoderpc('generatetoaddress', [num_blocks, self.ltc_addr]) - @classmethod - def tearDownClass(cls): - logging.info('Finalising LTC Test') - super(TestLTC, cls).tearDownClass() -''' + def test_001_nested_segwit(self): + logging.info('---------- Test {} p2sh nested segwit'.format(self.test_coin_from.name)) + logging.info('Skipped') -from .test_xmr import BaseTest, test_delay_event + def test_002_native_segwit(self): + logging.info('---------- Test {} p2sh native segwit'.format(self.test_coin_from.name)) -logger = logging.getLogger() + addr_segwit = self.callnoderpc('getnewaddress', ['segwit test', 'bech32']) + addr_info = self.callnoderpc('getaddressinfo', [addr_segwit, ]) + assert addr_info['iswitness'] is True + txid = self.callnoderpc('sendtoaddress', [addr_segwit, 1.0]) + assert len(txid) == 64 + tx_wallet = self.callnoderpc('gettransaction', [txid, ])['hex'] + tx = self.callnoderpc('decoderawtransaction', [tx_wallet, ]) -class Test(BaseTest): - __test__ = True + self.mineBlock() + ro = self.callnoderpc('scantxoutset', ['start', ['addr({})'.format(addr_segwit)]]) + assert (len(ro['unspents']) == 1) + assert (ro['unspents'][0]['txid'] == txid) - @classmethod - def setUpClass(cls): - cls.test_coin_from = Coins.LTC - cls.start_ltc_nodes = True - super(Test, cls).setUpClass() + prevout_n = -1 + for txo in tx['vout']: + if addr_segwit in txo['scriptPubKey']['addresses']: + prevout_n = txo['n'] + break + assert prevout_n > -1 - @classmethod - def tearDownClass(cls): - logging.info('Finalising LTC Test') - super(Test, cls).tearDownClass() + tx_funded = self.callnoderpc('createrawtransaction', [[{'txid': txid, 'vout': prevout_n}], {addr_segwit: 0.99}]) + tx_signed = self.callnoderpc('signrawtransactionwithwallet', [tx_funded, ])['hex'] + tx_funded_decoded = self.callnoderpc('decoderawtransaction', [tx_funded, ]) + tx_signed_decoded = self.callnoderpc('decoderawtransaction', [tx_signed, ]) + assert tx_funded_decoded['txid'] == tx_signed_decoded['txid'] - def getBalance(self, js_wallets): - return float(js_wallets[self.test_coin_from.name]['balance']) + float(js_wallets[self.test_coin_from.name]['unconfirmed']) + def test_007_hdwallet(self): + logging.info('---------- Test {} hdwallet'.format(self.test_coin_from.name)) - def getXmrBalance(self, js_wallets): - return float(js_wallets[Coins.XMR.name]['unconfirmed']) + float(js_wallets[Coins.XMR.name]['balance']) + test_seed = '8e54a313e6df8918df6d758fafdbf127a115175fdd2238d0e908dd8093c9ac3b' + test_wif = self.swap_clients[0].ci(self.test_coin_from).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) + addr = self.callnoderpc('getnewaddress', wallet=new_wallet_name) + self.callnoderpc('unloadwallet', [new_wallet_name]) + assert (addr == 'rltc1qps7hnjd866e9ynxadgseprkc2l56m00djr82la') - def test_01_full_swap(self): - logging.info('---------- Test {} to XMR'.format(str(self.test_coin_from))) + def test_20_btc_coin(self): + logging.info('---------- Test BTC to {}'.format(self.test_coin_from.name)) swap_clients = self.swap_clients - js_0 = read_json_api(1800, 'wallets') - node0_from_before = self.getBalance(js_0) + offer_id = swap_clients[0].postOffer(Coins.BTC, self.test_coin_from, 100 * COIN, 0.1 * COIN, 100 * COIN, SwapTypes.SELLER_FIRST) - js_1 = read_json_api(1801, 'wallets') - node1_from_before = self.getBalance(js_1) - - 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) - offer_id = swap_clients[0].postOffer(self.test_coin_from, Coins.XMR, amt_swap, rate_swap, amt_swap, SwapTypes.XMR_SWAP) - wait_for_offer(test_delay_event, swap_clients[1], offer_id) - offers = swap_clients[0].listOffers(filters={'offer_id': offer_id}) - offer = offers[0] - - 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) - - 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) - - amount_from = float(format_amount(amt_swap, 8)) - 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 = 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 = 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)) - amount_to_float = float(format_amount(amount_to, 12)) - node1_xmr_after = float(js_1_xmr_after['unconfirmed']) + float(js_1_xmr_after['balance']) - node1_xmr_before = float(js_1_xmr['unconfirmed']) + float(js_1_xmr['balance']) - assert (node1_xmr_after > node1_xmr_before + (amount_to_float - 0.02)) - - def test_02_leader_recover_a_lock_tx(self): - 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 = 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) - rate_swap = make_int(random.uniform(0.2, 20.0), scale=12, r=1) - offer_id = swap_clients[0].postOffer( - self.test_coin_from, Coins.XMR, amt_swap, rate_swap, amt_swap, SwapTypes.XMR_SWAP, - lock_type=TxLockTypes.SEQUENCE_LOCK_BLOCKS, lock_value=12) wait_for_offer(test_delay_event, swap_clients[1], offer_id) offer = swap_clients[1].getOffer(offer_id) + bid_id = swap_clients[1].postBid(offer_id, offer.amount_from) - bid_id = swap_clients[1].postXmrBid(offer_id, offer.amount_from) + wait_for_bid(test_delay_event, swap_clients[0], bid_id) + swap_clients[0].acceptBid(bid_id) - wait_for_bid(test_delay_event, swap_clients[0], bid_id, BidStates.BID_RECEIVED) + wait_for_in_progress(test_delay_event, swap_clients[1], bid_id, sent=True) + wait_for_bid(test_delay_event, swap_clients[0], bid_id, BidStates.SWAP_COMPLETED, wait_for=60) + wait_for_bid(test_delay_event, swap_clients[1], bid_id, BidStates.SWAP_COMPLETED, sent=True, wait_for=60) - bid, xmr_swap = swap_clients[0].getXmrBid(bid_id) - assert (xmr_swap) - - swap_clients[1].setBidDebugInd(bid_id, DebugTypes.BID_STOP_AFTER_COIN_A_LOCK) - - swap_clients[0].acceptXmrBid(bid_id) - - 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.BID_STALLED_FOR_TEST, BidStates.XMR_SWAP_FAILED], sent=True) - - js_w0_after = read_json_api(1800, 'wallets') - node0_from_after = self.getBalance(js_w0_after) - - # TODO: Discard block rewards - # assert (node0_from_before - node0_from_after < 0.02) - - def test_03_follower_recover_a_lock_tx(self): - 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 = 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) - offer_id = swap_clients[0].postOffer( - self.test_coin_from, Coins.XMR, amt_swap, rate_swap, amt_swap, SwapTypes.XMR_SWAP, - lock_type=TxLockTypes.SEQUENCE_LOCK_BLOCKS, lock_value=12) - wait_for_offer(test_delay_event, swap_clients[1], offer_id) - offer = swap_clients[1].getOffer(offer_id) - - 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[1].setBidDebugInd(bid_id, DebugTypes.BID_STOP_AFTER_COIN_A_LOCK) - swap_clients[0].setBidDebugInd(bid_id, DebugTypes.BID_DONT_SPEND_COIN_A_LOCK_REFUND) - - swap_clients[0].acceptXmrBid(bid_id) - - 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 = 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)) - # TODO: Discard block rewards - # assert (node1_from_after - node1_from_before > (amount_from - 0.02)) - - swap_clients[0].abandonBid(bid_id) - - wait_for_none_active(test_delay_event, 1800) - wait_for_none_active(test_delay_event, 1801) - - def test_04_follower_recover_b_lock_tx(self): - logging.info('---------- Test {} to XMR follower recovers coin b lock tx'.format(str(self.test_coin_from))) - - swap_clients = self.swap_clients - - 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) - offer_id = swap_clients[0].postOffer( - self.test_coin_from, Coins.XMR, amt_swap, rate_swap, amt_swap, SwapTypes.XMR_SWAP, - lock_type=TxLockTypes.SEQUENCE_LOCK_BLOCKS, lock_value=28) - wait_for_offer(test_delay_event, swap_clients[1], offer_id) - offer = swap_clients[1].getOffer(offer_id) - - 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[1].setBidDebugInd(bid_id, DebugTypes.CREATE_INVALID_COIN_B_LOCK) - swap_clients[0].acceptXmrBid(bid_id) - - 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 = 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) - - # TODO: Discard block rewards - # assert (node0_from_before - node0_from_after < 0.02) - - node1_xmr_before = self.getXmrBalance(js_w1_before) - node1_xmr_after = self.getXmrBalance(js_w1_after) - assert (node1_xmr_before - node1_xmr_after < 0.02) + 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) if __name__ == '__main__': diff --git a/tests/basicswap/test_run.py b/tests/basicswap/test_run.py index ef68d38..fbe5e23 100644 --- a/tests/basicswap/test_run.py +++ b/tests/basicswap/test_run.py @@ -154,11 +154,8 @@ class Test(BaseTest): offer_id = swap_clients[0].postOffer(Coins.PART, Coins.LTC, 100 * COIN, 0.1 * COIN, 100 * COIN, SwapTypes.SELLER_FIRST) wait_for_offer(test_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) + offer = swap_clients[1].getOffer(offer_id) + bid_id = swap_clients[1].postBid(offer_id, offer.amount_from) wait_for_bid(test_delay_event, swap_clients[0], bid_id) @@ -275,7 +272,6 @@ class Test(BaseTest): wait_for_offer(test_delay_event, swap_clients[0], offer_id) offer = swap_clients[0].getOffer(offer_id) - offers = swap_clients[0].listOffers() bid_id = swap_clients[0].postBid(offer_id, offer.amount_from) wait_for_bid(test_delay_event, swap_clients[0], bid_id) @@ -326,11 +322,8 @@ class Test(BaseTest): offer_id = swap_clients[0].postOffer(Coins.PART, Coins.LTC, 100 * COIN, 0.1 * COIN, 100 * COIN, SwapTypes.SELLER_FIRST, auto_accept_bids=True) wait_for_offer(test_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) + offer = swap_clients[1].getOffer(offer_id) + bid_id = swap_clients[1].postBid(offer_id, offer.amount_from) wait_for_bid(test_delay_event, swap_clients[0], bid_id, BidStates.SWAP_COMPLETED, wait_for=60) wait_for_bid(test_delay_event, swap_clients[1], bid_id, BidStates.SWAP_COMPLETED, sent=True, wait_for=60) diff --git a/tests/basicswap/test_xmr.py b/tests/basicswap/test_xmr.py index 061f04b..3f079e6 100644 --- a/tests/basicswap/test_xmr.py +++ b/tests/basicswap/test_xmr.py @@ -518,6 +518,10 @@ class BaseTest(unittest.TestCase): mweb_addr = callnoderpc(2, 'getnewaddress', ['mweb_addr', 'mweb'], base_rpc_port=LTC_BASE_RPC_PORT) callnoderpc(0, 'sendtoaddress', [mweb_addr, 1], base_rpc_port=LTC_BASE_RPC_PORT) + ltc_addr1 = callnoderpc(1, 'getnewaddress', ['initial addr'], base_rpc_port=LTC_BASE_RPC_PORT) + for i in range(5): + callnoderpc(0, 'sendtoaddress', [ltc_addr1, 100], base_rpc_port=LTC_BASE_RPC_PORT) + num_blocks = 69 cls.ltc_addr = cls.swap_clients[0].ci(Coins.LTC).pubkey_to_address(void_block_rewards_pubkey) callnoderpc(0, 'generatetoaddress', [num_blocks, cls.ltc_addr], base_rpc_port=LTC_BASE_RPC_PORT)