Debian Codegraph @

tests/regtest/regtest.sh

raw symbols SCIP
1 #!/usr/bin/env bash
2 export HOME=~
3 set -eu
4
5 TEST_SRK_CHANNELS=False
6
7 # alice -> bob -> carol
8
9 alice="./run_electrum --regtest -D /tmp/alice"
10 bob="./run_electrum --regtest -D /tmp/bob"
11 carol="./run_electrum --regtest -D /tmp/carol"
12
13 bitcoin_cli="bitcoin-cli -rpcuser=doggman -rpcpassword=donkey -rpcport=18554 -regtest"
14
15 function new_blocks()
16 {
17 printf "mining $1 blocks\n"
18 $bitcoin_cli generatetoaddress $1 $($bitcoin_cli getnewaddress) > /dev/null
19 }
20
21 function wait_until_htlcs_settled()
22 {
23 msg="wait until $1's local_unsettled_sent is zero"
24 cmd="./run_electrum --regtest -D /tmp/$1"
25 declare -i timeout_sec=120
26 declare -i elapsed_sec=0
27
28 while unsettled=$($cmd list_channels | jq '.[] | .local_unsettled_sent') && [ $unsettled != "0" ]; do
29 if ((elapsed_sec > timeout_sec)); then
30 printf "Timeout of %i s exceeded\n" "$elapsed_sec"
31 exit 1
32 fi
33
34 sleep 1
35 elapsed_sec=$((elapsed_sec + 1))
36 msg="$msg."
37 printf "$msg\r"
38 done
39 printf "\n"
40 }
41
42
43 function wait_for_balance()
44 {
45 msg="wait until $1's balance reaches $2"
46 cmd="./run_electrum --regtest -D /tmp/$1"
47 declare -i timeout_sec=120
48 declare -i elapsed_sec=0
49
50 while balance=$($cmd getbalance | jq '[.confirmed, .unconfirmed] | to_entries | map(select(.value != null).value) | map(tonumber) | add ') && (( $(echo "$balance < $2" | bc -l) )); do
51 if ((elapsed_sec > timeout_sec)); then
52 printf "Timeout of %i s exceeded\n" "$elapsed_sec"
53 exit 1
54 fi
55
56 sleep 1
57 elapsed_sec=$((elapsed_sec + 1))
58 msg="$msg."
59 printf "$msg\r"
60 done
61 printf "\n"
62 }
63
64 function wait_until_channel_open()
65 {
66 msg="wait until $1 sees channel open"
67 cmd="./run_electrum --regtest -D /tmp/$1"
68 declare -i timeout_sec=120
69 declare -i elapsed_sec=0
70
71 while channel_state=$($cmd list_channels | jq '.[0] | .state' | tr -d '"') && [ $channel_state != "OPEN" ]; do
72 if ((elapsed_sec > timeout_sec)); then
73 printf "Timeout of %i s exceeded\n" "$elapsed_sec"
74 exit 1
75 fi
76
77 sleep 1
78 elapsed_sec=$((elapsed_sec + 1))
79 msg="$msg."
80 printf "$msg\r"
81 done
82 printf "\n"
83 }
84
85 function wait_until_channel_closed()
86 {
87 msg="wait until $1 sees channel closed"
88 cmd="./run_electrum --regtest -D /tmp/$1"
89 declare -i timeout_sec=120
90 declare -i elapsed_sec=0
91
92 while [[ $($cmd list_channels | jq '.[0].state' | tr -d '"') != "CLOSED" ]]; do
93 if ((elapsed_sec > timeout_sec)); then
94 printf "Timeout of %i s exceeded\n" "$elapsed_sec"
95 exit 1
96 fi
97
98 sleep 1
99 elapsed_sec=$((elapsed_sec + 1))
100 msg="$msg."
101 printf "$msg\r"
102 done
103 printf "\n"
104 }
105
106 function wait_until_preimage()
107 {
108 msg="wait until $1 has preimage for $2"
109 cmd="./run_electrum --regtest -D /tmp/$1"
110 declare -i timeout_sec=120
111 declare -i elapsed_sec=0
112
113 while [[ $($cmd get_invoice $2 | jq '.preimage' | tr -d '"') == "null" ]]; do
114 if ((elapsed_sec > timeout_sec)); then
115 printf "Timeout of %i s exceeded\n" "$elapsed_sec"
116 exit 1
117 fi
118
119 sleep 1
120 elapsed_sec=$((elapsed_sec + 1))
121 msg="$msg."
122 printf "$msg\r"
123 done
124 printf "\n"
125 }
126
127 function wait_until_spent()
128 {
129 msg="wait until $1:$2 is spent"
130 declare -i timeout_sec=120
131 declare -i elapsed_sec=0
132
133 while true; do
134 utxo=$($bitcoin_cli gettxout $1 $2)
135 if [[ -z "$utxo" ]]; then break; fi # utxo is spent (or never existed!)
136 if ((elapsed_sec > timeout_sec)); then
137 printf "Timeout of %i s exceeded\n" "$elapsed_sec"
138 exit 1
139 fi
140
141 sleep 1
142 elapsed_sec=$((elapsed_sec + 1))
143 msg="$msg."
144 printf "$msg\r"
145 done
146 printf "\n"
147 }
148
149 function wait_for_chain_tip()
150 {
151 msg="waiting until $1 catches up to chain tip"
152 cmd="./run_electrum --regtest -D /tmp/$1"
153 declare -i timeout_sec=120
154 declare -i elapsed_sec=0
155
156 printf "$msg"
157 while (( $($cmd getinfo | jq '.blockchain_height') < $($bitcoin_cli getblockcount) )); do
158 if ((elapsed_sec > timeout_sec)); then
159 printf "Timeout of %i s exceeded\n" "$elapsed_sec"
160 exit 1
161 fi
162 sleep 1
163 elapsed_sec=$((elapsed_sec + 1))
164 printf '.'
165 done
166
167 $cmd wait_for_sync > /dev/null
168 printf "\n"
169 }
170
171 function assert_utxo_exists()
172 {
173 utxo=$($bitcoin_cli gettxout $1 $2)
174 if [[ -z "$utxo" ]]; then
175 echo "utxo $1:$2 does not exist"
176 exit 1
177 fi
178 }
179
180 if [[ $# -eq 0 ]]; then
181 echo "syntax: init|start|open|status|pay|close|stop"
182 exit 1
183 fi
184
185 if [[ $1 == "new_block" ]]; then
186 new_blocks 1
187 fi
188
189 if [[ $1 == "init" ]]; then
190 echo "initializing $2"
191 rm -rf /tmp/$2/
192 agent="./run_electrum --regtest -D /tmp/$2"
193 $agent create --offline > /dev/null
194 $agent setconfig --offline test_ln_open_srk_channels $TEST_SRK_CHANNELS
195 $agent setconfig --offline log_to_file True
196 $agent setconfig --offline use_gossip True
197 $agent setconfig --offline server 127.0.0.1:51001:t
198 $agent setconfig --offline lightning_to_self_delay 144
199 $agent setconfig --offline test_force_disable_mpp True
200 echo "funding $2"
201 # note: changing the funding amount affects all tests, as they rely on "wait_for_balance"
202 $bitcoin_cli sendtoaddress $($agent getunusedaddress -o -w "/tmp/$2/regtest/wallets/default_wallet") 1
203 fi
204
205 if [[ $1 == "setconfig" ]]; then
206 # use this to set config vars that need to be set before the daemon is started
207 agent="./run_electrum --regtest -D /tmp/$2"
208 $agent setconfig --offline $3 $4
209 fi
210
211 # start daemons. Bob is started first because he is listening
212 if [[ $1 == "start" ]]; then
213 agent="./run_electrum --regtest -D /tmp/$2"
214 $agent daemon -d
215 $agent load_wallet
216 $agent wait_for_sync
217 fi
218
219 if [[ $1 == "stop" ]]; then
220 agent="./run_electrum --regtest -D /tmp/$2"
221 $agent stop || true
222 fi
223
224
225 # alice sends two payments, then broadcast ctx after first payment.
226 # thus, bob needs to redeem both to_local and to_remote
227
228
229 if [[ $1 == "breach" ]]; then
230 wait_for_balance alice 1
231 echo "alice opens channel"
232 bob_node=$($bob nodeid)
233 channel=$($alice open_channel $bob_node 0.15 --password='')
234 new_blocks 3
235 wait_until_channel_open alice
236 request=$($bob add_request 0.01 --lightning | jq -r ".lightning_invoice")
237 echo "alice pays"
238 $alice lnpay $request
239 sleep 2
240 ctx=$($alice get_channel_ctx $channel --iknowwhatimdoing)
241 request=$($bob add_request 0.01 --lightning | jq -r ".lightning_invoice")
242 echo "alice pays again"
243 $alice lnpay $request
244 echo "alice broadcasts old ctx"
245 $bitcoin_cli sendrawtransaction $ctx
246 new_blocks 1
247 wait_until_channel_closed bob
248 new_blocks 1
249 wait_for_balance bob 1.14
250 $bob getbalance
251 fi
252
253
254 if [[ $1 == "backup" ]]; then
255 # Alice has two channels with Bob.
256 # - chan1 has on-chain op_return backups,
257 # - chan2 has an imported backup.
258 # Alice restores from seed, and also imports backup for chan2.
259 # Test "request_force_close" works for both channels.
260 wait_for_balance alice 1
261 echo "alice opens channel"
262 bob_node=$($bob nodeid)
263 channel1=$($alice open_channel $bob_node 0.15 --password='')
264 new_blocks 1 # cannot open multiple chans with same node in same block
265 $alice setconfig use_recoverable_channels False
266 channel2=$($alice open_channel $bob_node 0.15 --password='')
267 new_blocks 3
268 wait_until_channel_open alice # FIXME wait for *both* channels?
269 backup=$($alice export_channel_backup $channel2)
270 seed=$($alice getseed --password='')
271 $alice stop
272 mv /tmp/alice/regtest/wallets/default_wallet /tmp/alice/regtest/wallets/default_wallet.old
273 $alice -o restore "$seed"
274 $alice daemon -d
275 $alice load_wallet
276 $alice import_channel_backup $backup
277 $alice wait_for_sync
278 echo "request force close $channel1"
279 $alice request_force_close $channel1
280 echo "request force close $channel2"
281 $alice request_force_close $channel2
282 new_blocks 1
283 wait_for_balance alice 0.997
284 fi
285
286
287 if [[ $1 == "backup_local_forceclose" ]]; then
288 # Alice does a local-force-close, and then restores from seed before sweeping CSV-locked coins
289 wait_for_balance alice 1
290 echo "alice opens channel"
291 bob_node=$($bob nodeid)
292 $alice setconfig use_recoverable_channels False
293 channel=$($alice open_channel $bob_node 0.15 --password='')
294 new_blocks 3
295 wait_until_channel_open alice
296 backup=$($alice export_channel_backup $channel)
297 echo "local force close $channel"
298 $alice close_channel $channel --force
299 sleep 0.5
300 seed=$($alice getseed --password='')
301 $alice stop
302 mv /tmp/alice/regtest/wallets/default_wallet /tmp/alice/regtest/wallets/default_wallet.old
303 new_blocks 150
304 $alice -o restore "$seed"
305 $alice daemon -d
306 $alice load_wallet
307 $alice import_channel_backup $backup
308 wait_for_balance alice 0.998
309 fi
310
311
312 if [[ $1 == "collaborative_close" ]]; then
313 wait_for_balance alice 1
314 echo "alice opens channel"
315 bob_node=$($bob nodeid)
316 channel=$($alice open_channel $bob_node 0.15 --password='')
317 new_blocks 3
318 wait_until_channel_open alice
319 echo "alice closes channel"
320 request=$($bob close_channel $channel)
321 fi
322
323
324 if [[ $1 == "swapserver_success_reverse" ]]; then
325 wait_for_balance alice 1
326 echo "alice opens channel"
327 bob_node=$($bob nodeid)
328 channel=$($alice open_channel $bob_node 0.15 --password='')
329 new_blocks 3
330 wait_until_channel_open alice
331 echo "alice initiates reverse-swap"
332 dryrun=$($alice reverse_swap 0.02 dryrun)
333 onchain_amount=$(echo $dryrun| jq -r ".onchain_amount")
334 prepayment=$(echo $dryrun| jq -r ".prepayment")
335 swap=$($alice reverse_swap 0.02 $onchain_amount --prepayment $prepayment)
336 echo $swap | jq
337 funding_txid=$(echo $swap| jq -r ".funding_txid")
338 assert_utxo_exists $funding_txid 0
339 new_blocks 1
340 wait_until_spent $funding_txid 0
341 wait_until_htlcs_settled alice
342 fi
343
344
345 if [[ $1 == "swapserver_success_forward" ]]; then
346 wait_for_balance alice 1
347 echo "alice opens channel"
348 bob_node=$($bob nodeid)
349 channel=$($alice open_channel $bob_node 0.15 --password='' --push_amount=0.075)
350 new_blocks 3
351 wait_until_channel_open alice
352 echo "alice initiates forward-swap"
353 dryrun=$($alice normal_swap 0.02 dryrun)
354 lightning_amount=$(echo $dryrun| jq -r ".lightning_amount")
355 swap=$($alice normal_swap 0.02 $lightning_amount)
356 echo $swap | jq
357 funding_txid=$(echo $swap| jq -r ".txid")
358 assert_utxo_exists $funding_txid 0
359 new_blocks 1
360 wait_until_spent $funding_txid 0
361 wait_until_htlcs_settled bob
362 fi
363
364
365 if [[ $1 == "swapserver_forceclose" ]]; then
366 # Alice starts reverse-swap with Bob.
367 # Alice sends hold-HTLCs via LN, Bob funds locking script onchain.
368 # Bob force-closes the channel, before swap-funding-tx gets mined.
369 # After swap-funding-tx gets mined, Alice broadcasts onchain claim tx, revealing preimage.
370 # Bob finds preimage onchain, and creates HTLC-success tx to spend own ctx htlc output onchain.
371 wait_for_balance alice 1
372 echo "alice opens channel"
373 bob_node=$($bob nodeid)
374 channel=$($alice open_channel $bob_node 0.15 --password='')
375 new_blocks 3
376 wait_until_channel_open alice
377 echo "alice initiates swap"
378 dryrun=$($alice reverse_swap 0.02 dryrun)
379 onchain_amount=$(echo $dryrun| jq -r ".onchain_amount")
380 prepayment=$(echo $dryrun| jq -r ".prepayment")
381 swap=$($alice reverse_swap 0.02 $onchain_amount --prepayment $prepayment)
382 echo $swap | jq
383 funding_txid=$(echo $swap| jq -r ".funding_txid")
384 ctx_id=$($bob close_channel --force $channel)
385 new_blocks 1
386 wait_until_spent $funding_txid 0 # alice reveals preimage
387 new_blocks 1
388 if [ $TEST_SRK_CHANNELS != True ] ; then # anchors
389 output_index=3 # received_htlc_output in bob's ctx. FIXME index depends on Alice not using MPP
390 else # srk
391 output_index=1
392 fi
393 # wait until Bob finds preimage onchain and uses it to create an htlc_success tx
394 wait_until_spent $ctx_id $output_index
395 new_blocks 144
396 wait_for_balance bob 0.999
397 # check that the closing tx is in alice's onchain_history. Since this tx does not
398 # touch alice's wallet addresses, this test requires accounting_addresses to be set
399 $alice stop
400 if [[ ! $($alice -o onchain_history| jq --arg txid $ctx_id '.[]|select(.txid == $txid)') ]]; then
401 echo "accounting_address not set"
402 exit 1
403 fi
404 fi
405
406
407 if [[ $1 == "swapserver_refund" ]]; then
408 # Alice starts reverse-swap with Bob.
409 # Alice sends hold-HTLCs via LN, Bob funds locking script onchain.
410 # Alice never broadcasts onchain claim tx. Bob will use timeout path onchain.
411 # Then Bob fails hold-HTLCs via LN.
412 # Channel stays open.
413 $alice setconfig test_swapserver_refund true
414 wait_for_balance alice 1
415 echo "alice opens channel"
416 bob_node=$($bob nodeid)
417 channel=$($alice open_channel $bob_node 0.15 --password='')
418 new_blocks 3
419 wait_until_channel_open alice
420 echo "alice initiates swap"
421 dryrun=$($alice reverse_swap 0.02 dryrun)
422 onchain_amount=$(echo $dryrun| jq -r ".onchain_amount")
423 prepayment=$(echo $dryrun| jq -r ".prepayment")
424 swap=$($alice reverse_swap 0.02 $onchain_amount --prepayment $prepayment)
425 echo $swap | jq
426 funding_txid=$(echo $swap| jq -r ".funding_txid")
427 # mine past LOCKTIME_DELTA_REFUND (70), so that bob can refund
428 new_blocks 75
429 wait_until_spent $funding_txid 0
430 # bob only fails the hold-HTLCs once his refund tx is final (SPENDER_FINALITY_DELAY)
431 new_blocks 8
432 wait_until_htlcs_settled alice
433 fi
434
435
436 if [[ $1 == "lnwatcher_waits_until_fees_go_down" ]]; then
437 # Alice sends two HTLCs to Bob (one for small invoice, one for large invoice), which Bob will hold.
438 # Alice requests Bob to force-close the channel, while the HTLCs are pending. Bob force-closes.
439 # Fee levels rise, to the point where the small HTLC is not economical to claim.
440 # Alice sweeps the large HTLC (via onchain timeout), but not the small one.
441 # Then, fee levels go back down, and Alice sweeps the small HTLC.
442 # This test checks Alice does not abandon channel outputs that are temporarily ~dust due to
443 # mempool spikes, and keeps watching the channel in hope of fees going down.
444 $alice setconfig test_force_disable_mpp true
445 $alice setconfig test_force_mpp false
446 wait_for_balance alice 1
447 $alice setconfig test_disable_automatic_fee_eta_update true
448 $alice test_inject_fee_etas "{2:1000}"
449 $bob test_inject_fee_etas "{2:1000}"
450 echo "alice opens channel"
451 bob_node=$($bob nodeid)
452 channel=$($alice open_channel $bob_node 0.15 --password='')
453 chan_funding_txid=$(echo "$channel" | cut -d ":" -f 1)
454 chan_funding_outidx=$(echo "$channel" | cut -d ":" -f 2)
455 new_blocks 3
456 wait_until_channel_open alice
457 # Alice sends an HTLC to Bob, which Bob will hold indefinitely. Alice's lnpay will time out.
458 invoice1=$($bob add_hold_invoice deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbee1 \
459 --amount 0.0004 --min_final_cltv_expiry_delta 300 | jq -r ".invoice")
460 invoice2=$($bob add_hold_invoice deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbee2 \
461 --amount 0.04 --min_final_cltv_expiry_delta 300 | jq -r ".invoice")
462 set +e
463 $alice lnpay $invoice1 --timeout 3
464 $alice lnpay $invoice2 --timeout 3
465 set -e
466 # After a while, Alice gets impatient and gets Bob to close the channel.
467 new_blocks 20
468 $alice request_force_close $channel
469 wait_until_spent $chan_funding_txid $chan_funding_outidx
470 $bob stop # bob closes and then disappears. FIXME this is a hack to prevent Bob claiming the fake-hold-invoice-htlc onchain
471 new_blocks 1
472 wait_until_channel_closed alice
473 ctx_id=$($alice list_channels | jq -r ".[0].closing_txid")
474 if [ $TEST_SRK_CHANNELS != True ] ; then # anchors
475 htlc_output_index1=2
476 htlc_output_index2=3
477 to_alice_index=4 # Bob's to_remote
478 wait_until_spent $ctx_id $to_alice_index
479 else # srk
480 htlc_output_index1=0
481 htlc_output_index2=1
482 to_alice_index=2
483 fi
484 new_blocks 1
485 assert_utxo_exists $ctx_id $htlc_output_index1
486 assert_utxo_exists $ctx_id $htlc_output_index2
487 # fee levels rise. now small htlc is ~dust
488 $alice test_inject_fee_etas "{2:300000}"
489 new_blocks 300 # this goes past the CLTV of the HTLC-output in ctx
490 wait_until_spent $ctx_id $htlc_output_index2
491 assert_utxo_exists $ctx_id $htlc_output_index1
492 new_blocks 24 # note: >20 blocks depth is considered "DEEP" by lnwatcher
493 sleep 1 # give time for Alice to make mistakes, such as abandoning the channel. which it should NOT do.
494 new_blocks 1
495 # Alice goes offline and comes back later, 1
496 $alice stop
497 $alice daemon -d
498 $alice test_inject_fee_etas "{2:300000}"
499 $alice load_wallet
500 $alice wait_for_sync
501 new_blocks 1
502 sleep 1 # give time for Alice to make mistakes
503 # Alice goes offline and comes back later, 2
504 $alice stop
505 $alice daemon -d
506 $alice test_inject_fee_etas "{2:300000}"
507 $alice load_wallet
508 $alice wait_for_sync
509 new_blocks 1
510 sleep 1 # give time for Alice to make mistakes
511 # fee levels go down. time to claim the small htlc
512 $alice test_inject_fee_etas "{2:1000}"
513 new_blocks 1
514 wait_until_spent $ctx_id $htlc_output_index1
515 new_blocks 1
516 wait_for_balance alice 0.9995
517 fi
518
519
520 if [[ $1 == "extract_preimage" ]]; then
521 # Alice sends htlc1 to Bob. Bob sends htlc2 to Alice.
522 # Neither one of them settles, they hold the htlcs, and Bob force-closes.
523 # Bob's ctx contains two htlc outputs: "received" htlc1, and "offered" htlc2.
524 # Bob also broadcasts an HTLC-success tx for received htlc1, revealing the preimage.
525 # Alice broadcasts a direct-spend of the offered htlc2, revealing the preimage.
526 # This test checks that
527 # - Alice successfully extracts the preimage for htlc1 from Bob's HTLC-success tx, and
528 # - Bob successfully extracts the preimage for htlc2 from Alice's direct spend tx
529 # note: actually, due to MPP, there will be more htlcs in the ctx:
530 # we force alice to use MPP, but force bob NOT to use MPP
531 $alice setconfig test_force_disable_mpp false
532 $alice setconfig test_force_mpp true
533 $bob setconfig test_force_disable_mpp true
534 $bob setconfig test_force_mpp false
535 $alice enable_htlc_settle false
536 $bob enable_htlc_settle false
537 wait_for_balance alice 1
538 echo "alice opens channel"
539 bob_node=$($bob nodeid)
540 $alice open_channel $bob_node 0.15 --password='' --push_amount=0.075
541 new_blocks 3
542 wait_until_channel_open alice
543 chan_id=$($alice list_channels | jq -r ".[0].channel_point")
544 # alice pays bob
545 request1=$($bob add_request 0.04 --lightning --memo "test1")
546 invoice1=$(echo $request1 | jq -r ".lightning_invoice")
547 rhash1=$(echo $request1 | jq -r ".rhash")
548 screen -S alice_payment -dm -L -Logfile /tmp/alice/screen1.log $alice lnpay $invoice1 --timeout=600
549 sleep 1
550 unsettled=$($alice list_channels | jq '.[] | .local_unsettled_sent')
551 if [[ "$unsettled" == "0" ]]; then
552 echo 'enable_htlc_settle did not work (bob settled)'
553 exit 1
554 fi
555 # bob pays alice
556 request2=$($alice add_request 0.04 --lightning --memo "test2")
557 invoice2=$(echo $request2 | jq -r ".lightning_invoice")
558 rhash2=$(echo $request2 | jq -r ".rhash")
559 screen -S bob_payment -dm -L -Logfile /tmp/bob/screen2.log $bob lnpay $invoice2 --timeout=600
560 sleep 1
561 unsettled=$($bob list_channels | jq '.[] | .local_unsettled_sent')
562 if [[ "$unsettled" == "0" ]]; then
563 echo 'enable_htlc_settle did not work (alice settled)'
564 exit 1
565 fi
566 # bob force closes
567 $bob close_channel $chan_id --force
568 new_blocks 1
569 wait_until_preimage alice $rhash1
570 wait_until_preimage bob $rhash2
571 while screen -ls | grep -q _payment; do sleep 1; done # wait until lnpay return before checking logs
572 # check both "lnpay" commands succeeded
573 success=$(cat /tmp/alice/screen1.log | jq -r ".success")
574 if [[ "$success" != "true" ]]; then echo "alice payment failed"; exit 1; fi
575 success=$(cat /tmp/bob/screen2.log | jq -r ".success")
576 if [[ "$success" != "true" ]]; then echo "bob payment failed"; exit 1; fi
577 cat /tmp/alice/screen1.log
578 cat /tmp/bob/screen2.log
579 fi
580
581
582 if [[ $1 == "redeem_offered_htlcs" ]]; then
583 # alice force closes and redeems using htlc timeout
584 $bob enable_htlc_settle false
585 wait_for_balance alice 1
586 echo "alice opens channel"
587 bob_node=$($bob nodeid)
588 $alice open_channel $bob_node 0.15 --password=''
589 new_blocks 3
590 wait_until_channel_open alice
591 # alice pays bob
592 invoice=$($bob add_request 0.04 --lightning --memo "test" | jq -r ".lightning_invoice")
593 $alice lnpay $invoice --timeout=1 || true
594 unsettled=$($alice list_channels | jq '.[] | .local_unsettled_sent')
595 if [[ "$unsettled" == "0" ]]; then
596 echo 'enable_htlc_settle did not work'
597 exit 1
598 fi
599 # bob goes away
600 $bob stop
601 echo "alice balance before closing channel:" $($alice getbalance)
602 balance_before=$($alice getbalance | jq '[.confirmed, .unconfirmed, .lightning] | to_entries | map(select(.value != null).value) | map(tonumber) | add ')
603 # alice force closes the channel
604 chan_id=$($alice list_channels | jq -r ".[0].channel_point")
605 $alice close_channel $chan_id --force
606 new_blocks 1
607 sleep 3
608 echo "alice balance after closing channel:" $($alice getbalance)
609 new_blocks 150
610 sleep 10
611 new_blocks 1
612 sleep 3
613 echo "alice balance after CLTV" $($alice getbalance)
614 new_blocks 150
615 sleep 10
616 new_blocks 1
617 sleep 3
618 echo "alice balance after CSV" $($alice getbalance)
619 # fixme: add local to getbalance
620 wait_for_balance alice $(echo "$balance_before - 0.02" | bc -l)
621 $alice getbalance
622 fi
623
624
625 if [[ $1 == "redeem_received_htlcs" ]]; then
626 # bob force closes and redeems with the preimage
627 $bob enable_htlc_settle false
628 wait_for_balance alice 1
629 echo "alice opens channel"
630 bob_node=$($bob nodeid)
631 $alice open_channel $bob_node 0.15 --password=''
632 new_blocks 3
633 wait_until_channel_open alice
634 # alice pays bob
635 invoice=$($bob add_request 0.04 --lightning --memo "test" | jq -r ".lightning_invoice")
636 $alice lnpay $invoice --timeout=1 || true
637 unsettled=$($alice list_channels | jq '.[] | .local_unsettled_sent')
638 if [[ "$unsettled" == "0" ]]; then
639 echo 'enable_htlc_settle did not work'
640 exit 1
641 fi
642 $alice stop
643 chan_id=$($bob list_channels | jq -r ".[0].channel_point")
644 $bob close_channel $chan_id --force
645 # if we exit here, bob GUI will show a warning
646 new_blocks 1
647 wait_for_balance bob 1.038
648 fi
649
650
651 if [[ $1 == "breach_with_unspent_htlc" ]]; then
652 $bob enable_htlc_settle false
653 wait_for_balance alice 1
654 echo "alice opens channel"
655 bob_node=$($bob nodeid)
656 channel=$($alice open_channel $bob_node 0.15 --password='')
657 new_blocks 3
658 wait_until_channel_open alice
659 echo "alice pays bob"
660 invoice=$($bob add_request 0.04 --lightning --memo "test" | jq -r ".lightning_invoice")
661 $alice lnpay $invoice --timeout=1 || true
662 unsettled=$($alice list_channels | jq '.[] | .local_unsettled_sent')
663 if [[ "$unsettled" == "0" ]]; then
664 echo "enable_htlc_settle did not work, $unsettled"
665 exit 1
666 fi
667 ctx=$($alice get_channel_ctx $channel --iknowwhatimdoing)
668 $bob enable_htlc_settle true
669 unsettled=$($alice list_channels | jq '.[] | .local_unsettled_sent')
670 if [[ "$unsettled" != "0" ]]; then
671 echo "enable_htlc_settle did not work, $unsettled"
672 exit 1
673 fi
674 echo "alice breaches with old ctx"
675 $bitcoin_cli sendrawtransaction $ctx
676 new_blocks 1
677 wait_for_balance bob 1.14
678 fi
679
680
681 if [[ $1 == "breach_with_spent_htlc" ]]; then
682 $bob enable_htlc_settle false
683 wait_for_balance alice 1
684 echo "alice opens channel"
685 bob_node=$($bob nodeid)
686 channel=$($alice open_channel $bob_node 0.15 --password='')
687 new_blocks 3
688 wait_until_channel_open alice
689 echo "alice pays bob"
690 invoice=$($bob add_request 0.04 --lightning --memo "test" | jq -r ".lightning_invoice")
691 $alice lnpay $invoice --timeout=1 || true
692 ctx=$($alice get_channel_ctx $channel --iknowwhatimdoing)
693 unsettled=$($alice list_channels | jq '.[] | .local_unsettled_sent')
694 if [[ "$unsettled" == "0" ]]; then
695 echo "enable_htlc_settle did not work, $unsettled"
696 exit 1
697 fi
698 cp /tmp/alice/regtest/wallets/default_wallet /tmp/alice/regtest/wallets/toxic_wallet
699 $bob enable_htlc_settle true
700 wait_until_htlcs_settled alice
701 echo $($bob getbalance)
702 echo "bob goes offline"
703 $bob stop
704 ctx_id=$($bitcoin_cli sendrawtransaction $ctx)
705 echo "alice breaches with old ctx:" $ctx_id
706 new_blocks 1
707 if [[ $($bitcoin_cli gettxout $ctx_id 0 | jq '.confirmations') != "1" ]]; then
708 echo "breach tx not confirmed"
709 exit 1
710 fi
711 echo "wait for cltv_expiry blocks"
712 # note: this will let alice redeem both to_local and the htlc.
713 # (to_local needs to_self_delay blocks; htlc needs whatever we put in invoice)
714 new_blocks 150
715 $alice stop
716 $alice daemon -d
717 $alice load_wallet -w /tmp/alice/regtest/wallets/toxic_wallet
718 # wait until alice has spent both ctx outputs
719 echo "alice spends to_local and htlc outputs"
720 if [ $TEST_SRK_CHANNELS != True ] ; then # anchors
721 # to_local_anchor/to_remote_anchor: 0 and 1 (both are present due to untrimmed htlcs)
722 # htlc: 2, to_local: 3
723 wait_until_spent $ctx_id 2
724 wait_until_spent $ctx_id 3
725 else # srk
726 # htlc: 0, to_local: 1
727 wait_until_spent $ctx_id 0
728 wait_until_spent $ctx_id 1
729 fi
730 new_blocks 1
731 echo "bob comes back"
732 $bob daemon -d
733 sleep 1
734 $bob load_wallet
735 wait_for_balance bob 1.039
736 $bob getbalance
737 fi
738
739 if [[ $1 == "watchtower" ]]; then
740 wait_for_balance alice 1
741 echo "alice opens channel"
742 bob_node=$($bob nodeid)
743 channel=$($alice open_channel $bob_node 0.15 --password='')
744 echo "channel outpoint: $channel"
745 new_blocks 3
746 wait_until_channel_open alice
747 echo "alice pays bob"
748 invoice1=$($bob add_request 0.01 --lightning --memo "invoice1" | jq -r ".lightning_invoice")
749 $alice lnpay $invoice1
750 ctx=$($alice get_channel_ctx $channel --iknowwhatimdoing)
751 echo "alice pays bob again"
752 invoice2=$($bob add_request 0.01 --lightning --memo "invoice2" | jq -r ".lightning_invoice")
753 $alice lnpay $invoice2
754 bob_ctn=$($bob list_channels | jq '.[0].local_ctn')
755 msg="waiting until watchtower is synchronized"
756 # watchtower needs to be at latest revoked ctn
757 while watchtower_ctn=$($bob get_watchtower_ctn $channel) && [[ $watchtower_ctn != $((bob_ctn-1)) ]]; do
758 sleep 0.1
759 printf "$msg $bob_ctn $watchtower_ctn\r"
760 done
761 printf "\n"
762 echo "stopping alice and bob"
763 $bob stop
764 $alice stop
765 ctx_id=$($bitcoin_cli sendrawtransaction $ctx)
766 echo "alice breaches with old ctx:" $ctx_id
767 echo "watchtower publishes justice transaction"
768 if [ $TEST_SRK_CHANNELS != True ] ; then # anchors
769 output_index=3
770 else # srk
771 output_index=1
772 fi
773 wait_until_spent $ctx_id $output_index # alice's to_local gets punished
774 fi
775
776 if [[ $1 == "fw_fail_htlc" ]]; then
777 $carol enable_htlc_settle false
778 bob_node=$($bob nodeid)
779 wait_for_balance carol 1
780 echo "alice and carol open channels with bob"
781 chan_id1=$($alice open_channel $bob_node 0.15 --password='' --push_amount=0.075)
782 chan_id2=$($carol open_channel $bob_node 0.15 --password='' --push_amount=0.075)
783 new_blocks 3
784 wait_until_channel_open alice
785 wait_until_channel_open carol
786 echo "alice pays carol"
787 invoice=$($carol add_request 0.01 --lightning --memo "invoice" | jq -r ".lightning_invoice")
788 screen -S alice_payment -dm -L -Logfile /tmp/alice/screen1.log $alice lnpay $invoice --timeout=600
789 sleep 1
790 unsettled=$($alice list_channels | jq '.[] | .local_unsettled_sent')
791 if [[ "$unsettled" == "0" ]]; then
792 echo 'enable_htlc_settle did not work (carol settled)'
793 exit 1
794 fi
795 $carol stop
796 ctx_id=$($bob close_channel $chan_id2 --force)
797 new_blocks 1
798 sleep 1
799 new_blocks 150 # cltv before bob can broadcast
800 # index of htlc
801 if [ $TEST_SRK_CHANNELS != True ] ; then # anchors
802 output_index=2
803 else # srk
804 output_index=0
805 fi
806 wait_until_spent $ctx_id $output_index
807 new_blocks 1 # confirm 2nd stage.
808 sleep 1
809 new_blocks 100 # deep enough for is_deeply_mined (>20 confs)
810 wait_until_htlcs_settled alice # bob propagates the failure back once the HTLC-timeout tx is deeply mined
811 fi
812
813 if [[ $1 == "just_in_time" ]]; then
814 bob_node=$($bob nodeid)
815 $alice setconfig zeroconf_trusted_node $bob_node
816 $alice setconfig use_recoverable_channels false
817 wait_for_balance carol 1
818 echo "carol opens channel with bob"
819 $carol open_channel $bob_node 0.15 --password=''
820 new_blocks 3
821 wait_until_channel_open carol
822 echo "carol pays alice"
823 # note: set amount to 0.001 to test failure: 'payment too low'
824 invoice=$($alice add_request 0.01 --lightning --memo "invoice" | jq -r ".lightning_invoice")
825 success=$($carol lnpay $invoice | jq -r ".success")
826 if [[ "$success" != "true" ]]; then
827 echo "jit payment failed"
828 exit 1
829 fi
830 # try again, multiple jit openings should work without issues
831 new_blocks 3
832 wait_for_chain_tip bob
833 echo "carol pays alice again"
834 invoice=$($alice add_request 0.04 --lightning --memo "invoice2" | jq -r ".lightning_invoice")
835 success=$($carol lnpay $invoice | jq -r ".success")
836 if [[ "$success" != "true" ]]; then
837 echo "jit payment failed"
838 exit 1
839 fi
840 alice_chan_count=$($alice list_channels | jq '. | length')
841 if [[ "$alice_chan_count" != "2" ]]; then
842 echo "alice should have two jit channels"
843 exit 1
844 fi
845 fi
846
847 if [[ $1 == "unixsockets" ]]; then
848 # This looks different because it has to run the entire daemon
849 # Test domain socket behavior
850 ./run_electrum --regtest daemon -d --rpcsock=unix # Start daemon with unix domain socket
851 ./run_electrum --regtest stop # Errors if it can't connect
852 # Test custom socket path
853 f=$(mktemp --dry-run)
854 ./run_electrum --regtest daemon -d --rpcsock=unix --rpcsockpath=$f
855 [ -S $f ] # filename exists and is socket
856 ./run_electrum --regtest stop
857 rm $f # clean up
858 # Test for regressions in the ordinary TCP functionality.
859 ./run_electrum --regtest daemon -d --rpcsock=tcp
860 ./run_electrum --regtest stop
861 fi