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 """Test various net timeouts.
7 - Create three bitcoind nodes:
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.
13 - Start all three nodes
15 - Assert that we're connected
16 - Send a ping to no_verack_node and no_version_node
18 - Assert that we're still connected
19 - Send a ping to no_verack_node and no_version_node
21 - Assert that we're no longer connected (timeout to receive version/verack is 60 seconds)
24 from time
import sleep
26 from test_framework
.mininode
import *
27 from test_framework
.test_framework
import BitcoinTestFramework
28 from test_framework
.util
import *
30 class TestNode(NodeConnCB
):
31 def on_version(self
, conn
, message
):
32 # Don't send a verack in response
35 class TimeoutsTest(BitcoinTestFramework
):
36 def set_test_params(self
):
37 self
.setup_clean_chain
= True
41 # Setup the p2p connections and start up the network thread.
42 self
.no_verack_node
= TestNode() # never send verack
43 self
.no_version_node
= TestNode() # never send version (just ping)
44 self
.no_send_node
= TestNode() # never send anything
47 connections
.append(NodeConn('127.0.0.1', p2p_port(0), self
.nodes
[0], self
.no_verack_node
))
48 connections
.append(NodeConn('127.0.0.1', p2p_port(0), self
.nodes
[0], self
.no_version_node
, send_version
=False))
49 connections
.append(NodeConn('127.0.0.1', p2p_port(0), self
.nodes
[0], self
.no_send_node
, send_version
=False))
50 self
.no_verack_node
.add_connection(connections
[0])
51 self
.no_version_node
.add_connection(connections
[1])
52 self
.no_send_node
.add_connection(connections
[2])
54 NetworkThread().start() # Start up network handling in another thread
58 assert(self
.no_verack_node
.connected
)
59 assert(self
.no_version_node
.connected
)
60 assert(self
.no_send_node
.connected
)
63 connections
[0].send_message(ping_msg
)
64 connections
[1].send_message(ping_msg
)
68 assert "version" in self
.no_verack_node
.last_message
70 assert(self
.no_verack_node
.connected
)
71 assert(self
.no_version_node
.connected
)
72 assert(self
.no_send_node
.connected
)
74 connections
[0].send_message(ping_msg
)
75 connections
[1].send_message(ping_msg
)
79 assert(not self
.no_verack_node
.connected
)
80 assert(not self
.no_version_node
.connected
)
81 assert(not self
.no_send_node
.connected
)
83 if __name__
== '__main__':