• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

spesmilo / electrum / 44705814097

20 Nov 2026 08:49AM UTC coverage: 71.472% (+0.003%) from 71.469%
44705814097

push

github

ecdsa
Qt: force QTextEdit to display plain text

keep a pointer to the original setText

28310 of 39610 relevant lines covered (71.47%)

0.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

65.56
/electrum/interface.py
1
#!/usr/bin/env python
2
#
3
# Electrum - lightweight Bitcoin client
4
# Copyright (C) 2011 thomasv@gitorious
5
#
6
# Permission is hereby granted, free of charge, to any person
7
# obtaining a copy of this software and associated documentation files
8
# (the "Software"), to deal in the Software without restriction,
9
# including without limitation the rights to use, copy, modify, merge,
10
# publish, distribute, sublicense, and/or sell copies of the Software,
11
# and to permit persons to whom the Software is furnished to do so,
12
# subject to the following conditions:
13
#
14
# The above copyright notice and this permission notice shall be
15
# included in all copies or substantial portions of the Software.
16
#
17
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
# SOFTWARE.
25
import os
1 ✔
26
import re
1 ✔
27
import ssl
1 ✔
28
import sys
1 ✔
29
import time
1 ✔
30
import traceback
1 ✔
31
import asyncio
1 ✔
32
import socket
1 ✔
33
from typing import Tuple, Union, List, TYPE_CHECKING, Optional, Set, NamedTuple, Any, Sequence, Dict
1 ✔
34
from collections import defaultdict
1 ✔
35
from ipaddress import IPv4Network, IPv6Network, ip_address, IPv6Address, IPv4Address
1 ✔
36
import itertools
1 ✔
37
import logging
1 ✔
38
import hashlib
1 ✔
39
import functools
1 ✔
40
import random
1 ✔
41
import enum
1 ✔
42

43
import aiorpcx
1 ✔
44
from aiorpcx import RPCSession, Notification, NetAddress, NewlineFramer
1 ✔
45
from aiorpcx.curio import timeout_after, TaskTimeout
1 ✔
46
from aiorpcx.jsonrpc import JSONRPC, CodeMessageError
1 ✔
47
from aiorpcx.rawsocket import RSClient, RSTransport
1 ✔
48
import certifi
1 ✔
49

50
from .util import (ignore_exceptions, log_exceptions, bfh, ESocksProxy,
1 ✔
51
                   is_integer, is_non_negative_integer, is_hash256_str, is_hex_str,
52
                   is_int_or_float, is_non_negative_int_or_float, OldTaskGroup,
53
                   send_exception_to_crash_reporter, error_text_str_to_safe_str, versiontuple)
54
from . import util
1 ✔
55
from . import x509
1 ✔
56
from . import pem
1 ✔
57
from . import version
1 ✔
58
from . import blockchain
1 ✔
59
from .blockchain import Blockchain, HEADER_SIZE, CHUNK_SIZE
1 ✔
60
from . import bitcoin
1 ✔
61
from .bitcoin import DummyAddress, DummyAddressUsedInTxException
1 ✔
62
from . import constants
1 ✔
63
from .i18n import _
1 ✔
64
from .logging import Logger
1 ✔
65
from .transaction import Transaction
1 ✔
66
from .fee_policy import FEE_ETA_TARGETS
1 ✔
67
from .lrucache import LRUCache
1 ✔
68

69
if TYPE_CHECKING:
70
    from .network import Network
71
    from .simple_config import SimpleConfig
72

73

74
ca_path = certifi.where()
1 ✔
75

76
BUCKET_NAME_OF_ONION_SERVERS = 'onion'
1 ✔
77

78
KNOWN_ELEC_PROTOCOL_TRANSPORTS = {'t', 's'}
1 ✔
79
PREFERRED_NETWORK_PROTOCOL = 's'
1 ✔
80
assert PREFERRED_NETWORK_PROTOCOL in KNOWN_ELEC_PROTOCOL_TRANSPORTS
1 ✔
81

82
MAX_NUM_HEADERS_PER_REQUEST = 2016
1 ✔
83
assert MAX_NUM_HEADERS_PER_REQUEST >= CHUNK_SIZE
1 ✔
84

85
MAX_SERVER_BANNER_LEN = 2000  # in chars
1 ✔
86

87

88
class NetworkTimeout:
1 ✔
89
    # seconds
90
    class Generic:
1 ✔
91
        NORMAL = 30
1 ✔
92
        RELAXED = 45
1 ✔
93
        MOST_RELAXED = 600
1 ✔
94

95
    class Urgent(Generic):
1 ✔
96
        NORMAL = 10
1 ✔
97
        RELAXED = 20
1 ✔
98
        MOST_RELAXED = 60
1 ✔
99

100

101
def assert_non_negative_integer(val: Any) -> None:
1 ✔
102
    if not is_non_negative_integer(val):
1 ✔
103
        raise RequestCorrupted(f'{val!r} should be a non-negative integer')
×
104

105

106
def assert_integer(val: Any) -> None:
1 ✔
107
    if not is_integer(val):
1 ✔
108
        raise RequestCorrupted(f'{val!r} should be an integer')
×
109

110

111
def assert_int_or_float(val: Any) -> None:
1 ✔
112
    if not is_int_or_float(val):
×
113
        raise RequestCorrupted(f'{val!r} should be int or float')
×
114

115

116
def assert_non_negative_int_or_float(val: Any) -> None:
1 ✔
117
    if not is_non_negative_int_or_float(val):
1 ✔
118
        raise RequestCorrupted(f'{val!r} should be a non-negative int or float')
×
119

120

121
def assert_hash256_str(val: Any) -> None:
1 ✔
122
    if not is_hash256_str(val):
1 ✔
123
        raise RequestCorrupted(f'{val!r} should be a hash256 str')
×
124

125

126
def assert_hex_str(val: Any, *, allow_odd_len: bool = False) -> None:
1 ✔
127
    if not is_hex_str(val, allow_odd_len=allow_odd_len):
1 ✔
128
        raise RequestCorrupted(f'{val!r} should be a hex str')
×
129

130

131
def assert_dict(d: Any) -> None:
1 ✔
132
    if not isinstance(d, dict):
1 ✔
133
        raise RequestCorrupted(f'{d!r} should be a dict')
×
134

135

136
def assert_dict_contains_field(d: Any, *, field_name: str) -> Any:
1 ✔
137
    assert_dict(d)
1 ✔
138
    if field_name not in d:
1 ✔
139
        raise RequestCorrupted(f'required field {field_name!r} missing from dict')
×
140
    return d[field_name]
1 ✔
141

142

143
def assert_list_or_tuple(val: Any) -> None:
1 ✔
144
    if not isinstance(val, (list, tuple)):
1 ✔
145
        raise RequestCorrupted(f'{val!r} should be a list or tuple')
×
146

147

148
def protocol_tuple(s: Any) -> tuple[int, ...]:
1 ✔
149
    """Converts a protocol version number, such as "1.0" to a tuple (1, 0).
150

151
    If the version number is bad, (0, ) indicating version 0 is returned.
152
    """
153
    try:
1 ✔
154
        assert isinstance(s, str)
1 ✔
155
        return versiontuple(s)
1 ✔
156
    except Exception:
×
157
        return (0, )
×
158

159

160
class ChainResolutionMode(enum.Enum):
1 ✔
161
    CATCHUP = enum.auto()
1 ✔
162
    BACKWARD = enum.auto()
1 ✔
163
    BINARY = enum.auto()
1 ✔
164
    FORK = enum.auto()
1 ✔
165
    NO_FORK = enum.auto()
1 ✔
166

167

168
class NotificationSession(RPCSession):
1 ✔
169

170
    COST_INCOMING_REQUEST = 100
1 ✔
171

172
    def __init__(self, *args, interface: 'Interface', **kwargs):
1 ✔
173
        super(NotificationSession, self).__init__(*args, **kwargs)
1 ✔
174
        self.subscriptions = defaultdict(list)  # type: defaultdict[str, list[asyncio.Queue]]
1 ✔
175
        self.subs_cache = {}  # type: dict[str, Any]
1 ✔
176
        self._msg_counter = itertools.count(start=1)
1 ✔
177
        self.interface = interface
1 ✔
178
        self.taskgroup = interface.taskgroup
1 ✔
179
        self.set_strict_resource_limits()
1 ✔
180

181
        # To log pre-processed json traffic, uncomment:
182
        #self.logger.setLevel(logging.DEBUG)  # from aiorpcx
183
        #self.verbosity = 4
184

185
    async def handle_request(self, request):
1 ✔
186
        # note: we get called for incoming Requests and Notifications. (not for Responses)
187
        self.maybe_log(f"--> {request}")
1 ✔
188
        self.bump_cost(self.COST_INCOMING_REQUEST)
1 ✔
189
        try:
1 ✔
190
            if isinstance(request, Notification):
1 ✔
191
                params, result = request.args[:-1], request.args[-1]
1 ✔
192
                key = self.get_hashable_key_for_rpc_call(request.method, params)
1 ✔
193
                if key in self.subscriptions:
1 ✔
194
                    self.subs_cache[key] = result
1 ✔
195
                    for queue in self.subscriptions[key]:
1 ✔
196
                        await queue.put(request.args)
1 ✔
197
                else:
198
                    raise Exception(f'unexpected notification')
×
199
            else:
200
                raise Exception(f'unexpected request. not a notification')
1 ✔
201
        except Exception as e:
1 ✔
202
            self.interface.logger.info(f"error handling request {request}. exc: {repr(e)}")
1 ✔
203
            await self.close()
1 ✔
204

205
    async def send_request(self, *args, timeout=None, **kwargs):
1 ✔
206
        # note: semaphores/timeouts/backpressure etc are handled by
207
        # aiorpcx. the timeout arg here in most cases should not be set
208
        msg_id = next(self._msg_counter)
1 ✔
209
        self.maybe_log(f"<-- {args} {kwargs} (id: {msg_id})")
1 ✔
210
        try:
1 ✔
211
            # note: RPCSession.send_request raises TaskTimeout in case of a timeout.
212
            # TaskTimeout is a subclass of CancelledError, which is *suppressed* in TaskGroups
213
            response = await util.wait_for2(
1 ✔
214
                super().send_request(*args, **kwargs),
215
                timeout)
216
        except (TaskTimeout, asyncio.TimeoutError) as e:
1 ✔
217
            self.maybe_log(f"--> request timed out: {args} (id: {msg_id})")
×
218
            raise RequestTimedOut(f'request timed out: {args} (id: {msg_id})') from e
×
219
        except CodeMessageError as e:
1 ✔
220
            self.maybe_log(f"--> {repr(e)} (id: {msg_id})")
1 ✔
221
            raise
1 ✔
222
        except BaseException as e:  # cancellations, etc. are useful for debugging
1 ✔
223
            self.maybe_log(f"--> {repr(e)} (id: {msg_id})")
1 ✔
224
            raise
1 ✔
225
        else:
226
            self.maybe_log(f"--> {response} (id: {msg_id})")
1 ✔
227
            return response
1 ✔
228

229
    def set_default_timeout(self, timeout):
1 ✔
230
        assert hasattr(self, "sent_request_timeout")  # in base class
1 ✔
231
        self.sent_request_timeout = timeout
1 ✔
232
        assert hasattr(self, "max_send_delay")        # in base class
1 ✔
233
        self.max_send_delay = timeout
1 ✔
234

235
    async def subscribe(self, method: str, params: List, queue: asyncio.Queue):
1 ✔
236
        key = self.get_hashable_key_for_rpc_call(method, params)
1 ✔
237
        # note: multiple Synchronizers (from different Wallet objects) might sub to the same key,
238
        #       hence subscriptions map key->list[queue]
239
        self.subscriptions[key].append(queue)
1 ✔
240
        if key not in self.subs_cache:
1 ✔
241
            # note: until subs_cache is written for the first time,
242
            #       each 'subscribe' call might make a request on the network.
243
            result = await self.send_request(method, params)
1 ✔
244
            # don't override what was already set in handle_request, it might be newer than the send_request response
245
            if key not in self.subs_cache:
1 ✔
246
                self.subs_cache[key] = result
1 ✔
247
        await queue.put(params + [self.subs_cache[key]])
1 ✔
248

249
    def unsubscribe(self, queue):
1 ✔
250
        """Unsubscribe a callback to free object references to enable GC."""
251
        # note: we can't unsubscribe from the server, so we keep receiving
252
        # subsequent notifications
253
        for v in self.subscriptions.values():
1 ✔
254
            if queue in v:
1 ✔
255
                v.remove(queue)
1 ✔
256

257
    @classmethod
1 ✔
258
    def get_hashable_key_for_rpc_call(cls, method, params):
1 ✔
259
        """Hashable index for subscriptions and cache"""
260
        return str(method) + repr(params)
1 ✔
261

262
    def maybe_log(self, msg: str) -> None:
1 ✔
263
        if not self.interface: return
1 ✔
264
        if self.interface.debug or self.interface.network.debug:
1 ✔
265
            self.interface.logger.debug(msg)
1 ✔
266

267
    def default_framer(self):
1 ✔
268
        # overridden so that max_size can be customized
269
        max_size = self.interface.network.config.NETWORK_MAX_INCOMING_MSG_SIZE
1 ✔
270
        assert max_size > 500_000, f"{max_size=} (< 500_000) is too small"
1 ✔
271
        return NewlineFramer(max_size=max_size)
1 ✔
272

273
    async def close(self, *, force_after: int | None = None):
1 ✔
274
        """Closes the connection and waits for it to be closed.
275
        We try to flush buffered data to the wire, which can take some time.
276
        """
277
        if force_after is None:
1 ✔
278
            # We give up after a while and just abort the connection.
279
            # Note: specifically if the server is running Fulcrum, waiting seems hopeless,
280
            #       the connection must be aborted (see https://github.com/cculianu/Fulcrum/issues/76)
281
            # Note: if the ethernet cable was pulled or wifi disconnected, that too might
282
            #       wait until this timeout is triggered
283
            force_after = 1  # seconds
1 ✔
284
        await super().close(force_after=force_after)
1 ✔
285

286
    def set_strict_resource_limits(self):
1 ✔
287
        # Apply strict resource limits to each interface.
288
        # - This limits incoming bandwidth, and indirectly limits e.g. memory usage.
289
        #   FIXME limit memory usage directly
290
        #   - confusingly for our "client" use case, outgoing bandwidth is also counted,
291
        #     however any meaningful limiting only happens in _throttled_message->_incoming_concurrency,
292
        #     which only gets called on inc-notifications and inc-requests (reqs we should not receive though).
293
        #   - processing an inc-notification or an inc-request also incurs COST_INCOMING_REQUEST.
294
        # - These limits are intended for the non-main secondary interfaces,
295
        #   as the main interface is expected to have a lot of traffic.
296
        # - Secondary interfaces should generate minimal traffic:
297
        #   as they are only used for polled fee estimates (minimal data) and header subs (minimal data).
298
        #   Note a header notification can force us into fork resolution and downloading lots
299
        #   of headers. The initial headers download might also happen on any interface.
300
        #   Headers download uses non-trivial amounts of data, e.g. 100k x 160 hex headers take 16 MB.
301
        assert hasattr(NotificationSession, "cost_hard_limit")  # in base class
1 ✔
302
        self.bw_cost_per_byte = 1 / 1_000
1 ✔
303
        self.cost_hard_limit = 30_000  # 30 MB of bandwidth, in+out
1 ✔
304
        self.cost_soft_limit = self.cost_hard_limit - 1  # this effectively disables the soft limit
1 ✔
305
        self.cost_decay_per_sec = self.cost_hard_limit / 600  # refund over 10 minutes
1 ✔
306

307
    def remove_resource_limits(self):
1 ✔
308
        assert hasattr(NotificationSession, "cost_hard_limit")  # in base class
×
309
        # remove static limits:
310
        self.cost_hard_limit = 0
×
311
        self.cost_soft_limit = 0
×
312
        # try to reset costs incurred so far (e.g. when promoting a non-main interface to main):
313
        self._cost_last = self.cost = 0
×
314
        self._cost_fraction = 0
×
315
        self._incoming_concurrency.set_target(self.initial_concurrent)
×
316

317
    def on_disconnect_due_to_excessive_session_cost(self):
1 ✔
318
        self.interface.logger.info(f"closing session over resource usage. cost={self.cost}")
1 ✔
319

320

321
class NetworkException(Exception): pass
1 ✔
322

323

324
class GracefulDisconnect(NetworkException):
1 ✔
325
    log_level = logging.INFO
1 ✔
326

327
    def __init__(self, *args, log_level=None, **kwargs):
1 ✔
328
        Exception.__init__(self, *args, **kwargs)
1 ✔
329
        if log_level is not None:
1 ✔
330
            self.log_level = log_level
×
331

332

333
class RequestTimedOut(GracefulDisconnect):
1 ✔
334
    def __str__(self):
1 ✔
335
        return _("Network request timed out.")
×
336

337

338
class RequestCorrupted(Exception): pass
1 ✔
339

340
class ErrorParsingSSLCert(Exception): pass
1 ✔
341
class ErrorGettingSSLCertFromServer(Exception): pass
1 ✔
342
class ErrorSSLCertFingerprintMismatch(Exception): pass
1 ✔
343
class InvalidOptionCombination(Exception): pass
1 ✔
344
class ConnectError(NetworkException): pass
1 ✔
345

346

347
class TxBroadcastError(NetworkException):
1 ✔
348
    def get_message_for_gui(self):
1 ✔
349
        raise NotImplementedError()
×
350

351

352
class TxBroadcastHashMismatch(TxBroadcastError):
1 ✔
353
    def get_message_for_gui(self):
1 ✔
354
        return "{}\n{}\n\n{}" \
×
355
            .format(_("The server returned an unexpected transaction ID when broadcasting the transaction."),
356
                    _("Consider trying to connect to a different server, or updating Electrum."),
357
                    str(self))
358

359

360
class TxBroadcastServerReturnedError(TxBroadcastError):
1 ✔
361
    def get_message_for_gui(self):
1 ✔
362
        return "{}\n{}\n\n{}" \
×
363
            .format(_("The server returned an error when broadcasting the transaction."),
364
                    _("Consider trying to connect to a different server, or updating Electrum."),
365
                    str(self))
366

367

368
class TxBroadcastUnknownError(TxBroadcastError):
1 ✔
369
    def get_message_for_gui(self):
1 ✔
370
        return "{}\n{}" \
×
371
            .format(_("Unknown error when broadcasting the transaction."),
372
                    _("Consider trying to connect to a different server, or updating Electrum."))
373

374

375
class _RSClient(RSClient):
1 ✔
376
    async def create_connection(self):
1 ✔
377
        try:
1 ✔
378
            return await super().create_connection()
1 ✔
379
        except OSError as e:
×
380
            # note: using "from e" here will set __cause__ of ConnectError
381
            raise ConnectError(e) from e
×
382

383

384
class PaddedRSTransport(RSTransport):
1 ✔
385
    """A raw socket transport that provides basic countermeasures against traffic analysis
386
    by padding the jsonrpc payload with whitespaces to have ~uniform-size TCP packets.
387
    (it is assumed that a network observer does not see plaintext transport contents,
388
    due to it being wrapped e.g. in TLS)
389
    """
390

391
    MIN_PACKET_SIZE = 1024
1 ✔
392
    WAIT_FOR_BUFFER_GROWTH_SECONDS = 1.0
1 ✔
393
    # (unpadded) amount of bytes sent instantly before beginning with polling.
394
    # This makes the initial handshake where a few small messages are exchanged faster.
395
    WARMUP_BUDGET_SIZE = 1024
1 ✔
396

397
    session: Optional['RPCSession']
1 ✔
398

399
    def __init__(self, *args, **kwargs):
1 ✔
400
        RSTransport.__init__(self, *args, **kwargs)
1 ✔
401
        self._sbuffer = bytearray()  # "send buffer"
1 ✔
402
        self._sbuffer_task = None  # type: Optional[asyncio.Task]
1 ✔
403
        self._sbuffer_has_data_evt = asyncio.Event()
1 ✔
404
        self._last_send = time.monotonic()
1 ✔
405
        self._force_send = False  # type: bool
1 ✔
406

407
    # note: this does not call super().write() but is a complete reimplementation
408
    async def write(self, message):
1 ✔
409
        await self._can_send.wait()
1 ✔
410
        if self.is_closing():
1 ✔
411
            return
×
412
        framed_message = self._framer.frame(message)
1 ✔
413
        self._sbuffer += framed_message
1 ✔
414
        self._sbuffer_has_data_evt.set()
1 ✔
415
        self._maybe_consume_sbuffer()
1 ✔
416

417
    def _maybe_consume_sbuffer(self) -> None:
1 ✔
418
        """Maybe take some data from sbuffer and send it on the wire."""
419
        if not self._can_send.is_set() or self.is_closing():
1 ✔
420
            return
1 ✔
421
        buf = self._sbuffer
1 ✔
422
        if not buf:
1 ✔
423
            return
1 ✔
424
        # if there is enough data in the buffer, or if we haven't sent in a while, send now:
425
        if not (
1 ✔
426
            self._force_send
427
            or len(buf) >= self.MIN_PACKET_SIZE
428
            or self._last_send + self.WAIT_FOR_BUFFER_GROWTH_SECONDS < time.monotonic()
429
            or self.session.send_size < self.WARMUP_BUDGET_SIZE
430
        ):
431
            return
1 ✔
432
        assert buf[-2:] in (b"}\n", b"]\n"), f"unexpected json-rpc terminator: {buf[-2:]=!r}"
1 ✔
433
        # either (1) pad length to next power of two, to create "lsize" packet:
434
        payload_lsize = len(buf)
1 ✔
435
        total_lsize = max(self.MIN_PACKET_SIZE, 2 ** (payload_lsize.bit_length()))
1 ✔
436
        npad_lsize = total_lsize - payload_lsize
1 ✔
437
        # or if that wasted a lot of bandwidth with padding, (2) defer sending some messages
438
        # and create a packet with half that size ("ssize", s for small)
439
        total_ssize = max(self.MIN_PACKET_SIZE, total_lsize // 2)
1 ✔
440
        payload_ssize = buf.rfind(b"\n", 0, total_ssize)
1 ✔
441
        if payload_ssize != -1:
1 ✔
442
            payload_ssize += 1  # for "\n" char
1 ✔
443
            npad_ssize = total_ssize - payload_ssize
1 ✔
444
        else:
445
            npad_ssize = float("inf")
×
446
        # decide between (1) and (2):
447
        if self._force_send or npad_lsize <= npad_ssize:
1 ✔
448
            # (1) create "lsize" packet: consume full buffer
449
            npad = npad_lsize
1 ✔
450
            p_idx = payload_lsize
1 ✔
451
        else:
452
            # (2) create "ssize" packet: consume some, but defer some for later
453
            npad = npad_ssize
1 ✔
454
            p_idx = payload_ssize
1 ✔
455
        # pad by adding spaces near end
456
        # self.session.maybe_log(
457
        #     f"PaddedRSTransport. calling low-level write(). "
458
        #     f"chose between (lsize:{payload_lsize}+{npad_lsize}, ssize:{payload_ssize}+{npad_ssize}). "
459
        #     f"won: {'tie' if npad_lsize == npad_ssize else 'lsize' if npad_lsize < npad_ssize else 'ssize'}."
460
        # )
461
        json_rpc_terminator = buf[p_idx-2:p_idx]
1 ✔
462
        assert json_rpc_terminator in (b"}\n", b"]\n"), f"unexpected {json_rpc_terminator=!r}"
1 ✔
463
        buf2 = buf[:p_idx-2] + (npad * b" ") + json_rpc_terminator
1 ✔
464
        self._asyncio_transport.write(buf2)
1 ✔
465
        self._last_send = time.monotonic()
1 ✔
466
        del self._sbuffer[:p_idx]
1 ✔
467
        if not self._sbuffer:
1 ✔
468
            self._sbuffer_has_data_evt.clear()
1 ✔
469

470
    async def _poll_sbuffer(self):
1 ✔
471
        while not self.is_closing():
1 ✔
472
            await self._can_send.wait()
1 ✔
473
            await self._sbuffer_has_data_evt.wait()  # to avoid busy-waiting
1 ✔
474
            self._maybe_consume_sbuffer()
1 ✔
475
            # If there is still data in the buffer, sleep until it would time out.
476
            # note: If the transport is ~idle, when we wake up, we will send the current buf data,
477
            #       but if busy, we might wake up to completely new buffer contents. Either is fine.
478
            if len(self._sbuffer) > 0:
1 ✔
479
                timeout_abs = self._last_send + self.WAIT_FOR_BUFFER_GROWTH_SECONDS
1 ✔
480
                timeout_rel = max(0.0, timeout_abs - time.monotonic())
1 ✔
481
                await asyncio.sleep(timeout_rel)
1 ✔
482

483
    def connection_made(self, transport: asyncio.BaseTransport):
1 ✔
484
        super().connection_made(transport)
1 ✔
485
        if isinstance(self.session, NotificationSession):
1 ✔
486
            coro = self.session.taskgroup.spawn(self._poll_sbuffer())
1 ✔
487
            self._sbuffer_task = self.loop.create_task(coro)
1 ✔
488
        else:
489
            # This a short-lived "fetch_certificate"-type session.
490
            # No polling here, we always force-empty the buffer.
491
            self._force_send = True
×
492

493
    async def close(self, *args, **kwargs):
1 ✔
494
        '''Close the connection and return when closed.'''
495
        # Flush buffer before disconnecting. This makes ReplyAndDisconnect work:
496
        self._force_send = True
1 ✔
497
        self._maybe_consume_sbuffer()
1 ✔
498
        await super().close(*args, **kwargs)
1 ✔
499

500

501
class ServerAddr:
1 ✔
502

503
    def __init__(self, host: str, port: Union[int, str], *, protocol: str | None = None):
1 ✔
504
        assert isinstance(host, str), repr(host)
1 ✔
505
        if protocol is None:
1 ✔
506
            protocol = 's'
×
507
        if not host:
1 ✔
508
            raise ValueError('host must not be empty')
×
509
        if host[0] == '[' and host[-1] == ']':  # IPv6
1 ✔
510
            host = host[1:-1]
1 ✔
511
        try:
1 ✔
512
            net_addr = NetAddress(host, port)  # this validates host and port
1 ✔
513
        except Exception as e:
1 ✔
514
            raise ValueError(f"cannot construct ServerAddr: invalid host or port (host={host}, port={port})") from e
1 ✔
515
        if protocol not in KNOWN_ELEC_PROTOCOL_TRANSPORTS:
1 ✔
516
            raise ValueError(f"invalid network protocol: {protocol}")
×
517
        self.host = str(net_addr.host)  # canonical form (if e.g. IPv6 address)
1 ✔
518
        self.port = int(net_addr.port)
1 ✔
519
        self.protocol = protocol
1 ✔
520
        self._net_addr_str = str(net_addr)
1 ✔
521

522
    @classmethod
1 ✔
523
    def from_str(cls, s: str) -> 'ServerAddr':
1 ✔
524
        """Constructs a ServerAddr or raises ValueError."""
525
        # host might be IPv6 address, hence do rsplit:
526
        host, port, protocol = str(s).rsplit(':', 2)
1 ✔
527
        return ServerAddr(host=host, port=port, protocol=protocol)
1 ✔
528

529
    @classmethod
1 ✔
530
    def from_str_with_inference(cls, s: str) -> Optional['ServerAddr']:
1 ✔
531
        """Construct ServerAddr from str, guessing missing details.
532
        Does not raise - just returns None if guessing failed.
533
        Ongoing compatibility not guaranteed.
534
        """
535
        if not s:
1 ✔
536
            return None
×
537
        host = ""
1 ✔
538
        if s[0] == "[" and "]" in s:  # IPv6 address
1 ✔
539
            host_end = s.index("]")
1 ✔
540
            host = s[1:host_end]
1 ✔
541
            s = s[host_end+1:]
1 ✔
542
        items = str(s).rsplit(':', 2)
1 ✔
543
        if len(items) < 2:
1 ✔
544
            return None  # although maybe we could guess the port too?
1 ✔
545
        host = host or items[0]
1 ✔
546
        port = items[1]
1 ✔
547
        if len(items) >= 3:
1 ✔
548
            protocol = items[2]
1 ✔
549
        else:
550
            protocol = PREFERRED_NETWORK_PROTOCOL
1 ✔
551
        try:
1 ✔
552
            return ServerAddr(host=host, port=port, protocol=protocol)
1 ✔
553
        except ValueError:
1 ✔
554
            return None
1 ✔
555

556
    def to_friendly_name(self) -> str:
1 ✔
557
        # note: this method is closely linked to from_str_with_inference
558
        if self.protocol == 's':  # hide trailing ":s"
1 ✔
559
            return self.net_addr_str()
1 ✔
560
        return str(self)
1 ✔
561

562
    def __str__(self):
1 ✔
563
        return '{}:{}'.format(self.net_addr_str(), self.protocol)
1 ✔
564

565
    def to_json(self) -> str:
1 ✔
566
        return str(self)
×
567

568
    def __repr__(self):
1 ✔
569
        return f'<ServerAddr host={self.host} port={self.port} protocol={self.protocol}>'
×
570

571
    def net_addr_str(self) -> str:
1 ✔
572
        return self._net_addr_str
1 ✔
573

574
    def __eq__(self, other):
1 ✔
575
        if not isinstance(other, ServerAddr):
1 ✔
576
            return False
×
577
        return (self.host == other.host
1 ✔
578
                and self.port == other.port
579
                and self.protocol == other.protocol)
580

581
    def __ne__(self, other):
1 ✔
582
        return not (self == other)
×
583

584
    def __hash__(self):
1 ✔
585
        return hash((self.host, self.port, self.protocol))
1 ✔
586

587

588
def _get_cert_path_for_host(*, config: 'SimpleConfig', host: str) -> str:
1 ✔
589
    filename = host
1 ✔
590
    try:
1 ✔
591
        ip = ip_address(host)
1 ✔
592
    except ValueError:
1 ✔
593
        pass
1 ✔
594
    else:
595
        if isinstance(ip, IPv6Address):
1 ✔
596
            filename = f"ipv6_{ip.packed.hex()}"
×
597
    return os.path.join(config.path, 'certs', filename)
1 ✔
598

599

600
class Interface(Logger):
1 ✔
601

602
    def __init__(self, *, network: 'Network', server: ServerAddr):
1 ✔
603
        assert isinstance(server, ServerAddr), f"expected ServerAddr, got {type(server)}"
1 ✔
604
        self.ready = network.asyncio_loop.create_future()
1 ✔
605
        self.got_disconnected = asyncio.Event()
1 ✔
606
        self._blockchain_updated = asyncio.Event()
1 ✔
607
        self.server = server
1 ✔
608
        Logger.__init__(self)
1 ✔
609
        assert network.config.path
1 ✔
610
        self.cert_path = _get_cert_path_for_host(config=network.config, host=self.host)
1 ✔
611
        self.blockchain = None  # type: Optional[Blockchain]
1 ✔
612
        self.bc_mgr = network.bc_mgr
1 ✔
613
        self._requested_chunks = set()  # type: Set[int]
1 ✔
614
        self.network = network
1 ✔
615
        self.session = None  # type: Optional[NotificationSession]
1 ✔
616
        self._ipaddr_bucket = None
1 ✔
617
        # Set up proxy.
618
        # - for servers running on localhost, the proxy is not used. If user runs their own server
619
        #   on same machine, this lets them enable the proxy (which is used for e.g. FX rates).
620
        #   note: we could maybe relax this further and bypass the proxy for all private
621
        #         addresses...? e.g. 192.168.x.x
622
        if util.is_localhost(server.host):
1 ✔
623
            self.logger.info(f"looks like localhost: not using proxy for this server")
1 ✔
624
            self.proxy = None
1 ✔
625
        else:
626
            self.proxy = ESocksProxy.from_network_settings(network)
1 ✔
627

628
        # Latest block header and corresponding height, as claimed by the server.
629
        # Note that these values are updated before they are verified.
630
        # Especially during initial header sync, verification can take a long time.
631
        # Failing verification will get the interface closed.
632
        self.tip_header = None  # type: Optional[dict]
1 ✔
633
        self.tip = 0
1 ✔
634

635
        self._headers_cache = {}  # type: Dict[int, bytes]
1 ✔
636
        self._rawtx_cache = LRUCache(maxsize=20)  # type: LRUCache[str, bytes]  # txid->rawtx
1 ✔
637

638
        self.fee_estimates_eta = {}  # type: Dict[int, int]
1 ✔
639

640
        self.active_protocol_tuple = (0,)  # type: Optional[tuple[int, ...]]
1 ✔
641

642
        # Dump network messages (only for this interface).  Set at runtime from the console.
643
        self.debug = False
1 ✔
644

645
        self.taskgroup = OldTaskGroup()
1 ✔
646

647
        async def spawn_task():
1 ✔
648
            task = await self.network.taskgroup.spawn(self.run())
1 ✔
649
            task.set_name(f"interface::{str(server)}")
1 ✔
650
        asyncio.run_coroutine_threadsafe(spawn_task(), self.network.asyncio_loop)
1 ✔
651

652
    @property
1 ✔
653
    def host(self):
1 ✔
654
        return self.server.host
1 ✔
655

656
    @property
1 ✔
657
    def port(self):
1 ✔
658
        return self.server.port
1 ✔
659

660
    @property
1 ✔
661
    def protocol(self):
1 ✔
662
        return self.server.protocol
1 ✔
663

664
    def diagnostic_name(self):
1 ✔
665
        return self.server.net_addr_str()
1 ✔
666

667
    def __str__(self):
1 ✔
668
        return f"<Interface {self.diagnostic_name()}>"
×
669

670
    async def is_server_ca_signed(self, ca_ssl_context: ssl.SSLContext) -> bool:
1 ✔
671
        """Given a CA enforcing SSL context, returns True if the connection
672
        can be established. Returns False if the server has a self-signed
673
        certificate but otherwise is okay. Any other failures raise.
674
        """
675
        try:
×
676
            await self.open_session(ssl_context=ca_ssl_context, exit_early=True)
×
677
        except ConnectError as e:
×
678
            cause = e.__cause__
×
679
            if (isinstance(cause, ssl.SSLCertVerificationError)
×
680
                    and cause.reason == 'CERTIFICATE_VERIFY_FAILED'
681
                    and cause.verify_code == 18):  # "self signed certificate"
682
                # Good. We will use this server as self-signed.
683
                return False
×
684
            # Not good. Cannot use this server.
685
            raise
×
686
        # Good. We will use this server as CA-signed.
687
        return True
×
688

689
    async def _try_saving_ssl_cert_for_first_time(self, ca_ssl_context: ssl.SSLContext) -> None:
1 ✔
690
        ca_signed = await self.is_server_ca_signed(ca_ssl_context)
×
691
        if ca_signed:
×
692
            if self._get_expected_fingerprint():
×
693
                raise InvalidOptionCombination("cannot use --serverfingerprint with CA signed servers")
×
694
            with open(self.cert_path, 'w') as f:
×
695
                # empty file means this is CA signed, not self-signed
696
                f.write('')
×
697
        else:
698
            await self._save_certificate()
×
699

700
    def _is_saved_ssl_cert_available(self):
1 ✔
701
        if not os.path.exists(self.cert_path):
×
702
            return False
×
703
        with open(self.cert_path, 'r') as f:
×
704
            contents = f.read()
×
705
        if contents == '':  # CA signed
×
706
            if self._get_expected_fingerprint():
×
707
                raise InvalidOptionCombination("cannot use --serverfingerprint with CA signed servers")
×
708
            return True
×
709
        # pinned self-signed cert
710
        try:
×
711
            b = pem.dePem(contents, 'CERTIFICATE')
×
712
        except SyntaxError as e:
×
713
            self.logger.info(f"error parsing already saved cert: {e}")
×
714
            raise ErrorParsingSSLCert(e) from e
×
715
        try:
×
716
            x = x509.X509(b)
×
717
        except Exception as e:
×
718
            self.logger.info(f"error parsing already saved cert: {e}")
×
719
            raise ErrorParsingSSLCert(e) from e
×
720
        try:
×
721
            x.check_date()
×
722
        except x509.CertificateError as e:
×
723
            self.logger.info(f"certificate has expired: {e}")
×
724
            os.unlink(self.cert_path)  # delete pinned cert only in this case
×
725
            return False
×
726
        self._verify_certificate_fingerprint(bytes(b))
×
727
        return True
×
728

729
    async def _get_ssl_context(self) -> Optional[ssl.SSLContext]:
1 ✔
730
        if self.protocol != 's':
1 ✔
731
            # using plaintext TCP
732
            return None
1 ✔
733

734
        # see if we already have cert for this server; or get it for the first time
735
        ca_sslc = ssl.create_default_context(purpose=ssl.Purpose.SERVER_AUTH, cafile=ca_path)
×
736
        if not self._is_saved_ssl_cert_available():
×
737
            try:
×
738
                await self._try_saving_ssl_cert_for_first_time(ca_sslc)
×
739
            except (OSError, ConnectError, aiorpcx.socks.SOCKSError) as e:
×
740
                raise ErrorGettingSSLCertFromServer(e) from e
×
741
        # now we have a file saved in our certificate store
742
        siz = os.stat(self.cert_path).st_size
×
743
        if siz == 0:
×
744
            # CA signed cert
745
            sslc = ca_sslc
×
746
        else:
747
            # pinned self-signed cert
748
            sslc = ssl.create_default_context(purpose=ssl.Purpose.SERVER_AUTH, cafile=self.cert_path)
×
749
            # note: Flag "ssl.VERIFY_X509_STRICT" is enabled by default in python 3.13+ (disabled in older versions).
750
            #       We explicitly disable it as it breaks lots of servers.
751
            sslc.verify_flags &= ~ssl.VERIFY_X509_STRICT
×
752
            sslc.check_hostname = False
×
753
        return sslc
×
754

755
    def handle_disconnect(func):
1 ✔
756
        @functools.wraps(func)
1 ✔
757
        async def wrapper_func(self: 'Interface', *args, **kwargs):
1 ✔
758
            try:
1 ✔
759
                return await func(self, *args, **kwargs)
1 ✔
760
            except GracefulDisconnect as e:
1 ✔
761
                self.logger.log(e.log_level, f"disconnecting due to {repr(e)}")
1 ✔
762
            except aiorpcx.jsonrpc.RPCError as e:
×
763
                self.logger.warning(f"disconnecting due to {repr(e)}")
×
764
                self.logger.debug(f"(disconnect) trace for {repr(e)}", exc_info=True)
×
765
            finally:
766
                self.got_disconnected.set()
1 ✔
767
                # Make sure taskgroup gets cleaned-up. This explicit clean-up is needed here
768
                # in case the "with taskgroup" ctx mgr never got a chance to run:
769
                await self.taskgroup.cancel_remaining()
1 ✔
770
                await self.network.connection_down(self)
1 ✔
771
                # if was not 'ready' yet, schedule waiting coroutines:
772
                self.ready.cancel()
1 ✔
773
        return wrapper_func
1 ✔
774

775
    @ignore_exceptions  # do not kill network.taskgroup
1 ✔
776
    @log_exceptions
1 ✔
777
    @handle_disconnect
1 ✔
778
    async def run(self):
1 ✔
779
        try:
1 ✔
780
            ssl_context = await self._get_ssl_context()
1 ✔
781
        except (ErrorParsingSSLCert, ErrorGettingSSLCertFromServer) as e:
×
782
            self.logger.info(f'disconnecting due to: {repr(e)}')
×
783
            return
×
784
        try:
1 ✔
785
            await self.open_session(ssl_context=ssl_context)
1 ✔
786
        except (asyncio.CancelledError, ConnectError, aiorpcx.socks.SOCKSError) as e:
1 ✔
787
            # make SSL errors for main interface more visible (to help servers ops debug cert pinning issues)
788
            if (isinstance(e, ConnectError) and isinstance(e.__cause__, ssl.SSLError)
1 ✔
789
                    and self.is_main_server() and not self.network.auto_connect):
790
                self.logger.warning(f'Cannot connect to main server due to SSL error '
×
791
                                    f'(maybe cert changed compared to "{self.cert_path}"). Exc: {repr(e)}')
792
            else:
793
                self.logger.info(f'disconnecting due to: {repr(e)}')
1 ✔
794
            return
1 ✔
795

796
    def _mark_ready(self) -> None:
1 ✔
797
        if self.ready.cancelled():
1 ✔
798
            raise GracefulDisconnect('conn establishment was too slow; *ready* future was cancelled')
×
799
        if self.ready.done():
1 ✔
800
            return
1 ✔
801

802
        assert self.tip_header
1 ✔
803
        chain = self.bc_mgr.check_header(self.tip_header)
1 ✔
804
        if not chain:
1 ✔
805
            self.blockchain = self.bc_mgr.get_best_chain()
1 ✔
806
        else:
807
            self.blockchain = chain
×
808
        assert self.blockchain is not None
1 ✔
809

810
        self.logger.info(f"set blockchain with height {self.blockchain.height()}")
1 ✔
811

812
        self.ready.set_result(1)
1 ✔
813

814
    def is_connected_and_ready(self) -> bool:
1 ✔
815
        return self.ready.done() and not self.got_disconnected.is_set()
×
816

817
    async def _save_certificate(self) -> None:
1 ✔
818
        if not os.path.exists(self.cert_path):
×
819
            # we may need to retry this a few times, in case the handshake hasn't completed
820
            for _ in range(10):
×
821
                dercert = await self._fetch_certificate()
×
822
                if dercert:
×
823
                    self.logger.info("succeeded in getting cert")
×
824
                    self._verify_certificate_fingerprint(dercert)
×
825
                    with open(self.cert_path, 'w') as f:
×
826
                        cert = ssl.DER_cert_to_PEM_cert(dercert)
×
827
                        # workaround android bug
828
                        cert = re.sub("([^\n])-----END CERTIFICATE-----","\\1\n-----END CERTIFICATE-----",cert)
×
829
                        f.write(cert)
×
830
                        # even though close flushes, we can't fsync when closed.
831
                        # and we must flush before fsyncing, cause flush flushes to OS buffer
832
                        # fsync writes to OS buffer to disk
833
                        f.flush()
×
834
                        os.fsync(f.fileno())
×
835
                    break
×
836
                await asyncio.sleep(1)
×
837
            else:
838
                raise GracefulDisconnect("could not get certificate after 10 tries")
×
839

840
    async def _fetch_certificate(self) -> bytes:
1 ✔
841
        sslc = ssl.SSLContext(protocol=ssl.PROTOCOL_TLS_CLIENT)
×
842
        sslc.check_hostname = False
×
843
        sslc.verify_mode = ssl.CERT_NONE
×
844
        async with _RSClient(
×
845
            session_factory=RPCSession,
846
            host=self.host, port=self.port,
847
            ssl=sslc,
848
            proxy=self.proxy,
849
            transport=PaddedRSTransport,
850
        ) as session:
851
            asyncio_transport = session.transport._asyncio_transport  # type: asyncio.BaseTransport
×
852
            ssl_object = asyncio_transport.get_extra_info("ssl_object")  # type: ssl.SSLObject
×
853
            return ssl_object.getpeercert(binary_form=True)
×
854

855
    def _get_expected_fingerprint(self) -> Optional[str]:
1 ✔
856
        if self.is_main_server():
×
857
            return self.network.config.NETWORK_SERVERFINGERPRINT
×
858
        return None
×
859

860
    def _verify_certificate_fingerprint(self, certificate: bytes) -> None:
1 ✔
861
        expected_fingerprint = self._get_expected_fingerprint()
×
862
        if not expected_fingerprint:
×
863
            return
×
864
        fingerprint = hashlib.sha256(certificate).hexdigest()
×
865
        fingerprints_match = fingerprint.lower() == expected_fingerprint.lower()
×
866
        if not fingerprints_match:
×
867
            util.trigger_callback('cert_mismatch')
×
868
            raise ErrorSSLCertFingerprintMismatch('Refusing to connect to server due to cert fingerprint mismatch')
×
869
        self.logger.info("cert fingerprint verification passed")
×
870

871
    async def _maybe_warm_headers_cache(self, *, from_height: int, to_height: int, mode: ChainResolutionMode) -> None:
1 ✔
872
        """Populate header cache for block heights in range [from_height, to_height]."""
873
        assert from_height <= to_height, (from_height, to_height)
1 ✔
874
        assert to_height - from_height < MAX_NUM_HEADERS_PER_REQUEST
1 ✔
875
        if all(height in self._headers_cache for height in range(from_height, to_height+1)):
1 ✔
876
            # cache already has all requested headers
877
            return
1 ✔
878
        # use lower timeout as we usually have network.bhi_lock here
879
        timeout = self.network.get_network_timeout_seconds(NetworkTimeout.Urgent)
1 ✔
880
        count = to_height - from_height + 1
1 ✔
881
        headers = await self.get_block_headers(start_height=from_height, count=count, timeout=timeout, mode=mode)
1 ✔
882
        for idx, raw_header in enumerate(headers):
1 ✔
883
            header_height = from_height + idx
1 ✔
884
            self._headers_cache[header_height] = raw_header
1 ✔
885

886
    async def get_block_header(self, height: int, *, mode: ChainResolutionMode) -> dict:
1 ✔
887
        if not is_non_negative_integer(height):
1 ✔
888
            raise Exception(f"{repr(height)} is not a block height")
×
889
        #self.logger.debug(f'get_block_header() {height} in {mode=}')
890
        # use lower timeout as we usually have network.bhi_lock here
891
        timeout = self.network.get_network_timeout_seconds(NetworkTimeout.Urgent)
1 ✔
892
        if raw_header := self._headers_cache.get(height):
1 ✔
893
            return blockchain.deserialize_header(raw_header, height)
1 ✔
894
        self.logger.info(f'requesting block header {height} in {mode=}')
×
895
        res = await self.session.send_request('blockchain.block.header', [height], timeout=timeout)
×
896
        return blockchain.deserialize_header(bytes.fromhex(res), height)
×
897

898
    async def get_block_headers(
1 ✔
899
        self,
900
        *,
901
        start_height: int,
902
        count: int,
903
        timeout=None,
904
        mode: Optional[ChainResolutionMode] = None,
905
    ) -> Sequence[bytes]:
906
        """Request a number of consecutive block headers, starting at `start_height`.
907
        `count` is the num of requested headers, BUT note the server might return fewer than this
908
        (if range would extend beyond its tip).
909
        note: the returned headers are not verified or parsed at all.
910
        """
911
        if not is_non_negative_integer(start_height):
1 ✔
912
            raise Exception(f"{repr(start_height)} is not a block height")
×
913
        if not is_non_negative_integer(count) or not (0 < count <= MAX_NUM_HEADERS_PER_REQUEST):
1 ✔
914
            raise Exception(f"{repr(count)} not an int in range ]0, {MAX_NUM_HEADERS_PER_REQUEST}]")
×
915
        self.logger.info(
1 ✔
916
            f"requesting block headers: [{start_height}, {start_height+count-1}], {count=}"
917
            + (f" (in {mode=})" if mode is not None else "")
918
        )
919
        res = await self.session.send_request('blockchain.block.headers', [start_height, count], timeout=timeout)
1 ✔
920
        # check response
921
        assert_dict_contains_field(res, field_name='count')
1 ✔
922
        assert_dict_contains_field(res, field_name='max')
1 ✔
923
        assert_non_negative_integer(res['count'])
1 ✔
924
        assert_non_negative_integer(res['max'])
1 ✔
925
        if self.active_protocol_tuple >= (1, 6):
1 ✔
926
            hex_headers_list = assert_dict_contains_field(res, field_name='headers')
1 ✔
927
            assert_list_or_tuple(hex_headers_list)
1 ✔
928
            for item in hex_headers_list:
1 ✔
929
                assert_hex_str(item)
1 ✔
930
                if len(item) != HEADER_SIZE * 2:
1 ✔
931
                    raise RequestCorrupted(f"invalid header size. got {len(item)//2}, expected {HEADER_SIZE}")
×
932
            if len(hex_headers_list) != res['count']:
1 ✔
933
                raise RequestCorrupted(f"{len(hex_headers_list)=} != {res['count']=}")
×
934
            headers = list(bfh(hex_header) for hex_header in hex_headers_list)
1 ✔
935
        else: # proto 1.4
936
            hex_headers_concat = assert_dict_contains_field(res, field_name='hex')
×
937
            assert_hex_str(hex_headers_concat)
×
938
            if len(hex_headers_concat) != HEADER_SIZE * 2 * res['count']:
×
939
                raise RequestCorrupted('inconsistent chunk hex and count')
×
940
            headers = list(util.chunks(bfh(hex_headers_concat), size=HEADER_SIZE))
×
941
        # we never request more than MAX_NUM_HEADERS_IN_REQUEST headers, but we enforce those fit in a single response
942
        if res['max'] < MAX_NUM_HEADERS_PER_REQUEST:
1 ✔
943
            raise RequestCorrupted(f"server uses too low 'max' count for block.headers: {res['max']} < {MAX_NUM_HEADERS_PER_REQUEST}")
×
944
        if res['count'] > count:
1 ✔
945
            raise RequestCorrupted(f"asked for {count} headers but got more: {res['count']}")
×
946
        elif res['count'] < count:
1 ✔
947
            # we only tolerate getting fewer headers if it is due to reaching the tip
948
            end_height = start_height + res['count'] - 1
×
949
            if end_height < self.tip:  # still below tip. why did server not send more?!
×
950
                raise RequestCorrupted(
×
951
                    f"asked for {count} headers but got fewer: {res['count']}. ({start_height=}, {self.tip=})")
952
        # checks done.
953
        return headers
1 ✔
954

955
    async def request_chunk_below_max_checkpoint(
1 ✔
956
        self,
957
        *,
958
        height: int,
959
    ) -> None:
960
        if not is_non_negative_integer(height):
×
961
            raise Exception(f"{repr(height)} is not a block height")
×
962
        assert height <= constants.net.max_checkpoint(), f"{height=} must be <= cp={constants.net.max_checkpoint()}"
×
963
        index = height // CHUNK_SIZE
×
964
        if index in self._requested_chunks:
×
965
            return None
×
966
        self.logger.debug(f"requesting chunk from height {height}")
×
967
        try:
×
968
            self._requested_chunks.add(index)
×
969
            headers = await self.get_block_headers(start_height=index * CHUNK_SIZE, count=CHUNK_SIZE)
×
970
        finally:
971
            self._requested_chunks.discard(index)
×
972
        conn = self.blockchain.connect_chunk(index, data=b"".join(headers))
×
973
        if not conn:
×
974
            raise RequestCorrupted(f"chunk ({index=}, for {height=}) does not connect to blockchain")
×
975
        return None
×
976

977
    async def _fast_forward_chain(
1 ✔
978
        self,
979
        *,
980
        height: int,  # usually local chain tip + 1
981
        tip: int,  # server tip. we should not request past this.
982
    ) -> int:
983
        """Request some headers starting at `height` to grow the blockchain of this interface.
984
        Returns number of headers we managed to connect, starting at `height`.
985
        """
986
        if not is_non_negative_integer(height):
×
987
            raise Exception(f"{repr(height)} is not a block height")
×
988
        if not is_non_negative_integer(tip):
×
989
            raise Exception(f"{repr(tip)} is not a block height")
×
990
        if not (height > constants.net.max_checkpoint()
×
991
                or height == 0 == constants.net.max_checkpoint()):
992
            raise Exception(f"{height=} must be > cp={constants.net.max_checkpoint()}")
×
993
        assert height <= tip, f"{height=} must be <= {tip=}"
×
994
        # Request a few chunks of headers concurrently.
995
        # tradeoffs:
996
        # - more chunks: higher memory requirements
997
        # - more chunks: higher concurrency => syncing needs fewer network round-trips
998
        # - if a chunk does not connect, bandwidth for all later chunks is wasted
999
        async with OldTaskGroup() as group:
×
1000
            tasks = []  # type: List[Tuple[int, asyncio.Task[Sequence[bytes]]]]
×
1001
            index0 = height // CHUNK_SIZE
×
1002
            for chunk_cnt in range(10):
×
1003
                index = index0 + chunk_cnt
×
1004
                start_height = index * CHUNK_SIZE
×
1005
                if start_height > tip:
×
1006
                    break
×
1007
                end_height = min(start_height + CHUNK_SIZE - 1, tip)
×
1008
                size = end_height - start_height + 1
×
1009
                tasks.append((index, await group.spawn(self.get_block_headers(start_height=start_height, count=size))))
×
1010
        # try to connect chunks
1011
        num_headers = 0
×
1012
        for index, task in tasks:
×
1013
            headers = task.result()
×
1014
            conn = self.blockchain.connect_chunk(index, data=b"".join(headers))
×
1015
            if not conn:
×
1016
                break
×
1017
            num_headers += len(headers)
×
1018
        # We started at a chunk boundary, instead of requested `height`. Need to correct for that.
1019
        offset = height - index0 * CHUNK_SIZE
×
1020
        return max(0, num_headers - offset)
×
1021

1022
    def is_main_server(self) -> bool:
1 ✔
1023
        return (self.network.interface == self or
1 ✔
1024
                self.network.interface is None and self.network.default_server == self.server)
1025

1026
    def mark_as_main_server(self) -> None:
1 ✔
1027
        """Called when the network switches to this interface."""
1028
        assert self.session
×
1029
        self.session.remove_resource_limits()
×
1030

1031
    async def open_session(
1 ✔
1032
        self,
1033
        *,
1034
        ssl_context: Optional[ssl.SSLContext],
1035
        exit_early: bool = False,
1036
    ):
1037
        session_factory = lambda *args, iface=self, **kwargs: NotificationSession(*args, **kwargs, interface=iface)
1 ✔
1038
        async with _RSClient(
1 ✔
1039
            session_factory=session_factory,
1040
            host=self.host, port=self.port,
1041
            ssl=ssl_context,
1042
            proxy=self.proxy,
1043
            transport=PaddedRSTransport,
1044
        ) as session:
1045
            start = time.perf_counter()
1 ✔
1046
            self.session = session  # type: NotificationSession
1 ✔
1047
            self.session.set_default_timeout(self.network.get_network_timeout_seconds(NetworkTimeout.Generic))
1 ✔
1048
            client_prange = [version.PROTOCOL_VERSION_MIN, version.PROTOCOL_VERSION_MAX]
1 ✔
1049
            try:
1 ✔
1050
                ver = await session.send_request('server.version', [self.client_name(), client_prange])
1 ✔
1051
            except aiorpcx.jsonrpc.RPCError as e:
×
1052
                raise GracefulDisconnect(e)  # probably 'unsupported protocol version'
×
1053
            if exit_early:
1 ✔
1054
                return
×
1055
            self.active_protocol_tuple = protocol_tuple(ver[1])
1 ✔
1056
            client_pmin = protocol_tuple(client_prange[0])
1 ✔
1057
            client_pmax = protocol_tuple(client_prange[1])
1 ✔
1058
            if not (client_pmin <= self.active_protocol_tuple <= client_pmax):
1 ✔
1059
                raise GracefulDisconnect(f'server violated protocol-version-negotiation. '
×
1060
                                         f'we asked for {client_prange!r}, they sent {ver[1]!r}')
1061
            if not self.network.check_interface_against_healthy_spread_of_connected_servers(self):
1 ✔
1062
                raise GracefulDisconnect(f'too many connected servers already '
×
1063
                                         f'in bucket {self.bucket_based_on_ipaddress()}')
1064

1065
            try:
1 ✔
1066
                features = await session.send_request('server.features')
1 ✔
1067
                server_genesis_hash = assert_dict_contains_field(features, field_name='genesis_hash')
1 ✔
1068
            except (aiorpcx.jsonrpc.RPCError, RequestCorrupted) as e:
×
1069
                raise GracefulDisconnect(e)
×
1070
            if server_genesis_hash != constants.net.GENESIS:
1 ✔
1071
                raise GracefulDisconnect(f'server on different chain: {server_genesis_hash=}. ours: {constants.net.GENESIS}')
×
1072
            self.logger.info(f"connection established. version: {ver}, handshake duration: {(time.perf_counter() - start) * 1000:.2f} ms")
1 ✔
1073

1074
            try:
1 ✔
1075
                async with self.taskgroup as group:
1 ✔
1076
                    await group.spawn(self.ping)
1 ✔
1077
                    await group.spawn(self.request_fee_estimates)
1 ✔
1078
                    await group.spawn(self.run_fetch_blocks)
1 ✔
1079
                    await group.spawn(self.monitor_connection)
1 ✔
1080
            except aiorpcx.jsonrpc.RPCError as e:
1 ✔
1081
                if e.code in (
×
1082
                    JSONRPC.EXCESSIVE_RESOURCE_USAGE,
1083
                    JSONRPC.SERVER_BUSY,
1084
                    JSONRPC.METHOD_NOT_FOUND,
1085
                    JSONRPC.INTERNAL_ERROR,
1086
                ):
1087
                    log_level = logging.WARNING if self.is_main_server() else logging.INFO
×
1088
                    raise GracefulDisconnect(e, log_level=log_level) from e
×
1089
                raise
×
1090
            finally:
1091
                self.got_disconnected.set()  # set this ASAP, ideally before any awaits
1 ✔
1092

1093
    async def monitor_connection(self):
1 ✔
1094
        while True:
1 ✔
1095
            await asyncio.sleep(1)
1 ✔
1096
            # If the session/transport is no longer open, we disconnect.
1097
            # e.g. if the remote cleanly sends EOF, we would handle that here.
1098
            # note: If the user pulls the ethernet cable or disconnects wifi,
1099
            #       ideally we would detect that here, so that the GUI/etc can reflect that.
1100
            #       - On Android, this seems to work reliably , where asyncio.BaseProtocol.connection_lost()
1101
            #         gets called with e.g. ConnectionAbortedError(103, 'Software caused connection abort').
1102
            #       - On desktop Linux/Win, it seems BaseProtocol.connection_lost() is not called in such cases.
1103
            #         Hence, in practice the connection issue will only be detected the next time we try
1104
            #         to send a message (plus timeout), which can take minutes...
1105
            if not self.session or self.session.is_closing():
1 ✔
1106
                raise GracefulDisconnect('session was closed')
1 ✔
1107

1108
    async def ping(self):
1 ✔
1109
        # We periodically send a "ping" msg to make sure the server knows we are still here.
1110
        # Adding a bit of randomness generates some noise against traffic analysis.
1111
        while True:
1 ✔
1112
            await asyncio.sleep(random.random() * 300)
1 ✔
1113
            await self.session.send_request('server.ping')
×
1114
            await self._maybe_send_noise()
×
1115

1116
    async def _maybe_send_noise(self):
1 ✔
1117
        while random.random() < 0.2:
1 ✔
1118
            await asyncio.sleep(random.random())
1 ✔
1119
            await self.session.send_request('server.ping')
1 ✔
1120

1121
    async def request_fee_estimates(self):
1 ✔
1122
        while True:
1 ✔
1123
            async with OldTaskGroup() as group:
1 ✔
1124
                fee_tasks = []
1 ✔
1125
                for i in FEE_ETA_TARGETS[0:-1]:
1 ✔
1126
                    fee_tasks.append((i, await group.spawn(self.get_estimatefee(i))))
1 ✔
1127
            for nblock_target, task in fee_tasks:
1 ✔
1128
                fee = task.result()
1 ✔
1129
                if fee < 0: continue
1 ✔
1130
                assert isinstance(fee, int)
1 ✔
1131
                self.fee_estimates_eta[nblock_target] = fee
1 ✔
1132
            self.network.update_fee_estimates()
1 ✔
1133
            await asyncio.sleep(60)
1 ✔
1134

1135
    async def close(self, *, force_after: int | None = None):
1 ✔
1136
        """Closes the connection and waits for it to be closed.
1137
        We try to flush buffered data to the wire, which can take some time.
1138
        """
1139
        if self.session:
1 ✔
1140
            await self.session.close(force_after=force_after)
1 ✔
1141
        # monitor_connection will cancel tasks
1142

1143
    async def run_fetch_blocks(self):
1 ✔
1144
        header_queue = asyncio.Queue()
1 ✔
1145
        await self.session.subscribe('blockchain.headers.subscribe', [], header_queue)
1 ✔
1146
        while True:
1 ✔
1147
            item = await header_queue.get()
1 ✔
1148
            if not header_queue.empty():
1 ✔
1149
                continue  # skip stale notifications
1 ✔
1150
            # parse response
1151
            assert len(item) == 1
1 ✔
1152
            resp_header = item[0]
1 ✔
1153
            assert_dict(resp_header)
1 ✔
1154
            height = assert_dict_contains_field(resp_header, field_name='height')
1 ✔
1155
            assert_non_negative_integer(height)
1 ✔
1156
            header_hex = assert_dict_contains_field(resp_header, field_name='hex')
1 ✔
1157
            header_bytes = bfh(header_hex)
1 ✔
1158
            assert len(resp_header) == 2, f"resp_header contains redundant fields. got {resp_header.keys()}"
1 ✔
1159
            header_dict = blockchain.deserialize_header(header_bytes, height)
1 ✔
1160
            # process header
1161
            self.tip_header = header_dict  # TODO assert it got changed to something different?
1 ✔
1162
            self.tip = height
1 ✔
1163
            if self.tip < constants.net.max_checkpoint():
1 ✔
1164
                raise GracefulDisconnect(
×
1165
                    f"server tip below max checkpoint. ({self.tip} < {constants.net.max_checkpoint()})")
1166
            self._mark_ready()
1 ✔
1167
            self._headers_cache.clear()  # tip changed, so assume anything could have happened with chain
1 ✔
1168
            self._headers_cache[height] = header_bytes
1 ✔
1169
            try:
1 ✔
1170
                blockchain_updated = await self._process_header_at_tip()
1 ✔
1171
            finally:
1172
                self._headers_cache.clear()  # to reduce memory usage
1 ✔
1173
            # header processing done
1174
            if self.is_main_server() or blockchain_updated:
1 ✔
1175
                self.logger.info(f"new chain tip. {height=}")
1 ✔
1176
            if blockchain_updated:
1 ✔
1177
                util.trigger_callback('blockchain_updated')
1 ✔
1178
                self._blockchain_updated.set()
1 ✔
1179
                self._blockchain_updated.clear()
1 ✔
1180
            util.trigger_callback('network_updated')
1 ✔
1181
            await self.network.switch_unwanted_fork_interface()
1 ✔
1182
            await self.network.switch_lagging_interface()
1 ✔
1183
            await self.taskgroup.spawn(self._maybe_send_noise())
1 ✔
1184

1185
    async def _process_header_at_tip(self) -> bool:
1 ✔
1186
        """Returns:
1187
        False - boring fast-forward: we already have this header as part of this blockchain from another interface,
1188
        True - new header we didn't have, or reorg
1189
        """
1190
        height, header = self.tip, self.tip_header
1 ✔
1191
        async with self.network.bhi_lock:
1 ✔
1192
            if self.blockchain.height() >= height and self.blockchain.check_header(header):
1 ✔
1193
                # another interface amended the blockchain
1194
                return False
1 ✔
1195
            await self.sync_until(height)
1 ✔
1196
            return True
1 ✔
1197

1198
    async def sync_until(
1 ✔
1199
        self,
1200
        height: int,
1201
        *,
1202
        next_height: Optional[int] = None,  # sync target. typically the tip, except in unit tests
1203
    ) -> Tuple[ChainResolutionMode, int]:
1204
        if next_height is None:
1 ✔
1205
            next_height = self.tip
1 ✔
1206
        last = None  # type: Optional[ChainResolutionMode]
1 ✔
1207
        while last is None or height <= next_height:
1 ✔
1208
            prev_last, prev_height = last, height
1 ✔
1209
            if next_height > height + 144:
1 ✔
1210
                # We are far from the tip.
1211
                # It is more efficient to process headers in large batches (CPU/disk_usage/logging).
1212
                # (but this wastes a little bandwidth, if we are not on a chunk boundary)
1213
                num_headers = await self._fast_forward_chain(
×
1214
                    height=height, tip=next_height)
1215
                if num_headers == 0:
×
1216
                    if height <= constants.net.max_checkpoint():
×
1217
                        raise GracefulDisconnect('server chain conflicts with checkpoints or genesis')
×
1218
                    last, height = await self.step(height)
×
1219
                    continue
×
1220
                # report progress to gui/etc
1221
                util.trigger_callback('blockchain_updated')
×
1222
                self._blockchain_updated.set()
×
1223
                self._blockchain_updated.clear()
×
1224
                util.trigger_callback('network_updated')
×
1225
                height += num_headers
×
1226
                assert height <= next_height+1, (height, self.tip)
×
1227
                last = ChainResolutionMode.CATCHUP
×
1228
            else:
1229
                # We are close to the tip, so process headers one-by-one.
1230
                # (note: due to headers_cache, to save network latency, this can still batch-request headers)
1231
                last, height = await self.step(height)
1 ✔
1232
            assert (prev_last, prev_height) != (last, height), 'had to prevent infinite loop in interface.sync_until'
1 ✔
1233
        return last, height
1 ✔
1234

1235
    async def step(
1 ✔
1236
        self,
1237
        height: int,
1238
    ) -> Tuple[ChainResolutionMode, int]:
1239
        assert 0 <= height <= self.tip, (height, self.tip)
1 ✔
1240
        await self._maybe_warm_headers_cache(
1 ✔
1241
            from_height=height,
1242
            to_height=min(self.tip, height+MAX_NUM_HEADERS_PER_REQUEST-1),
1243
            mode=ChainResolutionMode.CATCHUP,
1244
        )
1245
        header = await self.get_block_header(height, mode=ChainResolutionMode.CATCHUP)
1 ✔
1246

1247
        chain = self.bc_mgr.check_header(header)
1 ✔
1248
        if chain:
1 ✔
1249
            self.blockchain = chain
1 ✔
1250
            # note: there is an edge case here that is not handled.
1251
            # we might know the blockhash (enough for check_header) but
1252
            # not have the header itself. e.g. regtest chain with only genesis.
1253
            # this situation resolves itself on the next block
1254
            return ChainResolutionMode.CATCHUP, height+1
1 ✔
1255

1256
        can_connect = self.bc_mgr.can_connect(header)
1 ✔
1257
        if not can_connect:
1 ✔
1258
            self.logger.info(f"can't connect new block: {height=}")
1 ✔
1259
            height, header, bad, bad_header = await self._search_headers_backwards(height, header=header)
1 ✔
1260
            chain = self.bc_mgr.check_header(header)
1 ✔
1261
            can_connect = self.bc_mgr.can_connect(header)
1 ✔
1262
            assert chain or can_connect
1 ✔
1263
        if can_connect:
1 ✔
1264
            height += 1
1 ✔
1265
            self.blockchain = can_connect
1 ✔
1266
            self.blockchain.save_header(header)
1 ✔
1267
            return ChainResolutionMode.CATCHUP, height
1 ✔
1268

1269
        good, bad, bad_header = await self._search_headers_binary(height, bad, bad_header, chain)
1 ✔
1270
        return await self._resolve_potential_chain_fork_given_forkpoint(good, bad, bad_header)
1 ✔
1271

1272
    async def _search_headers_binary(
1 ✔
1273
        self,
1274
        height: int,
1275
        bad: int,
1276
        bad_header: dict,
1277
        chain: Optional[Blockchain],
1278
    ) -> Tuple[int, int, dict]:
1279
        assert bad == bad_header['block_height']
1 ✔
1280
        self._assert_header_does_not_check_against_any_chain(bad_header)
1 ✔
1281

1282
        self.blockchain = chain
1 ✔
1283
        good = height
1 ✔
1284
        while True:
1 ✔
1285
            assert 0 <= good < bad, (good, bad)
1 ✔
1286
            height = (good + bad) // 2
1 ✔
1287
            self.logger.info(f"binary step. good {good}, bad {bad}, height {height}")
1 ✔
1288
            if bad - good + 1 <= MAX_NUM_HEADERS_PER_REQUEST:  # if interval is small, trade some bandwidth for lower latency
1 ✔
1289
                await self._maybe_warm_headers_cache(
1 ✔
1290
                    from_height=good, to_height=bad, mode=ChainResolutionMode.BINARY)
1291
            header = await self.get_block_header(height, mode=ChainResolutionMode.BINARY)
1 ✔
1292
            chain = self.bc_mgr.check_header(header)
1 ✔
1293
            if chain:
1 ✔
1294
                self.blockchain = chain
1 ✔
1295
                good = height
1 ✔
1296
            else:
1297
                bad = height
1 ✔
1298
                bad_header = header
1 ✔
1299
            if good + 1 == bad:
1 ✔
1300
                break
1 ✔
1301

1302
        if not self.blockchain.can_connect(bad_header, check_height=False):
1 ✔
1303
            raise Exception('unexpected bad header during binary: {}'.format(bad_header))
×
1304
        self._assert_header_does_not_check_against_any_chain(bad_header)
1 ✔
1305

1306
        self.logger.info(f"binary search exited. good {good}, bad {bad}. {chain=}")
1 ✔
1307
        return good, bad, bad_header
1 ✔
1308

1309
    async def _resolve_potential_chain_fork_given_forkpoint(
1 ✔
1310
        self,
1311
        good: int,
1312
        bad: int,
1313
        bad_header: dict,
1314
    ) -> Tuple[ChainResolutionMode, int]:
1315
        assert good + 1 == bad
1 ✔
1316
        assert bad == bad_header['block_height']
1 ✔
1317
        self._assert_header_does_not_check_against_any_chain(bad_header)
1 ✔
1318
        # 'good' is the height of a block 'good_header', somewhere in self.blockchain.
1319
        # bad_header connects to good_header; bad_header itself is NOT in self.blockchain.
1320

1321
        bh = self.blockchain.height()
1 ✔
1322
        assert bh >= good, (bh, good)
1 ✔
1323
        if bh == good:
1 ✔
1324
            height = good + 1
1 ✔
1325
            self.logger.info(f"catching up from {height}")
1 ✔
1326
            return ChainResolutionMode.NO_FORK, height
1 ✔
1327

1328
        # this is a new fork we don't yet have
1329
        height = bad + 1
1 ✔
1330
        self.logger.info(f"new fork at bad height {bad}")
1 ✔
1331
        b = self.blockchain.fork(bad_header)  # type: Blockchain
1 ✔
1332
        self.blockchain = b
1 ✔
1333
        assert b.forkpoint == bad
1 ✔
1334
        return ChainResolutionMode.FORK, height
1 ✔
1335

1336
    async def _search_headers_backwards(
1 ✔
1337
        self,
1338
        height: int,
1339
        *,
1340
        header: dict,
1341
    ) -> Tuple[int, dict, int, dict]:
1342
        async def iterate():
1 ✔
1343
            nonlocal height, header
1344
            checkp = False
1 ✔
1345
            if height <= constants.net.max_checkpoint():
1 ✔
1346
                height = constants.net.max_checkpoint()
1 ✔
1347
                checkp = True
1 ✔
1348
            header = await self.get_block_header(height, mode=ChainResolutionMode.BACKWARD)
1 ✔
1349
            chain = self.bc_mgr.check_header(header)
1 ✔
1350
            can_connect = self.bc_mgr.can_connect(header)
1 ✔
1351
            if chain or can_connect:
1 ✔
1352
                return False
1 ✔
1353
            if checkp:
1 ✔
1354
                raise GracefulDisconnect("server chain conflicts with checkpoints")
×
1355
            return True
1 ✔
1356

1357
        bad, bad_header = height, header
1 ✔
1358
        self._assert_header_does_not_check_against_any_chain(bad_header)
1 ✔
1359
        with self.bc_mgr.blockchains_lock:
1 ✔
1360
            chains = list(self.bc_mgr.blockchains.values())
1 ✔
1361
        local_max = max([0] + [x.height() for x in chains])
1 ✔
1362
        height = min(local_max + 1, height - 1)
1 ✔
1363
        assert height >= 0
1 ✔
1364

1365
        await self._maybe_warm_headers_cache(
1 ✔
1366
            from_height=max(0, height-10), to_height=height, mode=ChainResolutionMode.BACKWARD)
1367

1368
        delta = 2
1 ✔
1369
        while await iterate():
1 ✔
1370
            bad, bad_header = height, header
1 ✔
1371
            height -= delta
1 ✔
1372
            delta *= 2
1 ✔
1373

1374
        self._assert_header_does_not_check_against_any_chain(bad_header)
1 ✔
1375
        self.logger.info(f"exiting backward mode at {height}")
1 ✔
1376
        return height, header, bad, bad_header
1 ✔
1377

1378
    @classmethod
1 ✔
1379
    def client_name(cls) -> str:
1 ✔
1380
        return f'electrum/{version.ELECTRUM_VERSION}'
×
1381

1382
    def is_tor(self):
1 ✔
1383
        return self.host.endswith('.onion')
×
1384

1385
    def ip_addr(self) -> Optional[str]:
1 ✔
1386
        session = self.session
×
1387
        if not session: return None
×
1388
        peer_addr = session.remote_address()
×
1389
        if not peer_addr: return None
×
1390
        return str(peer_addr.host)
×
1391

1392
    def bucket_based_on_ipaddress(self) -> str:
1 ✔
1393
        def do_bucket():
×
1394
            if self.is_tor():
×
1395
                return BUCKET_NAME_OF_ONION_SERVERS
×
1396
            try:
×
1397
                ip_addr = ip_address(self.ip_addr())  # type: Union[IPv4Address, IPv6Address]
×
1398
            except ValueError:
×
1399
                return ''
×
1400
            if not ip_addr:
×
1401
                return ''
×
1402
            if ip_addr.is_loopback:  # localhost is exempt
×
1403
                return ''
×
1404
            if ip_addr.version == 4:
×
1405
                slash16 = IPv4Network(ip_addr).supernet(prefixlen_diff=32-16)
×
1406
                return str(slash16)
×
1407
            elif ip_addr.version == 6:
×
1408
                slash48 = IPv6Network(ip_addr).supernet(prefixlen_diff=128-48)
×
1409
                return str(slash48)
×
1410
            return ''
×
1411

1412
        if not self._ipaddr_bucket:
×
1413
            self._ipaddr_bucket = do_bucket()
×
1414
        return self._ipaddr_bucket
×
1415

1416
    async def get_merkle_for_transaction(self, tx_hash: str, tx_height: int) -> dict:
1 ✔
1417
        if not is_hash256_str(tx_hash):
1 ✔
1418
            raise Exception(f"{repr(tx_hash)} is not a txid")
×
1419
        if not is_non_negative_integer(tx_height):
1 ✔
1420
            raise Exception(f"{repr(tx_height)} is not a block height")
×
1421
        # do request
1422
        res = await self.session.send_request('blockchain.transaction.get_merkle', [tx_hash, tx_height])
1 ✔
1423
        # check response
1424
        block_height = assert_dict_contains_field(res, field_name='block_height')
1 ✔
1425
        merkle = assert_dict_contains_field(res, field_name='merkle')
1 ✔
1426
        pos = assert_dict_contains_field(res, field_name='pos')
1 ✔
1427
        # note: tx_height was just a hint to the server, don't enforce the response to match it
1428
        assert_non_negative_integer(block_height)
1 ✔
1429
        assert_non_negative_integer(pos)
1 ✔
1430
        assert_list_or_tuple(merkle)
1 ✔
1431
        for item in merkle:
1 ✔
1432
            assert_hash256_str(item)
1 ✔
1433
        return res
1 ✔
1434

1435
    async def get_transaction(self, tx_hash: str, *, timeout=None) -> str:
1 ✔
1436
        if not is_hash256_str(tx_hash):
1 ✔
1437
            raise Exception(f"{repr(tx_hash)} is not a txid")
×
1438
        if rawtx_bytes := self._rawtx_cache.get(tx_hash):
1 ✔
1439
            return rawtx_bytes.hex()
1 ✔
1440
        raw = await self.session.send_request('blockchain.transaction.get', [tx_hash], timeout=timeout)
1 ✔
1441
        # validate response
1442
        if not is_hex_str(raw):
1 ✔
1443
            raise RequestCorrupted(f"received garbage (non-hex) as tx data (txid {tx_hash}): {raw!r}")
×
1444
        tx = Transaction(raw)
1 ✔
1445
        try:
1 ✔
1446
            tx.deserialize()  # see if raises
1 ✔
1447
        except Exception as e:
×
1448
            raise RequestCorrupted(f"cannot deserialize received transaction (txid {tx_hash})") from e
×
1449
        if tx.txid() != tx_hash:
1 ✔
1450
            raise RequestCorrupted(f"received tx does not match expected txid {tx_hash} (got {tx.txid()})")
×
1451
        self._rawtx_cache[tx_hash] = bytes.fromhex(raw)
1 ✔
1452
        return raw
1 ✔
1453

1454
    async def broadcast_transaction(self, tx: 'Transaction', *, timeout=None) -> None:
1 ✔
1455
        """caller should handle TxBroadcastError and RequestTimedOut"""
1456
        txid_calc = tx.txid()
1 ✔
1457
        assert txid_calc is not None
1 ✔
1458
        rawtx = tx.serialize()
1 ✔
1459
        assert is_hex_str(rawtx)
1 ✔
1460
        if timeout is None:
1 ✔
1461
            timeout = self.network.get_network_timeout_seconds(NetworkTimeout.Urgent)
1 ✔
1462
        if any(DummyAddress.is_dummy_address(txout.address) for txout in tx.outputs()):
1 ✔
1463
            raise DummyAddressUsedInTxException("tried to broadcast tx with dummy address!")
×
1464
        try:
1 ✔
1465
            out = await self.session.send_request('blockchain.transaction.broadcast', [rawtx], timeout=timeout)
1 ✔
1466
            # note: both 'out' and exception messages are untrusted input from the server
1467
        except (RequestTimedOut, asyncio.CancelledError, asyncio.TimeoutError):
1 ✔
1468
            raise  # pass-through
×
1469
        except aiorpcx.jsonrpc.CodeMessageError as e:
1 ✔
1470
            self.logger.info(f"broadcast_transaction error [DO NOT TRUST THIS MESSAGE]: {error_text_str_to_safe_str(repr(e))}. tx={str(tx)}")
1 ✔
1471
            raise TxBroadcastServerReturnedError(sanitize_tx_broadcast_response(e.message)) from e
1 ✔
1472
        except BaseException as e:  # intentional BaseException for sanity!
×
1473
            self.logger.info(f"broadcast_transaction error2 [DO NOT TRUST THIS MESSAGE]: {error_text_str_to_safe_str(repr(e))}. tx={str(tx)}")
×
1474
            send_exception_to_crash_reporter(e)
×
1475
            raise TxBroadcastUnknownError() from e
×
1476
        if out != txid_calc:
1 ✔
1477
            self.logger.info(f"unexpected txid for broadcast_transaction [DO NOT TRUST THIS MESSAGE]: "
×
1478
                             f"{error_text_str_to_safe_str(out)} != {txid_calc}. tx={str(tx)}")
1479
            raise TxBroadcastHashMismatch(_("Server returned unexpected transaction ID."))
×
1480
        # broadcast succeeded.
1481
        # We now cache the rawtx, for *this interface only*. The tx likely touches some ismine addresses, affecting
1482
        # the status of a scripthash we are subscribed to. Caching here will save a future get_transaction RPC.
1483
        self._rawtx_cache[txid_calc] = bytes.fromhex(rawtx)
1 ✔
1484

1485
    async def broadcast_txpackage(self, txs: Sequence['Transaction']) -> bool:
1 ✔
1486
        assert self.active_protocol_tuple >= (1, 6), f"server using old protocol: {self.active_protocol_tuple}"
×
1487
        rawtxs = [tx.serialize() for tx in txs]
×
1488
        assert all(is_hex_str(rawtx) for rawtx in rawtxs)
×
1489
        assert all(tx.txid() is not None for tx in txs)
×
1490
        timeout = self.network.get_network_timeout_seconds(NetworkTimeout.Urgent)
×
1491
        for tx in txs:
×
1492
            if any(DummyAddress.is_dummy_address(txout.address) for txout in tx.outputs()):
×
1493
                raise DummyAddressUsedInTxException("tried to broadcast tx with dummy address!")
×
1494
        try:
×
1495
            res = await self.session.send_request('blockchain.transaction.broadcast_package', [rawtxs], timeout=timeout)
×
1496
        except aiorpcx.jsonrpc.CodeMessageError as e:
×
1497
            self.logger.info(f"broadcast_txpackage error [DO NOT TRUST THIS MESSAGE]: {error_text_str_to_safe_str(repr(e))}. {rawtxs=}")
×
1498
            return False
×
1499
        success = assert_dict_contains_field(res, field_name='success')
×
1500
        if not success:
×
1501
            errors = assert_dict_contains_field(res, field_name='errors')
×
1502
            self.logger.info(f"broadcast_txpackage error [DO NOT TRUST THIS MESSAGE]: {error_text_str_to_safe_str(repr(errors))}. {rawtxs=}")
×
1503
            return False
×
1504
        assert success
×
1505
        # broadcast succeeded.
1506
        # We now cache the rawtx, for *this interface only*. The tx likely touches some ismine addresses, affecting
1507
        # the status of a scripthash we are subscribed to. Caching here will save a future get_transaction RPC.
1508
        for tx, rawtx in zip(txs, rawtxs):
×
1509
            self._rawtx_cache[tx.txid()] = bytes.fromhex(rawtx)
×
1510
        return True
×
1511

1512
    async def get_history_for_scripthash(self, sh: str) -> List[dict]:
1 ✔
1513
        if not is_hash256_str(sh):
1 ✔
1514
            raise Exception(f"{repr(sh)} is not a scripthash")
×
1515
        # do request
1516
        res = await self.session.send_request('blockchain.scripthash.get_history', [sh])
1 ✔
1517
        # check response
1518
        assert_list_or_tuple(res)
1 ✔
1519
        prev_height = 1
1 ✔
1520
        for tx_item in res:
1 ✔
1521
            height = assert_dict_contains_field(tx_item, field_name='height')
1 ✔
1522
            assert_dict_contains_field(tx_item, field_name='tx_hash')
1 ✔
1523
            assert_integer(height)
1 ✔
1524
            if height < -1:
1 ✔
1525
                raise RequestCorrupted(f'{height!r} is not a valid block height')
×
1526
            assert_hash256_str(tx_item['tx_hash'])
1 ✔
1527
            if height in (-1, 0):
1 ✔
1528
                assert_dict_contains_field(tx_item, field_name='fee')
1 ✔
1529
                assert_non_negative_integer(tx_item['fee'])
1 ✔
1530
                prev_height = float("inf")  # this ensures confirmed txs can't follow mempool txs
1 ✔
1531
            else:
1532
                # check monotonicity of heights
1533
                if height < prev_height:
1 ✔
1534
                    raise RequestCorrupted(f'heights of confirmed txs must be in increasing order')
×
1535
                prev_height = height
1 ✔
1536
        if self.active_protocol_tuple >= (1, 6):
1 ✔
1537
            # enforce order of mempool txs
1538
            mempool_txs = [tx_item for tx_item in res if tx_item['height'] <= 0]
1 ✔
1539
            if mempool_txs != sorted(mempool_txs, key=lambda x: (-x['height'], bytes.fromhex(x['tx_hash']))):
1 ✔
1540
                raise RequestCorrupted(f'mempool txs not in canonical order')
×
1541
        hashes = set(map(lambda item: item['tx_hash'], res))
1 ✔
1542
        if len(hashes) != len(res):
1 ✔
1543
            # Either server is sending garbage... or maybe if server is race-prone
1544
            # a recently mined tx could be included in both last block and mempool?
1545
            # Still, it's simplest to just disregard the response.
1546
            raise RequestCorrupted(f"server history has non-unique txids for sh={sh}")
×
1547
        return res
1 ✔
1548

1549
    async def listunspent_for_scripthash(self, sh: str) -> List[dict]:
1 ✔
1550
        if not is_hash256_str(sh):
×
1551
            raise Exception(f"{repr(sh)} is not a scripthash")
×
1552
        # do request
1553
        res = await self.session.send_request('blockchain.scripthash.listunspent', [sh])
×
1554
        # check response
1555
        assert_list_or_tuple(res)
×
1556
        for utxo_item in res:
×
1557
            assert_dict_contains_field(utxo_item, field_name='tx_pos')
×
1558
            assert_dict_contains_field(utxo_item, field_name='value')
×
1559
            assert_dict_contains_field(utxo_item, field_name='tx_hash')
×
1560
            assert_dict_contains_field(utxo_item, field_name='height')
×
1561
            assert_non_negative_integer(utxo_item['tx_pos'])
×
1562
            assert_non_negative_integer(utxo_item['value'])
×
1563
            assert_non_negative_integer(utxo_item['height'])
×
1564
            assert_hash256_str(utxo_item['tx_hash'])
×
1565
        return res
×
1566

1567
    async def get_balance_for_scripthash(self, sh: str) -> dict:
1 ✔
1568
        if not is_hash256_str(sh):
×
1569
            raise Exception(f"{repr(sh)} is not a scripthash")
×
1570
        # do request
1571
        res = await self.session.send_request('blockchain.scripthash.get_balance', [sh])
×
1572
        # check response
1573
        assert_dict_contains_field(res, field_name='confirmed')
×
1574
        assert_dict_contains_field(res, field_name='unconfirmed')
×
1575
        assert_non_negative_integer(res['confirmed'])
×
1576
        assert_integer(res['unconfirmed'])
×
1577
        return res
×
1578

1579
    async def get_txid_from_txpos(self, tx_height: int, tx_pos: int, merkle: bool):
1 ✔
1580
        if not is_non_negative_integer(tx_height):
×
1581
            raise Exception(f"{repr(tx_height)} is not a block height")
×
1582
        if not is_non_negative_integer(tx_pos):
×
1583
            raise Exception(f"{repr(tx_pos)} should be non-negative integer")
×
1584
        # do request
1585
        res = await self.session.send_request(
×
1586
            'blockchain.transaction.id_from_pos',
1587
            [tx_height, tx_pos, merkle],
1588
        )
1589
        # check response
1590
        if merkle:
×
1591
            assert_dict_contains_field(res, field_name='tx_hash')
×
1592
            assert_dict_contains_field(res, field_name='merkle')
×
1593
            assert_hash256_str(res['tx_hash'])
×
1594
            assert_list_or_tuple(res['merkle'])
×
1595
            for node_hash in res['merkle']:
×
1596
                assert_hash256_str(node_hash)
×
1597
        else:
1598
            assert_hash256_str(res)
×
1599
        return res
×
1600

1601
    async def get_fee_histogram(self) -> Sequence[Tuple[Union[float, int], int]]:
1 ✔
1602
        # do request
1603
        res = await self.session.send_request('mempool.get_fee_histogram')
×
1604
        # check response
1605
        assert_list_or_tuple(res)
×
1606
        prev_fee = float('inf')
×
1607
        for fee, s in res:
×
1608
            assert_non_negative_int_or_float(fee)
×
1609
            assert_non_negative_integer(s)
×
1610
            if fee >= prev_fee:  # check monotonicity
×
1611
                raise RequestCorrupted(f'fees must be in decreasing order')
×
1612
            prev_fee = fee
×
1613
        return res
×
1614

1615
    async def get_server_banner(self) -> str:
1 ✔
1616
        # do request
1617
        res = await self.session.send_request('server.banner')
×
1618
        # check response
1619
        if not isinstance(res, str):
×
1620
            raise RequestCorrupted(f'{res!r} should be a str')
×
1621
        # Qt is slow to render some specific characters (e.g. emojis), limit the size
1622
        # and allow only printable ascii characters to prevent DoS and unicode tricks
1623
        if len(res) > MAX_SERVER_BANNER_LEN:
×
1624
            self.logger.info(f"server banner too long ({len(res)} chars), truncating")
×
1625
            res = res[:MAX_SERVER_BANNER_LEN] + f" [truncated, {MAX_SERVER_BANNER_LEN=}]"
×
1626
        res = res.replace('\r\n', '\n')  # normalize CRLF, so Windows line endings don't become '?'
×
1627
        res = re.sub(r'[^\x20-\x7e\n]', '?', res)  # keep only printable ASCII and newlines
×
1628
        return res
×
1629

1630
    async def get_donation_address(self) -> str:
1 ✔
1631
        # do request
1632
        res = await self.session.send_request('server.donation_address')
×
1633
        # check response
1634
        if not res:  # ignore empty string
×
1635
            return ''
×
1636
        if not isinstance(res, str):
×
1637
            raise RequestCorrupted(f'{res!r} should be a str')
×
1638
        address = res.removeprefix('bitcoin:')
×
1639
        if not bitcoin.is_address(address):
×
1640
            # note: do not hard-fail -- allow server to use future-type
1641
            #       bitcoin address we do not recognize
1642
            self.logger.info(f"invalid donation address from server: {repr(res)}")
×
1643
            return ''
×
1644
        return address
×
1645

1646
    async def get_relay_fee(self) -> int:
1 ✔
1647
        """Returns the min relay feerate in sat/kbyte."""
1648
        # do request
1649
        if self.active_protocol_tuple >= (1, 6):
1 ✔
1650
            res = await self.session.send_request('mempool.get_info')
1 ✔
1651
            minrelaytxfee = assert_dict_contains_field(res, field_name='minrelaytxfee')
1 ✔
1652
        else:
1653
            minrelaytxfee = await self.session.send_request('blockchain.relayfee')
×
1654
        # check response
1655
        assert_non_negative_int_or_float(minrelaytxfee)
1 ✔
1656
        relayfee = int(minrelaytxfee * bitcoin.COIN)
1 ✔
1657
        relayfee = max(0, relayfee)
1 ✔
1658
        return relayfee
1 ✔
1659

1660
    async def get_estimatefee(self, num_blocks: int) -> int:
1 ✔
1661
        """Returns a feerate estimate for getting confirmed within
1662
        num_blocks blocks, in sat/kbyte.
1663
        Returns -1 if the server could not provide an estimate.
1664
        """
1665
        if not is_non_negative_integer(num_blocks):
1 ✔
1666
            raise Exception(f"{repr(num_blocks)} is not a num_blocks")
×
1667
        # do request
1668
        try:
1 ✔
1669
            res = await self.session.send_request('blockchain.estimatefee', [num_blocks])
1 ✔
1670
        except aiorpcx.jsonrpc.ProtocolError as e:
×
1671
            # The protocol spec says the server itself should already have returned -1
1672
            # if it cannot provide an estimate, however apparently "electrs" does not conform
1673
            # and sends an error instead. Convert it here:
1674
            if "cannot estimate fee" in e.message:
×
1675
                res = -1
×
1676
            else:
1677
                raise
×
1678
        except aiorpcx.jsonrpc.RPCError as e:
×
1679
            # The protocol spec says the server itself should already have returned -1
1680
            # if it cannot provide an estimate. "Fulcrum" often sends:
1681
            #   aiorpcx.jsonrpc.RPCError: (-32603, 'internal error: bitcoind request timed out')
1682
            if e.code == JSONRPC.INTERNAL_ERROR:
×
1683
                res = -1
×
1684
            else:
1685
                raise
×
1686
        # check response
1687
        if res != -1:
1 ✔
1688
            assert_non_negative_int_or_float(res)
1 ✔
1689
            res = int(res * bitcoin.COIN)
1 ✔
1690
        return res
1 ✔
1691

1692
    async def get_server_peers(self) -> list[tuple[str, str, Sequence[str]]]:
1 ✔
1693
        # do request
1694
        peers = await self.session.send_request('server.peers.subscribe')
×
1695
        # check response
1696
        assert_list_or_tuple(peers)
×
1697
        for peer in peers:
×
1698
            assert_list_or_tuple(peer)
×
1699
            if len(peer) != 3:
×
1700
                raise RequestCorrupted(f"found peer in list with unexpected length. {peer=!r}")
×
1701
            ip_addr, hostname, features = peer
×
1702
            if not isinstance(ip_addr, str):
×
1703
                raise RequestCorrupted(f"peer ip_addr should be str, got {ip_addr!r}")
×
1704
            if not isinstance(hostname, str):
×
1705
                raise RequestCorrupted(f"peer hostname should be str, got {hostname!r}")
×
1706
            assert_list_or_tuple(features)
×
1707
            for feat in features:
×
1708
                if not isinstance(feat, str):
×
1709
                    raise RequestCorrupted(f"peer feature should be str, got {feat!r}")
×
1710
        return [tuple(peer) for peer in peers]
×
1711

1712
    def _assert_header_does_not_check_against_any_chain(self, header: dict) -> None:
1 ✔
1713
        chain_bad = self.bc_mgr.check_header(header)
1 ✔
1714
        if chain_bad:
1 ✔
1715
            raise Exception('bad_header must not check!')
×
1716

1717

1718
def sanitize_tx_broadcast_response(server_msg) -> str:
1 ✔
1719
    # Unfortunately, bitcoind and hence the Electrum protocol doesn't return a useful error code.
1720
    # So, we use substring matching to grok the error message.
1721
    # server_msg is untrusted input so it should not be shown to the user. see #4968
1722
    server_msg = str(server_msg)
1 ✔
1723
    server_msg = server_msg.replace("\n", r"\n")
1 ✔
1724

1725
    # https://github.com/bitcoin/bitcoin/blob/5bb64acd9d3ced6e6f95df282a1a0f8b98522cb0/src/script/script_error.cpp
1726
    script_error_messages = {
1 ✔
1727
        r"Script evaluated without error but finished with a false/empty top stack element",
1728
        r"Script failed an OP_VERIFY operation",
1729
        r"Script failed an OP_EQUALVERIFY operation",
1730
        r"Script failed an OP_CHECKMULTISIGVERIFY operation",
1731
        r"Script failed an OP_CHECKSIGVERIFY operation",
1732
        r"Script failed an OP_NUMEQUALVERIFY operation",
1733
        r"Script is too big",
1734
        r"Push value size limit exceeded",
1735
        r"Operation limit exceeded",
1736
        r"Stack size limit exceeded",
1737
        r"Signature count negative or greater than pubkey count",
1738
        r"Pubkey count negative or limit exceeded",
1739
        r"Opcode missing or not understood",
1740
        r"Attempted to use a disabled opcode",
1741
        r"Operation not valid with the current stack size",
1742
        r"Operation not valid with the current altstack size",
1743
        r"OP_RETURN was encountered",
1744
        r"Invalid OP_IF construction",
1745
        r"Negative locktime",
1746
        r"Locktime requirement not satisfied",
1747
        r"Signature hash type missing or not understood",
1748
        r"Non-canonical DER signature",
1749
        r"Data push larger than necessary",
1750
        r"Only push operators allowed in signatures",
1751
        r"Non-canonical signature: S value is unnecessarily high",
1752
        r"Dummy CHECKMULTISIG argument must be zero",
1753
        r"OP_IF/NOTIF argument must be minimal",
1754
        r"Signature must be zero for failed CHECK(MULTI)SIG operation",
1755
        r"NOPx reserved for soft-fork upgrades",
1756
        r"Witness version reserved for soft-fork upgrades",
1757
        r"Taproot version reserved for soft-fork upgrades",
1758
        r"OP_SUCCESSx reserved for soft-fork upgrades",
1759
        r"Public key version reserved for soft-fork upgrades",
1760
        r"Public key is neither compressed or uncompressed",
1761
        r"Stack size must be exactly one after execution",
1762
        r"Extra items left on stack after execution",
1763
        r"Witness program has incorrect length",
1764
        r"Witness program was passed an empty witness",
1765
        r"Witness program hash mismatch",
1766
        r"Witness requires empty scriptSig",
1767
        r"Witness requires only-redeemscript scriptSig",
1768
        r"Witness provided for non-witness script",
1769
        r"Using non-compressed keys in segwit",
1770
        r"Invalid Schnorr signature size",
1771
        r"Invalid Schnorr signature hash type",
1772
        r"Invalid Schnorr signature",
1773
        r"Invalid Taproot control block size",
1774
        r"Too much signature validation relative to witness weight",
1775
        r"OP_CHECKMULTISIG(VERIFY) is not available in tapscript",
1776
        r"OP_IF/NOTIF argument must be minimal in tapscript",
1777
        r"Using OP_CODESEPARATOR in non-witness script",
1778
        r"Signature is found in scriptCode",
1779
    }
1780
    for substring in script_error_messages:
1 ✔
1781
        if substring in server_msg:
1 ✔
1782
            return substring
×
1783
    # https://github.com/bitcoin/bitcoin/blob/5bb64acd9d3ced6e6f95df282a1a0f8b98522cb0/src/validation.cpp
1784
    # grep "REJECT_"
1785
    # grep "TxValidationResult"
1786
    # should come after script_error.cpp (due to e.g. "non-mandatory-script-verify-flag")
1787
    validation_error_messages = {
1 ✔
1788
        r"coinbase": None,
1789
        r"tx-size-small": None,
1790
        r"non-final": None,
1791
        r"txn-already-in-mempool": None,
1792
        r"txn-mempool-conflict": None,
1793
        r"txn-already-known": None,
1794
        r"non-BIP68-final": None,
1795
        r"bad-txns-nonstandard-inputs": None,
1796
        r"bad-witness-nonstandard": None,
1797
        r"bad-txns-too-many-sigops": None,
1798
        r"mempool min fee not met":
1799
            ("mempool min fee not met\n" +
1800
             _("Your transaction is paying a fee that is so low that the bitcoin node cannot "
1801
               "fit it into its mempool. The mempool is already full of hundreds of megabytes "
1802
               "of transactions that all pay higher fees. Try to increase the fee.")),
1803
        r"min relay fee not met": None,
1804
        r"absurdly-high-fee": None,
1805
        r"max-fee-exceeded": None,
1806
        r"too-long-mempool-chain": None,
1807
        r"bad-txns-spends-conflicting-tx": None,
1808
        r"insufficient fee": ("insufficient fee\n" +
1809
             _("Your transaction is trying to replace another one in the mempool but it "
1810
               "does not meet the rules to do so. Try to increase the fee.")),
1811
        r"too many potential replacements": None,
1812
        r"replacement-adds-unconfirmed": None,
1813
        r"mempool full": None,
1814
        r"non-mandatory-script-verify-flag": None,
1815
        r"mandatory-script-verify-flag-failed": None,
1816
        r"Transaction check failed": None,
1817
    }
1818
    for substring in validation_error_messages:
1 ✔
1819
        if substring in server_msg:
1 ✔
1820
            msg = validation_error_messages[substring]
×
1821
            return msg if msg else substring
×
1822
    # https://github.com/bitcoin/bitcoin/blob/5bb64acd9d3ced6e6f95df282a1a0f8b98522cb0/src/rpc/rawtransaction.cpp
1823
    # https://github.com/bitcoin/bitcoin/blob/5bb64acd9d3ced6e6f95df282a1a0f8b98522cb0/src/util/error.cpp
1824
    # https://github.com/bitcoin/bitcoin/blob/3f83c744ac28b700090e15b5dda2260724a56f49/src/common/messages.cpp#L126
1825
    # grep "RPC_TRANSACTION"
1826
    # grep "RPC_DESERIALIZATION_ERROR"
1827
    # grep "TransactionError"
1828
    rawtransaction_error_messages = {
1 ✔
1829
        r"Missing inputs": None,
1830
        r"Inputs missing or spent": None,
1831
        r"transaction already in block chain": None,
1832
        r"Transaction already in block chain": None,
1833
        r"Transaction outputs already in utxo set": None,
1834
        r"TX decode failed": None,
1835
        r"Peer-to-peer functionality missing or disabled": None,
1836
        r"Transaction rejected by AcceptToMemoryPool": None,
1837
        r"AcceptToMemoryPool failed": None,
1838
        r"Transaction rejected by mempool": None,
1839
        r"Mempool internal error": None,
1840
        r"Fee exceeds maximum configured by user": None,
1841
        r"Unspendable output exceeds maximum configured by user": None,
1842
        r"Transaction rejected due to invalid package": None,
1843
    }
1844
    for substring in rawtransaction_error_messages:
1 ✔
1845
        if substring in server_msg:
1 ✔
1846
            msg = rawtransaction_error_messages[substring]
×
1847
            return msg if msg else substring
×
1848
    # https://github.com/bitcoin/bitcoin/blob/5bb64acd9d3ced6e6f95df282a1a0f8b98522cb0/src/consensus/tx_verify.cpp
1849
    # https://github.com/bitcoin/bitcoin/blob/c7ad94428ab6f54661d7a5441e1fdd0ebf034903/src/consensus/tx_check.cpp
1850
    # grep "REJECT_"
1851
    # grep "TxValidationResult"
1852
    tx_verify_error_messages = {
1 ✔
1853
        r"bad-txns-vin-empty": None,
1854
        r"bad-txns-vout-empty": None,
1855
        r"bad-txns-oversize": None,
1856
        r"bad-txns-vout-negative": None,
1857
        r"bad-txns-vout-toolarge": None,
1858
        r"bad-txns-txouttotal-toolarge": None,
1859
        r"bad-txns-inputs-duplicate": None,
1860
        r"bad-cb-length": None,
1861
        r"bad-txns-prevout-null": None,
1862
        r"bad-txns-inputs-missingorspent":
1863
            ("bad-txns-inputs-missingorspent\n" +
1864
             _("You might have a local transaction in your wallet that this transaction "
1865
               "builds on top. You need to either broadcast or remove the local tx.")),
1866
        r"bad-txns-premature-spend-of-coinbase": None,
1867
        r"bad-txns-inputvalues-outofrange": None,
1868
        r"bad-txns-in-belowout": None,
1869
        r"bad-txns-fee-outofrange": None,
1870
    }
1871
    for substring in tx_verify_error_messages:
1 ✔
1872
        if substring in server_msg:
1 ✔
1873
            msg = tx_verify_error_messages[substring]
×
1874
            return msg if msg else substring
×
1875
    # https://github.com/bitcoin/bitcoin/blob/5bb64acd9d3ced6e6f95df282a1a0f8b98522cb0/src/policy/policy.cpp
1876
    # grep "reason ="
1877
    # should come after validation.cpp (due to "tx-size" vs "tx-size-small")
1878
    # should come after script_error.cpp (due to e.g. "version")
1879
    policy_error_messages = {
1 ✔
1880
        r"version": _("Transaction uses non-standard version."),
1881
        r"tx-size": _("The transaction was rejected because it is too large (in bytes)."),
1882
        r"scriptsig-size": None,
1883
        r"scriptsig-not-pushonly": None,
1884
        r"scriptpubkey":
1885
            ("scriptpubkey\n" +
1886
             _("Some of the outputs pay to a non-standard script.")),
1887
        r"bare-multisig": None,
1888
        r"dust":
1889
            (_("Transaction could not be broadcast due to dust outputs.\n"
1890
               "Some of the outputs are too small in value, probably lower than 1000 satoshis.\n"
1891
               "Check the units, make sure you haven't confused e.g. mBTC and BTC.")),
1892
        r"multi-op-return": _("The transaction was rejected because it contains multiple OP_RETURN outputs."),
1893
    }
1894
    for substring in policy_error_messages:
1 ✔
1895
        if substring in server_msg:
1 ✔
1896
            msg = policy_error_messages[substring]
×
1897
            return msg if msg else substring
×
1898
    # otherwise:
1899
    return _("Unknown error")
1 ✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc