2017-02-07 22:17:58 -08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright (c) 2017 The Bitcoin Core developers
|
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2017-01-17 15:34:40 -08:00
|
|
|
"""Test message sending before handshake completion.
|
2017-02-07 22:17:58 -08:00
|
|
|
|
|
|
|
A node should never send anything other than VERSION/VERACK/REJECT until it's
|
|
|
|
received a VERACK.
|
|
|
|
|
|
|
|
This test connects to a node and sends it a few messages, trying to intice it
|
|
|
|
into sending us something it shouldn't.
|
2017-08-07 08:37:49 -07:00
|
|
|
|
|
|
|
Also test that nodes that send unsupported service bits to bitcoind are disconnected
|
|
|
|
and don't receive a VERACK. Unsupported service bits are currently 1 << 5 and
|
|
|
|
1 << 7 (until August 1st 2018)."""
|
2017-01-17 15:34:40 -08:00
|
|
|
|
|
|
|
from test_framework.mininode import *
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
|
|
from test_framework.util import *
|
2017-02-07 22:17:58 -08:00
|
|
|
|
|
|
|
banscore = 10
|
|
|
|
|
|
|
|
class CLazyNode(NodeConnCB):
|
|
|
|
def __init__(self):
|
2017-03-28 10:41:22 -07:00
|
|
|
super().__init__()
|
2017-02-07 22:17:58 -08:00
|
|
|
self.unexpected_msg = False
|
2017-03-29 11:07:39 -07:00
|
|
|
self.ever_connected = False
|
2017-02-07 22:17:58 -08:00
|
|
|
|
|
|
|
def bad_message(self, message):
|
|
|
|
self.unexpected_msg = True
|
2017-03-07 15:46:17 -08:00
|
|
|
self.log.info("should not have received message: %s" % message.command)
|
2017-02-07 22:17:58 -08:00
|
|
|
|
|
|
|
def on_open(self, conn):
|
|
|
|
self.connected = True
|
2017-03-29 11:07:39 -07:00
|
|
|
self.ever_connected = True
|
2017-02-07 22:17:58 -08:00
|
|
|
|
|
|
|
def on_version(self, conn, message): self.bad_message(message)
|
|
|
|
def on_verack(self, conn, message): self.bad_message(message)
|
|
|
|
def on_reject(self, conn, message): self.bad_message(message)
|
|
|
|
def on_inv(self, conn, message): self.bad_message(message)
|
|
|
|
def on_addr(self, conn, message): self.bad_message(message)
|
|
|
|
def on_alert(self, conn, message): self.bad_message(message)
|
|
|
|
def on_getdata(self, conn, message): self.bad_message(message)
|
|
|
|
def on_getblocks(self, conn, message): self.bad_message(message)
|
|
|
|
def on_tx(self, conn, message): self.bad_message(message)
|
|
|
|
def on_block(self, conn, message): self.bad_message(message)
|
|
|
|
def on_getaddr(self, conn, message): self.bad_message(message)
|
|
|
|
def on_headers(self, conn, message): self.bad_message(message)
|
|
|
|
def on_getheaders(self, conn, message): self.bad_message(message)
|
|
|
|
def on_ping(self, conn, message): self.bad_message(message)
|
|
|
|
def on_mempool(self, conn): self.bad_message(message)
|
|
|
|
def on_pong(self, conn, message): self.bad_message(message)
|
|
|
|
def on_feefilter(self, conn, message): self.bad_message(message)
|
|
|
|
def on_sendheaders(self, conn, message): self.bad_message(message)
|
|
|
|
def on_sendcmpct(self, conn, message): self.bad_message(message)
|
|
|
|
def on_cmpctblock(self, conn, message): self.bad_message(message)
|
|
|
|
def on_getblocktxn(self, conn, message): self.bad_message(message)
|
|
|
|
def on_blocktxn(self, conn, message): self.bad_message(message)
|
|
|
|
|
|
|
|
# Node that never sends a version. We'll use this to send a bunch of messages
|
|
|
|
# anyway, and eventually get disconnected.
|
|
|
|
class CNodeNoVersionBan(CLazyNode):
|
|
|
|
# send a bunch of veracks without sending a message. This should get us disconnected.
|
|
|
|
# NOTE: implementation-specific check here. Remove if bitcoind ban behavior changes
|
|
|
|
def on_open(self, conn):
|
|
|
|
super().on_open(conn)
|
|
|
|
for i in range(banscore):
|
|
|
|
self.send_message(msg_verack())
|
|
|
|
|
|
|
|
def on_reject(self, conn, message): pass
|
|
|
|
|
|
|
|
# Node that never sends a version. This one just sits idle and hopes to receive
|
|
|
|
# any message (it shouldn't!)
|
|
|
|
class CNodeNoVersionIdle(CLazyNode):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
# Node that sends a version but not a verack.
|
|
|
|
class CNodeNoVerackIdle(CLazyNode):
|
|
|
|
def __init__(self):
|
|
|
|
self.version_received = False
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
def on_reject(self, conn, message): pass
|
|
|
|
def on_verack(self, conn, message): pass
|
|
|
|
# When version is received, don't reply with a verack. Instead, see if the
|
|
|
|
# node will give us a message that it shouldn't. This is not an exhaustive
|
|
|
|
# list!
|
|
|
|
def on_version(self, conn, message):
|
|
|
|
self.version_received = True
|
|
|
|
conn.send_message(msg_ping())
|
|
|
|
conn.send_message(msg_getaddr())
|
|
|
|
|
|
|
|
class P2PLeakTest(BitcoinTestFramework):
|
2017-06-09 15:21:21 -07:00
|
|
|
def set_test_params(self):
|
2017-02-07 22:17:58 -08:00
|
|
|
self.num_nodes = 1
|
2017-04-03 06:34:04 -07:00
|
|
|
self.extra_args = [['-banscore='+str(banscore)]]
|
2017-02-07 22:17:58 -08:00
|
|
|
|
|
|
|
def run_test(self):
|
2017-08-07 08:37:49 -07:00
|
|
|
self.nodes[0].setmocktime(1501545600) # August 1st 2017
|
2017-08-24 12:36:02 -07:00
|
|
|
|
|
|
|
no_version_bannode = self.nodes[0].add_p2p_connection(CNodeNoVersionBan(), send_version=False)
|
|
|
|
no_version_idlenode = self.nodes[0].add_p2p_connection(CNodeNoVersionIdle(), send_version=False)
|
|
|
|
no_verack_idlenode = self.nodes[0].add_p2p_connection(CNodeNoVerackIdle())
|
|
|
|
unsupported_service_bit5_node = self.nodes[0].add_p2p_connection(CLazyNode(), services=NODE_NETWORK|NODE_UNSUPPORTED_SERVICE_BIT_5)
|
|
|
|
unsupported_service_bit7_node = self.nodes[0].add_p2p_connection(CLazyNode(), services=NODE_NETWORK|NODE_UNSUPPORTED_SERVICE_BIT_7)
|
2017-02-07 22:17:58 -08:00
|
|
|
|
|
|
|
NetworkThread().start() # Start up network handling in another thread
|
|
|
|
|
2017-08-16 09:17:34 -07:00
|
|
|
wait_until(lambda: no_version_bannode.ever_connected, timeout=10, lock=mininode_lock)
|
|
|
|
wait_until(lambda: no_version_idlenode.ever_connected, timeout=10, lock=mininode_lock)
|
|
|
|
wait_until(lambda: no_verack_idlenode.version_received, timeout=10, lock=mininode_lock)
|
|
|
|
wait_until(lambda: unsupported_service_bit5_node.ever_connected, timeout=10, lock=mininode_lock)
|
|
|
|
wait_until(lambda: unsupported_service_bit7_node.ever_connected, timeout=10, lock=mininode_lock)
|
2017-02-07 22:17:58 -08:00
|
|
|
|
|
|
|
# Mine a block and make sure that it's not sent to the connected nodes
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
|
|
|
|
#Give the node enough time to possibly leak out a message
|
|
|
|
time.sleep(5)
|
|
|
|
|
|
|
|
#This node should have been banned
|
2017-03-29 11:07:39 -07:00
|
|
|
assert not no_version_bannode.connected
|
2017-02-07 22:17:58 -08:00
|
|
|
|
2017-08-07 08:37:49 -07:00
|
|
|
# These nodes should have been disconnected
|
|
|
|
assert not unsupported_service_bit5_node.connected
|
|
|
|
assert not unsupported_service_bit7_node.connected
|
|
|
|
|
2017-08-24 12:36:02 -07:00
|
|
|
for _ in range(5):
|
|
|
|
self.nodes[0].disconnect_p2p()
|
2017-02-07 22:17:58 -08:00
|
|
|
|
2017-08-17 08:35:45 -07:00
|
|
|
# Wait until all connections are closed
|
|
|
|
wait_until(lambda: len(self.nodes[0].getpeerinfo()) == 0)
|
|
|
|
|
2017-02-07 22:17:58 -08:00
|
|
|
# Make sure no unexpected messages came in
|
|
|
|
assert(no_version_bannode.unexpected_msg == False)
|
|
|
|
assert(no_version_idlenode.unexpected_msg == False)
|
|
|
|
assert(no_verack_idlenode.unexpected_msg == False)
|
2017-08-07 08:37:49 -07:00
|
|
|
assert not unsupported_service_bit5_node.unexpected_msg
|
|
|
|
assert not unsupported_service_bit7_node.unexpected_msg
|
|
|
|
|
|
|
|
self.log.info("Service bits 5 and 7 are allowed after August 1st 2018")
|
|
|
|
self.nodes[0].setmocktime(1533168000) # August 2nd 2018
|
|
|
|
|
2017-08-24 12:36:02 -07:00
|
|
|
allowed_service_bit5_node = self.nodes[0].add_p2p_connection(NodeConnCB(), services=NODE_NETWORK|NODE_UNSUPPORTED_SERVICE_BIT_5)
|
|
|
|
allowed_service_bit7_node = self.nodes[0].add_p2p_connection(NodeConnCB(), services=NODE_NETWORK|NODE_UNSUPPORTED_SERVICE_BIT_7)
|
2017-08-07 08:37:49 -07:00
|
|
|
|
2017-08-17 08:35:45 -07:00
|
|
|
NetworkThread().start() # Network thread stopped when all previous NodeConnCBs disconnected. Restart it
|
|
|
|
|
|
|
|
wait_until(lambda: allowed_service_bit5_node.message_count["verack"], lock=mininode_lock)
|
|
|
|
wait_until(lambda: allowed_service_bit7_node.message_count["verack"], lock=mininode_lock)
|
2017-02-07 22:17:58 -08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
P2PLeakTest().main()
|