2018-06-03 10:07:56 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-11-05 17:23:49 +01:00
|
|
|
import traceback
|
2018-10-10 13:47:23 +02:00
|
|
|
import asyncio
|
2018-12-04 18:56:30 +01:00
|
|
|
from PyQt5 import QtCore, QtWidgets, QtGui
|
2018-06-27 12:44:43 +02:00
|
|
|
from PyQt5.QtWidgets import *
|
|
|
|
|
|
|
|
|
|
from electrum.util import inv_dict, bh2u, bfh
|
2018-06-03 10:07:56 +02:00
|
|
|
from electrum.i18n import _
|
2019-02-09 10:29:33 +01:00
|
|
|
from electrum.lnchannel import Channel
|
2018-09-27 16:43:33 +02:00
|
|
|
from electrum.lnutil import LOCAL, REMOTE, ConnStringFormatError
|
2018-08-01 18:23:11 +02:00
|
|
|
|
2019-01-30 17:24:43 +01:00
|
|
|
from .util import MyTreeView, WindowModalDialog, Buttons, OkButton, CancelButton, EnterButton, WWLabel
|
2018-06-27 12:44:43 +02:00
|
|
|
from .amountedit import BTCAmountEdit
|
2018-11-19 18:09:43 +01:00
|
|
|
from .channel_details import ChannelDetailsDialog
|
2018-06-03 10:07:56 +02:00
|
|
|
|
2018-12-04 18:56:30 +01:00
|
|
|
class ChannelsList(MyTreeView):
|
2018-06-06 17:42:06 +02:00
|
|
|
update_rows = QtCore.pyqtSignal()
|
2018-10-11 17:15:25 +02:00
|
|
|
update_single_row = QtCore.pyqtSignal(Channel)
|
2018-06-03 10:07:56 +02:00
|
|
|
|
|
|
|
|
def __init__(self, parent):
|
2018-12-04 18:56:30 +01:00
|
|
|
super().__init__(parent, self.create_menu, 0)
|
|
|
|
|
self.setModel(QtGui.QStandardItemModel(self))
|
2018-06-03 10:07:56 +02:00
|
|
|
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):
|
2018-10-17 20:01:45 +02:00
|
|
|
labels = {}
|
|
|
|
|
for subject in (REMOTE, LOCAL):
|
2018-10-24 20:39:07 +02:00
|
|
|
bal_minus_htlcs = chan.balance_minus_outgoing_htlcs(subject)//1000
|
|
|
|
|
label = self.parent.format_amount(bal_minus_htlcs)
|
2019-01-21 21:27:27 +01:00
|
|
|
other = subject.inverted()
|
|
|
|
|
bal_other = chan.balance(other)//1000
|
|
|
|
|
bal_minus_htlcs_other = chan.balance_minus_outgoing_htlcs(other)//1000
|
2018-10-24 20:39:07 +02:00
|
|
|
if bal_other != bal_minus_htlcs_other:
|
|
|
|
|
label += ' (+' + self.parent.format_amount(bal_other - bal_minus_htlcs_other) + ')'
|
2018-10-17 20:01:45 +02:00
|
|
|
labels[subject] = label
|
2018-06-07 08:56:00 +02:00
|
|
|
return [
|
|
|
|
|
bh2u(chan.node_id),
|
2018-10-17 20:01:45 +02:00
|
|
|
labels[LOCAL],
|
|
|
|
|
labels[REMOTE],
|
2018-07-30 13:51:03 +02:00
|
|
|
chan.get_state()
|
2018-06-07 08:56:00 +02:00
|
|
|
]
|
2018-06-03 10:07:56 +02:00
|
|
|
|
|
|
|
|
def create_menu(self, position):
|
2018-10-24 18:26:05 +02:00
|
|
|
from .util import WaitingDialog
|
|
|
|
|
network = self.parent.network
|
|
|
|
|
lnworker = self.parent.wallet.lnworker
|
2018-06-27 12:44:43 +02:00
|
|
|
menu = QMenu()
|
2018-12-04 18:56:30 +01:00
|
|
|
idx = self.selectionModel().currentIndex()
|
|
|
|
|
item = self.model().itemFromIndex(idx)
|
2018-11-27 21:43:28 +01:00
|
|
|
if not item:
|
|
|
|
|
return
|
2018-12-04 18:56:30 +01:00
|
|
|
channel_id = idx.sibling(idx.row(), 0).data(QtCore.Qt.UserRole)
|
2018-10-24 18:26:05 +02:00
|
|
|
def on_success(txid):
|
|
|
|
|
self.main_window.show_error('Channel closed' + '\n' + txid)
|
|
|
|
|
def on_failure(exc_info):
|
2018-11-05 17:23:49 +01:00
|
|
|
type_, e, tb = exc_info
|
|
|
|
|
traceback.print_tb(tb)
|
2018-10-24 18:26:05 +02:00
|
|
|
self.main_window.show_error('Failed to close channel:\n{}'.format(repr(e)))
|
2018-06-03 10:07:56 +02:00
|
|
|
def close():
|
2018-10-24 18:26:05 +02:00
|
|
|
def task():
|
|
|
|
|
coro = lnworker.close_channel(channel_id)
|
|
|
|
|
return network.run_from_another_thread(coro)
|
|
|
|
|
WaitingDialog(self, 'please wait..', task, on_success, on_failure)
|
2018-10-24 17:36:07 +02:00
|
|
|
def force_close():
|
2018-10-24 18:26:05 +02:00
|
|
|
def task():
|
|
|
|
|
coro = lnworker.force_close_channel(channel_id)
|
|
|
|
|
return network.run_from_another_thread(coro)
|
2019-01-23 13:47:29 +01:00
|
|
|
if self.parent.question('Force-close channel?\nClaiming funds will not be immediately available.'):
|
|
|
|
|
WaitingDialog(self, 'please wait..', task, on_success, on_failure)
|
2018-11-19 18:09:43 +01:00
|
|
|
menu.addAction(_("Details..."), lambda: self.details(channel_id))
|
2018-10-24 17:36:07 +02:00
|
|
|
menu.addAction(_("Close channel"), close)
|
|
|
|
|
menu.addAction(_("Force-close channel"), force_close)
|
2018-06-03 10:07:56 +02:00
|
|
|
menu.exec_(self.viewport().mapToGlobal(position))
|
|
|
|
|
|
2018-11-19 18:09:43 +01:00
|
|
|
def details(self, channel_id):
|
|
|
|
|
assert self.parent.wallet
|
|
|
|
|
ChannelDetailsDialog(self.parent, channel_id).show()
|
|
|
|
|
|
2018-10-11 17:15:25 +02:00
|
|
|
@QtCore.pyqtSlot(Channel)
|
2018-06-03 10:07:56 +02:00
|
|
|
def do_update_single_row(self, chan):
|
2018-12-04 18:56:30 +01:00
|
|
|
for row in range(self.model().rowCount()):
|
|
|
|
|
item = self.model().item(row,0)
|
|
|
|
|
if item.data(QtCore.Qt.UserRole) == chan.channel_id:
|
|
|
|
|
for column, v in enumerate(self.format_fields(chan)):
|
|
|
|
|
self.model().item(row, column).setData(v, QtCore.Qt.DisplayRole)
|
2018-06-03 10:07:56 +02:00
|
|
|
|
2018-06-06 17:42:06 +02:00
|
|
|
@QtCore.pyqtSlot()
|
|
|
|
|
def do_update_rows(self):
|
2018-12-04 18:56:30 +01:00
|
|
|
self.model().clear()
|
2019-05-06 15:58:12 +02:00
|
|
|
self.update_headers([_('Node ID'), _('Local'), _('Remote'), _('Status')])
|
2018-06-06 17:42:06 +02:00
|
|
|
for chan in self.parent.wallet.lnworker.channels.values():
|
2018-12-04 18:56:30 +01:00
|
|
|
items = [QtGui.QStandardItem(x) for x in self.format_fields(chan)]
|
|
|
|
|
items[0].setData(chan.channel_id, QtCore.Qt.UserRole)
|
|
|
|
|
self.model().insertRow(0, items)
|
2018-06-03 10:07:56 +02:00
|
|
|
|
|
|
|
|
def get_toolbar(self):
|
2018-06-27 12:44:43 +02:00
|
|
|
h = QHBoxLayout()
|
|
|
|
|
h.addStretch()
|
2018-12-09 11:56:37 +01:00
|
|
|
h.addWidget(EnterButton(_('Open Channel'), self.new_channel_dialog))
|
2018-06-03 10:07:56 +02:00
|
|
|
return h
|
|
|
|
|
|
2018-12-09 11:56:37 +01:00
|
|
|
|
|
|
|
|
def statistics_dialog(self):
|
|
|
|
|
channel_db = self.parent.network.channel_db
|
|
|
|
|
capacity = self.parent.format_amount(channel_db.capacity()) + ' '+ self.parent.base_unit()
|
|
|
|
|
d = WindowModalDialog(self.parent, _('Lightning Network Statistics'))
|
|
|
|
|
d.setMinimumWidth(400)
|
|
|
|
|
vbox = QVBoxLayout(d)
|
|
|
|
|
h = QGridLayout()
|
|
|
|
|
h.addWidget(QLabel(_('Nodes') + ':'), 0, 0)
|
2019-02-01 20:59:59 +01:00
|
|
|
h.addWidget(QLabel('{}'.format(channel_db.num_nodes)), 0, 1)
|
2018-12-09 11:56:37 +01:00
|
|
|
h.addWidget(QLabel(_('Channels') + ':'), 1, 0)
|
2019-02-01 20:59:59 +01:00
|
|
|
h.addWidget(QLabel('{}'.format(channel_db.num_channels)), 1, 1)
|
2018-12-09 11:56:37 +01:00
|
|
|
h.addWidget(QLabel(_('Capacity') + ':'), 2, 0)
|
|
|
|
|
h.addWidget(QLabel(capacity), 2, 1)
|
|
|
|
|
vbox.addLayout(h)
|
|
|
|
|
vbox.addLayout(Buttons(OkButton(d)))
|
|
|
|
|
d.exec_()
|
2018-06-27 12:44:43 +02:00
|
|
|
|
|
|
|
|
def new_channel_dialog(self):
|
2018-07-14 19:39:28 +02:00
|
|
|
lnworker = self.parent.wallet.lnworker
|
2018-06-27 12:44:43 +02:00
|
|
|
d = WindowModalDialog(self.parent, _('Open Channel'))
|
2018-09-27 17:55:48 +02:00
|
|
|
d.setMinimumWidth(700)
|
2018-06-27 12:44:43 +02:00
|
|
|
vbox = QVBoxLayout(d)
|
|
|
|
|
h = QGridLayout()
|
|
|
|
|
local_nodeid = QLineEdit()
|
2018-10-05 15:37:47 +02:00
|
|
|
local_nodeid.setText(bh2u(lnworker.node_keypair.pubkey))
|
2018-06-27 12:44:43 +02:00
|
|
|
local_nodeid.setReadOnly(True)
|
|
|
|
|
local_nodeid.setCursorPosition(0)
|
|
|
|
|
remote_nodeid = QLineEdit()
|
|
|
|
|
local_amt_inp = BTCAmountEdit(self.parent.get_decimal_point)
|
|
|
|
|
local_amt_inp.setAmount(200000)
|
|
|
|
|
push_amt_inp = BTCAmountEdit(self.parent.get_decimal_point)
|
|
|
|
|
push_amt_inp.setAmount(0)
|
|
|
|
|
h.addWidget(QLabel(_('Your Node ID')), 0, 0)
|
|
|
|
|
h.addWidget(local_nodeid, 0, 1)
|
2018-08-01 18:23:11 +02:00
|
|
|
h.addWidget(QLabel(_('Remote Node ID or connection string or invoice')), 1, 0)
|
2018-06-27 12:44:43 +02:00
|
|
|
h.addWidget(remote_nodeid, 1, 1)
|
|
|
|
|
h.addWidget(QLabel('Local amount'), 2, 0)
|
|
|
|
|
h.addWidget(local_amt_inp, 2, 1)
|
|
|
|
|
h.addWidget(QLabel('Push amount'), 3, 0)
|
|
|
|
|
h.addWidget(push_amt_inp, 3, 1)
|
|
|
|
|
vbox.addLayout(h)
|
2018-09-27 17:55:48 +02:00
|
|
|
ok_button = OkButton(d)
|
|
|
|
|
ok_button.setDefault(True)
|
|
|
|
|
vbox.addLayout(Buttons(CancelButton(d), ok_button))
|
2018-07-14 19:39:28 +02:00
|
|
|
suggestion = lnworker.suggest_peer() or b''
|
|
|
|
|
remote_nodeid.setText(bh2u(suggestion))
|
|
|
|
|
remote_nodeid.setCursorPosition(0)
|
2018-06-27 12:44:43 +02:00
|
|
|
if not d.exec_():
|
|
|
|
|
return
|
|
|
|
|
local_amt = local_amt_inp.get_amount()
|
|
|
|
|
push_amt = push_amt_inp.get_amount()
|
2018-09-27 16:43:33 +02:00
|
|
|
connect_contents = str(remote_nodeid.text()).strip()
|
2018-10-11 18:10:31 +02:00
|
|
|
self.parent.open_channel(connect_contents, local_amt, push_amt)
|