Skip to content

Commit

Permalink
[test] Test that we only relay addrs to peers who sent us addr relate…
Browse files Browse the repository at this point in the history
…d messages

The `AddrReceiver` class used in the existing tests sends a `getaddr` message
to indicate that it is interested in participating in address relay. This
commit introduces another class, `AddrBlackhole` which does not initiate any
addr related messages. The tests check that addresses received via the network
are forwarded to the `AddrReceiver` and not the `AddrBlackhole`
  • Loading branch information
amitiuttarwar committed Mar 29, 2021
1 parent 85c4d1c commit bcb89d9
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions test/functional/p2p_addr_relay.py
Expand Up @@ -36,6 +36,15 @@ def on_addr(self, message):
self.num_ipv4_received += 1


class AddrBlackhole(P2PInterface):
""" Does not initiate any addr related messages """
num_ipv4_received = 0

def on_addr(self, message):
for addr in message.addrs:
self.num_ipv4_received += 1


class AddrTest(BitcoinTestFramework):
counter = 0

Expand All @@ -45,6 +54,7 @@ def set_test_params(self):
def run_test(self):
self.oversized_addr_test()
self.relay_tests()
self.blackhole_tests()

def setup_addr_msg(self, num, add_delay=False):
addrs = []
Expand Down Expand Up @@ -105,6 +115,25 @@ def relay_tests(self):

self.nodes[0].disconnect_p2ps()

def blackhole_tests(self):
self.log.info('Check that we only relay addresses to peers who have previously sent us addr related messages')

addr_source = self.nodes[0].add_p2p_connection(P2PInterface())
receiver_peer = self.nodes[0].add_p2p_connection(AddrReceiver())
blackhole_peer = self.nodes[0].add_p2p_connection(AddrBlackhole())

msg = self.setup_addr_msg(2, add_delay=True)
addr_source.send_and_ping(msg)
# pop m_next_addr_send timer
self.nodes[0].setmocktime(int(time.time()) + 30 * 60 * 2)
receiver_peer.sync_with_ping()
blackhole_peer.sync_with_ping()

assert_equal(receiver_peer.num_ipv4_received, 2)
assert_equal(blackhole_peer.num_ipv4_received, 0)

self.nodes[0].disconnect_p2ps()


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

0 comments on commit bcb89d9

Please sign in to comment.