Skip to content

Commit

Permalink
[test] Add tests for getaddr behavior
Browse files Browse the repository at this point in the history
Co-authored-by: Amiti Uttarwar <amiti@uttarwar.org>
  • Loading branch information
mzumsande and amitiuttarwar committed Apr 21, 2021
1 parent d2dbfe6 commit 8188b77
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions test/functional/p2p_addr_relay.py
Expand Up @@ -11,6 +11,7 @@
NODE_NETWORK,
NODE_WITNESS,
msg_addr,
msg_getaddr
)
from test_framework.p2p import P2PInterface
from test_framework.test_framework import BitcoinTestFramework
Expand All @@ -32,6 +33,21 @@ def on_addr(self, message):
self.num_ipv4_received += 1


class GetAddrStore(P2PInterface):
getaddr_received = False
num_ipv4_received = 0

def on_getaddr(self, message):
self.getaddr_received = True

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

def addr_received(self):
return self.num_ipv4_received != 0


class AddrTest(BitcoinTestFramework):
counter = 0
mocktime = int(time.time())
Expand All @@ -42,6 +58,7 @@ def set_test_params(self):
def run_test(self):
self.oversized_addr_test()
self.relay_tests()
self.getaddr_tests()

def setup_addr_msg(self, num):
addrs = []
Expand Down Expand Up @@ -105,6 +122,42 @@ def relay_tests(self):

self.nodes[0].disconnect_p2ps()

def getaddr_tests(self):
self.log.info('Test getaddr behavior')
self.log.info('Check that we send a getaddr message upon connecting to an outbound-full-relay peer')
full_outbound_peer = self.nodes[0].add_outbound_p2p_connection(GetAddrStore(), p2p_idx=0, connection_type="outbound-full-relay")
full_outbound_peer.sync_with_ping()
assert full_outbound_peer.getaddr_received

self.log.info('Check that we do not send a getaddr message upon connecting to a block-relay-only peer')
block_relay_peer = self.nodes[0].add_outbound_p2p_connection(GetAddrStore(), p2p_idx=1, connection_type="block-relay-only")
block_relay_peer.sync_with_ping()
assert_equal(block_relay_peer.getaddr_received, False)

self.log.info('Check that we answer getaddr messages only from inbound peers')
inbound_peer = self.nodes[0].add_p2p_connection(GetAddrStore())
inbound_peer.sync_with_ping()
# Add some addresses to addrman
for i in range(1000):
first_octet = i >> 8
second_octet = i % 256
a = f"{first_octet}.{second_octet}.1.1"
self.nodes[0].addpeeraddress(a, 8333)

full_outbound_peer.send_and_ping(msg_getaddr())
block_relay_peer.send_and_ping(msg_getaddr())
inbound_peer.send_and_ping(msg_getaddr())

self.mocktime += 5 * 60
self.nodes[0].setmocktime(self.mocktime)
inbound_peer.wait_until(inbound_peer.addr_received)

assert_equal(full_outbound_peer.num_ipv4_received, 0)
assert_equal(block_relay_peer.num_ipv4_received, 0)
assert inbound_peer.num_ipv4_received > 100

self.nodes[0].disconnect_p2ps()


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

0 comments on commit 8188b77

Please sign in to comment.