Skip to content

Commit d3f04a9

Browse files
committed
regtests: add test "swapserver_server_skip_onchain_funding"
related #9980
1 parent 8c00e4d commit d3f04a9

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
@@ -515,6 +515,8 @@ async def hold_invoice_callback(self, payment_hash: bytes) -> None:
515515
key = payment_hash.hex()
516516
if swap := self._swaps.get(key):
517517
if not swap.is_funded():
518+
if self.network.config.TEST_SWAPSERVER_SKIP_ONCHAIN_FUNDING: # for testing
519+
return
518520
output = self.create_funding_output(swap)
519521
self.wallet.txbatcher.add_payment_output('swaps', output)
520522
swap._payment_pending = True

tests/regtest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ def test_swapserver_forceclose(self):
110110
def test_swapserver_refund(self):
111111
self.run_shell(['swapserver_refund'])
112112

113+
def test_swapserver_server_skip_onchain_funding(self):
114+
self.run_shell(['swapserver_server_skip_onchain_funding'])
115+
113116

114117

115118
class TestLightningWatchtower(TestLightning):

tests/regtest/regtest.sh

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

319319

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

0 commit comments

Comments
 (0)