interface: add warmup budget to PaddedRSTransport
What changed, and why it matters
This commit is a performance tweak, not a security fix. It changes how Electrum sends small network messages at the start of a connection so the initial handshake feels faster. There is no indication it addresses a vulnerability.
No security action needed. Review as normal code-quality/performance change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch adds a 1024-byte ‘warmup budget’ to PaddedRSTransport. During the first 1024 unpadded bytes of a session, small messages are flushed immediately instead of being buffered and padded to MIN_PACKET_SIZE (1024 bytes). It also adds timing instrumentation to the handshake log line. The change is purely UX/performance-oriented.
Changed components
electrum/interface.pyPaddedRSTransportInterface.connection_established handshakeInspect captured patch +6 / −1
diff --git a/electrum/interface.py b/electrum/interface.py
index 606c9b9..1e42fce 100644
--- a/electrum/interface.py
+++ b/electrum/interface.py
@@ -328,6 +328,9 @@ class PaddedRSTransport(RSTransport):
MIN_PACKET_SIZE = 1024
WAIT_FOR_BUFFER_GROWTH_SECONDS = 1.0
+ # (unpadded) amount of bytes sent instantly before beginning with polling.
+ # This makes the initial handshake where a few small messages are exchanged faster.
+ WARMUP_BUDGET_SIZE = 1024
session: Optional['RPCSession']
@@ -361,6 +364,7 @@ class PaddedRSTransport(RSTransport):
self._force_send
or len(buf) >= self.MIN_PACKET_SIZE
or self._last_send + self.WAIT_FOR_BUFFER_GROWTH_SECONDS < time.monotonic()
+ or self.session.send_size < self.WARMUP_BUDGET_SIZE
):
return
assert buf[-2:] in (b"}\n", b"]\n"), f"unexpected json-rpc terminator: {buf[-2:]=!r}"
@@ -950,6 +954,7 @@ class Interface(Logger):
proxy=self.proxy,
transport=PaddedRSTransport,
) as session:
+ start = time.perf_counter()
self.session = session # type: NotificationSession
self.session.set_default_timeout(self.network.get_network_timeout_seconds(NetworkTimeout.Generic))
try:
@@ -972,7 +977,7 @@ class Interface(Logger):
raise GracefulDisconnect(e)
if server_genesis_hash != constants.net.GENESIS:
raise GracefulDisconnect(f'server on different chain: {server_genesis_hash=}. ours: {constants.net.GENESIS}')
- self.logger.info(f"connection established. version: {ver}")
+ self.logger.info(f"connection established. version: {ver}, handshake duration: {(time.perf_counter() - start) * 1000:.2f} ms")
try:
async with self.taskgroup as group:
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.