Skip to content

Commit

Permalink
lnpeer: make forwarding partly event-driven
Browse files Browse the repository at this point in the history
  • Loading branch information
SomberNight committed Nov 4, 2021
1 parent f066f25 commit 16c6655
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions electrum/lnpeer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import functools

import aiorpcx
from aiorpcx import TaskGroup
from aiorpcx import TaskGroup, ignore_after

from .crypto import sha256, sha256d
from . import bitcoin, util
Expand Down Expand Up @@ -109,6 +109,7 @@ def __init__(
self.received_htlc_removed_event = asyncio.Event()
self._htlc_switch_iterstart_event = asyncio.Event()
self._htlc_switch_iterdone_event = asyncio.Event()
self._received_revack_event = asyncio.Event()

def send_message(self, message_name: str, **kwargs):
assert type(message_name) is str
Expand Down Expand Up @@ -1629,6 +1630,8 @@ def on_revoke_and_ack(self, chan: Channel, payload):
chan.receive_revocation(rev)
self.lnworker.save_channel(chan)
self.maybe_send_commitment(chan)
self._received_revack_event.set()
self._received_revack_event.clear()

def on_update_fee(self, chan: Channel, payload):
feerate = payload["feerate_per_kw"]
Expand Down Expand Up @@ -1836,7 +1839,12 @@ async def htlc_switch(self):
while True:
self._htlc_switch_iterdone_event.set()
self._htlc_switch_iterdone_event.clear()
await asyncio.sleep(0.1) # TODO maybe make this partly event-driven
# We poll every 0.1 sec to check if there is work to do,
# or we can be woken up when receiving a revack.
# TODO when forwarding, we should also be woken up when there are
# certain events with the downstream peer
async with ignore_after(0.1):
await self._received_revack_event.wait()
self._htlc_switch_iterstart_event.set()
self._htlc_switch_iterstart_event.clear()
self.ping_if_required()
Expand Down

0 comments on commit 16c6655

Please sign in to comment.