From a7c634b1ab4cb98fd0a781d64d2495fd45c7a9e0 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Fri, 7 Mar 2025 17:06:55 +0000 Subject: [PATCH] regtests: extend "extract_preimage" to cover both types of extracts --- electrum/lnchannel.py | 3 +- tests/regtest/regtest.sh | 65 +++++++++++++++++++++++++++++----------- 2 files changed, 49 insertions(+), 19 deletions(-) diff --git a/electrum/lnchannel.py b/electrum/lnchannel.py index c5bd9fc32..2562e78e3 100644 --- a/electrum/lnchannel.py +++ b/electrum/lnchannel.py @@ -1376,7 +1376,8 @@ class Channel(AbstractChannel): return if self.lnworker.get_preimage(payment_hash) is not None: return - self.logger.info(f'found preimage for {payment_hash.hex()} in witness of length {len(witness)}') + self.logger.info(f"found preimage in witness of length {len(witness)}, for {payment_hash.hex()}") + # ^ note: log message text grepped for in regtests self.lnworker.save_preimage(payment_hash, preimage) for htlc, is_sent in found.values(): if is_sent: diff --git a/tests/regtest/regtest.sh b/tests/regtest/regtest.sh index 62f04943f..7ac59f751 100755 --- a/tests/regtest/regtest.sh +++ b/tests/regtest/regtest.sh @@ -36,8 +36,8 @@ function wait_for_balance() cmd="./run_electrum --regtest -D /tmp/$1" while balance=$($cmd getbalance | jq '[.confirmed, .unconfirmed] | to_entries | map(select(.value != null).value) | map(tonumber) | add ') && (( $(echo "$balance < $2" | bc -l) )); do sleep 1 - msg="$msg." - printf "$msg\r" + msg="$msg." + printf "$msg\r" done printf "\n" } @@ -48,8 +48,8 @@ function wait_until_channel_open() cmd="./run_electrum --regtest -D /tmp/$1" while channel_state=$($cmd list_channels | jq '.[0] | .state' | tr -d '"') && [ $channel_state != "OPEN" ]; do sleep 1 - msg="$msg." - printf "$msg\r" + msg="$msg." + printf "$msg\r" done printf "\n" } @@ -60,8 +60,8 @@ function wait_until_channel_closed() cmd="./run_electrum --regtest -D /tmp/$1" while [[ $($cmd list_channels | jq '.[0].state' | tr -d '"') != "CLOSED" ]]; do sleep 1 - msg="$msg." - printf "$msg\r" + msg="$msg." + printf "$msg\r" done printf "\n" } @@ -71,8 +71,8 @@ function wait_until_spent() msg="wait until $1:$2 is spent" while [[ $($bitcoin_cli gettxout $1 $2) ]]; do sleep 1 - msg="$msg." - printf "$msg\r" + msg="$msg." + printf "$msg\r" done printf "\n" } @@ -277,36 +277,65 @@ fi if [[ $1 == "extract_preimage" ]]; then - # instead of settling bob will broadcast + # Alice sends htlc1 to Bob. Bob sends htlc2 to Alice. + # Neither one of them settles, they hold the htlcs, and Bob force-closes. + # Bob's ctx contains two htlc outputs: "received" htlc1, and "offered" htlc2. + # Bob also broadcasts an HTLC-success tx for received htlc1, revealing the preimage. + # Alice broadcasts a direct-spend of the offered htlc2, revealing the preimage. + # This test checks that + # - Alice successfully extracts the preimage for htlc1 from Bob's HTLC-success tx, and + # - Bob successfully extracts the preimage for htlc2 from Alice's direct spend tx + # note: actually, due to MPP, there will be more htlcs in the ctx: + # we force alice to use MPP, but force bob NOT to use MPP $alice setconfig test_force_disable_mpp false $alice setconfig test_force_mpp true + $bob setconfig test_force_disable_mpp true + $bob setconfig test_force_mpp false + $alice enable_htlc_settle false $bob enable_htlc_settle false wait_for_balance alice 1 echo "alice opens channel" bob_node=$($bob nodeid) - $alice open_channel $bob_node 0.15 --password='' + $alice open_channel $bob_node 0.15 --password='' --push_amount=0.075 new_blocks 3 wait_until_channel_open alice chan_id=$($alice list_channels | jq -r ".[0].channel_point") # alice pays bob - invoice=$($bob add_request 0.04 --lightning -m "test" | jq -r ".lightning_invoice") - screen -S alice_payment -dm -L -Logfile /tmp/alice/screen.log $alice lnpay $invoice --timeout=600 + invoice1=$($bob add_request 0.04 --lightning -m "test1" | jq -r ".lightning_invoice") + screen -S alice_payment -dm -L -Logfile /tmp/alice/screen1.log $alice lnpay $invoice1 --timeout=600 sleep 1 unsettled=$($alice list_channels | jq '.[] | .local_unsettled_sent') if [[ "$unsettled" == "0" ]]; then - echo 'enable_htlc_settle did not work' + echo 'enable_htlc_settle did not work (bob settled)' + exit 1 + fi + # bob pays alice + invoice2=$($alice add_request 0.04 --lightning -m "test2" | jq -r ".lightning_invoice") + screen -S bob_payment -dm -L -Logfile /tmp/bob/screen2.log $bob lnpay $invoice2 --timeout=600 + sleep 1 + unsettled=$($bob list_channels | jq '.[] | .local_unsettled_sent') + if [[ "$unsettled" == "0" ]]; then + echo 'enable_htlc_settle did not work (alice settled)' exit 1 fi # bob force closes $bob close_channel $chan_id --force new_blocks 1 wait_until_channel_closed bob + wait_until_channel_closed alice sleep 5 - success=$(cat /tmp/alice/screen.log | jq -r ".success") - if [[ "$success" != "true" ]]; then - exit 1 - fi - cat /tmp/alice/screen.log + # check logs + alice_log_found=$(grep -rnw "/tmp/alice/regtest/logs/" -e "found preimage in witness of length 5" | wc -l) + bob_log_found=$(grep -rnw "/tmp/bob/regtest/logs/" -e "found preimage in witness of length 3" | wc -l) + if [[ "$alice_log_found" != "1" ]]; then exit 1; fi + if [[ "$bob_log_found" != "1" ]]; then exit 1; fi + # check both "lnpay" commands succeeded + success=$(cat /tmp/alice/screen1.log | jq -r ".success") + if [[ "$success" != "true" ]]; then exit 1; fi + success=$(cat /tmp/bob/screen2.log | jq -r ".success") + if [[ "$success" != "true" ]]; then exit 1; fi + cat /tmp/alice/screen1.log + cat /tmp/bob/screen2.log fi