Skip to content

Commit 6130956

Browse files
authored
Merge pull request #316 from f321x/check_daemon_version
check daemon version on startup, fix version in banner
2 parents 2c41a23 + 8d83c0d commit 6130956

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

src/electrumx/lib/coins.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
from decimal import Decimal
3636
from functools import partial
3737
from hashlib import sha256
38-
from typing import Sequence, Tuple
38+
from typing import Sequence, Tuple, Optional
3939

4040
import electrumx.lib.util as util
4141
from electrumx.lib.hash import Base58, double_sha256, hash_to_hex_str
@@ -114,6 +114,7 @@ class Coin:
114114
RPC_PORT: int
115115
NAME: str
116116
NET: str
117+
MIN_REQUIRED_DAEMON_VERSION: Optional[str] = None
117118

118119
# only used for initial db sync ETAs:
119120
TX_COUNT_HEIGHT: int # at a given snapshot of the chain,
@@ -306,6 +307,8 @@ class Bitcoin(BitcoinMixin, Coin):
306307
TX_COUNT_HEIGHT = 646855
307308
TX_PER_BLOCK = 2200
308309
CRASH_CLIENT_VER = (3, 2, 3)
310+
# core version 28 introduced 1p1c package relay required for protocol 1.6
311+
MIN_REQUIRED_DAEMON_VERSION = "28.0"
309312
BLACKLIST_URL = 'https://electrum.org/blacklist.json'
310313
PEERS = [
311314
'electrum.vom-stausee.de s t',
@@ -382,6 +385,7 @@ class BitcoinTestnet(BitcoinTestnetMixin, Coin):
382385
NAME = "Bitcoin"
383386
DESERIALIZER = lib_tx.DeserializerSegWit
384387
CRASH_CLIENT_VER = (3, 2, 3)
388+
MIN_REQUIRED_DAEMON_VERSION = "28.0"
385389
PEERS = [
386390
'testnet.hsmiths.com t53011 s53012',
387391
'testnet.qtornado.com s t',

src/electrumx/server/controller.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ async def on_block(self, touched, height):
7373

7474

7575
class Controller(ServerBase):
76-
'''Manages server initialisation and stutdown.
76+
'''Manages server initialisation and shutdown.
7777
7878
Servers are started once the mempool is synced after the block
7979
processor first catches up with the daemon.
@@ -127,9 +127,12 @@ def get_db_height():
127127
)
128128

129129
# Test daemon authentication, and also ensure it has a cached
130-
# height. Do this before entering the task group.
130+
# height. Do this before entering the task group.
131131
await daemon.height()
132132

133+
# Check if daemon is recent enough
134+
await daemon.check_daemon_version()
135+
133136
caught_up_event = Event()
134137
mempool_event = Event()
135138

src/electrumx/server/daemon.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,21 @@ async def __aexit__(self, exc_type, exc_value, traceback):
9393
def connector(self):
9494
return None
9595

96+
async def check_daemon_version(self):
97+
assert self.session is not None and self.coin is not None, f"{self.session=}, {self.coin=}"
98+
if self.coin.MIN_REQUIRED_DAEMON_VERSION is None:
99+
return
100+
101+
required_version = tuple(map(int, self.coin.MIN_REQUIRED_DAEMON_VERSION.split('.')))
102+
network_info = await self.getnetworkinfo()
103+
ni_version = network_info['version']
104+
daemon_major = ni_version // 10_000
105+
daemon_minor = (ni_version % 10_000) // 100
106+
daemon_rev = ni_version % 100
107+
daemon_version = (daemon_major, daemon_minor, daemon_rev)
108+
if daemon_version < required_version:
109+
raise RuntimeError(f"Bitcoin Core {daemon_version=} < {required_version=}.")
110+
96111
def set_url(self, url):
97112
'''Set the URLS to the given list, and switch to the first one.'''
98113
urls = url.split(',')
@@ -137,7 +152,7 @@ async def _send_data(self, data):
137152
async def _send(self, payload, processor):
138153
'''Send a payload to be converted to JSON.
139154
140-
Handles temporary connection issues. Daemon response errors
155+
Handles temporary connection issues. Daemon response errors
141156
are raise through DaemonError.
142157
'''
143158
def log_error(error):

src/electrumx/server/session.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,7 @@ class ElectrumX(SessionBase):
10361036
'''A TCP server that handles incoming Electrum connections.'''
10371037

10381038
PROTOCOL_MIN = (1, 4)
1039+
# consider bumping Coin.MIN_REQUIRED_DAEMON_VERSION too when releasing a new protocol version
10391040
PROTOCOL_MAX = (1, 4, 3)
10401041

10411042
def __init__(self, *args, **kwargs):
@@ -1355,10 +1356,10 @@ def is_tor(self):
13551356

13561357
async def replaced_banner(self, banner):
13571358
network_info = await self.daemon_request('getnetworkinfo')
1358-
ni_version = network_info['version']
1359-
major, minor = divmod(ni_version, 1000000)
1360-
minor, revision = divmod(minor, 10000)
1361-
revision //= 100
1359+
ni_version = network_info['version'] # e.g. 290100 (for /Satoshi:29.1.0/)
1360+
major = ni_version // 10_000
1361+
minor = (ni_version % 10_000) // 100
1362+
revision = ni_version % 100
13621363
daemon_version = f'{major:d}.{minor:d}.{revision:d}'
13631364
for pair in [
13641365
('$SERVER_VERSION', electrumx.version_short),

0 commit comments

Comments
 (0)