electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 71046371ecb293a016f5777c3070fbdc933638ff
parent 117b809bab531ec5d8eb2aa0f20a00c8f922a6a2
Author: ThomasV <electrumdev@gmail.com>
Date:   Sun, 24 May 2015 16:43:17 +0200

Merge pull request #1242 from kyuupichan/pick_random_server

Clean up pick_random_server() and filter_protocol() and their
Diffstat:
Melectrum | 6+-----
Mgui/gtk.py | 1-
Mgui/qt/network_dialog.py | 2+-
Mlib/network.py | 46++++++++++++++++------------------------------
Mscripts/servers | 4++--
5 files changed, 20 insertions(+), 39 deletions(-)

diff --git a/electrum b/electrum @@ -309,11 +309,7 @@ if __name__ == '__main__': else: 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 - # 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()) + # If config doesn't have a server, network.py will pick a random one #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):") diff --git a/gui/gtk.py b/gui/gtk.py @@ -34,7 +34,6 @@ import platform MONOSPACE_FONT = 'Lucida Console' if platform.system() == 'Windows' else 'monospace' from electrum.util import format_satoshis, parse_URI -from electrum.network import DEFAULT_SERVERS from electrum.bitcoin import MIN_RELAY_TX_FEE def numbify(entry, is_int = False): diff --git a/gui/qt/network_dialog.py b/gui/qt/network_dialog.py @@ -23,7 +23,7 @@ import os.path, json, ast, traceback from PyQt4.QtGui import * from PyQt4.QtCore import * -from electrum import DEFAULT_SERVERS, DEFAULT_PORTS +from electrum import DEFAULT_PORTS from util import * diff --git a/lib/network.py b/lib/network.py @@ -68,19 +68,19 @@ def parse_servers(result): return servers - - -def filter_protocol(servers, p): - l = [] - for k, protocols in servers.items(): - if p in protocols: - s = serialize_server(k, protocols[p], p) - l.append(s) - return l - - -def pick_random_server(p='s'): - return random.choice( filter_protocol(DEFAULT_SERVERS,p) ) +def filter_protocol(hostmap = DEFAULT_SERVERS, protocol = 's'): + '''Filters the hostmap for those implementing protocol. + The result is a list in serialized form.''' + eligible = [] + for host, portmap in hostmap.items(): + port = portmap.get(protocol) + if port: + eligible.append(serialize_server(host, port, protocol)) + return eligible + +def pick_random_server(hostmap = DEFAULT_SERVERS, protocol = 's', exclude_set = set()): + eligible = list(set(filter_protocol(hostmap, protocol)) - exclude_set) + return random.choice(eligible) if eligible else None from simple_config import SimpleConfig @@ -149,7 +149,7 @@ class Network(util.DaemonThread): except: self.default_server = None 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.recent_servers = self.read_recent_servers() @@ -247,21 +247,6 @@ class Network(util.DaemonThread): value = self.get_status_value(key) 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): host, port, protocol = deserialize_server(self.default_server) auto_connect = self.config.get('auto_cycle', True) @@ -293,7 +278,8 @@ class Network(util.DaemonThread): i.start() 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: self.start_interface(server) diff --git a/scripts/servers b/scripts/servers @@ -1,7 +1,7 @@ #!/usr/bin/env python from electrum import SimpleConfig, set_verbosity -from electrum.network import DEFAULT_SERVERS, filter_protocol +from electrum.network import filter_protocol import time, Queue from collections import defaultdict @@ -9,7 +9,7 @@ import util, json set_verbosity(False) config = SimpleConfig() -servers = filter_protocol(DEFAULT_SERVERS,'t') +servers = filter_protocol(protocol = 't') results = util.send_request(servers, {'method':'blockchain.headers.subscribe', 'params':[]}) d = defaultdict(int)