2017-02-08 07:17:58 +01:00
|
|
|
#!/usr/bin/env python3
|
2020-04-16 19:14:08 +02:00
|
|
|
# Copyright (c) 2017-2020 The Bitcoin Core developers
|
2017-02-08 07:17:58 +01:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2017-01-18 00:34:40 +01:00
|
|
|
"""Test message sending before handshake completion.
|
2017-02-08 07:17:58 +01:00
|
|
|
|
2021-03-03 00:16:16 +01:00
|
|
|
Before receiving a VERACK, a node should not send anything but VERSION/VERACK
|
|
|
|
and feature negotiation messages (WTXIDRELAY, SENDADDRV2).
|
2017-02-08 07:17:58 +01:00
|
|
|
|
2018-03-18 15:26:45 +01:00
|
|
|
This test connects to a node and sends it a few messages, trying to entice it
|
2017-11-08 19:43:28 +01:00
|
|
|
into sending us something it shouldn't."""
|
2017-01-18 00:34:40 +01:00
|
|
|
|
2018-07-07 00:10:35 +02:00
|
|
|
import time
|
|
|
|
|
2020-04-14 03:39:19 +02:00
|
|
|
from test_framework.messages import (
|
|
|
|
msg_getaddr,
|
|
|
|
msg_ping,
|
|
|
|
msg_version,
|
|
|
|
)
|
2020-11-28 12:41:25 +01:00
|
|
|
from test_framework.p2p import (
|
|
|
|
P2PInterface,
|
|
|
|
P2P_SUBVERSION,
|
2020-11-28 12:41:25 +01:00
|
|
|
P2P_SERVICES,
|
2020-11-28 12:41:25 +01:00
|
|
|
P2P_VERSION_RELAY,
|
2020-11-28 12:41:25 +01:00
|
|
|
)
|
2017-01-18 00:34:40 +01:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2020-04-10 16:54:30 +02:00
|
|
|
from test_framework.util import (
|
|
|
|
assert_equal,
|
|
|
|
assert_greater_than_or_equal,
|
|
|
|
)
|
2017-02-08 07:17:58 +01:00
|
|
|
|
2021-06-04 21:37:24 +02:00
|
|
|
PEER_TIMEOUT = 3
|
|
|
|
|
2020-03-02 20:14:30 +01:00
|
|
|
|
2020-08-17 02:08:44 +02:00
|
|
|
class LazyPeer(P2PInterface):
|
2017-02-08 07:17:58 +01:00
|
|
|
def __init__(self):
|
2017-03-28 19:41:22 +02:00
|
|
|
super().__init__()
|
2017-02-08 07:17:58 +01:00
|
|
|
self.unexpected_msg = False
|
2017-03-29 20:07:39 +02:00
|
|
|
self.ever_connected = False
|
2021-03-03 00:16:16 +01:00
|
|
|
self.got_wtxidrelay = False
|
|
|
|
self.got_sendaddrv2 = False
|
2017-02-08 07:17:58 +01:00
|
|
|
|
|
|
|
def bad_message(self, message):
|
|
|
|
self.unexpected_msg = True
|
2021-03-03 00:16:16 +01:00
|
|
|
print("should not have received message: %s" % message.msgtype)
|
2017-02-08 07:17:58 +01:00
|
|
|
|
2017-11-17 21:01:24 +01:00
|
|
|
def on_open(self):
|
2017-03-29 20:07:39 +02:00
|
|
|
self.ever_connected = True
|
2017-02-08 07:17:58 +01:00
|
|
|
|
2020-08-15 07:38:27 +02:00
|
|
|
# Does not respond to "version" with "verack"
|
2017-11-17 21:01:24 +01:00
|
|
|
def on_version(self, message): self.bad_message(message)
|
|
|
|
def on_verack(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)
|
2021-03-03 00:16:16 +01:00
|
|
|
def on_wtxidrelay(self, message): self.got_wtxidrelay = True
|
|
|
|
def on_sendaddrv2(self, message): self.got_sendaddrv2 = True
|
2017-02-08 07:17:58 +01:00
|
|
|
|
2020-08-04 12:55:35 +02:00
|
|
|
|
2020-08-17 02:08:44 +02:00
|
|
|
# Peer that sends a version but not a verack.
|
|
|
|
class NoVerackIdlePeer(LazyPeer):
|
2017-02-08 07:17:58 +01:00
|
|
|
def __init__(self):
|
|
|
|
self.version_received = False
|
|
|
|
super().__init__()
|
|
|
|
|
2017-11-17 21:01:24 +01:00
|
|
|
def on_verack(self, message): pass
|
2017-02-08 07:17:58 +01: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 21:01:24 +01:00
|
|
|
def on_version(self, message):
|
2017-02-08 07:17:58 +01:00
|
|
|
self.version_received = True
|
2017-11-17 21:01:24 +01:00
|
|
|
self.send_message(msg_ping())
|
|
|
|
self.send_message(msg_getaddr())
|
2017-02-08 07:17:58 +01:00
|
|
|
|
2020-03-02 20:14:30 +01:00
|
|
|
|
2020-04-10 16:54:30 +02:00
|
|
|
class P2PVersionStore(P2PInterface):
|
|
|
|
version_received = None
|
|
|
|
|
|
|
|
def on_version(self, msg):
|
2020-08-15 07:38:27 +02:00
|
|
|
# Responds with an appropriate verack
|
2020-04-10 16:54:30 +02:00
|
|
|
super().on_version(msg)
|
|
|
|
self.version_received = msg
|
|
|
|
|
|
|
|
|
2017-02-08 07:17:58 +01:00
|
|
|
class P2PLeakTest(BitcoinTestFramework):
|
2017-06-10 00:21:21 +02:00
|
|
|
def set_test_params(self):
|
2017-02-08 07:17:58 +01:00
|
|
|
self.num_nodes = 1
|
2021-06-04 21:37:24 +02:00
|
|
|
self.extra_args = [[f"-peertimeout={PEER_TIMEOUT}"]]
|
2021-03-03 00:16:16 +01:00
|
|
|
|
|
|
|
def create_old_version(self, nversion):
|
|
|
|
old_version_msg = msg_version()
|
|
|
|
old_version_msg.nVersion = nversion
|
|
|
|
old_version_msg.strSubVer = P2P_SUBVERSION
|
|
|
|
old_version_msg.nServices = P2P_SERVICES
|
|
|
|
old_version_msg.relay = P2P_VERSION_RELAY
|
|
|
|
return old_version_msg
|
2017-02-08 07:17:58 +01:00
|
|
|
|
|
|
|
def run_test(self):
|
2021-03-03 00:16:16 +01:00
|
|
|
self.log.info('Check that the node doesn\'t send unexpected messages before handshake completion')
|
|
|
|
# Peer that never sends a version, nor any other messages. It shouldn't receive anything from the node.
|
2020-08-17 02:08:44 +02:00
|
|
|
no_version_idle_peer = self.nodes[0].add_p2p_connection(LazyPeer(), send_version=False, wait_for_verack=False)
|
2020-08-15 07:42:30 +02:00
|
|
|
|
|
|
|
# Peer that sends a version but not a verack.
|
2020-08-17 02:08:44 +02:00
|
|
|
no_verack_idle_peer = self.nodes[0].add_p2p_connection(NoVerackIdlePeer(), wait_for_verack=False)
|
2020-03-02 20:14:30 +01:00
|
|
|
|
2021-03-03 00:16:16 +01:00
|
|
|
# Pre-wtxidRelay peer that sends a version but not a verack and does not support feature negotiation
|
|
|
|
# messages which start at nVersion == 70016
|
|
|
|
pre_wtxidrelay_peer = self.nodes[0].add_p2p_connection(NoVerackIdlePeer(), send_version=False, wait_for_verack=False)
|
|
|
|
pre_wtxidrelay_peer.send_message(self.create_old_version(70015))
|
|
|
|
|
|
|
|
# Wait until the peer gets the verack in response to the version. Though, don't wait for the node to receive the
|
|
|
|
# verack, since the peer never sent one
|
2020-08-17 02:08:44 +02:00
|
|
|
no_verack_idle_peer.wait_for_verack()
|
2021-03-03 00:16:16 +01:00
|
|
|
pre_wtxidrelay_peer.wait_for_verack()
|
2017-02-08 07:17:58 +01:00
|
|
|
|
2020-08-27 11:52:52 +02:00
|
|
|
no_version_idle_peer.wait_until(lambda: no_version_idle_peer.ever_connected)
|
|
|
|
no_verack_idle_peer.wait_until(lambda: no_verack_idle_peer.version_received)
|
2021-03-03 00:16:16 +01:00
|
|
|
pre_wtxidrelay_peer.wait_until(lambda: pre_wtxidrelay_peer.version_received)
|
2017-02-08 07:17:58 +01:00
|
|
|
|
2020-08-17 02:08:44 +02:00
|
|
|
# Mine a block and make sure that it's not sent to the connected peers
|
2021-08-19 17:10:24 +02:00
|
|
|
self.generate(self.nodes[0], nblocks=1)
|
2017-02-08 07:17:58 +01:00
|
|
|
|
2021-03-03 00:16:16 +01:00
|
|
|
# Give the node enough time to possibly leak out a message
|
2021-06-04 21:37:24 +02:00
|
|
|
time.sleep(PEER_TIMEOUT + 2)
|
2017-02-08 07:17:58 +01:00
|
|
|
|
2021-03-03 00:16:16 +01:00
|
|
|
# Make sure only expected messages came in
|
|
|
|
assert not no_version_idle_peer.unexpected_msg
|
|
|
|
assert not no_version_idle_peer.got_wtxidrelay
|
|
|
|
assert not no_version_idle_peer.got_sendaddrv2
|
2017-02-08 07:17:58 +01:00
|
|
|
|
2021-03-03 00:16:16 +01:00
|
|
|
assert not no_verack_idle_peer.unexpected_msg
|
|
|
|
assert no_verack_idle_peer.got_wtxidrelay
|
|
|
|
assert no_verack_idle_peer.got_sendaddrv2
|
|
|
|
|
|
|
|
assert not pre_wtxidrelay_peer.unexpected_msg
|
|
|
|
assert not pre_wtxidrelay_peer.got_wtxidrelay
|
|
|
|
assert not pre_wtxidrelay_peer.got_sendaddrv2
|
|
|
|
|
|
|
|
# Expect peers to be disconnected due to timeout
|
|
|
|
assert not no_version_idle_peer.is_connected
|
|
|
|
assert not no_verack_idle_peer.is_connected
|
|
|
|
assert not pre_wtxidrelay_peer.is_connected
|
2017-02-08 07:17:58 +01:00
|
|
|
|
2020-04-10 16:54:30 +02:00
|
|
|
self.log.info('Check that the version message does not leak the local address of the node')
|
|
|
|
p2p_version_store = self.nodes[0].add_p2p_connection(P2PVersionStore())
|
|
|
|
ver = p2p_version_store.version_received
|
2020-05-01 14:46:16 +02:00
|
|
|
# Check that received time is within one hour of now
|
|
|
|
assert_greater_than_or_equal(ver.nTime, time.time() - 3600)
|
|
|
|
assert_greater_than_or_equal(time.time() + 3600, ver.nTime)
|
2020-04-10 16:54:30 +02:00
|
|
|
assert_equal(ver.addrFrom.port, 0)
|
|
|
|
assert_equal(ver.addrFrom.ip, '0.0.0.0')
|
|
|
|
assert_equal(ver.nStartingHeight, 201)
|
2020-11-28 12:41:25 +01:00
|
|
|
assert_equal(ver.relay, 1)
|
2020-04-10 16:54:30 +02:00
|
|
|
|
2020-08-17 02:08:44 +02:00
|
|
|
self.log.info('Check that old peers are disconnected')
|
|
|
|
p2p_old_peer = self.nodes[0].add_p2p_connection(P2PInterface(), send_version=False, wait_for_verack=False)
|
2021-03-03 00:16:16 +01:00
|
|
|
with self.nodes[0].assert_debug_log(['peer=4 using obsolete version 31799; disconnecting']):
|
|
|
|
p2p_old_peer.send_message(self.create_old_version(31799))
|
2020-08-17 02:08:44 +02:00
|
|
|
p2p_old_peer.wait_for_disconnect()
|
2020-04-14 03:39:19 +02:00
|
|
|
|
2018-06-18 23:28:37 +02:00
|
|
|
|
2017-02-08 07:17:58 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
P2PLeakTest().main()
|