commit bf6d28e1f0146d8de0359e05e9ea64cd999fdd90
parent 4fe912f4b3a05c2dc2a5cbe3bde17f29021bd6af
Author: ThomasV <thomasv@electrum.org>
Date: Sun, 3 Jun 2018 10:07:56 +0200
integrate channels_list with existing framework
Diffstat:
5 files changed, 100 insertions(+), 316 deletions(-)
diff --git a/electrum/gui/qt/main_window.py b/electrum/gui/qt/main_window.py
@@ -88,7 +88,7 @@ from .util import (read_QIcon, ColorScheme, text_dialog, icon_path, WaitingDialo
from .installwizard import WIF_HELP_TEXT
from .history_list import HistoryList, HistoryModel
from .update_checker import UpdateCheck, UpdateCheckThread
-from .lightning_channels_list import LightningChannelsList
+from .channels_list import ChannelsList
class StatusBarButton(QPushButton):
@@ -172,12 +172,10 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
self.utxo_tab = self.create_utxo_tab()
self.console_tab = self.create_console_tab()
self.contacts_tab = self.create_contacts_tab()
+ self.channels_tab = self.create_channels_tab(wallet)
tabs.addTab(self.create_history_tab(), QIcon(":icons/tab_history.png"), _('History'))
tabs.addTab(self.send_tab, QIcon(":icons/tab_send.png"), _('Send'))
tabs.addTab(self.receive_tab, QIcon(":icons/tab_receive.png"), _('Receive'))
- if config.get("lnbase", False):
- self.lightning_channels_tab = self.create_lightning_channels_tab(wallet)
- tabs.addTab(self.lightning_channels_tab, QIcon(":icons/lightning.png"), _("Channels"))
def add_optional_tab(tabs, tab, icon, description, name):
tab.tab_icon = icon
@@ -188,6 +186,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
tabs.addTab(tab, icon, description.replace("&", ""))
add_optional_tab(tabs, self.addresses_tab, read_QIcon("tab_addresses.png"), _("&Addresses"), "addresses")
+ add_optional_tab(tabs, self.channels_tab, QIcon("lightning.png"), _("Channels"), "channels")
add_optional_tab(tabs, self.utxo_tab, read_QIcon("tab_coins.png"), _("Co&ins"), "utxo")
add_optional_tab(tabs, self.contacts_tab, read_QIcon("tab_contacts.png"), _("Con&tacts"), "contacts")
add_optional_tab(tabs, self.console_tab, read_QIcon("tab_console.png"), _("Con&sole"), "console")
@@ -221,7 +220,8 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
self.network_signal.connect(self.on_network_qt)
interests = ['wallet_updated', 'network_updated', 'blockchain_updated',
'new_transaction', 'status',
- 'banner', 'verified', 'fee', 'fee_histogram']
+ 'banner', 'verified', 'fee', 'fee_histogram', 'on_quotes',
+ 'on_history', 'channel', 'channels']
# To avoid leaking references to "self" that prevent the
# window from being GC-ed when closed, callbacks should be
# methods of this class only, and specifically not be
@@ -229,8 +229,6 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
self.network.register_callback(self.on_network, interests)
# set initial message
self.console.showMessage(self.network.banner)
- self.network.register_callback(self.on_quotes, ['on_quotes'])
- self.network.register_callback(self.on_history, ['on_history'])
self.new_fx_quotes_signal.connect(self.on_fx_quotes)
self.new_fx_history_signal.connect(self.on_fx_history)
@@ -259,10 +257,6 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
self._update_check_thread.checked.connect(on_version_received)
self._update_check_thread.start()
- def on_history(self, b):
- self.wallet.clear_coin_price_cache()
- self.new_fx_history_signal.emit()
-
def setup_exception_hook(self):
Exception_Hook(self)
@@ -270,9 +264,6 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
self.history_model.refresh('fx_history')
self.address_list.update()
- def on_quotes(self, b):
- self.new_fx_quotes_signal.emit()
-
def on_fx_quotes(self):
self.update_status()
# Refresh edits with the new rate
@@ -370,6 +361,14 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
elif event in ['status', 'banner', 'verified', 'fee', 'fee_histogram']:
# Handle in GUI thread
self.network_signal.emit(event, args)
+ elif event == 'on_quotes':
+ self.new_fx_quotes_signal.emit()
+ elif event == 'on_history':
+ self.new_fx_history_signal.emit()
+ elif event == 'channels':
+ self.channels_list.update_rows.emit(*args)
+ elif event == 'channel':
+ self.channels_list.update_single_row.emit(*args)
else:
self.logger.info(f"unexpected network message: {event} {args}")
@@ -416,6 +415,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
def load_wallet(self, wallet):
wallet.thread = TaskThread(self, self.on_error)
self.update_recently_visited(wallet.storage.path)
+ wallet.lnworker.on_channels_updated()
self.need_update.set()
# Once GUI has been initialized check if we want to announce something since the callback has been called before the GUI was initialized
# update menus
@@ -609,6 +609,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
view_menu = menubar.addMenu(_("&View"))
add_toggle_action(view_menu, self.addresses_tab)
add_toggle_action(view_menu, self.utxo_tab)
+ add_toggle_action(view_menu, self.channels_tab)
add_toggle_action(view_menu, self.contacts_tab)
add_toggle_action(view_menu, self.console_tab)
@@ -877,9 +878,10 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
self.invoice_list.update()
self.update_completions()
- def create_lightning_channels_tab(self, wallet):
- self.lightning_channels_list = LightningChannelsList(self, wallet.lnworker)
- return self.lightning_channels_list
+ def create_channels_tab(self, wallet):
+ self.channels_list = ChannelsList(self)
+ t = self.channels_list.get_toolbar()
+ return self.create_list_tab(self.channels_list, t)
def create_history_tab(self):
self.history_model = HistoryModel(self)
@@ -1955,8 +1957,8 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
w.searchable_list = l
vbox = QVBoxLayout()
w.setLayout(vbox)
- vbox.setContentsMargins(0, 0, 0, 0)
- vbox.setSpacing(0)
+ #vbox.setContentsMargins(0, 0, 0, 0)
+ #vbox.setSpacing(0)
if toolbar:
vbox.addLayout(toolbar)
vbox.addWidget(l)
diff --git a/gui/qt/channels_list.py b/gui/qt/channels_list.py
@@ -0,0 +1,76 @@
+# -*- coding: utf-8 -*-
+from PyQt5 import QtCore, QtWidgets
+from electrum.util import inv_dict, bh2u
+from electrum.i18n import _
+from .util import MyTreeWidget, SortableTreeWidgetItem
+
+class ChannelsList(MyTreeWidget):
+ update_rows = QtCore.pyqtSignal(list)
+ update_single_row = QtCore.pyqtSignal(dict)
+
+ def __init__(self, parent):
+ MyTreeWidget.__init__(self, parent, self.create_menu, [_('Node ID'), _('Capacity'), _('Balance')], 0)
+ self.main_window = parent
+ self.update_rows.connect(self.do_update_rows)
+ self.update_single_row.connect(self.do_update_single_row)
+
+ def format_fields(self, chan):
+ return [bh2u(chan.node_id), self.parent.format_amount(chan.constraints.capacity), self.parent.format_amount(chan.local_state.amount_msat//1000)]
+
+ def create_menu(self, position):
+ menu = QtWidgets.QMenu()
+ cur = self.currentItem()
+ def close():
+ print("closechannel result", self.parent.network.lnworker.close_channel_from_other_thread(cur.di))
+ menu.addAction(_("Close channel"), close)
+ menu.exec_(self.viewport().mapToGlobal(position))
+
+ @QtCore.pyqtSlot(dict)
+ def do_update_single_row(self, chan):
+ items = self.findItems(chan.channel_id, QtCore.Qt.UserRole|QtCore.Qt.MatchContains|QtCore.Qt.MatchRecursive, column=1)
+ for item in items:
+ for i, v in enumerate(self.format_fields(chan)):
+ item.setData(i, QtCore.Qt.DisplayRole, v)
+
+ @QtCore.pyqtSlot(list)
+ def do_update_rows(self, channels):
+ self.clear()
+ for chan in channels:
+ item = SortableTreeWidgetItem(self.format_fields(chan))
+ item.setData(0, QtCore.Qt.UserRole, chan.channel_id)
+ self.insertTopLevelItem(0, item)
+
+ def get_toolbar(self):
+ nodeid_inp = QtWidgets.QLineEdit(self)
+ local_amt_inp = QtWidgets.QLineEdit(self, text='200000')
+ push_amt_inp = QtWidgets.QLineEdit(self, text='0')
+ button = QtWidgets.QPushButton(_('Open channel'), self)
+ button.clicked.connect(lambda: self.main_window.protect(self.open_channel, (nodeid_inp, local_amt_inp, push_amt_inp)))
+ l=QtWidgets.QVBoxLayout(self)
+ h=QtWidgets.QGridLayout(self)
+ nodeid_label = QtWidgets.QLabel(self)
+ nodeid_label.setText(_("Node ID"))
+ local_amt_label = QtWidgets.QLabel(self)
+ local_amt_label.setText("Local amount (sat)")
+ push_amt_label = QtWidgets.QLabel(self)
+ push_amt_label.setText("Push amount (sat)")
+ h.addWidget(nodeid_label, 0, 0)
+ h.addWidget(local_amt_label, 0, 1)
+ h.addWidget(push_amt_label, 0, 2)
+ h.addWidget(nodeid_inp, 1, 0)
+ h.addWidget(local_amt_inp, 1, 1)
+ h.addWidget(push_amt_inp, 1, 2)
+ h.addWidget(button, 1, 3)
+ h.setColumnStretch(0, 3)
+ h.setColumnStretch(1, 1)
+ h.setColumnStretch(2, 1)
+ h.setColumnStretch(3, 1)
+ return h
+
+ def open_channel(self, nodeIdInput, local_amt_inp, push_amt_inp, password):
+ node_id = str(nodeIdInput.text())
+ local_amt = int(local_amt_inp.text())
+ push_amt = int(push_amt_inp.text())
+ assert local_amt >= 200000
+ assert local_amt >= push_amt
+ obj = self.parent.network.lnworker.open_channel(node_id, local_amt, push_amt, password)
diff --git a/gui/qt/lightning_channels_list.py b/gui/qt/lightning_channels_list.py
@@ -1,124 +0,0 @@
-# -*- coding: utf-8 -*-
-import binascii, base64
-from PyQt5 import QtCore, QtWidgets
-from collections import OrderedDict
-import logging
-import traceback
-
-# https://api.lightning.community/#listchannels
-mapping = {0: "chan_id"}
-revMapp = {"chan_id": 0}
-datatable = OrderedDict([])
-
-class MyTableRow(QtWidgets.QTreeWidgetItem):
- def __init__(self, di):
- strs = [str(di[mapping[key]]) for key in range(len(mapping))]
- super(MyTableRow, self).__init__(strs)
- assert isinstance(di, dict)
- self.di = di
- def __getitem__(self, idx):
- return self.di[idx]
- def __setitem__(self, idx, val):
- self.di[idx] = val
- try:
- self.setData(revMapp[idx], QtCore.Qt.DisplayRole, '{0}'.format(val))
- except KeyError:
- logging.warning("Lightning Channel field %s unknown", idx)
- def __str__(self):
- return str(self.di)
-
-def addChannelRow(new):
- made = MyTableRow(new)
- datatable[new["chan_id"]] = made
- datatable.move_to_end(new["chan_id"], last=False)
- return made
-
-
-class LightningChannelsList(QtWidgets.QWidget):
- update_rows = QtCore.pyqtSignal(dict)
- update_single_row = QtCore.pyqtSignal(dict)
-
- def open_channel(self, nodeIdInput, local_amt_inp, push_amt_inp, password):
- node_id = str(nodeIdInput.text())
- print("creating channel with {}".format(node_id))
- local_amt = int(local_amt_inp.text())
- push_amt = int(push_amt_inp.text())
- assert local_amt >= 200000
- assert local_amt >= push_amt
- obj = self.lnworker.open_channel(node_id, local_amt, push_amt, password)
-
- def create_menu(self, position):
- menu = QtWidgets.QMenu()
- cur = self._tv.currentItem()
- def close():
- print("closechannel result", lnworker.close_channel_from_other_thread(cur.di))
- menu.addAction("Close channel", close)
- menu.exec_(self._tv.viewport().mapToGlobal(position))
-
- @QtCore.pyqtSlot(dict)
- def do_update_single_row(self, new):
- try:
- obj = datatable[new["chan_id"]]
- except KeyError:
- print("lightning chan_id {} unknown!".format(new["chan_id"]))
- else:
- for k, v in new.items():
- try:
- if obj[k] != v: obj[k] = v
- except KeyError:
- obj[k] = v
-
- @QtCore.pyqtSlot(dict)
- def do_update_rows(self, obj):
- self._tv.clear()
- for i in obj["channels"]:
- self._tv.insertTopLevelItem(0, addChannelRow(i))
-
- def __init__(self, parent, lnworker):
- QtWidgets.QWidget.__init__(self, parent)
- self.main_window = parent
-
- self.update_rows.connect(self.do_update_rows)
- self.update_single_row.connect(self.do_update_single_row)
-
- self.lnworker = lnworker
- lnworker.register_callback(self.update_rows.emit, ['channels_updated'])
- lnworker.register_callback(self.update_single_row.emit, ['channel_updated'])
-
- self._tv=QtWidgets.QTreeWidget(self)
- self._tv.setHeaderLabels([mapping[i] for i in range(len(mapping))])
- self._tv.setColumnCount(len(mapping))
- self._tv.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
- self._tv.customContextMenuRequested.connect(self.create_menu)
-
- nodeid_inp = QtWidgets.QLineEdit(self)
- local_amt_inp = QtWidgets.QLineEdit(self, text='200000')
- push_amt_inp = QtWidgets.QLineEdit(self, text='0')
- button = QtWidgets.QPushButton('Open channel', self)
- button.clicked.connect(lambda: self.main_window.protect(self.open_channel, (nodeid_inp, local_amt_inp, push_amt_inp)))
-
- l=QtWidgets.QVBoxLayout(self)
- h=QtWidgets.QGridLayout(self)
- nodeid_label = QtWidgets.QLabel(self)
- nodeid_label.setText("Node ID")
- local_amt_label = QtWidgets.QLabel(self)
- local_amt_label.setText("Local amount (sat)")
- push_amt_label = QtWidgets.QLabel(self)
- push_amt_label.setText("Push amount (sat)")
- h.addWidget(nodeid_label, 0, 0)
- h.addWidget(local_amt_label, 0, 1)
- h.addWidget(push_amt_label, 0, 2)
-
- h.addWidget(nodeid_inp, 1, 0)
- h.addWidget(local_amt_inp, 1, 1)
- h.addWidget(push_amt_inp, 1, 2)
- h.addWidget(button, 1, 3)
- h.setColumnStretch(0, 3)
- h.setColumnStretch(1, 1)
- h.setColumnStretch(2, 1)
- h.setColumnStretch(3, 1)
- l.addLayout(h)
- l.addWidget(self._tv)
-
- self.resize(2500,1000)
- lnworker.on_channels_updated()
diff --git a/gui/qt/lightning_invoice_list.py b/gui/qt/lightning_invoice_list.py
@@ -1,153 +0,0 @@
-# -*- coding: utf-8 -*-
-import base64
-import binascii
-from PyQt5 import QtCore, QtWidgets
-from collections import OrderedDict
-import logging
-from .qrcodewidget import QRDialog
-from PyQt5.QtCore import pyqtSignal, pyqtSlot
-
-mapping = {0: "r_hash", 1: "pay_req", 2: "settled"}
-revMapp = {"r_hash": 0, "pay_req": 1, "settled": 2}
-datatable = OrderedDict([])
-idx = 0
-
-class MyTableRow(QtWidgets.QTreeWidgetItem):
- def __init__(self, di):
- if "settled" not in di:
- di["settled"] = False
- strs = [str(di[mapping[key]]) for key in range(len(mapping))]
- print(strs)
- super(MyTableRow, self).__init__(strs)
- assert isinstance(di, dict)
- self.di = di
- def __getitem__(self, idx):
- return self.di[idx]
- def __setitem__(self, idx, val):
- self.di[idx] = val
- try:
- self.setData(revMapp[idx], QtCore.Qt.DisplayRole, '{0}'.format(val))
- except KeyError:
- logging.warning("Lightning Invoice field %s unknown", idx)
- def __str__(self):
- return str(self.di)
-
-def addInvoiceRow(new):
- made = MyTableRow(new)
- datatable[new["r_hash"]] = made
- datatable.move_to_end(new["r_hash"], last=False)
- return made
-
-class LightningInvoiceList(QtWidgets.QWidget):
- invoice_added_signal = QtCore.pyqtSignal(dict)
-
- @QtCore.pyqtSlot(dict)
- def invoice_added_handler(self, di):
- self._tv.insertTopLevelItem(0, addInvoiceRow(invoice))
-
- def clickHandler(self, numInput, treeView, lnworker):
- amt = numInput.value()
- if amt < 1:
- print("value too small")
- return
- print("creating invoice with value {}".format(amt))
- global idx
- #obj = {
- # "r_hash": binascii.hexlify((int.from_bytes(bytearray.fromhex("9500edb0994b7bc23349193486b25c82097045db641f35fa988c0e849acdec29"), "big")+idx).to_bytes(byteorder="big", length=32)).decode("ascii"),
- # "pay_req": "lntb81920n1pdf258s" + str(idx),
- # "settled": False
- #}
- #treeView.insertTopLevelItem(0, addInvoiceRow(obj))
- idx += 1
- lnworker.add_invoice(amt)
-
- def create_menu(self, position):
- menu = QtWidgets.QMenu()
- pay_req = self._tv.currentItem()["pay_req"]
- cb = QtWidgets.QApplication.instance().clipboard()
- def copy():
- print(pay_req)
- cb.setText(pay_req)
- def qr():
- d = QRDialog(pay_req, self, "Lightning invoice")
- d.exec_()
- menu.addAction("Copy payment request", copy)
- menu.addAction("Show payment request as QR code", qr)
- menu.exec_(self._tv.viewport().mapToGlobal(position))
-
- payment_received_signal = pyqtSignal(dict)
-
- @pyqtSlot(dict)
- def paymentReceived(self, new):
- try:
- obj = datatable[new["r_hash"]]
- except KeyError:
- print("lightning payment invoice r_hash {} unknown!".format(new["r_hash"]))
- else:
- for k, v in new.items():
- try:
- if obj[k] != v: obj[k] = v
- except KeyError:
- obj[k] = v
-
- def __init__(self, parent, lnworker):
- QtWidgets.QWidget.__init__(self, parent)
-
- self.payment_received_signal.connect(self.paymentReceived)
- self.invoice_added_signal.connect(self.invoice_added_handler)
-
- #lnworker.subscribe_payment_received_from_other_thread(self.payment_received_signal.emit)
- #lnworker.subscribe_invoice_added_from_other_thread(self.invoice_added_signal.emit)
-
- self._tv=QtWidgets.QTreeWidget(self)
- self._tv.setHeaderLabels([mapping[i] for i in range(len(mapping))])
- self._tv.setColumnCount(len(mapping))
- self._tv.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
- self._tv.customContextMenuRequested.connect(self.create_menu)
-
- class SatoshiCountSpinBox(QtWidgets.QSpinBox):
- def keyPressEvent(self2, e):
- super(SatoshiCountSpinBox, self2).keyPressEvent(e)
- if QtCore.Qt.Key_Return == e.key():
- self.clickHandler(self2, self._tv, lnworker)
-
- numInput = SatoshiCountSpinBox(self)
-
- button = QtWidgets.QPushButton('Add invoice', self)
- button.clicked.connect(lambda: self.clickHandler(numInput, self._tv, lnworker))
-
- l=QtWidgets.QVBoxLayout(self)
- h=QtWidgets.QGridLayout(self)
- h.addWidget(numInput, 0, 0)
- h.addWidget(button, 0, 1)
- #h.addItem(QtWidgets.QSpacerItem(100, 200, QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred), 0, 2)
- #h.setSizePolicy(
- h.setColumnStretch(0, 1)
- h.setColumnStretch(1, 1)
- h.setColumnStretch(2, 2)
- l.addLayout(h)
- l.addWidget(self._tv)
-
- self.resize(2500,1000)
-
-def tick():
- key = "9500edb0994b7bc23349193486b25c82097045db641f35fa988c0e849acdec29"
- if not key in datatable:
- return
- row = datatable[key]
- row["settled"] = not row["settled"]
- print("data changed")
-
-if __name__=="__main__":
- from sys import argv, exit
-
- a=QtWidgets.QApplication(argv)
-
- w=LightningInvoiceList()
- w.show()
- w.raise_()
-
- timer = QtCore.QTimer()
- timer.timeout.connect(tick)
- timer.start(1000)
- exit(a.exec_())
diff --git a/lib/lnworker.py b/lib/lnworker.py
@@ -135,7 +135,7 @@ class LNWorker(PrintError):
dumped = serialize_channels(self.channels)
self.wallet.storage.put("channels", dumped)
self.wallet.storage.write()
- self.trigger_callback('channel_updated', {"chan_id": openchannel.channel_id})
+ self.network.trigger_callback('channel', openchannel)
def save_short_chan_id(self, chan):
"""
@@ -188,8 +188,7 @@ class LNWorker(PrintError):
self.on_channels_updated()
def on_channels_updated(self):
- std_chan = [{"chan_id": chan.channel_id} for chan in self.channels.values()]
- self.trigger_callback('channels_updated', {'channels':std_chan})
+ self.network.trigger_callback('channels', list(self.channels.values()))
def open_channel(self, node_id, local_amt_sat, push_amt_sat, pw):
coro = self._open_channel_coroutine(node_id, local_amt_sat, push_amt_sat, None if pw == "" else pw)
@@ -199,7 +198,7 @@ class LNWorker(PrintError):
coro = self._pay_coroutine(invoice)
return asyncio.run_coroutine_threadsafe(coro, self.network.asyncio_loop)
- # not aiosafe because we call .result() which will propagate an exception
+ @aiosafe
async def _pay_coroutine(self, invoice):
openchannel = next(iter(self.channels.values()))
addr = lndecode(invoice, expected_hrp=constants.net.SEGWIT_HRP)
@@ -233,19 +232,3 @@ class LNWorker(PrintError):
def list_channels(self):
return serialize_channels(self.channels)
-
- def register_callback(self, callback, events):
- with self.lock:
- for event in events:
- self.callbacks[event].append(callback)
-
- def unregister_callback(self, callback):
- with self.lock:
- for callbacks in self.callbacks.values():
- if callback in callbacks:
- callbacks.remove(callback)
-
- def trigger_callback(self, event, *args):
- with self.lock:
- callbacks = self.callbacks[event][:]
- [callback(*args) for callback in callbacks]