commit 0966edc637ba927ea6767b44aa937b0ca7805dd7
parent fe550c6c73ae3772d70ef7c5d5bd9eaffa0202ea
Author: ThomasV <thomasv@electrum.org>
Date: Sat, 12 Oct 2019 19:15:51 +0200
fine-grained callbacks for lightning network dialog
Diffstat:
4 files changed, 29 insertions(+), 29 deletions(-)
diff --git a/electrum/channel_db.py b/electrum/channel_db.py
@@ -253,11 +253,13 @@ class ChannelDB(SqlDB):
self._addresses = defaultdict(set)
self._channels_for_node = defaultdict(set)
self.data_loaded = asyncio.Event()
+ self.network = network # only for callback
def update_counts(self):
+ self.num_nodes = len(self._nodes)
self.num_channels = len(self._channels)
self.num_policies = len(self._policies)
- self.num_nodes = len(self._nodes)
+ self.network.trigger_callback('channel_db', self.num_nodes, self.num_channels, self.num_policies)
def get_channel_ids(self):
return set(self._channels.keys())
@@ -489,10 +491,11 @@ class ChannelDB(SqlDB):
def prune_old_policies(self, delta):
l = self.get_old_policies(delta)
- for k in l:
- self._policies.pop(k)
- self.delete_policy(*k)
if l:
+ for k in l:
+ self._policies.pop(k)
+ self.delete_policy(*k)
+ self.update_counts()
self.logger.info(f'Deleting {len(l)} old policies')
def get_orphaned_channels(self):
@@ -501,11 +504,11 @@ class ChannelDB(SqlDB):
def prune_orphaned_channels(self):
l = self.get_orphaned_channels()
- for short_channel_id in l:
- self.remove_channel(short_channel_id)
- self.delete_channel(short_channel_id)
- self.update_counts()
if l:
+ for short_channel_id in l:
+ self.remove_channel(short_channel_id)
+ self.delete_channel(short_channel_id)
+ self.update_counts()
self.logger.info(f'Deleting {len(l)} orphaned channels')
def add_channel_update_for_private_channel(self, msg_payload: dict, start_node_id: bytes):
diff --git a/electrum/gui/qt/lightning_dialog.py b/electrum/gui/qt/lightning_dialog.py
@@ -43,7 +43,6 @@ class LightningDialog(QDialog):
self.network = gui_object.daemon.network
self.setWindowTitle(_('Lightning Network'))
self.setMinimumSize(600, 20)
-
vbox = QVBoxLayout(self)
self.num_peers = QLabel('')
vbox.addWidget(self.num_peers)
@@ -54,16 +53,24 @@ class LightningDialog(QDialog):
self.status = QLabel('')
vbox.addWidget(self.status)
vbox.addStretch(1)
-
b = QPushButton(_('Close'))
b.clicked.connect(self.close)
vbox.addLayout(Buttons(b))
- self.network.register_callback(self.update_status, ['ln_status'])
+ self.network.register_callback(self.on_channel_db, ['channel_db'])
+ self.network.register_callback(self.set_num_peers, ['gossip_peers'])
+ self.network.register_callback(self.set_unknown_channels, ['unknown_channels'])
+ self.network.channel_db.update_counts() # trigger callback
+ self.set_num_peers('', self.network.lngossip.num_peers())
+ self.set_unknown_channels('', len(self.network.lngossip.unknown_ids))
- def update_status(self, event, num_peers, num_nodes, known, unknown):
- self.num_peers.setText(_(f'Connected to {num_peers} peers'))
+ def on_channel_db(self, event, num_nodes, num_channels, num_policies):
self.num_nodes.setText(_(f'{num_nodes} nodes'))
- self.num_channels.setText(_(f'{known} channels'))
+ self.num_channels.setText(_(f'{num_channels} channels'))
+
+ def set_num_peers(self, event, num_peers):
+ self.num_peers.setText(_(f'Connected to {num_peers} peers'))
+
+ def set_unknown_channels(self, event, unknown):
self.status.setText(_(f'Requesting {unknown} channels...') if unknown else '')
def is_hidden(self):
diff --git a/electrum/lnpeer.py b/electrum/lnpeer.py
@@ -293,9 +293,6 @@ class Peer(Logger):
self.orphan_channel_updates.popitem(last=False)
if categorized_chan_upds.good:
self.logger.debug(f'on_channel_update: {len(categorized_chan_upds.good)}/{len(chan_upds_chunk)}')
- # refresh gui
- if chan_anns or node_anns or chan_upds:
- self.network.lngossip.refresh_gui()
def verify_channel_announcements(self, chan_anns):
for payload in chan_anns:
diff --git a/electrum/lnworker.py b/electrum/lnworker.py
@@ -155,10 +155,11 @@ class LNWorker(Logger):
peer = Peer(self, node_id, transport)
await self.network.main_taskgroup.spawn(peer.main_loop())
self.peers[node_id] = peer
- #if self.network.lngossip:
- # self.network.lngossip.refresh_gui()
return peer
+ def num_peers(self):
+ return sum([p.initialized.is_set() for p in self.peers.values()])
+
def start_network(self, network: 'Network'):
self.network = network
self.config = network.config
@@ -279,28 +280,20 @@ class LNGossip(LNWorker):
super().start_network(network)
asyncio.run_coroutine_threadsafe(self.network.main_taskgroup.spawn(self.maintain_db()), self.network.asyncio_loop)
- def refresh_gui(self):
- # refresh gui
- known = self.channel_db.num_channels
- unknown = len(self.unknown_ids)
- num_nodes = self.channel_db.num_nodes
- num_peers = sum([p.initialized.is_set() for p in self.peers.values()])
- self.logger.info(f'Channels: {known}. Missing: {unknown}')
- self.network.trigger_callback('ln_status', num_peers, num_nodes, known, unknown)
-
async def maintain_db(self):
await self.channel_db.load_data()
while True:
if len(self.unknown_ids) == 0:
self.channel_db.prune_old_policies(self.max_age)
self.channel_db.prune_orphaned_channels()
- self.refresh_gui()
await asyncio.sleep(120)
async def add_new_ids(self, ids):
known = self.channel_db.get_channel_ids()
new = set(ids) - set(known)
self.unknown_ids.update(new)
+ self.network.trigger_callback('unknown_channels', len(self.unknown_ids))
+ self.network.trigger_callback('gossip_peers', self.num_peers())
def get_ids_to_query(self):
N = 500