mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-09-03 02:35:20 +00:00
Separate open_channel dialog. In open_channel_coroutine, use host and port from channel announcements
This commit is contained in:
parent
dbdabcfc5d
commit
c7e47b74a9
5 changed files with 71 additions and 44 deletions
|
@ -425,6 +425,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
|
||||||
self.update_console()
|
self.update_console()
|
||||||
self.clear_receive_tab()
|
self.clear_receive_tab()
|
||||||
self.request_list.update()
|
self.request_list.update()
|
||||||
|
self.channels_list.update()
|
||||||
self.tabs.show()
|
self.tabs.show()
|
||||||
self.init_geometry()
|
self.init_geometry()
|
||||||
if self.config.get('hide_gui') and self.gui_object.tray.isVisible():
|
if self.config.get('hide_gui') and self.gui_object.tray.isVisible():
|
||||||
|
|
|
@ -300,6 +300,7 @@ class Network(Logger):
|
||||||
self._set_status('disconnected')
|
self._set_status('disconnected')
|
||||||
|
|
||||||
# lightning network
|
# lightning network
|
||||||
|
self.lightning_nodes = {}
|
||||||
self.channel_db = lnrouter.ChannelDB()
|
self.channel_db = lnrouter.ChannelDB()
|
||||||
self.path_finder = lnrouter.LNPathFinder(self.channel_db)
|
self.path_finder = lnrouter.LNPathFinder(self.channel_db)
|
||||||
self.lnwatcher = lnwatcher.LNWatcher(self)
|
self.lnwatcher = lnwatcher.LNWatcher(self)
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from PyQt5 import QtCore, QtWidgets
|
from PyQt5 import QtCore, QtWidgets
|
||||||
from electrum.util import inv_dict, bh2u
|
from PyQt5.QtWidgets import *
|
||||||
|
|
||||||
|
from electrum.util import inv_dict, bh2u, bfh
|
||||||
from electrum.i18n import _
|
from electrum.i18n import _
|
||||||
from electrum.lnbase import OpenChannel
|
from electrum.lnbase import OpenChannel
|
||||||
from .util import MyTreeWidget, SortableTreeWidgetItem
|
from .util import MyTreeWidget, SortableTreeWidgetItem, WindowModalDialog, Buttons, OkButton, CancelButton
|
||||||
|
from .amountedit import BTCAmountEdit
|
||||||
|
|
||||||
class ChannelsList(MyTreeWidget):
|
class ChannelsList(MyTreeWidget):
|
||||||
update_rows = QtCore.pyqtSignal()
|
update_rows = QtCore.pyqtSignal()
|
||||||
|
@ -14,6 +17,7 @@ class ChannelsList(MyTreeWidget):
|
||||||
self.main_window = parent
|
self.main_window = parent
|
||||||
self.update_rows.connect(self.do_update_rows)
|
self.update_rows.connect(self.do_update_rows)
|
||||||
self.update_single_row.connect(self.do_update_single_row)
|
self.update_single_row.connect(self.do_update_single_row)
|
||||||
|
self.status = QLabel('')
|
||||||
|
|
||||||
def format_fields(self, chan):
|
def format_fields(self, chan):
|
||||||
status = self.parent.wallet.lnworker.channel_state[chan.channel_id]
|
status = self.parent.wallet.lnworker.channel_state[chan.channel_id]
|
||||||
|
@ -25,7 +29,7 @@ class ChannelsList(MyTreeWidget):
|
||||||
]
|
]
|
||||||
|
|
||||||
def create_menu(self, position):
|
def create_menu(self, position):
|
||||||
menu = QtWidgets.QMenu()
|
menu = QMenu()
|
||||||
channel_id = self.currentItem().data(0, QtCore.Qt.UserRole)
|
channel_id = self.currentItem().data(0, QtCore.Qt.UserRole)
|
||||||
print('ID', bh2u(channel_id))
|
print('ID', bh2u(channel_id))
|
||||||
def close():
|
def close():
|
||||||
|
@ -50,35 +54,59 @@ class ChannelsList(MyTreeWidget):
|
||||||
self.insertTopLevelItem(0, item)
|
self.insertTopLevelItem(0, item)
|
||||||
|
|
||||||
def get_toolbar(self):
|
def get_toolbar(self):
|
||||||
nodeid_inp = QtWidgets.QLineEdit(self)
|
b = QPushButton(_('Open Channel'))
|
||||||
local_amt_inp = QtWidgets.QLineEdit(self, text='200000')
|
b.clicked.connect(self.new_channel_dialog)
|
||||||
push_amt_inp = QtWidgets.QLineEdit(self, text='0')
|
h = QHBoxLayout()
|
||||||
button = QtWidgets.QPushButton(_('Open channel'), self)
|
h.addWidget(self.status)
|
||||||
button.clicked.connect(lambda: self.main_window.protect(self.open_channel, (nodeid_inp, local_amt_inp, push_amt_inp)))
|
h.addStretch()
|
||||||
h = QtWidgets.QGridLayout()
|
h.addWidget(b)
|
||||||
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
|
return h
|
||||||
|
|
||||||
def open_channel(self, nodeIdInput, local_amt_inp, push_amt_inp, password):
|
def on_update(self):
|
||||||
node_id = str(nodeIdInput.text())
|
n = len(self.parent.network.lightning_nodes)
|
||||||
local_amt = int(local_amt_inp.text())
|
np = len(self.parent.wallet.lnworker.peers)
|
||||||
push_amt = int(push_amt_inp.text())
|
self.status.setText(_('{} peers, {} nodes').format(np, n))
|
||||||
|
|
||||||
|
def new_channel_dialog(self):
|
||||||
|
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.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)
|
||||||
|
h.addWidget(QLabel(_('Remote Node ID')), 1, 0)
|
||||||
|
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)
|
||||||
|
vbox.addLayout(Buttons(CancelButton(d), OkButton(d)))
|
||||||
|
if not d.exec_():
|
||||||
|
return
|
||||||
|
nodeid_hex = str(remote_nodeid.text())
|
||||||
|
local_amt = local_amt_inp.get_amount()
|
||||||
|
push_amt = push_amt_inp.get_amount()
|
||||||
|
try:
|
||||||
|
node_id = bfh(nodeid_hex)
|
||||||
|
except:
|
||||||
|
self.parent.show_error(_('Invalid node ID'))
|
||||||
|
return
|
||||||
|
if node_id not in self.parent.wallet.lnworker.peers and node_id not in self.parent.network.lightning_nodes:
|
||||||
|
self.parent.show_error(_('Unknown node:') + ' ' + nodeid_hex)
|
||||||
|
return
|
||||||
assert local_amt >= 200000
|
assert local_amt >= 200000
|
||||||
assert local_amt >= push_amt
|
assert local_amt >= push_amt
|
||||||
obj = self.parent.wallet.lnworker.open_channel(node_id, local_amt, push_amt, password)
|
self.main_window.protect(self.open_channel, (node_id, local_amt, push_amt))
|
||||||
|
|
||||||
|
def open_channel(self, *args, **kwargs):
|
||||||
|
self.parent.wallet.lnworker.open_channel(*args, **kwargs)
|
||||||
|
|
|
@ -607,7 +607,6 @@ class Peer(PrintError):
|
||||||
self.announcement_signatures = defaultdict(asyncio.Queue)
|
self.announcement_signatures = defaultdict(asyncio.Queue)
|
||||||
self.update_fail_htlc = defaultdict(asyncio.Queue)
|
self.update_fail_htlc = defaultdict(asyncio.Queue)
|
||||||
self.localfeatures = (0x08 if request_initial_sync else 0)
|
self.localfeatures = (0x08 if request_initial_sync else 0)
|
||||||
self.nodes = {}
|
|
||||||
self.channels = lnworker.channels
|
self.channels = lnworker.channels
|
||||||
self.invoices = lnworker.invoices
|
self.invoices = lnworker.invoices
|
||||||
self.attempted_route = {}
|
self.attempted_route = {}
|
||||||
|
@ -757,7 +756,7 @@ class Peer(PrintError):
|
||||||
pass
|
pass
|
||||||
continue
|
continue
|
||||||
alias = payload['alias'].rstrip(b'\x00')
|
alias = payload['alias'].rstrip(b'\x00')
|
||||||
self.nodes[pubkey] = {
|
self.network.lightning_nodes[pubkey] = {
|
||||||
'alias': alias,
|
'alias': alias,
|
||||||
'addresses': addresses
|
'addresses': addresses
|
||||||
}
|
}
|
||||||
|
|
|
@ -90,8 +90,6 @@ class LNWorker(PrintError):
|
||||||
self.pubkey = ECPrivkey(self.privkey).get_public_key_bytes()
|
self.pubkey = ECPrivkey(self.privkey).get_public_key_bytes()
|
||||||
self.config = network.config
|
self.config = network.config
|
||||||
self.peers = {}
|
self.peers = {}
|
||||||
# view of the network
|
|
||||||
self.nodes = {} # received node announcements
|
|
||||||
self.channels = {x.channel_id: x for x in map(reconstruct_namedtuples, wallet.storage.get("channels", []))}
|
self.channels = {x.channel_id: x for x in map(reconstruct_namedtuples, wallet.storage.get("channels", []))}
|
||||||
self.invoices = wallet.storage.get('lightning_invoices', {})
|
self.invoices = wallet.storage.get('lightning_invoices', {})
|
||||||
peer_list = network.config.get('lightning_peers', node_list)
|
peer_list = network.config.get('lightning_peers', node_list)
|
||||||
|
@ -99,7 +97,7 @@ class LNWorker(PrintError):
|
||||||
for chan_id, chan in self.channels.items():
|
for chan_id, chan in self.channels.items():
|
||||||
self.network.lnwatcher.watch_channel(chan, self.on_channel_utxos)
|
self.network.lnwatcher.watch_channel(chan, self.on_channel_utxos)
|
||||||
for host, port, pubkey in peer_list:
|
for host, port, pubkey in peer_list:
|
||||||
self.add_peer(host, int(port), pubkey)
|
self.add_peer(host, int(port), bfh(pubkey))
|
||||||
# wait until we see confirmations
|
# wait until we see confirmations
|
||||||
self.network.register_callback(self.on_network_update, ['updated', 'verified']) # thread safe
|
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.on_network_update('updated') # shortcut (don't block) if funding tx locked and verified
|
||||||
|
@ -108,8 +106,7 @@ class LNWorker(PrintError):
|
||||||
assert type(node_id) is bytes
|
assert type(node_id) is bytes
|
||||||
return {x: y for (x, y) in self.channels.items() if y.node_id == node_id}
|
return {x: y for (x, y) in self.channels.items() if y.node_id == node_id}
|
||||||
|
|
||||||
def add_peer(self, host, port, pubkey):
|
def add_peer(self, host, port, node_id):
|
||||||
node_id = bfh(pubkey)
|
|
||||||
peer = Peer(self, host, int(port), node_id, request_initial_sync=self.config.get("request_initial_sync", True))
|
peer = Peer(self, host, int(port), node_id, request_initial_sync=self.config.get("request_initial_sync", True))
|
||||||
self.network.futures.append(asyncio.run_coroutine_threadsafe(peer.main_loop(), asyncio.get_event_loop()))
|
self.network.futures.append(asyncio.run_coroutine_threadsafe(peer.main_loop(), asyncio.get_event_loop()))
|
||||||
self.peers[node_id] = peer
|
self.peers[node_id] = peer
|
||||||
|
@ -171,14 +168,15 @@ class LNWorker(PrintError):
|
||||||
conf = self.wallet.get_tx_height(chan.funding_outpoint.txid)[1]
|
conf = self.wallet.get_tx_height(chan.funding_outpoint.txid)[1]
|
||||||
peer.on_network_update(chan, conf)
|
peer.on_network_update(chan, conf)
|
||||||
|
|
||||||
# not aiosafe because we call .result() which will propagate an exception
|
|
||||||
async def _open_channel_coroutine(self, node_id, amount_sat, push_sat, password):
|
async def _open_channel_coroutine(self, node_id, amount_sat, push_sat, password):
|
||||||
if node_id == "":
|
if node_id not in self.peers:
|
||||||
peer = next(iter(self.peers.values()))
|
node = self.network.lightning_nodes.get(node_id)
|
||||||
else:
|
if node is None:
|
||||||
peer = self.peers[bfh(node_id)]
|
return False
|
||||||
|
host, port = node['addresses'][0]
|
||||||
|
self.add_peer(host, port, node_id)
|
||||||
|
peer = self.peers[node_id]
|
||||||
openingchannel = await peer.channel_establishment_flow(self.wallet, self.config, password, amount_sat, push_sat * 1000, temp_channel_id=os.urandom(32))
|
openingchannel = await peer.channel_establishment_flow(self.wallet, self.config, password, amount_sat, push_sat * 1000, temp_channel_id=os.urandom(32))
|
||||||
self.print_error("SAVING OPENING CHANNEL")
|
|
||||||
self.save_channel(openingchannel)
|
self.save_channel(openingchannel)
|
||||||
self.on_channels_updated()
|
self.on_channels_updated()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue