|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2020 The Bitcoin Core developers |
| 3 | +# Distributed under the MIT software license, see the accompanying |
| 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | +""" |
| 6 | +Test addr relay |
| 7 | +""" |
| 8 | + |
| 9 | +from test_framework.messages import ( |
| 10 | + CAddress, |
| 11 | + NODE_NETWORK, |
| 12 | + NODE_WITNESS, |
| 13 | + msg_addr, |
| 14 | +) |
| 15 | +from test_framework.mininode import ( |
| 16 | + P2PInterface, |
| 17 | +) |
| 18 | +from test_framework.test_framework import BitcoinTestFramework |
| 19 | +from test_framework.util import ( |
| 20 | + assert_equal, |
| 21 | +) |
| 22 | +import time |
| 23 | + |
| 24 | +ADDRS = [] |
| 25 | +for i in range(10): |
| 26 | + addr = CAddress() |
| 27 | + addr.time = int(time.time()) + i |
| 28 | + addr.nServices = NODE_NETWORK | NODE_WITNESS |
| 29 | + addr.ip = "123.123.123.{}".format(i % 256) |
| 30 | + addr.port = 8333 + i |
| 31 | + ADDRS.append(addr) |
| 32 | + |
| 33 | + |
| 34 | +class AddrReceiver(P2PInterface): |
| 35 | + def on_addr(self, message): |
| 36 | + for addr in message.addrs: |
| 37 | + assert_equal(addr.nServices, 9) |
| 38 | + assert addr.ip.startswith('123.123.123.') |
| 39 | + assert (8333 <= addr.port < 8343) |
| 40 | + |
| 41 | + |
| 42 | +class AddrTest(BitcoinTestFramework): |
| 43 | + def set_test_params(self): |
| 44 | + self.setup_clean_chain = False |
| 45 | + self.num_nodes = 1 |
| 46 | + |
| 47 | + def run_test(self): |
| 48 | + self.log.info('Create connection that sends addr messages') |
| 49 | + addr_source = self.nodes[0].add_p2p_connection(P2PInterface()) |
| 50 | + msg = msg_addr() |
| 51 | + |
| 52 | + self.log.info('Send too large addr message') |
| 53 | + msg.addrs = ADDRS * 101 |
| 54 | + with self.nodes[0].assert_debug_log(['message addr size() = 1010']): |
| 55 | + addr_source.send_and_ping(msg) |
| 56 | + |
| 57 | + self.log.info('Check that addr message content is relayed and added to addrman') |
| 58 | + addr_receiver = self.nodes[0].add_p2p_connection(AddrReceiver()) |
| 59 | + msg.addrs = ADDRS |
| 60 | + with self.nodes[0].assert_debug_log([ |
| 61 | + 'Added 10 addresses from 127.0.0.1: 0 tried', |
| 62 | + 'received: addr (301 bytes) peer=0', |
| 63 | + 'sending addr (301 bytes) peer=1', |
| 64 | + ]): |
| 65 | + addr_source.send_and_ping(msg) |
| 66 | + self.nodes[0].setmocktime(int(time.time()) + 30 * 60) |
| 67 | + addr_receiver.sync_with_ping() |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == '__main__': |
| 71 | + AddrTest().main() |
0 commit comments