commit 782f9ed2731583687cbdf824d166bd3546f685e8
parent 7da8c2dfe5ec887fa41064258117bc2f5e4ed057
Author: ThomasV <thomasv@electrum.org>
Date: Wed, 20 May 2020 13:49:44 +0200
lnwatcher: use generic callbacks
Diffstat:
1 file changed, 13 insertions(+), 7 deletions(-)
diff --git a/electrum/lnwatcher.py b/electrum/lnwatcher.py
@@ -138,7 +138,7 @@ class LNWatcher(AddressSynchronizer):
def __init__(self, network: 'Network'):
AddressSynchronizer.__init__(self, WalletDB({}, manual_upgrades=False))
self.config = network.config
- self.channels = {}
+ self.callbacks = {} # address -> lambda: coroutine
self.network = network
util.register_callback(
self.on_network_update,
@@ -157,12 +157,19 @@ class LNWatcher(AddressSynchronizer):
def add_channel(self, outpoint: str, address: str) -> None:
assert isinstance(outpoint, str)
assert isinstance(address, str)
- self.add_address(address)
- self.channels[address] = outpoint
+ cb = lambda: self.check_onchain_situation(address, outpoint)
+ self.add_callback(address, cb)
async def unwatch_channel(self, address, funding_outpoint):
self.logger.info(f'unwatching {funding_outpoint}')
- self.channels.pop(address, None)
+ self.remove_callback(address)
+
+ def remove_callback(self, address):
+ self.callbacks.pop(address, None)
+
+ def add_callback(self, address, callback):
+ self.add_address(address)
+ self.callbacks[address] = callback
@log_exceptions
async def on_network_update(self, event, *args):
@@ -172,9 +179,8 @@ class LNWatcher(AddressSynchronizer):
if not self.synchronizer:
self.logger.info("synchronizer not set yet")
return
- channels_items = list(self.channels.items()) # copy
- for address, outpoint in channels_items:
- await self.check_onchain_situation(address, outpoint)
+ for address, callback in list(self.callbacks.items()):
+ await callback()
async def check_onchain_situation(self, address, funding_outpoint):
# early return if address has not been added yet