|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2016 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 | +""" TimeoutsTest -- test various net timeouts (only in extended tests) |
| 6 | +
|
| 7 | +- Create three bitcoind nodes: |
| 8 | +
|
| 9 | + no_verack_node - we never send a verack in response to their version |
| 10 | + no_version_node - we never send a version (only a ping) |
| 11 | + no_send_node - we never send any P2P message. |
| 12 | +
|
| 13 | +- Start all three nodes |
| 14 | +- Wait 1 second |
| 15 | +- Assert that we're connected |
| 16 | +- Send a ping to no_verack_node and no_version_node |
| 17 | +- Wait 30 seconds |
| 18 | +- Assert that we're still connected |
| 19 | +- Send a ping to no_verack_node and no_version_node |
| 20 | +- Wait 31 seconds |
| 21 | +- Assert that we're no longer connected (timeout to receive version/verack is 60 seconds) |
| 22 | +""" |
| 23 | + |
| 24 | +from time import sleep |
| 25 | + |
| 26 | +from test_framework.mininode import * |
| 27 | +from test_framework.test_framework import BitcoinTestFramework |
| 28 | +from test_framework.util import * |
| 29 | + |
| 30 | +class TestNode(SingleNodeConnCB): |
| 31 | + def __init__(self): |
| 32 | + SingleNodeConnCB.__init__(self) |
| 33 | + self.connected = False |
| 34 | + self.received_version = False |
| 35 | + |
| 36 | + def on_open(self, conn): |
| 37 | + self.connected = True |
| 38 | + |
| 39 | + def on_close(self, conn): |
| 40 | + self.connected = False |
| 41 | + |
| 42 | + def on_version(self, conn, message): |
| 43 | + # Don't send a verack in response |
| 44 | + self.received_version = True |
| 45 | + |
| 46 | +class TimeoutsTest(BitcoinTestFramework): |
| 47 | + def __init__(self): |
| 48 | + super().__init__() |
| 49 | + self.setup_clean_chain = True |
| 50 | + self.num_nodes = 1 |
| 51 | + |
| 52 | + def setup_network(self): |
| 53 | + self.nodes = [] |
| 54 | + |
| 55 | + # Start up node0 to be a version 1, pre-segwit node. |
| 56 | + self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, |
| 57 | + [["-debug", "-logtimemicros=1"]]) |
| 58 | + |
| 59 | + def run_test(self): |
| 60 | + # Setup the p2p connections and start up the network thread. |
| 61 | + self.no_verack_node = TestNode() # never send verack |
| 62 | + self.no_version_node = TestNode() # never send version (just ping) |
| 63 | + self.no_send_node = TestNode() # never send anything |
| 64 | + |
| 65 | + connections = [] |
| 66 | + connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], self.no_verack_node)) |
| 67 | + connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], self.no_version_node, send_version=False)) |
| 68 | + connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], self.no_send_node, send_version=False)) |
| 69 | + self.no_verack_node.add_connection(connections[0]) |
| 70 | + self.no_version_node.add_connection(connections[1]) |
| 71 | + self.no_send_node.add_connection(connections[2]) |
| 72 | + |
| 73 | + NetworkThread().start() # Start up network handling in another thread |
| 74 | + |
| 75 | + sleep(1) |
| 76 | + |
| 77 | + assert(self.no_verack_node.connected) |
| 78 | + assert(self.no_version_node.connected) |
| 79 | + assert(self.no_send_node.connected) |
| 80 | + |
| 81 | + ping_msg = msg_ping() |
| 82 | + connections[0].send_message(ping_msg) |
| 83 | + connections[1].send_message(ping_msg) |
| 84 | + |
| 85 | + sleep(30) |
| 86 | + |
| 87 | + assert(self.no_verack_node.received_version) |
| 88 | + |
| 89 | + assert(self.no_verack_node.connected) |
| 90 | + assert(self.no_version_node.connected) |
| 91 | + assert(self.no_send_node.connected) |
| 92 | + |
| 93 | + connections[0].send_message(ping_msg) |
| 94 | + connections[1].send_message(ping_msg) |
| 95 | + |
| 96 | + sleep(31) |
| 97 | + |
| 98 | + assert(not self.no_verack_node.connected) |
| 99 | + assert(not self.no_version_node.connected) |
| 100 | + assert(not self.no_send_node.connected) |
| 101 | + |
| 102 | +if __name__ == '__main__': |
| 103 | + TimeoutsTest().main() |
0 commit comments