From 2c49d13aa0cdc3d6b0ddac2119fdb24b2e47d4a9 Mon Sep 17 00:00:00 2001 From: tecnovert Date: Mon, 18 Jul 2022 22:56:28 +0200 Subject: [PATCH] tests: Add non xmr swap failure states to tests. --- basicswap/basicswap.py | 4 +- .../xmr.bidder.alt.xu.min.svg | 359 +++++++++--------- doc/protocols/sequence_diagrams/bidder.alt.xu | 15 +- tests/basicswap/common.py | 10 +- tests/basicswap/test_run.py | 28 +- 5 files changed, 223 insertions(+), 193 deletions(-) diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py index 72f47c2..36d229f 100644 --- a/basicswap/basicswap.py +++ b/basicswap/basicswap.py @@ -3199,8 +3199,8 @@ class BasicSwap(BaseApp): if state > BidStates.BID_ACCEPTED: # Wait for spend of all known swap txns - if (bid.getITxState() is None or bid.getITxState() >= TxStates.TX_REDEEMED) \ - and (bid.getPTxState() is None or bid.getPTxState() >= TxStates.TX_REDEEMED): + if (bid.getITxState() is None or bid.getITxState() >= TxStates.TX_REDEEMED) and \ + (bid.getPTxState() is None or bid.getPTxState() >= TxStates.TX_REDEEMED): self.log.info('Swap completed for bid %s', bid_id.hex()) if bid.getITxState() == TxStates.TX_REDEEMED: diff --git a/basicswap/static/sequence_diagrams/xmr.bidder.alt.xu.min.svg b/basicswap/static/sequence_diagrams/xmr.bidder.alt.xu.min.svg index c402bf4..ae54aed 100644 --- a/basicswap/static/sequence_diagrams/xmr.bidder.alt.xu.min.svg +++ b/basicswap/static/sequence_diagrams/xmr.bidder.alt.xu.min.svg @@ -1,4 +1,4 @@ - + @@ -35,10 +35,10 @@ - + - - + + @@ -93,15 +93,15 @@ - - - - - - - - - + + + + + + + + + @@ -109,23 +109,23 @@ - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + @@ -145,15 +145,15 @@ - - - - - - - - - + + + + + + + + + @@ -169,19 +169,19 @@ - - - - - - - - - - - - - + + + + + + + + + + + + + @@ -205,15 +205,15 @@ - - - - - - - - - + + + + + + + + + @@ -225,11 +225,7 @@ - - - - - + @@ -265,83 +261,83 @@ Sends script-coin-lock-tx - - - Wait for - - script-coin-lock-tx to - - confirm - - - Sends noscript-coin-lock-tx - - - Wait for - - noscript-coin-lock-tx to - - confirm - - - Wait for - - noscript-coin-lock-tx to - - confirm - - - Sends script-coin-lock-tx - - release message - - - Sends script-coin-lock-spend-tx - - - fail path - - - Wait for - - script-coin-lock-tx lock to - - expire - - - Sends - - script-coin-lock-pre-refund-tx - - - script-coin-lock-pre-refund-tx - - - Wait for - - pre-refund tx to confirm - - - Sends - - script-coin-lock-pre-refund-spend-tx - - - Detects script-coin-lock-pre-refund-spend-tx - - - Sends scriptless-coin-lock-recover-tx - - - bidder swipes script coin lock tx - - - Wait for - - pre-refund tx lock to expire - - - Sends script-coin-lock-pre-refund-swipe-tx + + + Wait for + + script-coin-lock-tx to + + confirm + + + Sends noscript-coin-lock-tx + + + Wait for + + noscript-coin-lock-tx to + + confirm + + + Wait for + + noscript-coin-lock-tx to + + confirm + + + Sends script-coin-lock-tx + + release message + + + Sends script-coin-lock-spend-tx + + + fail path + + + Wait for + + script-coin-lock-tx lock to + + expire + + + Sends + + script-coin-lock-pre-refund-tx + + + script-coin-lock-pre-refund-tx + + + Wait for + + pre-refund tx to confirm + + + Sends + + script-coin-lock-pre-refund-spend-tx + + + Detects script-coin-lock-pre-refund-spend-tx + + + Sends scriptless-coin-lock-recover-tx + + + bidder swipes script coin lock tx + + + Wait for + + pre-refund tx lock to expire + + + Sends script-coin-lock-pre-refund-swipe-tx @@ -367,48 +363,45 @@ the offerer's signature for it. Bid Script coin spend tx valid - - Exchanged script lock spend tx - msg - - Bid Script coin locked - - alt: success path - - Bid Scriptless coin locked - - The XmrBidLockReleaseMessage contains the offerer's OTVES for it. - The bidder decodes the offerer's signature - from the OTVES. When the offerer has the - plaintext signature, they can decode the bidder's noscript-coin-lock-tx - signature. - - Script coin lock released + + Bid Script coin locked + + alt: success path + + Bid Scriptless coin locked + + The XmrBidLockReleaseMessage contains the offerer's OTVES for it. + The bidder decodes the offerer's signature + from the OTVES. When the offerer has the + plaintext signature, they can decode the bidder's noscript-coin-lock-tx + signature. + + Script coin lock released + + Script tx redeemed - Script tx redeemed - - Bid Completed - - tx can be sent by either party. - - Bid Script pre-refund tx in - chain - - alt: offerer refunds script coin lock tx - - Refunds the script lock tx, with the offerer's cleartext signature - the bidder can refund the noscript lock tx. - Once the lock expires the pre-refund tx can be spent by the bidder. - - Bid Failed, refunded - - Bidder recovers the offerer's scriptless chain key-shard. + Bid Completed + + tx can be sent by either party. + + Bid Script pre-refund tx in + chain + + alt: offerer refunds script coin lock tx + + Refunds the script lock tx, with the offerer's cleartext signature + the bidder can refund the noscript lock tx. + Once the lock expires the pre-refund tx can be spent by the bidder. + + Bid Failed, refunded + + Bidder recovers the offerer's scriptless chain key-shard. + + Bid Scriptless tx recovered - Bid Scriptless tx recovered - - Bid Failed, refunded - - Bid Failed, swiped + Bid Failed, refunded + + Bid Failed, swiped diff --git a/doc/protocols/sequence_diagrams/bidder.alt.xu b/doc/protocols/sequence_diagrams/bidder.alt.xu index b0f2e6a..ac77e04 100644 --- a/doc/protocols/sequence_diagrams/bidder.alt.xu +++ b/doc/protocols/sequence_diagrams/bidder.alt.xu @@ -39,11 +39,11 @@ xu { B => B [label="Wait for PTX to confirm"], O => O [label="Wait for PTX to confirm"]; B abox B [label="PTX Confirmed", textbgcolor="#f1db4b"]; B abox B [label="Bid Participating"]; - O =>> N [label="Sends Participate Redeem Tx"], - C note C2 - [label="Reveals secret_value", - textbgcolor="#FFFFCC"]; CB alt C [label="success path"] { + O =>> N [label="Sends Participate Redeem Tx"], + C note C2 + [label="Reveals secret_value", + textbgcolor="#FFFFCC"]; N >> B [label="Detects Participate Redeem Tx"]; B abox B [label="PTX Redeemed", textbgcolor="#f1db4b"]; B =>> N [label="Sends Initiate Redeem Tx"]; @@ -51,10 +51,17 @@ xu { B abox B [label="ITX Redeemed", textbgcolor="#4bdbf1"]; B abox B [label="Bid Completed"]; --- [label="fail path"]; + CB alt C [label="offerer may reclaim ITX"] { + O => O [label="Wait for ITX locktime to expire"]; + O =>> N [label="ITX Refund Tx"]; + N >> B [label="Detects Initiate Tx refund Tx"]; + B abox B [label="ITX Refunded", textbgcolor="#4bdbf1"]; + }; B => B [label="Wait for PTX locktime to expire"]; B =>> N [label="PTX Refund Tx"]; B => B [label="Wait for PTX Refund to confirm"]; B abox B [label="PTX Refunded", textbgcolor="#f1db4b"]; + B abox B [label="Bid Completed"]; }; } diff --git a/tests/basicswap/common.py b/tests/basicswap/common.py index a9607be..1f3c73c 100644 --- a/tests/basicswap/common.py +++ b/tests/basicswap/common.py @@ -363,15 +363,19 @@ def extract_states_from_xu_file(file_path): return states -def compare_bid_states(states, expect_states): +def compare_bid_states(states, expect_states, exact_match=True): for i in range(len(states) - 1, -1, -1): if states[i][1] == 'Bid Delaying': del states[i] - assert(len(states) == len(expect_states)) + if exact_match: + assert(len(states) == len(expect_states)) + else: + assert(len(states) >= len(expect_states)) - for i, s in enumerate(states): + for i in range(len(expect_states)): + s = states[i] if s[1] != expect_states[i]: if 'Bid ' + expect_states[i] == s[1]: logging.warning(f'Expected state {expect_states[i]} not an exact match to {s[1]}.') diff --git a/tests/basicswap/test_run.py b/tests/basicswap/test_run.py index 320f129..4122e99 100644 --- a/tests/basicswap/test_run.py +++ b/tests/basicswap/test_run.py @@ -228,6 +228,14 @@ class Test(BaseTest): 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) + bid_id_hex = bid_id.hex() + path = f'bids/{bid_id_hex}/states' + offerer_states = read_json_api(1800, path) + bidder_states = read_json_api(1801, path) + + assert(compare_bid_states(offerer_states, self.states_offerer[1]) is True) + assert(bidder_states[-1][1] == 'Bid Abandoned') + def test_06_self_bid(self): logging.info('---------- Test same client, BTC to LTC') swap_clients = self.swap_clients @@ -329,6 +337,14 @@ class Test(BaseTest): 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) + bid_id_hex = bid_id.hex() + path = f'bids/{bid_id_hex}/states' + offerer_states = read_json_api(1800, path) + bidder_states = read_json_api(1801, path) + + assert(compare_bid_states(offerer_states, self.states_offerer[1]) is True) + assert(compare_bid_states(bidder_states, self.states_bidder[1]) is True) + ''' def test_11_refund(self): # Seller submits initiate txn, buyer doesn't respond, repeat of test 5 using debug_ind @@ -389,7 +405,7 @@ class Test(BaseTest): swap_value = make_int(random.uniform(2.0, 20.0), scale=8, r=1) logging.info('swap_value {}'.format(format_amount(swap_value, 8))) offer_id = swap_clients[0].postOffer(Coins.LTC, Coins.BTC, swap_value, 0.5 * COIN, swap_value, SwapTypes.SELLER_FIRST, - TxLockTypes.SEQUENCE_LOCK_BLOCKS, 10) + TxLockTypes.SEQUENCE_LOCK_BLOCKS, 16) wait_for_offer(test_delay_event, swap_clients[1], offer_id) offer = swap_clients[1].getOffer(offer_id) @@ -425,6 +441,16 @@ class Test(BaseTest): assert(node1_btc_after < node1_btc_before - btc_swap_value) assert(node1_ltc_before == node1_ltc_after) + wait_for_bid(test_delay_event, swap_clients[0], bid_id, BidStates.SWAP_COMPLETED, wait_for=60) + + bid_id_hex = bid_id.hex() + path = f'bids/{bid_id_hex}/states' + offerer_states = read_json_api(1800, path) + bidder_states = read_json_api(1801, path) + + assert(compare_bid_states(offerer_states, self.states_offerer[2]) is True) + assert(compare_bid_states(bidder_states, self.states_bidder[2], exact_match=False) is True) + def pass_99_delay(self): logging.info('Delay') for i in range(60 * 10):