Skip to content

Instantly share code, notes, and snippets.

@l0rinc
Created July 29, 2026 06:16
Show Gist options
  • Select an option

  • Save l0rinc/39e471dd6835fe332205d3812ebb9e92 to your computer and use it in GitHub Desktop.

Select an option

Save l0rinc/39e471dd6835fe332205d3812ebb9e92 to your computer and use it in GitHub Desktop.
Send an empty headers response and wait for a replacement request
#!/usr/bin/env python3
"""Send an empty headers response and wait for a replacement request."""
import sys
sys.path.insert(0, "test/functional")
from test_framework.messages import msg_headers
from test_framework.p2p import NetworkThread, P2PInterface
from test_framework.util import wait_until_helper_internal
P2P_PORT = 18455
network_thread = NetworkThread()
network_thread.start()
wait_until_helper_internal(lambda: network_thread.network_event_loop is not None and network_thread.network_event_loop.is_running())
def connect():
peer = P2PInterface()
peer.peer_connect(dstaddr="127.0.0.1", dstport=P2P_PORT, net="regtest", timeout_factor=1, supports_v2_p2p=False, send_version=True)()
peer.wait_for_connect()
peer.wait_for_verack()
# Ensure the node has processed the handshake before sending headers.
peer.sync_with_ping()
return peer
peers = []
try:
initial = connect()
peers.append(initial)
initial.wait_for_getheaders(timeout=10)
# The empty response clears the peer's local headers state.
initial.send_and_ping(msg_headers())
replacement = connect()
peers.append(replacement)
# This wait returns on the fix and times out when the slot remains stuck.
replacement.wait_for_getheaders(timeout=10)
finally:
for peer in peers:
peer.peer_disconnect()
network_thread.close(timeout=10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment