1
0

lnwatcher: add some type hints

This commit is contained in:
SomberNight
2025-09-08 15:01:50 +00:00
parent 317bf998e3
commit b036eaf3eb
3 changed files with 13 additions and 12 deletions

View File

@@ -227,7 +227,7 @@ class AddressSynchronizer(Logger, EventListener):
self.unregister_callbacks() self.unregister_callbacks()
self.network = None self.network = None
def add_address(self, address): def add_address(self, address: str) -> None:
if address not in self.db.history: if address not in self.db.history:
self.db.history[address] = [] self.db.history[address] = []
if self.synchronizer: if self.synchronizer:

View File

@@ -2,7 +2,7 @@
# Distributed under the MIT software license, see the accompanying # Distributed under the MIT software license, see the accompanying
# file LICENCE or http://www.opensource.org/licenses/mit-license.php # file LICENCE or http://www.opensource.org/licenses/mit-license.php
from typing import TYPE_CHECKING, Optional from typing import TYPE_CHECKING, Optional, Dict, Callable, Awaitable
from . import util from . import util
from .util import TxMinedInfo, BelowDustLimit, NoDynamicFeeEstimates from .util import TxMinedInfo, BelowDustLimit, NoDynamicFeeEstimates
@@ -27,11 +27,9 @@ class LNWatcher(Logger, EventListener):
Logger.__init__(self) Logger.__init__(self)
self.adb = lnworker.wallet.adb self.adb = lnworker.wallet.adb
self.config = lnworker.config self.config = lnworker.config
self.callbacks = {} # address -> lambda function self.callbacks = {} # type: Dict[str, Callable[[], Awaitable[None]]] # address -> lambda function
self.network = None self.network = None
self.register_callbacks() self.register_callbacks()
# status gets populated when we run
self.channel_status = {}
self._pending_force_closes = set() self._pending_force_closes = set()
def start_network(self, network: 'Network'): def start_network(self, network: 'Network'):
@@ -40,18 +38,21 @@ class LNWatcher(Logger, EventListener):
def stop(self): def stop(self):
self.unregister_callbacks() self.unregister_callbacks()
def get_channel_status(self, outpoint): def remove_callback(self, address: str) -> None:
return self.channel_status.get(outpoint, 'unknown')
def remove_callback(self, address):
self.callbacks.pop(address, None) self.callbacks.pop(address, None)
def add_callback(self, address, callback, *, subscribe=True): def add_callback(
self,
address: str,
callback: Callable[[], Awaitable[None]],
*,
subscribe: bool = True,
) -> None:
if subscribe: if subscribe:
self.adb.add_address(address) self.adb.add_address(address)
self.callbacks[address] = callback self.callbacks[address] = callback
async def trigger_callbacks(self, *, requires_synchronizer=True): async def trigger_callbacks(self, *, requires_synchronizer: bool = True):
if requires_synchronizer and not self.adb.synchronizer: if requires_synchronizer and not self.adb.synchronizer:
self.logger.info("synchronizer not set yet") self.logger.info("synchronizer not set yet")
return return

View File

@@ -83,7 +83,7 @@ class SynchronizerBase(NetworkJobOnDefaultServer):
# we are being cancelled now # we are being cancelled now
self.session.unsubscribe(self.status_queue) self.session.unsubscribe(self.status_queue)
def add(self, addr): def add(self, addr: str) -> None:
if not is_address(addr): raise ValueError(f"invalid bitcoin address {addr}") if not is_address(addr): raise ValueError(f"invalid bitcoin address {addr}")
self._adding_addrs.add(addr) # this lets is_up_to_date already know about addr self._adding_addrs.add(addr) # this lets is_up_to_date already know about addr