electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 31b67c422ba118f9b09a17f1612aaa97a6a8bdf2
parent 2ee41975f9efba4ef6b6bbe00a07654aaec3fce6
Author: ThomasV <thomasv@electrum.org>
Date:   Sat, 14 Jul 2018 19:39:28 +0200

add peer suggestion to open channel dialog. move add_peer code back to lnworker constructor

Diffstat:
Melectrum/gui/qt/channels_list.py | 11+++++++----
Melectrum/lnbase.py | 2+-
Melectrum/lnworker.py | 17++++++++++++-----
3 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/electrum/gui/qt/channels_list.py b/electrum/gui/qt/channels_list.py @@ -72,12 +72,13 @@ class ChannelsList(MyTreeWidget): self.status.setText(_('{} peers, {} nodes, {} channels').format(np, n, nc)) def new_channel_dialog(self): + lnworker = self.parent.wallet.lnworker d = WindowModalDialog(self.parent, _('Open Channel')) d.setFixedWidth(700) vbox = QVBoxLayout(d) h = QGridLayout() local_nodeid = QLineEdit() - local_nodeid.setText(bh2u(self.parent.wallet.lnworker.pubkey)) + local_nodeid.setText(bh2u(lnworker.pubkey)) local_nodeid.setReadOnly(True) local_nodeid.setCursorPosition(0) remote_nodeid = QLineEdit() @@ -95,6 +96,9 @@ class ChannelsList(MyTreeWidget): h.addWidget(push_amt_inp, 3, 1) vbox.addLayout(h) vbox.addLayout(Buttons(CancelButton(d), OkButton(d))) + suggestion = lnworker.suggest_peer() or b'' + remote_nodeid.setText(bh2u(suggestion)) + remote_nodeid.setCursorPosition(0) if not d.exec_(): return local_amt = local_amt_inp.get_amount() @@ -111,8 +115,8 @@ class ChannelsList(MyTreeWidget): except: self.parent.show_error(_('Invalid node ID, must be 33 bytes and hexadecimal')) return - peer = self.parent.wallet.lnworker.peers.get(node_id) + peer = lnworker.peers.get(node_id) if not peer: known = node_id in self.parent.network.lightning_nodes if rest is not None: @@ -131,8 +135,7 @@ class ChannelsList(MyTreeWidget): except: self.parent.show_error(_('Port number must be decimal')) return - - self.parent.wallet.lnworker.add_peer(host, port, node_id) + lnworker.add_peer(host, port, node_id) self.main_window.protect(self.open_channel, (node_id, local_amt, push_amt)) diff --git a/electrum/lnbase.py b/electrum/lnbase.py @@ -305,7 +305,7 @@ class Peer(PrintError): self.announcement_signatures = defaultdict(asyncio.Queue) self.update_fail_htlc = defaultdict(asyncio.Queue) self.localfeatures = (0x08 if request_initial_sync else 0) - self.channels = lnworker.channels + self.channels = lnworker.channels_for_peer(pubkey) self.invoices = lnworker.invoices self.attempted_route = {} diff --git a/electrum/lnworker.py b/electrum/lnworker.py @@ -43,11 +43,23 @@ class LNWorker(PrintError): self.channel_state = {chan.channel_id: "DISCONNECTED" for chan in self.channels.values()} for chan_id, chan in self.channels.items(): self.network.lnwatcher.watch_channel(chan, self.on_channel_utxos) + peer_list = self.config.get('lightning_peers', node_list) + for host, port, pubkey in peer_list: + self.add_peer(host, int(port), bfh(pubkey)) # wait until we see confirmations self.network.register_callback(self.on_network_update, ['updated', 'verified']) # thread safe self.on_network_update('updated') # shortcut (don't block) if funding tx locked and verified self.network.futures.append(asyncio.run_coroutine_threadsafe(self.main_loop(), asyncio.get_event_loop())) + def suggest_peer(self): + for node_id, peer in self.peers.items(): + print(bh2u(node_id), len(peer.channels)) + if len(peer.channels) > 0: + continue + if not(peer.initialized.done()): + continue + return node_id + def channels_for_peer(self, node_id): assert type(node_id) is bytes return {x: y for (x, y) in self.channels.items() if y.node_id == node_id} @@ -63,8 +75,6 @@ class LNWorker(PrintError): if openchannel.channel_id not in self.channel_state: self.channel_state[openchannel.channel_id] = "OPENING" self.channels[openchannel.channel_id] = openchannel - for node_id, peer in self.peers.items(): - peer.channels = self.channels_for_peer(node_id) if openchannel.remote_state.next_per_commitment_point == openchannel.remote_state.current_per_commitment_point: raise Exception("Tried to save channel with next_point == current_point, this should not happen") dumped = [x.serialize() for x in self.channels.values()] @@ -188,9 +198,6 @@ class LNWorker(PrintError): @aiosafe async def main_loop(self): - peer_list = self.config.get('lightning_peers', node_list) - for host, port, pubkey in peer_list: - self.add_peer(host, int(port), bfh(pubkey)) while True: await asyncio.sleep(1) for k, peer in list(self.peers.items()):