2 # Copyright (c) 2017 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 message sending before handshake completion.
7 A node should never send anything other than VERSION/VERACK/REJECT until it's
10 This test connects to a node and sends it a few messages, trying to intice it
11 into sending us something it shouldn't.
13 Also test that nodes that send unsupported service bits to bitcoind are disconnected
14 and don't receive a VERACK. Unsupported service bits are currently 1 << 5 and
15 1 << 7 (until August 1st 2018)."""
17 from test_framework
.mininode
import *
18 from test_framework
.test_framework
import BitcoinTestFramework
19 from test_framework
.util
import *
23 class CLazyNode(P2PInterface
):
26 self
.unexpected_msg
= False
27 self
.ever_connected
= False
29 def bad_message(self
, message
):
30 self
.unexpected_msg
= True
31 self
.log
.info("should not have received message: %s" % message
.command
)
34 self
.ever_connected
= True
36 def on_version(self
, message
): self
.bad_message(message
)
37 def on_verack(self
, message
): self
.bad_message(message
)
38 def on_reject(self
, message
): self
.bad_message(message
)
39 def on_inv(self
, message
): self
.bad_message(message
)
40 def on_addr(self
, message
): self
.bad_message(message
)
41 def on_getdata(self
, message
): self
.bad_message(message
)
42 def on_getblocks(self
, message
): self
.bad_message(message
)
43 def on_tx(self
, message
): self
.bad_message(message
)
44 def on_block(self
, message
): self
.bad_message(message
)
45 def on_getaddr(self
, message
): self
.bad_message(message
)
46 def on_headers(self
, message
): self
.bad_message(message
)
47 def on_getheaders(self
, message
): self
.bad_message(message
)
48 def on_ping(self
, message
): self
.bad_message(message
)
49 def on_mempool(self
, message
): self
.bad_message(message
)
50 def on_pong(self
, message
): self
.bad_message(message
)
51 def on_feefilter(self
, message
): self
.bad_message(message
)
52 def on_sendheaders(self
, message
): self
.bad_message(message
)
53 def on_sendcmpct(self
, message
): self
.bad_message(message
)
54 def on_cmpctblock(self
, message
): self
.bad_message(message
)
55 def on_getblocktxn(self
, message
): self
.bad_message(message
)
56 def on_blocktxn(self
, message
): self
.bad_message(message
)
58 # Node that never sends a version. We'll use this to send a bunch of messages
59 # anyway, and eventually get disconnected.
60 class CNodeNoVersionBan(CLazyNode
):
61 # send a bunch of veracks without sending a message. This should get us disconnected.
62 # NOTE: implementation-specific check here. Remove if bitcoind ban behavior changes
65 for i
in range(banscore
):
66 self
.send_message(msg_verack())
68 def on_reject(self
, message
): pass
70 # Node that never sends a version. This one just sits idle and hopes to receive
71 # any message (it shouldn't!)
72 class CNodeNoVersionIdle(CLazyNode
):
76 # Node that sends a version but not a verack.
77 class CNodeNoVerackIdle(CLazyNode
):
79 self
.version_received
= False
82 def on_reject(self
, message
): pass
83 def on_verack(self
, message
): pass
84 # When version is received, don't reply with a verack. Instead, see if the
85 # node will give us a message that it shouldn't. This is not an exhaustive
87 def on_version(self
, message
):
88 self
.version_received
= True
89 self
.send_message(msg_ping())
90 self
.send_message(msg_getaddr())
92 class P2PLeakTest(BitcoinTestFramework
):
93 def set_test_params(self
):
95 self
.extra_args
= [['-banscore='+str(banscore
)]]
98 self
.nodes
[0].setmocktime(1501545600) # August 1st 2017
100 no_version_bannode
= self
.nodes
[0].add_p2p_connection(CNodeNoVersionBan(), send_version
=False)
101 no_version_idlenode
= self
.nodes
[0].add_p2p_connection(CNodeNoVersionIdle(), send_version
=False)
102 no_verack_idlenode
= self
.nodes
[0].add_p2p_connection(CNodeNoVerackIdle())
103 unsupported_service_bit5_node
= self
.nodes
[0].add_p2p_connection(CLazyNode(), services
=NODE_NETWORK|NODE_UNSUPPORTED_SERVICE_BIT_5
)
104 unsupported_service_bit7_node
= self
.nodes
[0].add_p2p_connection(CLazyNode(), services
=NODE_NETWORK|NODE_UNSUPPORTED_SERVICE_BIT_7
)
106 network_thread_start()
108 wait_until(lambda: no_version_bannode
.ever_connected
, timeout
=10, lock
=mininode_lock
)
109 wait_until(lambda: no_version_idlenode
.ever_connected
, timeout
=10, lock
=mininode_lock
)
110 wait_until(lambda: no_verack_idlenode
.version_received
, timeout
=10, lock
=mininode_lock
)
111 wait_until(lambda: unsupported_service_bit5_node
.ever_connected
, timeout
=10, lock
=mininode_lock
)
112 wait_until(lambda: unsupported_service_bit7_node
.ever_connected
, timeout
=10, lock
=mininode_lock
)
114 # Mine a block and make sure that it's not sent to the connected nodes
115 self
.nodes
[0].generate(1)
117 #Give the node enough time to possibly leak out a message
120 #This node should have been banned
121 assert no_version_bannode
.state
!= "connected"
123 # These nodes should have been disconnected
124 assert unsupported_service_bit5_node
.state
!= "connected"
125 assert unsupported_service_bit7_node
.state
!= "connected"
127 self
.nodes
[0].disconnect_p2ps()
129 # Wait until all connections are closed and the network thread has terminated
130 wait_until(lambda: len(self
.nodes
[0].getpeerinfo()) == 0)
131 network_thread_join()
133 # Make sure no unexpected messages came in
134 assert(no_version_bannode
.unexpected_msg
== False)
135 assert(no_version_idlenode
.unexpected_msg
== False)
136 assert(no_verack_idlenode
.unexpected_msg
== False)
137 assert not unsupported_service_bit5_node
.unexpected_msg
138 assert not unsupported_service_bit7_node
.unexpected_msg
140 self
.log
.info("Service bits 5 and 7 are allowed after August 1st 2018")
141 self
.nodes
[0].setmocktime(1533168000) # August 2nd 2018
143 allowed_service_bit5_node
= self
.nodes
[0].add_p2p_connection(P2PInterface(), services
=NODE_NETWORK|NODE_UNSUPPORTED_SERVICE_BIT_5
)
144 allowed_service_bit7_node
= self
.nodes
[0].add_p2p_connection(P2PInterface(), services
=NODE_NETWORK|NODE_UNSUPPORTED_SERVICE_BIT_7
)
146 # Network thread stopped when all previous P2PInterfaces disconnected. Restart it
147 network_thread_start()
149 wait_until(lambda: allowed_service_bit5_node
.message_count
["verack"], lock
=mininode_lock
)
150 wait_until(lambda: allowed_service_bit7_node
.message_count
["verack"], lock
=mininode_lock
)
152 if __name__
== '__main__':