Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
cculianu committed Feb 9, 2019
2 parents 856ecf9 + b7e0fcb commit 5b92d0b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
9 changes: 4 additions & 5 deletions lib/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from .i18n import _
from .interface import Connection, Interface
from . import blockchain
from .version import PACKAGE_VERSION, PROTOCOL_VERSION
from . import version


DEFAULT_AUTO_CONNECT = True
Expand All @@ -52,7 +52,6 @@

def parse_servers(result):
""" parse servers list into dict format"""
from .version import PROTOCOL_VERSION
servers = {}
for item in result:
host = item[1]
Expand All @@ -79,7 +78,7 @@ def parse_servers(result):
def filter_version(servers):
def is_recent(version):
try:
return util.normalize_version(version) >= util.normalize_version(PROTOCOL_VERSION)
return version.normalize_version(version) >= version.normalize_version(version.PROTOCOL_VERSION)
except Exception as e:
return False
return {k: v for k, v in servers.items() if is_recent(v.get('version'))}
Expand Down Expand Up @@ -122,7 +121,7 @@ def servers_to_hostmap(servers):
m[protocol] = port
if need_add:
m['pruning'] = '-' # hmm. this info is missing, so give defaults just to make the map complete.
m['version'] = PROTOCOL_VERSION
m['version'] = version.PROTOCOL_VERSION
ret[host] = m
return ret

Expand Down Expand Up @@ -814,7 +813,7 @@ def new_interface(self, server_key, socket):
self.interfaces[server_key] = interface

# server.version should be the first message
params = [PACKAGE_VERSION, PROTOCOL_VERSION]
params = [version.PACKAGE_VERSION, version.PROTOCOL_VERSION]
self.queue_request('server.version', params, interface)
# The interface will immediately respond with it's last known header.
self.queue_request('blockchain.headers.subscribe', [], interface)
Expand Down
3 changes: 0 additions & 3 deletions lib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ def inv_dict(d):
base_units = {'BCH':8, 'mBCH':5, 'cash':2}
fee_levels = [_('Within 25 blocks'), _('Within 10 blocks'), _('Within 5 blocks'), _('Within 2 blocks'), _('In the next block')]

def normalize_version(v):
return [int(x) for x in re.sub(r'(\.0+)*$','', v).split(".")]

class NotEnoughFunds(Exception): pass

class ExcessiveFee(Exception): pass
Expand Down
15 changes: 13 additions & 2 deletions lib/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,30 @@ def seed_prefix(seed_type):
assert seed_type == 'standard'
return SEED_PREFIX

import re

_RX_VERSION_PARSE = re.compile(r'(\d+)[.](\d+)[.]?(\d*)\s*(\S*)')
_RX_NORMALIZER = re.compile(r'(\.0+)*$')

def parse_package_version(pvstr):
"""
Parse a package version string e.g.:
'3.3.5CS' -> (3, 3, 5, 'CS')
'3.4.5_iOS' -> (3, 4, 5, '_iOS')
'3.3.5' -> (3, 3, 5, '')
'3.3' -> (3, 3, 0, '')
and.. perhaps unexpectedly:
'3.3.5.1_iOS' -> (3, 3, 5, '.1_iOS') .. so be sure not to have more than 3 version fields + 1 'extra' field!
etc...
"""
import re
m = re.search(r'(\d+)[.](\d+)[.]?(\d*)\s*(\S*)', pvstr)
m = _RX_VERSION_PARSE.search(pvstr)
if not m:
raise ValueError('Failed to parse package version for: ' + str(pvstr))
major, minor, rev, variant = int(m.group(1)), int(m.group(2)), m.group(3), m.group(4)
rev = int(rev) if rev else 0
return major, minor, rev, variant

def normalize_version(v):
"""Used for PROTOCOL_VERSION normalization, e.g '1.4.0' -> (1,4) """
return tuple(int(x) for x in _RX_NORMALIZER.sub('', v.strip()).split("."))

0 comments on commit 5b92d0b

Please sign in to comment.