No need for this specialized subclass. The base P2PInterface counts messages received by type, so you can just use that:
diff --git a/test/functional/p2p_addrfetch.py b/test/functional/p2p_addrfetch.py
index b50a710adb..e50e9e7ee4 100755
--- a/test/functional/p2p_addrfetch.py
+++ b/test/functional/p2p_addrfetch.py
@@ -9,7 +9,7 @@ Test p2p addr-fetch connections
import time
from test_framework.messages import msg_addr, CAddress, NODE_NETWORK, NODE_WITNESS
-from test_framework.p2p import P2PInterface
+from test_framework.p2p import P2PInterface, p2p_lock
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal
@@ -19,17 +19,6 @@ ADDR.nServices = NODE_NETWORK | NODE_WITNESS
ADDR.ip = "192.0.0.8"
ADDR.port = 18444
-
-class AddrFetchPeer(P2PInterface):
- def __init__(self):
- super().__init__()
- self.received_getheaders = False
- self.received_getaddr = False
-
- def on_getheaders(self, message): self.received_getheaders = True
- def on_getaddr(self, message): self.received_getaddr = True
-
-
class P2PAddrFetch(BitcoinTestFramework):
def set_test_params(self):
@@ -39,15 +28,16 @@ class P2PAddrFetch(BitcoinTestFramework):
def run_test(self):
node = self.nodes[0]
self.log.info("Connect to an addr-fetch peer")
- peer = node.add_outbound_p2p_connection(AddrFetchPeer(), p2p_idx=0, connection_type="addr-fetch")
+ peer = node.add_outbound_p2p_connection(P2PInterface(), p2p_idx=0, connection_type="addr-fetch")
info = node.getpeerinfo()
assert_equal(len(info), 1)
assert_equal(info[0]['connection_type'], 'addr-fetch')
self.log.info("Check that we send getaddr but don't try to sync headers with the addr-fetch peer")
peer.sync_send_with_ping()
- assert peer.received_getaddr
- assert not peer.received_getheaders
+ with p2p_lock:
+ assert peer.message_count['getaddr'] >= 1
+ assert peer.message_count['getheaders'] == 0
self.log.info("Check that answering the getaddr with a single address does not lead to disconnect")
# This prevents disconnecting on self-announcements
@@ -62,7 +52,7 @@ class P2PAddrFetch(BitcoinTestFramework):
peer.wait_for_disconnect(timeout=5)
self.log.info("Check timeout for addr-fetch peer that does not send addrs")
- peer = node.add_outbound_p2p_connection(AddrFetchPeer(), p2p_idx=1, connection_type="addr-fetch")
+ peer = node.add_outbound_p2p_connection(P2PInterface(), p2p_idx=1, connection_type="addr-fetch")
node.setmocktime(int(time.time()) + 121) # Timeout: 2 minutes
peer.wait_for_disconnect(timeout=5)
(this also fixes a potential race where data in the P2PInterface was being read without locking the p2p_lock)