timeout bids before the script coin lock tx is mined.

This commit is contained in:
tecnovert
2025-09-14 03:08:59 +02:00
parent 918bf60200
commit 4ebb6d6441
6 changed files with 258 additions and 33 deletions

View File

@@ -619,12 +619,9 @@ class TestFunctions(BaseTest):
assert node1_to_before - node1_to_after < max_fee_to
def do_test_05_self_bid(self, coin_from, coin_to):
logging.info(
"---------- Test {} to {} same client".format(coin_from.name, coin_to.name)
)
logging.info(f"---------- Test {coin_from.name} to {coin_to.name} Same Client")
id_both: int = self.node_b_id
swap_clients = self.swap_clients
ci_from = swap_clients[id_both].ci(coin_from)
ci_to = swap_clients[id_both].ci(coin_to)
@@ -653,10 +650,9 @@ class TestFunctions(BaseTest):
def do_test_08_insufficient_funds(self, coin_from, coin_to):
logging.info(
"---------- Test {} to {} Insufficient Funds".format(
coin_from.name, coin_to.name
)
f"---------- Test {coin_from.name} to {coin_to.name} Insufficient Funds"
)
swap_clients = self.swap_clients
reverse_bid: bool = swap_clients[0].is_reverse_ads_bid(coin_from, coin_to)
@@ -775,6 +771,144 @@ class TestFunctions(BaseTest):
else:
assert False, "Should fail"
def do_test_09_expire_accepted(self, coin_from, coin_to):
logging.info(
f"---------- Test {coin_from.name} to {coin_to.name} Expire Accepted"
)
swap_clients = self.swap_clients
reverse_bid: bool = swap_clients[0].is_reverse_ads_bid(coin_from, coin_to)
id_offerer: int = self.node_a_id
id_bidder: int = self.node_b_id
# Leader sends the initial (chain a) lock tx.
# Follower sends the participate (chain b) lock tx.
id_leader: int = id_bidder if reverse_bid else id_offerer
id_follower: int = id_offerer if reverse_bid else id_bidder
swap_clients = self.swap_clients
reverse_bid: bool = swap_clients[0].is_reverse_ads_bid(coin_from, coin_to)
ci_from = swap_clients[id_offerer].ci(coin_from)
ci_to = swap_clients[id_bidder].ci(coin_to)
self.prepare_balance(
coin_from, 100.0, 1800 + id_offerer, 1801 if reverse_bid else 1800
)
amt_swap = ci_from.make_int(random.uniform(0.1, 2.0), r=1)
rate_swap = ci_to.make_int(random.uniform(0.2, 20.0), r=1)
offer_id = swap_clients[id_offerer].postOffer(
coin_from, coin_to, amt_swap, rate_swap, amt_swap, SwapTypes.XMR_SWAP
)
wait_for_offer(test_delay_event, swap_clients[id_bidder], offer_id)
offer = swap_clients[id_bidder].listOffers(filters={"offer_id": offer_id})[0]
bid_id = swap_clients[id_bidder].postXmrBid(offer_id, offer.amount_from)
wait_for_bid(
test_delay_event,
swap_clients[id_offerer],
bid_id,
BidStates.BID_RECEIVED,
wait_for=(self.extra_wait_time + 40),
)
try:
# Stop BTC mining
old_btc_addr: str = self.__class__.btc_addr
self.__class__.btc_addr = None
old_check_expired_seconds = swap_clients[0].check_expired_seconds
swap_clients[id_offerer].acceptBid(bid_id)
wait_for_event(
test_delay_event,
swap_clients[id_follower],
Concepts.BID,
bid_id,
event_type=EventLogTypes.LOCK_TX_A_IN_MEMPOOL,
wait_for=90,
)
post_json = {"show_extra": True}
bid = read_json_api(1800 + id_leader, f"bids/{bid_id.hex()}", post_json)
chain_a_lock_txid = None
for tx in bid["txns"]:
if tx["type"] == "Chain A Lock":
chain_a_lock_txid = tx["txid"]
assert chain_a_lock_txid
ci_btc_l = swap_clients[id_leader].ci(Coins.BTC)
rv = ci_btc_l.rpc_wallet(
"psbtbumpfee",
[
chain_a_lock_txid,
],
)
rv = ci_btc_l.rpc_wallet(
"walletprocesspsbt",
[
rv["psbt"],
],
)
rv = ci_btc_l.rpc_wallet(
"finalizepsbt",
[
rv["psbt"],
],
)
new_tx_hex = rv["hex"]
rv = ci_btc_l.rpc_wallet(
"sendrawtransaction",
[
new_tx_hex,
],
)
assert rv != chain_a_lock_txid
wait_for_event(
test_delay_event,
swap_clients[id_follower],
Concepts.BID,
bid_id,
event_type=EventLogTypes.LOCK_TX_A_CONFLICTS,
wait_for=90,
)
# Mine the replacement tx
ci_btc_l.rpc_wallet("generatetoaddress", [1, old_btc_addr])
swap_clients[0].setMockTimeOffset(13 * 3600)
swap_clients[1].setMockTimeOffset(13 * 3600)
swap_clients[0].check_expired_seconds = 2
swap_clients[1].check_expired_seconds = 2
wait_for_bid(
test_delay_event,
swap_clients[id_follower],
bid_id,
BidStates.SWAP_TIMEDOUT,
sent=None,
wait_for=(self.extra_wait_time + 40),
)
# Leader (which funded the lock tx) should not timeout.
wait_for_bid(
test_delay_event,
swap_clients[id_leader],
bid_id,
BidStates.XMR_SWAP_MSG_SCRIPT_LOCK_SPEND_TX,
sent=None,
wait_for=(self.extra_wait_time + 40),
)
finally:
# Restore BTC mining:
self.__class__.btc_addr = old_btc_addr
swap_clients[0].setMockTimeOffset(0)
swap_clients[1].setMockTimeOffset(0)
swap_clients[0].check_expired_seconds = old_check_expired_seconds
swap_clients[1].check_expired_seconds = old_check_expired_seconds
class BasicSwapTest(TestFunctions):
@@ -2155,6 +2289,12 @@ class BasicSwapTest(TestFunctions):
def test_08_insufficient_funds_rev(self):
self.do_test_08_insufficient_funds(Coins.XMR, self.test_coin_from)
def test_09_expire_accepted(self):
self.do_test_09_expire_accepted(self.test_coin_from, Coins.XMR)
def test_09_expire_accepted_rev(self):
self.do_test_09_expire_accepted(Coins.XMR, self.test_coin_from)
class TestBTC(BasicSwapTest):
__test__ = True