URI: 
       tReplace lightning_settle_delay with enable_htlc_settle (asyncio.Event) - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit 077f7786327167e70919a15880d880e565ba26af
   DIR parent ce81957d25e66296c0baaf48683adcc623a3dae4
  HTML Author: ThomasV <thomasv@electrum.org>
       Date:   Thu, 27 Feb 2020 20:53:50 +0100
       
       Replace lightning_settle_delay with enable_htlc_settle (asyncio.Event)
       
       Diffstat:
         M electrum/commands.py                |       5 +++++
         M electrum/lnpeer.py                  |       2 +-
         M electrum/lnworker.py                |       3 +++
         M electrum/tests/regtest/regtest.sh   |      21 ++++++++++-----------
         M electrum/tests/test_lnpeer.py       |      12 ++++++++----
       
       5 files changed, 27 insertions(+), 16 deletions(-)
       ---
   DIR diff --git a/electrum/commands.py b/electrum/commands.py
       t@@ -992,6 +992,11 @@ class Commands:
                self.network.config.fee_estimates = ast.literal_eval(fees)
                self.network.notify('fee')
        
       +    @command('wn')
       +    async def enable_htlc_settle(self, b: bool, wallet: Abstract_Wallet = None):
       +        e = wallet.lnworker.enable_htlc_settle
       +        e.set() if b else e.clear()
       +
            @command('n')
            async def clear_ln_blacklist(self):
                self.network.path_finder.blacklist.clear()
   DIR diff --git a/electrum/lnpeer.py b/electrum/lnpeer.py
       t@@ -1282,7 +1282,7 @@ class Peer(Logger):
                    await self.fail_htlc(chan, htlc.htlc_id, onion_packet, reason)
                    return
                #self.network.trigger_callback('htlc_added', htlc, invoice, RECEIVED)
       -        await asyncio.sleep(self.network.config.get('lightning_settle_delay', 0))
       +        await self.lnworker.enable_htlc_settle.wait()
                await self._fulfill_htlc(chan, htlc.htlc_id, preimage)
        
            async def _fulfill_htlc(self, chan: Channel, htlc_id: int, preimage: bytes):
   DIR diff --git a/electrum/lnworker.py b/electrum/lnworker.py
       t@@ -385,6 +385,9 @@ class LNWallet(LNWorker):
                self.sweep_address = wallet.get_receiving_address()
                self.lock = threading.RLock()
                self.logs = defaultdict(list)  # type: Dict[str, List[PaymentAttemptLog]]  # key is RHASH
       +        # used in tests
       +        self.enable_htlc_settle = asyncio.Event()
       +        self.enable_htlc_settle.set()
        
                # note: accessing channels (besides simple lookup) needs self.lock!
                self.channels = {}
   DIR diff --git a/electrum/tests/regtest/regtest.sh b/electrum/tests/regtest/regtest.sh
       t@@ -162,7 +162,7 @@ if [[ $1 == "breach" ]]; then
        fi
        
        if [[ $1 == "redeem_htlcs" ]]; then
       -    $bob setconfig lightning_settle_delay 10
       +    $bob enable_htlc_settle false
            wait_for_balance alice 1
            echo "alice opens channel"
            bob_node=$($bob nodeid)
       t@@ -172,10 +172,9 @@ if [[ $1 == "redeem_htlcs" ]]; then
            # alice pays bob
            invoice=$($bob add_lightning_request 0.05 -m "test")
            $alice lnpay $invoice --timeout=1 || true
       -    sleep 1
            settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
            if [[ "$settled" != "0" ]]; then
       -        echo 'SETTLE_DELAY did not work'
       +        echo 'enable_htlc_settle did not work'
                exit 1
            fi
            # bob goes away
       t@@ -205,7 +204,7 @@ fi
        
        
        if [[ $1 == "breach_with_unspent_htlc" ]]; then
       -    $bob setconfig lightning_settle_delay 3
       +    $bob enable_htlc_settle false
            wait_for_balance alice 1
            echo "alice opens channel"
            bob_node=$($bob nodeid)
       t@@ -217,14 +216,14 @@ if [[ $1 == "breach_with_unspent_htlc" ]]; then
            $alice lnpay $invoice --timeout=1 || true
            settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
            if [[ "$settled" != "0" ]]; then
       -        echo "SETTLE_DELAY did not work, $settled != 0"
       +        echo "enable_htlc_settle did not work, $settled != 0"
                exit 1
            fi
            ctx=$($alice get_channel_ctx $channel)
       -    sleep 5
       +    $bob enable_htlc_settle true
            settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
            if [[ "$settled" != "1" ]]; then
       -        echo "SETTLE_DELAY did not work, $settled != 1"
       +        echo "enable_htlc_settle did not work, $settled != 1"
                exit 1
            fi
            echo "alice breaches with old ctx"
       t@@ -234,7 +233,7 @@ fi
        
        
        if [[ $1 == "breach_with_spent_htlc" ]]; then
       -    $bob setconfig lightning_settle_delay 3
       +    $bob enable_htlc_settle false
            wait_for_balance alice 1
            echo "alice opens channel"
            bob_node=$($bob nodeid)
       t@@ -247,14 +246,14 @@ if [[ $1 == "breach_with_spent_htlc" ]]; then
            ctx=$($alice get_channel_ctx $channel)
            settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
            if [[ "$settled" != "0" ]]; then
       -        echo "SETTLE_DELAY did not work, $settled != 0"
       +        echo "enable_htlc_settle did not work, $settled != 0"
                exit 1
            fi
            cp /tmp/alice/regtest/wallets/default_wallet /tmp/alice/regtest/wallets/toxic_wallet
       -    sleep 5
       +    $bob enable_htlc_settle true
            settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
            if [[ "$settled" != "1" ]]; then
       -        echo "SETTLE_DELAY did not work, $settled != 1"
       +        echo "enable_htlc_settle did not work, $settled != 1"
                exit 1
            fi
            echo $($bob getbalance)
   DIR diff --git a/electrum/tests/test_lnpeer.py b/electrum/tests/test_lnpeer.py
       t@@ -91,6 +91,9 @@ class MockLNWallet:
                self.pending_payments = defaultdict(asyncio.Future)
                chan.lnworker = self
                chan.node_id = remote_keypair.pubkey
       +        # used in tests
       +        self.enable_htlc_settle = asyncio.Event()
       +        self.enable_htlc_settle.set()
        
            def get_invoice_status(self, key):
                pass
       t@@ -295,7 +298,7 @@ class TestPeer(ElectrumTestCase):
                w2.network.config.set_key('dynamic_fees', False)
                w1.network.config.set_key('fee_per_kb', 5000)
                w2.network.config.set_key('fee_per_kb', 1000)
       -        w2.network.config.set_key('lightning_settle_delay', 0.1)
       +        w2.enable_htlc_settle.clear()
                pay_req = self.prepare_invoice(w2)
                lnaddr = lndecode(pay_req, expected_hrp=constants.net.SEGWIT_HRP)
                async def pay():
       t@@ -304,12 +307,13 @@ class TestPeer(ElectrumTestCase):
                    # alice sends htlc
                    route = await w1._create_route_from_invoice(decoded_invoice=lnaddr)
                    htlc = await p1.pay(route, alice_channel, int(lnaddr.amount * COIN * 1000), lnaddr.paymenthash, lnaddr.get_min_final_cltv_expiry())
       -            # time to settle
       -            #await asyncio.sleep(1)
                    # alice closes
                    await p1.close_channel(alice_channel.channel_id)
                    gath.cancel()
       -        gath = asyncio.gather(pay(), p1._message_loop(), p2._message_loop())
       +        async def set_settle():
       +            await asyncio.sleep(0.1)
       +            w2.enable_htlc_settle.set()
       +        gath = asyncio.gather(pay(), set_settle(), p1._message_loop(), p2._message_loop())
                async def f():
                    await gath
                with self.assertRaises(concurrent.futures.CancelledError):