In fb9c393564ef098267ad2a921b75e0716f38b047 "test: address self-announcement"
I noticed some duplication in the test that could be removed with the following diff. I don't feel too strongly about this suggestion and if you think such a de-duplicated code might make it more difficult to update the test in the future, then feel free to not implement.
diff --git a/test/functional/p2p_addr_selfannouncement.py b/test/functional/p2p_addr_selfannouncement.py
index 65527d1e77..47de6156f8 100755
--- a/test/functional/p2p_addr_selfannouncement.py
+++ b/test/functional/p2p_addr_selfannouncement.py
@@ -69,62 +69,28 @@ class AddrSelfAnnouncementTest(BitcoinTestFramework):
a = f"{first_octet}.{second_octet}.1.1"
self.nodes[0].addpeeraddress(a, 8333)
- self.self_announcement_inbound_test(addrv2=False)
- self.self_announcement_inbound_test(addrv2=True)
- self.self_announcement_outbound_test(addrv2=False)
- self.self_announcement_outbound_test(addrv2=True)
-
- def self_announcement_inbound_test(self, addrv2=False):
- addr_version = "addrv2" if addrv2 else "addrv1"
- self.log.info(f"Test that the node does an address self-announcement to inbound connections ({addr_version})")
-
- # We only self-announce after initial block download is done
- assert (not self.nodes[0].getblockchaininfo()["initialblockdownload"])
-
- netinfo = self.nodes[0].getnetworkinfo()
- port = netinfo["localaddresses"][0]["port"]
- self.nodes[0].setmocktime(int(time.time()))
-
- expected = CAddress()
- expected.nServices = int(netinfo["localservices"], 16)
- expected.ip = IP_TO_ANNOUNCE
- expected.port = port
- expected.time = self.nodes[0].mocktime
-
- self.log.info(f"Check that we get an initial self-announcement when connecting to a node and sending a GETADDR (inbound, {addr_version})")
- with self.nodes[0].assert_debug_log([f'Advertising address {IP_TO_ANNOUNCE}:{port}']):
- addr_receiver = self.nodes[0].add_p2p_connection(SelfAnnouncementReceiver(expected=expected, addrv2=addrv2))
- addr_receiver.sync_with_ping()
+ self.self_announcement_test(outbound=False, addrv2=False, assert_on_connection_open=self.inbound_connection_open_assertions)
+ self.self_announcement_test(outbound=False, addrv2=True, assert_on_connection_open=self.inbound_connection_open_assertions)
+ self.self_announcement_test(outbound=True, addrv2=False, assert_on_connection_open=self.outbound_connection_open_assertions)
+ self.self_announcement_test(outbound=True, addrv2=True, assert_on_connection_open=self.outbound_connection_open_assertions)
+ def inbound_connection_open_assertions(self, addr_receiver):
# We expect one self-announcement and multiple other addresses in
# response to a GETADDR in a single addr / addrv2 message.
assert_equal(addr_receiver.self_announcements_received, 1)
assert_equal(addr_receiver.addr_messages_received, 1)
assert_greater_than(addr_receiver.addresses_received, 1)
- self.log.info(f"Check that we get more self-announcements sometime later (inbound, {addr_version})")
- for _ in range(5):
- last_self_announcements_received = addr_receiver.self_announcements_received
- last_addr_messages_received = addr_receiver.addr_messages_received
- last_addresses_received = addr_receiver.addresses_received
- with self.nodes[0].assert_debug_log([f'Advertising address {IP_TO_ANNOUNCE}:{port}']):
- # m_next_local_addr_send and AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL:
- # self-announcements are sent on an exponential distribution with mean interval of 24h.
- # Setting the mocktime 20d forward gives a probability of (1 - e^-(480/24)) that
- # the event will occur (i.e. this fails once in ~500 million repeats).
- self.nodes[0].bumpmocktime(20 * ONE_DAY)
- addr_receiver.expected.time = self.nodes[0].mocktime
- addr_receiver.sync_with_ping()
-
- assert_equal(addr_receiver.self_announcements_received, last_self_announcements_received + 1)
- assert_equal(addr_receiver.addr_messages_received, last_addr_messages_received + 1)
- assert_equal(addr_receiver.addresses_received, last_addresses_received + 1)
-
- self.nodes[0].disconnect_p2ps()
+ def outbound_connection_open_assertions(self, addr_receiver):
+ # We expect only the self-announcement.
+ assert_equal(addr_receiver.self_announcements_received, 1)
+ assert_equal(addr_receiver.addr_messages_received, 1)
+ assert_equal(addr_receiver.addresses_received, 1)
- def self_announcement_outbound_test(self, addrv2=True):
+ def self_announcement_test(self, outbound, addrv2, assert_on_connection_open):
+ connection_type = "outbound" if outbound else "inbound"
addr_version = "addrv2" if addrv2 else "addrv1"
- self.log.info(f"Test that the node does an address self-announcement to outbound connections ({addr_version})")
+ self.log.info(f"Test that the node does an address self-announcement to {connection_type} connections ({addr_version})")
# We only self-announce after initial block download is done
assert (not self.nodes[0].getblockchaininfo()["initialblockdownload"])
@@ -139,21 +105,19 @@ class AddrSelfAnnouncementTest(BitcoinTestFramework):
expected.port = port
expected.time = self.nodes[0].mocktime
- self.log.info(f"Check that we get an initial self-announcement on a outbound connection from the node (outbound, {addr_version})")
+ self.log.info(f"Check that we get an initial self-announcement when connecting to a node and sending a GETADDR ({connection_type}, {addr_version})")
with self.nodes[0].assert_debug_log([f'Advertising address {IP_TO_ANNOUNCE}:{port}']):
- addr_receiver = self.nodes[0].add_outbound_p2p_connection(SelfAnnouncementReceiver(expected=expected, addrv2=addrv2), p2p_idx=0, connection_type="outbound-full-relay")
+ addr_receiver = self.nodes[0].add_outbound_p2p_connection(SelfAnnouncementReceiver(expected=expected, addrv2=addrv2), p2p_idx=0, connection_type="outbound-full-relay") if outbound else self.nodes[0].add_p2p_connection(SelfAnnouncementReceiver(expected=expected, addrv2=addrv2))
addr_receiver.sync_with_ping()
- # We expect only the self-announcement.
- assert_equal(addr_receiver.self_announcements_received, 1)
- assert_equal(addr_receiver.addr_messages_received, 1)
- assert_equal(addr_receiver.addresses_received, 1)
+ assert_on_connection_open(addr_receiver)
- # to avoid the node evicting the outbound peer, protect it by announcing the most recent header to it
- tip_header = from_hex(CBlockHeader(), self.nodes[0].getblockheader(self.nodes[0].getbestblockhash(), False))
- addr_receiver.send_and_ping(msg_headers([tip_header]))
+ if outbound:
+ # to avoid the node evicting the outbound peer, protect it by announcing the most recent header to it
+ tip_header = from_hex(CBlockHeader(), self.nodes[0].getblockheader(self.nodes[0].getbestblockhash(), False))
+ addr_receiver.send_and_ping(msg_headers([tip_header]))
- self.log.info(f"Check that we get more self-announcements sometime later (outbound, {addr_version})")
+ self.log.info(f"Check that we get more self-announcements sometime later ({connection_type}, {addr_version})")
for _ in range(5):
last_self_announcements_received = addr_receiver.self_announcements_received
last_addr_messages_received = addr_receiver.addr_messages_received
@@ -173,6 +137,5 @@ class AddrSelfAnnouncementTest(BitcoinTestFramework):
self.nodes[0].disconnect_p2ps()
-
if __name__ == '__main__':
AddrSelfAnnouncementTest(__file__).main()