Skip to content

Commit 8d8f5f3

Browse files
committed
regtests: add test "swapserver_server_skip_onchain_funding"
trying to reproduce #9980
1 parent 3f755e1 commit 8d8f5f3

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

electrum/simple_config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,7 @@ def __setattr__(self, name, value):
891891
# connect to remote submarine swap server
892892
SWAPSERVER_URL = ConfigVar('swapserver_url', default='', type_=str)
893893
TEST_SWAPSERVER_REFUND = ConfigVar('test_swapserver_refund', default=False, type_=bool)
894+
TEST_SWAPSERVER_SKIP_ONCHAIN_FUNDING = ConfigVar('test_swapserver_skip_onchain_funding', default=False, type_=bool)
894895
SWAPSERVER_NPUB = ConfigVar('swapserver_npub', default=None, type_=str)
895896
SWAPSERVER_POW_TARGET = ConfigVar('swapserver_pow_target', default=30, type_=int)
896897

electrum/submarine_swaps.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,8 @@ async def hold_invoice_callback(self, payment_hash: bytes) -> None:
512512
key = payment_hash.hex()
513513
if swap := self._swaps.get(key):
514514
if not swap.is_funded():
515+
if self.network.config.TEST_SWAPSERVER_SKIP_ONCHAIN_FUNDING: # for testing
516+
return
515517
output = self.create_funding_output(swap)
516518
self.wallet.txbatcher.add_payment_output('swaps', output)
517519
swap._payment_pending = True

tests/regtest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ def test_swapserver_forceclose(self):
107107
def test_swapserver_refund(self):
108108
self.run_shell(['swapserver_refund'])
109109

110+
def test_swapserver_server_skip_onchain_funding(self):
111+
self.run_shell(['swapserver_server_skip_onchain_funding'])
112+
110113

111114

112115
class TestLightningWatchtower(TestLightning):

tests/regtest/regtest.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,49 @@ if [[ $1 == "swapserver_refund" ]]; then
308308
fi
309309

310310

311+
if [[ $1 == "swapserver_server_skip_onchain_funding" ]]; then
312+
# Alice starts reverse-swap with Bob.
313+
# Alice sends hold-HTLCs via LN. Bob does NOT fund locking script onchain.
314+
# After a while, Alice requests Bob to force-close chan, and Bob does.
315+
# Alice will broadcast HTLC-timeout tx to reclaim the swap amount from Bob's commitment tx.
316+
$bob setconfig test_swapserver_skip_onchain_funding true
317+
wait_for_balance alice 1
318+
echo "alice opens channel"
319+
bob_node=$($bob nodeid)
320+
channel=$($alice open_channel $bob_node 0.15 --password='')
321+
chan_funding_txid=$(echo "$channel" | cut -d ":" -f 1)
322+
chan_funding_outidx=$(echo "$channel" | cut -d ":" -f 2)
323+
new_blocks 3
324+
wait_until_channel_open alice
325+
echo "alice initiates swap"
326+
dryrun=$($alice reverse_swap 0.02 dryrun)
327+
onchain_amount=$(echo $dryrun| jq -r ".onchain_amount")
328+
# Alice starts a reverse-swap, but will time out waiting for Bob's swap-funding-tx to appear in mempool.
329+
$alice setconfig timeout 10
330+
set +e
331+
swap=$($alice reverse_swap 0.02 $onchain_amount)
332+
set -e
333+
$alice unsetconfig timeout
334+
# After a while, Alice gets impatient and gets Bob to close the channel.
335+
new_blocks 20
336+
$alice request_force_close $channel
337+
wait_until_spent $chan_funding_txid $chan_funding_outidx
338+
new_blocks 1
339+
wait_until_channel_closed alice
340+
ctx_id=$($alice list_channels | jq -r ".[0].closing_txid")
341+
# need more blocks to reach CLTV of HTLC-output in ctx
342+
new_blocks 130
343+
if [ $TEST_ANCHOR_CHANNELS = True ] ; then
344+
htlc_output_index=3 # FIXME index depends on Alice not using MPP
345+
else
346+
htlc_output_index=1
347+
fi
348+
wait_until_spent $ctx_id $htlc_output_index
349+
new_blocks 1
350+
wait_for_balance alice 0.997
351+
fi
352+
353+
311354
if [[ $1 == "extract_preimage" ]]; then
312355
# Alice sends htlc1 to Bob. Bob sends htlc2 to Alice.
313356
# Neither one of them settles, they hold the htlcs, and Bob force-closes.

0 commit comments

Comments
 (0)