From 8dbddedd3060ac4a81b7f6e29fb8212436093e86 Mon Sep 17 00:00:00 2001 From: Sander van Grieken Date: Mon, 18 Sep 2023 15:36:10 +0200 Subject: [PATCH] submarine swaps: register hold invoice early in wait_for_htlcs_and_broadcast, so a SwapManager.cancel_normal_swap() does not race. cancel_normal_swap unregisters the hold invoice, which can occur before register_hold_invoice in wait_for_htlcs_and_broadcast if the network call has not returned yet. --- electrum/submarine_swaps.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/electrum/submarine_swaps.py b/electrum/submarine_swaps.py index 58785e7e9..77a235422 100644 --- a/electrum/submarine_swaps.py +++ b/electrum/submarine_swaps.py @@ -674,6 +674,11 @@ class SwapManager(Logger): payment_hash = swap.payment_hash refund_pubkey = ECPrivkey(swap.privkey).get_public_key_bytes(compressed=True) if self.server_supports_htlc_first: + async def callback(payment_hash): + await self.broadcast_funding_tx(swap, tx) + + self.lnworker.register_hold_invoice(payment_hash, callback) + # send invoice to server and wait for htlcs request_data = { "preimageHash": payment_hash.hex(), @@ -686,9 +691,6 @@ class SwapManager(Logger): json=request_data, timeout=30) data = json.loads(response) - async def callback(payment_hash): - await self.broadcast_funding_tx(swap, tx) - self.lnworker.register_hold_invoice(payment_hash, callback) # wait for funding tx lnaddr = lndecode(invoice) while swap.funding_txid is None and not lnaddr.is_expired(): @@ -739,7 +741,7 @@ class SwapManager(Logger): lightning_amount_sat: int, expected_onchain_amount_sat: int, channels = None, - ) -> bool: + ) -> Optional[str]: """send on Lightning, receive on-chain - User generates preimage, RHASH. Sends RHASH to server.