From 00c2981b4e7c6c4f480e457441de3a89c6b5d297 Mon Sep 17 00:00:00 2001 From: Calin Culianu Date: Mon, 4 Feb 2019 04:23:31 +0200 Subject: [PATCH] Allow protocol versions and client versions to contain extra text (#718) * Allow protocol versions and client versions to contain extra text E.g. 3.3.4CS client version is accepted and treated as 3.3.4 * added tests --- electrumx/lib/util.py | 5 +++++ tests/lib/test_util.py | 1 + 2 files changed, 6 insertions(+) diff --git a/electrumx/lib/util.py b/electrumx/lib/util.py index 11a83ce..705d859 100644 --- a/electrumx/lib/util.py +++ b/electrumx/lib/util.py @@ -272,11 +272,16 @@ def is_valid_hostname(hostname): return all(SEGMENT_REGEX.match(x) for x in hostname.split(".")) +VERSION_CLEANUP_REGEX = re.compile(r'([0-9.]*)') + + def protocol_tuple(s): '''Converts a protocol version number, such as "1.0" to a tuple (1, 0). If the version number is bad, (0, ) indicating version 0 is returned.''' try: + # clean up extra text at end of version e.g. '3.3.4CS' -> '3.3.4' + s = VERSION_CLEANUP_REGEX.match(s).group(1) return tuple(int(part) for part in s.split('.')) except Exception: return (0, ) diff --git a/tests/lib/test_util.py b/tests/lib/test_util.py index 1635cd3..8d00b11 100644 --- a/tests/lib/test_util.py +++ b/tests/lib/test_util.py @@ -164,6 +164,7 @@ def test_protocol_tuple(): assert util.protocol_tuple("0.1") == (0, 1) assert util.protocol_tuple("0.10") == (0, 10) assert util.protocol_tuple("2.5.3") == (2, 5, 3) + assert util.protocol_tuple("11.2.33ExtraXYZ") == (11, 2, 33) def test_version_string(): assert util.version_string(()) == "0.0"