Skip to content

Commit

Permalink
test: p2p: check disconnect due to lack of desirable service flags
Browse files Browse the repository at this point in the history
  • Loading branch information
theStack committed Jan 19, 2024
1 parent 5f3a057 commit 0f7b25f
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions test/functional/p2p_handshake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python3
# Copyright (c) 2024 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""
Test P2P behaviour during the handshake phase (VERSION, VERACK messages).
"""
import random

from test_framework.test_framework import BitcoinTestFramework
from test_framework.messages import (
NODE_NETWORK,
NODE_WITNESS,
)
from test_framework.p2p import P2PInterface


DESIRABLE_SERVICE_FLAGS = NODE_NETWORK | NODE_WITNESS


class P2PHandshakeTest(BitcoinTestFramework):
def set_test_params(self):
self.num_nodes = 1

def run_test(self):
node = self.nodes[0]
self.log.info("Check that lacking desired service flags leads to disconnect")
for i, conn_type in enumerate(["outbound-full-relay", "block-relay-only", "addr-fetch"]):
services = random.choice([0, NODE_NETWORK, NODE_WITNESS])
assert (services & DESIRABLE_SERVICE_FLAGS) != DESIRABLE_SERVICE_FLAGS
expected_debug_log = f'peer={i} does not offer the expected services ' \
f'({services:08x} offered, {DESIRABLE_SERVICE_FLAGS:08x} expected)'
self.log.info(f' - services 0x{services:08x}, type "{conn_type}"')
with node.assert_debug_log([expected_debug_log]):
peer = node.add_outbound_p2p_connection(P2PInterface(), wait_for_verack=False, p2p_idx=i,
connection_type=conn_type, services=services)
peer.wait_for_disconnect()

self.log.info("Check that feeler connections get disconnected immediately")
with node.assert_debug_log(["feeler connection completed"]):
peer = node.add_outbound_p2p_connection(P2PInterface(), p2p_idx=i+1, connection_type="feeler", services=0)
peer.wait_for_disconnect()


if __name__ == '__main__':
P2PHandshakeTest().main()

0 comments on commit 0f7b25f

Please sign in to comment.