Skip to content

Commit

Permalink
test: add test for interaction with -blocksonly peers
Browse files Browse the repository at this point in the history
  • Loading branch information
mzumsande committed Oct 16, 2023
1 parent 916abe1 commit a361ac4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
25 changes: 25 additions & 0 deletions test/functional/p2p_blocksonly.py
Expand Up @@ -78,6 +78,31 @@ def blocksonly_mode_tests(self):
self.nodes[0].disconnect_p2ps()
self.generate(self.nodes[0], 1)

self.log.info("Check that we sustain full-relay outbound connections to blocksonly peers when not at the full oubound limit")
blocksonly_peer = self.nodes[0].add_outbound_p2p_connection(P2PInterface(), p2p_idx=1, txrelay=False)
# Trigger CheckForStaleTipAndEvictPeers, which is scheduled every 45s (EXTRA_PEER_CHECK_INTERVAL)
self.nodes[0].mockscheduler(46)
blocksonly_peer.sync_with_ping()
blocksonly_peer.peer_disconnect()

self.log.info("Check that we evict full-relay outbound connections to blocksonly peers at the full outbound limit")
# Simulate the situation of being at the max limit of full-outbound connections by setting -maxconnections to a low value
self.restart_node(0, ["-maxconnections=2"])
# Have one blocksonly peer that is not being disconnected
blocksonly_peer1 = self.nodes[0].add_outbound_p2p_connection(P2PInterface(), p2p_idx=1, txrelay=False)
blocksonly_peer2 = self.nodes[0].add_outbound_p2p_connection(P2PInterface(), p2p_idx=2, txrelay=False)
self.nodes[0].mockscheduler(46)
blocksonly_peer2.wait_for_disconnect()
blocksonly_peer1.sync_with_ping()
blocksonly_peer1.peer_disconnect()

self.log.info("Check that we don't evict full-relay outbound connections to blocksonly peers if we are blocksonly ourselves")
self.restart_node(0, ["-blocksonly", "-maxconnections=1"])
blocksonly_peer = self.nodes[0].add_outbound_p2p_connection(P2PInterface(), p2p_idx=1, txrelay=False)
self.nodes[0].mockscheduler(46)
blocksonly_peer.sync_with_ping()
blocksonly_peer.peer_disconnect()

def blocks_relay_conn_tests(self):
self.log.info('Tests with node in normal mode with block-relay-only connections')
self.restart_node(0, ["-noblocksonly"]) # disables blocks only mode
Expand Down
9 changes: 5 additions & 4 deletions test/functional/test_framework/p2p.py
Expand Up @@ -351,12 +351,12 @@ def __init__(self, support_addrv2=False, wtxidrelay=True):
# If the peer supports wtxid-relay
self.wtxidrelay = wtxidrelay

def peer_connect_send_version(self, services):
def peer_connect_send_version(self, services, txrelay):
# Send a version msg
vt = msg_version()
vt.nVersion = P2P_VERSION
vt.strSubVer = P2P_SUBVERSION
vt.relay = P2P_VERSION_RELAY
vt.relay = P2P_VERSION_RELAY if txrelay else 0
vt.nServices = services
vt.addrTo.ip = self.dstaddr
vt.addrTo.port = self.dstport
Expand All @@ -368,13 +368,14 @@ def peer_connect(self, *args, services=P2P_SERVICES, send_version=True, **kwargs
create_conn = super().peer_connect(*args, **kwargs)

if send_version:
self.peer_connect_send_version(services)
self.peer_connect_send_version(services, txrelay=True)

return create_conn

def peer_accept_connection(self, *args, services=P2P_SERVICES, **kwargs):
txrelay = kwargs.pop('txrelay', True)
create_conn = super().peer_accept_connection(*args, **kwargs)
self.peer_connect_send_version(services)
self.peer_connect_send_version(services, txrelay)

return create_conn

Expand Down

0 comments on commit a361ac4

Please sign in to comment.