2017-02-08 03:17:58 -03:00
|
|
|
#!/usr/bin/env python3
|
2019-02-20 22:03:13 -03:00
|
|
|
# Copyright (c) 2017-2019 The Bitcoin Core developers
|
2017-02-08 03:17:58 -03:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2017-01-17 20:34:40 -03:00
|
|
|
"""Test message sending before handshake completion.
|
2017-02-08 03:17:58 -03:00
|
|
|
|
|
|
|
A node should never send anything other than VERSION/VERACK/REJECT until it's
|
|
|
|
received a VERACK.
|
|
|
|
|
2018-03-18 11:26:45 -03:00
|
|
|
This test connects to a node and sends it a few messages, trying to entice it
|
2017-11-08 15:43:28 -03:00
|
|
|
into sending us something it shouldn't."""
|
2017-01-17 20:34:40 -03:00
|
|
|
|
2018-07-06 18:10:35 -04:00
|
|
|
import time
|
|
|
|
|
|
|
|
from test_framework.messages import msg_getaddr, msg_ping, msg_verack
|
|
|
|
from test_framework.mininode import mininode_lock, P2PInterface
|
2017-01-17 20:34:40 -03:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2018-07-06 18:10:35 -04:00
|
|
|
from test_framework.util import wait_until
|
2017-02-08 03:17:58 -03:00
|
|
|
|
|
|
|
banscore = 10
|
|
|
|
|
2020-03-02 16:14:30 -03:00
|
|
|
|
2017-10-17 17:16:39 -03:00
|
|
|
class CLazyNode(P2PInterface):
|
2017-02-08 03:17:58 -03:00
|
|
|
def __init__(self):
|
2017-03-28 14:41:22 -03:00
|
|
|
super().__init__()
|
2017-02-08 03:17:58 -03:00
|
|
|
self.unexpected_msg = False
|
2017-03-29 15:07:39 -03:00
|
|
|
self.ever_connected = False
|
2017-02-08 03:17:58 -03:00
|
|
|
|
|
|
|
def bad_message(self, message):
|
|
|
|
self.unexpected_msg = True
|
2017-03-07 20:46:17 -03:00
|
|
|
self.log.info("should not have received message: %s" % message.command)
|
2017-02-08 03:17:58 -03:00
|
|
|
|
2017-11-17 17:01:24 -03:00
|
|
|
def on_open(self):
|
2017-03-29 15:07:39 -03:00
|
|
|
self.ever_connected = True
|
2017-02-08 03:17:58 -03:00
|
|
|
|
2017-11-17 17:01:24 -03:00
|
|
|
def on_version(self, message): self.bad_message(message)
|
|
|
|
def on_verack(self, message): self.bad_message(message)
|
|
|
|
def on_reject(self, message): self.bad_message(message)
|
|
|
|
def on_inv(self, message): self.bad_message(message)
|
|
|
|
def on_addr(self, message): self.bad_message(message)
|
|
|
|
def on_getdata(self, message): self.bad_message(message)
|
|
|
|
def on_getblocks(self, message): self.bad_message(message)
|
|
|
|
def on_tx(self, message): self.bad_message(message)
|
|
|
|
def on_block(self, message): self.bad_message(message)
|
|
|
|
def on_getaddr(self, message): self.bad_message(message)
|
|
|
|
def on_headers(self, message): self.bad_message(message)
|
|
|
|
def on_getheaders(self, message): self.bad_message(message)
|
|
|
|
def on_ping(self, message): self.bad_message(message)
|
|
|
|
def on_mempool(self, message): self.bad_message(message)
|
|
|
|
def on_pong(self, message): self.bad_message(message)
|
|
|
|
def on_feefilter(self, message): self.bad_message(message)
|
|
|
|
def on_sendheaders(self, message): self.bad_message(message)
|
|
|
|
def on_sendcmpct(self, message): self.bad_message(message)
|
|
|
|
def on_cmpctblock(self, message): self.bad_message(message)
|
|
|
|
def on_getblocktxn(self, message): self.bad_message(message)
|
|
|
|
def on_blocktxn(self, message): self.bad_message(message)
|
2017-02-08 03:17:58 -03:00
|
|
|
|
|
|
|
# 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
|
2017-11-17 17:01:24 -03:00
|
|
|
def on_open(self):
|
|
|
|
super().on_open()
|
2017-02-08 03:17:58 -03:00
|
|
|
for i in range(banscore):
|
|
|
|
self.send_message(msg_verack())
|
|
|
|
|
2017-11-17 17:01:24 -03:00
|
|
|
def on_reject(self, message): pass
|
2017-02-08 03:17:58 -03:00
|
|
|
|
|
|
|
# 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__()
|
|
|
|
|
2017-11-17 17:01:24 -03:00
|
|
|
def on_reject(self, message): pass
|
|
|
|
def on_verack(self, message): pass
|
2017-02-08 03:17:58 -03:00
|
|
|
# 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!
|
2017-11-17 17:01:24 -03:00
|
|
|
def on_version(self, message):
|
2017-02-08 03:17:58 -03:00
|
|
|
self.version_received = True
|
2017-11-17 17:01:24 -03:00
|
|
|
self.send_message(msg_ping())
|
|
|
|
self.send_message(msg_getaddr())
|
2017-02-08 03:17:58 -03:00
|
|
|
|
2020-03-02 16:14:30 -03:00
|
|
|
|
2017-02-08 03:17:58 -03:00
|
|
|
class P2PLeakTest(BitcoinTestFramework):
|
2017-06-09 18:21:21 -04:00
|
|
|
def set_test_params(self):
|
2017-02-08 03:17:58 -03:00
|
|
|
self.num_nodes = 1
|
2018-08-08 17:22:45 -04:00
|
|
|
self.extra_args = [['-banscore=' + str(banscore)]]
|
2017-02-08 03:17:58 -03:00
|
|
|
|
|
|
|
def run_test(self):
|
2018-08-08 17:22:45 -04:00
|
|
|
no_version_bannode = self.nodes[0].add_p2p_connection(CNodeNoVersionBan(), send_version=False, wait_for_verack=False)
|
|
|
|
no_version_idlenode = self.nodes[0].add_p2p_connection(CNodeNoVersionIdle(), send_version=False, wait_for_verack=False)
|
2020-03-02 16:14:30 -03:00
|
|
|
no_verack_idlenode = self.nodes[0].add_p2p_connection(CNodeNoVerackIdle(), wait_for_verack=False)
|
|
|
|
|
|
|
|
# Wait until we got the verack in response to the version. Though, don't wait for the other node to receive the
|
|
|
|
# verack, since we never sent one
|
|
|
|
no_verack_idlenode.wait_for_verack()
|
2017-02-08 03:17:58 -03:00
|
|
|
|
2017-08-16 13:17:34 -03: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)
|
2017-02-08 03:17:58 -03:00
|
|
|
|
|
|
|
# Mine a block and make sure that it's not sent to the connected nodes
|
2018-09-15 06:32:12 -03:00
|
|
|
self.nodes[0].generatetoaddress(1, self.nodes[0].get_deterministic_priv_key().address)
|
2017-02-08 03:17:58 -03:00
|
|
|
|
|
|
|
#Give the node enough time to possibly leak out a message
|
|
|
|
time.sleep(5)
|
|
|
|
|
|
|
|
#This node should have been banned
|
2018-06-20 21:24:29 -04:00
|
|
|
assert not no_version_bannode.is_connected
|
2017-02-08 03:17:58 -03:00
|
|
|
|
2017-11-08 18:28:17 -03:00
|
|
|
self.nodes[0].disconnect_p2ps()
|
2017-02-08 03:17:58 -03:00
|
|
|
|
2018-06-18 17:28:37 -04:00
|
|
|
# Wait until all connections are closed
|
2017-08-17 12:35:45 -03:00
|
|
|
wait_until(lambda: len(self.nodes[0].getpeerinfo()) == 0)
|
|
|
|
|
2017-02-08 03:17:58 -03:00
|
|
|
# Make sure no unexpected messages came in
|
2019-02-19 19:43:44 -03:00
|
|
|
assert no_version_bannode.unexpected_msg == False
|
|
|
|
assert no_version_idlenode.unexpected_msg == False
|
|
|
|
assert no_verack_idlenode.unexpected_msg == False
|
2017-02-08 03:17:58 -03:00
|
|
|
|
2018-06-18 17:28:37 -04:00
|
|
|
|
2017-02-08 03:17:58 -03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
P2PLeakTest().main()
|