From 6320597f2c14978f8dbcd78cdcef0083913ea05d Mon Sep 17 00:00:00 2001 From: SomberNight Date: Thu, 15 May 2025 14:19:49 +0000 Subject: [PATCH] regtests: rm sleep from "swapserver_forceclose" test less reliance on timing (OTOH it hardcodes the output index of the commitment tx... meh) --- electrum/commands.py | 3 ++- electrum/lnworker.py | 3 +++ tests/regtest/regtest.sh | 10 ++++++++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/electrum/commands.py b/electrum/commands.py index 084a9326a..d970dfdda 100644 --- a/electrum/commands.py +++ b/electrum/commands.py @@ -1639,7 +1639,8 @@ class Commands(Logger): @command('wnpl') async def close_channel(self, channel_point, force=False, password=None, wallet: Abstract_Wallet = None): """ - Close a lightning channel + Close a lightning channel. + Returns txid of closing tx. arg:str:channel_point:channel point arg:bool:force:Force closes (broadcast local commitment transaction) diff --git a/electrum/lnworker.py b/electrum/lnworker.py index b15718b8f..5ff848ed9 100644 --- a/electrum/lnworker.py +++ b/electrum/lnworker.py @@ -2288,6 +2288,9 @@ class LNWallet(LNWorker): def save_preimage(self, payment_hash: bytes, preimage: bytes, *, write_to_disk: bool = True): if sha256(preimage) != payment_hash: raise Exception("tried to save incorrect preimage for payment_hash") + if self._preimages.get(payment_hash.hex()) is not None: + return # we already have this preimage + self.logger.debug(f"saving preimage for {payment_hash.hex()}") self._preimages[payment_hash.hex()] = preimage.hex() if write_to_disk: self.wallet.save_db() diff --git a/tests/regtest/regtest.sh b/tests/regtest/regtest.sh index 3725dc77e..d50212ac2 100755 --- a/tests/regtest/regtest.sh +++ b/tests/regtest/regtest.sh @@ -249,11 +249,17 @@ if [[ $1 == "swapserver_forceclose" ]]; then swap=$($alice reverse_swap 0.02 $onchain_amount) echo $swap | jq funding_txid=$(echo $swap| jq -r ".funding_txid") - $bob close_channel --force $channel + ctx_id=$($bob close_channel --force $channel) new_blocks 1 wait_until_spent $funding_txid 0 # alice reveals preimage new_blocks 1 - sleep 2 + if [ $TEST_ANCHOR_CHANNELS = True ] ; then + output_index=3 # received_htlc_output in bob's ctx. FIXME index depends on Alice not using MPP + else + output_index=1 + fi + # wait until Bob finds preimage onchain and uses it to create an htlc_success tx + wait_until_spent $ctx_id $output_index # alice's to_local gets punished new_blocks 144 wait_for_balance bob 0.999 fi