Browse Source

Clean up pick_random_server() and filter_protocol() and their

call sites.

Remove unused imports and redundant random pick of server
283
Neil Booth 10 years ago
parent
commit
01d8f79a75
  1. 6
      electrum
  2. 1
      gui/gtk.py
  3. 2
      gui/qt/network_dialog.py
  4. 46
      lib/network.py
  5. 4
      scripts/servers

6
electrum

@ -309,11 +309,7 @@ if __name__ == '__main__':
else: else:
password = prompt_password("Password (hit return if you do not wish to encrypt your wallet):") password = prompt_password("Password (hit return if you do not wish to encrypt your wallet):")
# if config.server is set, the user either passed the server on command line # If config doesn't have a server, network.py will pick a random one
# or chose it previously already. if he didn't pass a server on the command line,
# we just pick up a random one.
if not config.get('server'):
config.set_key('server', pick_random_server())
#fee = options.tx_fee if options.tx_fee else raw_input("fee (default:%s):" % (str(Decimal(wallet.fee)/100000000))) #fee = options.tx_fee if options.tx_fee else raw_input("fee (default:%s):" % (str(Decimal(wallet.fee)/100000000)))
#gap = options.gap_limit if options.gap_limit else raw_input("gap limit (default 5):") #gap = options.gap_limit if options.gap_limit else raw_input("gap limit (default 5):")

1
gui/gtk.py

@ -34,7 +34,6 @@ import platform
MONOSPACE_FONT = 'Lucida Console' if platform.system() == 'Windows' else 'monospace' MONOSPACE_FONT = 'Lucida Console' if platform.system() == 'Windows' else 'monospace'
from electrum.util import format_satoshis, parse_URI from electrum.util import format_satoshis, parse_URI
from electrum.network import DEFAULT_SERVERS
from electrum.bitcoin import MIN_RELAY_TX_FEE from electrum.bitcoin import MIN_RELAY_TX_FEE
def numbify(entry, is_int = False): def numbify(entry, is_int = False):

2
gui/qt/network_dialog.py

@ -23,7 +23,7 @@ import os.path, json, ast, traceback
from PyQt4.QtGui import * from PyQt4.QtGui import *
from PyQt4.QtCore import * from PyQt4.QtCore import *
from electrum import DEFAULT_SERVERS, DEFAULT_PORTS from electrum import DEFAULT_PORTS
from util import * from util import *

46
lib/network.py

@ -68,19 +68,19 @@ def parse_servers(result):
return servers return servers
def filter_protocol(hostmap = DEFAULT_SERVERS, protocol = 's'):
'''Filters the hostmap for those implementing protocol.
def filter_protocol(servers, p): The result is a list in serialized form.'''
l = [] eligible = []
for k, protocols in servers.items(): for host, portmap in hostmap.items():
if p in protocols: port = portmap.get(protocol)
s = serialize_server(k, protocols[p], p) if port:
l.append(s) eligible.append(serialize_server(host, port, protocol))
return l return eligible
def pick_random_server(hostmap = DEFAULT_SERVERS, protocol = 's', exclude_set = set()):
def pick_random_server(p='s'): eligible = list(set(filter_protocol(hostmap, protocol)) - exclude_set)
return random.choice( filter_protocol(DEFAULT_SERVERS,p) ) return random.choice(eligible) if eligible else None
from simple_config import SimpleConfig from simple_config import SimpleConfig
@ -149,7 +149,7 @@ class Network(util.DaemonThread):
except: except:
self.default_server = None self.default_server = None
if not self.default_server: if not self.default_server:
self.default_server = pick_random_server('s') self.default_server = pick_random_server()
self.irc_servers = {} # returned by interface (list from irc) self.irc_servers = {} # returned by interface (list from irc)
self.recent_servers = self.read_recent_servers() self.recent_servers = self.read_recent_servers()
@ -247,21 +247,6 @@ class Network(util.DaemonThread):
value = self.get_status_value(key) value = self.get_status_value(key)
self.response_queue.put({'method':'network.status', 'params':[key, value]}) self.response_queue.put({'method':'network.status', 'params':[key, value]})
def random_server(self):
choice_list = []
l = filter_protocol(self.get_servers(), self.protocol)
for s in l:
if s in self.pending_servers or s in self.disconnected_servers or s in self.interfaces.keys():
continue
else:
choice_list.append(s)
if not choice_list:
return
server = random.choice( choice_list )
return server
def get_parameters(self): def get_parameters(self):
host, port, protocol = deserialize_server(self.default_server) host, port, protocol = deserialize_server(self.default_server)
auto_connect = self.config.get('auto_cycle', True) auto_connect = self.config.get('auto_cycle', True)
@ -293,7 +278,8 @@ class Network(util.DaemonThread):
i.start() i.start()
def start_random_interface(self): def start_random_interface(self):
server = self.random_server() exclude_set = self.disconnected_servers.union(self.pending_servers).union(set(self.interfaces))
server = pick_random_server(self.get_servers(), self.protocol, exclude_set)
if server: if server:
self.start_interface(server) self.start_interface(server)

4
scripts/servers

@ -1,7 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
from electrum import SimpleConfig, set_verbosity from electrum import SimpleConfig, set_verbosity
from electrum.network import DEFAULT_SERVERS, filter_protocol from electrum.network import filter_protocol
import time, Queue import time, Queue
from collections import defaultdict from collections import defaultdict
@ -9,7 +9,7 @@ import util, json
set_verbosity(False) set_verbosity(False)
config = SimpleConfig() config = SimpleConfig()
servers = filter_protocol(DEFAULT_SERVERS,'t') servers = filter_protocol(protocol = 't')
results = util.send_request(servers, {'method':'blockchain.headers.subscribe', 'params':[]}) results = util.send_request(servers, {'method':'blockchain.headers.subscribe', 'params':[]})
d = defaultdict(int) d = defaultdict(int)

Loading…
Cancel
Save