commit b2cfaddff25b93189e8fb26f04fe9667958fe2d6
parent adc3784bc24ab0e83e412846a4bfc776b33feef7
Author: SomberNight <somber.night@protonmail.com>
Date: Thu, 16 Apr 2020 20:30:53 +0200
network.NetworkParameters: merge host+port+protocol into "server" field
Diffstat:
7 files changed, 43 insertions(+), 37 deletions(-)
diff --git a/electrum/commands.py b/electrum/commands.py
@@ -187,7 +187,7 @@ class Commands:
net_params = self.network.get_parameters()
response = {
'path': self.network.config.path,
- 'server': net_params.host,
+ 'server': net_params.server.host,
'blockchain_height': self.network.get_local_height(),
'server_height': self.network.get_server_height(),
'spv_nodes': len(self.network.get_interfaces()),
diff --git a/electrum/gui/kivy/main_window.py b/electrum/gui/kivy/main_window.py
@@ -145,6 +145,19 @@ class ElectrumWindow(App):
servers = self.network.get_servers()
ChoiceDialog(_('Choose a server'), sorted(servers), popup.ids.host.text, cb2).open()
+ def maybe_switch_to_server(self, *, host: str, port: str):
+ from electrum.interface import ServerAddr
+ net_params = self.network.get_parameters()
+ try:
+ server = ServerAddr(host=host,
+ port=port,
+ protocol=net_params.server.protocol)
+ except Exception as e:
+ self.show_error(_("Invalid server details: {}").format(repr(e)))
+ return
+ net_params = net_params._replace(server=server)
+ self.network.run_from_another_thread(self.network.set_parameters(net_params))
+
def choose_blockchain_dialog(self, dt):
from .uix.dialogs.choice_dialog import ChoiceDialog
chains = self.network.get_blockchains()
@@ -348,8 +361,8 @@ class ElectrumWindow(App):
self.num_blocks = self.network.get_local_height()
self.num_nodes = len(self.network.get_interfaces())
net_params = self.network.get_parameters()
- self.server_host = net_params.host
- self.server_port = net_params.port
+ self.server_host = net_params.server.host
+ self.server_port = str(net_params.server.port)
self.auto_connect = net_params.auto_connect
self.oneserver = net_params.oneserver
self.proxy_config = net_params.proxy if net_params.proxy else {}
@@ -814,7 +827,7 @@ class ElectrumWindow(App):
if interface:
self.server_host = interface.host
else:
- self.server_host = str(net_params.host) + ' (connecting...)'
+ self.server_host = str(net_params.server.host) + ' (connecting...)'
self.proxy_config = net_params.proxy or {}
self.update_proxy_str(self.proxy_config)
diff --git a/electrum/gui/kivy/uix/ui_screens/server.kv b/electrum/gui/kivy/uix/ui_screens/server.kv
@@ -1,3 +1,5 @@
+#:import ServerAddr electrum.interface.ServerAddr
+
Popup:
id: nd
title: _('Server')
@@ -23,7 +25,7 @@ Popup:
height: '36dp'
size_hint_x: 3
size_hint_y: None
- text: app.network.get_parameters().host
+ text: app.network.get_parameters().server.host
Label:
height: '36dp'
size_hint_x: 1
@@ -36,7 +38,7 @@ Popup:
height: '36dp'
size_hint_x: 3
size_hint_y: None
- text: app.network.get_parameters().port
+ text: str(app.network.get_parameters().server.port)
Widget
Button:
id: chooser
@@ -56,7 +58,5 @@ Popup:
height: '48dp'
text: _('OK')
on_release:
- net_params = app.network.get_parameters()
- net_params = net_params._replace(host=str(root.ids.host.text), port=str(root.ids.port.text))
- app.network.run_from_another_thread(app.network.set_parameters(net_params))
+ app.maybe_switch_to_server(host=str(root.ids.host.text), port=str(root.ids.port.text))
nd.dismiss()
diff --git a/electrum/gui/qt/main_window.py b/electrum/gui/qt/main_window.py
@@ -738,7 +738,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
def donate_to_server(self):
d = self.network.get_donation_address()
if d:
- host = self.network.get_parameters().host
+ host = self.network.get_parameters().server.host
self.pay_to_URI('bitcoin:%s?message=donation for %s'%(d, host))
else:
self.show_error(_('No donation address for this server'))
diff --git a/electrum/gui/qt/network_dialog.py b/electrum/gui/qt/network_dialog.py
@@ -364,11 +364,11 @@ class NetworkChoiceLayout(object):
def update(self):
net_params = self.network.get_parameters()
- host, port, protocol = net_params.host, net_params.port, net_params.protocol
+ server = net_params.server
proxy_config, auto_connect = net_params.proxy, net_params.auto_connect
if not self.server_host.hasFocus() and not self.server_port.hasFocus():
- self.server_host.setText(host)
- self.server_port.setText(str(port))
+ self.server_host.setText(server.host)
+ self.server_port.setText(str(server.port))
self.autoconnect_cb.setChecked(auto_connect)
interface = self.network.interface
@@ -448,8 +448,13 @@ class NetworkChoiceLayout(object):
def set_server(self):
net_params = self.network.get_parameters()
- net_params = net_params._replace(host=str(self.server_host.text()),
- port=str(self.server_port.text()),
+ try:
+ server = ServerAddr(host=str(self.server_host.text()),
+ port=str(self.server_port.text()),
+ protocol=net_params.server.protocol)
+ except Exception:
+ return
+ net_params = net_params._replace(server=server,
auto_connect=self.autoconnect_cb.isChecked())
self.network.run_from_another_thread(self.network.set_parameters(net_params))
diff --git a/electrum/gui/text.py b/electrum/gui/text.py
@@ -409,7 +409,7 @@ class ElectrumGui:
if not self.network:
return
net_params = self.network.get_parameters()
- host, port, protocol = net_params.host, net_params.port, net_params.protocol
+ server_addr = net_params.server
proxy_config, auto_connect = net_params.proxy, net_params.auto_connect
srv = 'auto-connect' if auto_connect else str(self.network.default_server)
out = self.run_dialog('Network', [
@@ -426,12 +426,11 @@ class ElectrumGui:
except Exception:
self.show_message("Error:" + server_str + "\nIn doubt, type \"auto-connect\"")
return False
- host = server_addr.host
- port = str(server_addr.port)
- protocol = server_addr.protocol
if out.get('server') or out.get('proxy'):
proxy = electrum.network.deserialize_proxy(out.get('proxy')) if out.get('proxy') else proxy_config
- net_params = NetworkParameters(host, port, protocol, proxy, auto_connect)
+ net_params = NetworkParameters(server=server_addr,
+ proxy=proxy,
+ auto_connect=auto_connect)
self.network.run_from_another_thread(self.network.set_parameters(net_params))
def settings_dialog(self):
diff --git a/electrum/network.py b/electrum/network.py
@@ -144,9 +144,7 @@ def pick_random_server(hostmap=None, *, allowed_protocols: Iterable[str],
class NetworkParameters(NamedTuple):
- host: str
- port: str
- protocol: str
+ server: ServerAddr
proxy: Optional[dict]
auto_connect: bool
oneserver: bool = False
@@ -483,10 +481,7 @@ class Network(Logger, NetworkRetryManager[ServerAddr]):
util.trigger_callback(key, self.get_status_value(key))
def get_parameters(self) -> NetworkParameters:
- server = self.default_server
- return NetworkParameters(host=server.host,
- port=str(server.port),
- protocol=server.protocol,
+ return NetworkParameters(server=self.default_server,
proxy=self.proxy,
auto_connect=self.auto_connect,
oneserver=self.oneserver)
@@ -585,13 +580,12 @@ class Network(Logger, NetworkRetryManager[ServerAddr]):
util.trigger_callback('proxy_set', self.proxy)
@log_exceptions
- async def set_parameters(self, net_params: NetworkParameters): # TODO
+ async def set_parameters(self, net_params: NetworkParameters):
proxy = net_params.proxy
proxy_str = serialize_proxy(proxy)
- host, port, protocol = net_params.host, net_params.port, net_params.protocol
+ server = net_params.server
# sanitize parameters
try:
- server = ServerAddr(host, port, protocol=protocol)
if proxy:
proxy_modes.index(proxy['mode']) + 1
int(proxy['port'])
@@ -1112,10 +1106,7 @@ class Network(Logger, NetworkRetryManager[ServerAddr]):
chosen_iface = random.choice(interfaces_on_selected_chain) # type: Interface
# switch to server (and save to config)
net_params = self.get_parameters()
- server = chosen_iface.server
- net_params = net_params._replace(host=server.host,
- port=str(server.port),
- protocol=server.protocol)
+ net_params = net_params._replace(server=chosen_iface.server)
await self.set_parameters(net_params)
async def follow_chain_given_server(self, server: ServerAddr) -> None:
@@ -1126,9 +1117,7 @@ class Network(Logger, NetworkRetryManager[ServerAddr]):
self._set_preferred_chain(iface.blockchain)
# switch to server (and save to config)
net_params = self.get_parameters()
- net_params = net_params._replace(host=server.host,
- port=str(server.port),
- protocol=server.protocol)
+ net_params = net_params._replace(server=server)
await self.set_parameters(net_params)
def get_local_height(self):