Changeset View
Changeset View
Standalone View
Standalone View
test/functional/p2p_leak.py
Show All 10 Lines | |||||
into sending us something it shouldn't. | into sending us something it shouldn't. | ||||
""" | """ | ||||
import time | import time | ||||
from test_framework.messages import ( | from test_framework.messages import ( | ||||
msg_getaddr, | msg_getaddr, | ||||
msg_ping, | msg_ping, | ||||
msg_verack, | |||||
msg_version, | msg_version, | ||||
) | ) | ||||
from test_framework.p2p import ( | from test_framework.p2p import ( | ||||
p2p_lock, | p2p_lock, | ||||
P2PInterface, | P2PInterface, | ||||
) | ) | ||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
from test_framework.util import ( | from test_framework.util import ( | ||||
assert_equal, | assert_equal, | ||||
assert_greater_than_or_equal, | assert_greater_than_or_equal, | ||||
wait_until, | wait_until, | ||||
) | ) | ||||
DISCOURAGEMENT_THRESHOLD = 10 | DISCOURAGEMENT_THRESHOLD = 10 | ||||
class CLazyNode(P2PInterface): | class LazyPeer(P2PInterface): | ||||
def __init__(self): | def __init__(self): | ||||
super().__init__() | super().__init__() | ||||
self.unexpected_msg = False | self.unexpected_msg = False | ||||
self.ever_connected = False | self.ever_connected = False | ||||
def bad_message(self, message): | def bad_message(self, message): | ||||
self.unexpected_msg = True | self.unexpected_msg = True | ||||
self.log.info( | self.log.info( | ||||
"should not have received message: {}".format(message.msgtype)) | "should not have received message: {}".format(message.msgtype)) | ||||
def on_open(self): | def on_open(self): | ||||
self.ever_connected = True | self.ever_connected = True | ||||
# Does not respond to "version" with "verack" | |||||
def on_version(self, message): self.bad_message(message) | def on_version(self, message): self.bad_message(message) | ||||
def on_verack(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_inv(self, message): self.bad_message(message) | ||||
def on_addr(self, message): self.bad_message(message) | def on_addr(self, message): self.bad_message(message) | ||||
Show All 25 Lines | class LazyPeer(P2PInterface): | ||||
def on_cmpctblock(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_getblocktxn(self, message): self.bad_message(message) | ||||
def on_blocktxn(self, message): self.bad_message(message) | def on_blocktxn(self, message): self.bad_message(message) | ||||
# Node that never sends a version. We'll use this to send a bunch of messages | # Peer that sends a version but not a verack. | ||||
# anyway, and eventually get disconnected. | class NoVerackIdlePeer(LazyPeer): | ||||
class CNodeNoVersionMisbehavior(CLazyNode): | |||||
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): | def __init__(self): | ||||
self.version_received = False | self.version_received = False | ||||
super().__init__() | super().__init__() | ||||
def on_verack(self, message): pass | def on_verack(self, message): pass | ||||
# When version is received, don't reply with a verack. Instead, see if the | # 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 | # node will give us a message that it shouldn't. This is not an exhaustive | ||||
# list! | # list! | ||||
def on_version(self, message): | def on_version(self, message): | ||||
self.version_received = True | self.version_received = True | ||||
self.send_message(msg_ping()) | self.send_message(msg_ping()) | ||||
self.send_message(msg_getaddr()) | self.send_message(msg_getaddr()) | ||||
class P2PVersionStore(P2PInterface): | class P2PVersionStore(P2PInterface): | ||||
version_received = None | version_received = None | ||||
def on_version(self, msg): | def on_version(self, msg): | ||||
# Responds with an appropriate verack | |||||
super().on_version(msg) | super().on_version(msg) | ||||
self.version_received = msg | self.version_received = msg | ||||
class P2PLeakTest(BitcoinTestFramework): | class P2PLeakTest(BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.num_nodes = 1 | self.num_nodes = 1 | ||||
def run_test(self): | def run_test(self): | ||||
no_version_disconnect_node = self.nodes[0].add_p2p_connection( | # Peer that never sends a version. We will send a bunch of messages | ||||
CNodeNoVersionMisbehavior(), send_version=False, wait_for_verack=False) | # from this peer anyway and verify eventual disconnection. | ||||
no_version_idlenode = self.nodes[0].add_p2p_connection( | no_version_disconnect_peer = self.nodes[0].add_p2p_connection( | ||||
CNodeNoVersionIdle(), send_version=False, wait_for_verack=False) | LazyPeer(), send_version=False, wait_for_verack=False) | ||||
no_verack_idlenode = self.nodes[0].add_p2p_connection( | |||||
CNodeNoVerackIdle(), wait_for_verack=False) | # Another peer that never sends a version, nor any other messages. | ||||
# It shouldn't receive anything from the node. | |||||
# Send enough veracks without a message to reach the peer discouragement | no_version_idle_peer = self.nodes[0].add_p2p_connection( | ||||
# threshold. This should get us disconnected. | LazyPeer(), send_version=False, wait_for_verack=False) | ||||
# Peer that sends a version but not a verack. | |||||
no_verack_idle_peer = self.nodes[0].add_p2p_connection( | |||||
NoVerackIdlePeer(), wait_for_verack=False) | |||||
# Send enough ping messages (any non-version message will do) prior | |||||
# to sending version to reach the peer discouragement threshold. This | |||||
# should get us disconnected. | |||||
for _ in range(DISCOURAGEMENT_THRESHOLD): | for _ in range(DISCOURAGEMENT_THRESHOLD): | ||||
no_version_disconnect_node.send_message(msg_verack()) | no_version_disconnect_peer.send_message(msg_ping()) | ||||
# Wait until we got the verack in response to the version. Though, don't wait for the other node to receive the | # Wait until we got the verack in response to the version. Though, don't wait for the node to receive the | ||||
# verack, since we never sent one | # verack, since we never sent one | ||||
no_verack_idlenode.wait_for_verack() | no_verack_idle_peer.wait_for_verack() | ||||
wait_until( | wait_until( | ||||
lambda: no_version_disconnect_node.ever_connected, | lambda: no_version_disconnect_peer.ever_connected, | ||||
timeout=10, | timeout=10, | ||||
lock=p2p_lock) | lock=p2p_lock) | ||||
wait_until(lambda: no_version_idlenode.ever_connected, | wait_until(lambda: no_version_idle_peer.ever_connected, | ||||
timeout=10, lock=p2p_lock) | timeout=10, lock=p2p_lock) | ||||
wait_until(lambda: no_verack_idlenode.version_received, | wait_until(lambda: no_verack_idle_peer.version_received, | ||||
timeout=10, lock=p2p_lock) | timeout=10, lock=p2p_lock) | ||||
# Mine a block and make sure that it's not sent to the connected nodes | # Mine a block and make sure that it's not sent to the connected peers | ||||
self.nodes[0].generatetoaddress( | self.nodes[0].generate(nblocks=1) | ||||
1, self.nodes[0].get_deterministic_priv_key().address) | |||||
# Give the node enough time to possibly leak out a message | # Give the node enough time to possibly leak out a message | ||||
time.sleep(5) | time.sleep(5) | ||||
# Expect this node to be disconnected for misbehavior | # Expect this peer to be disconnected for misbehavior | ||||
assert not no_version_disconnect_node.is_connected | assert not no_version_disconnect_peer.is_connected | ||||
self.nodes[0].disconnect_p2ps() | self.nodes[0].disconnect_p2ps() | ||||
# Make sure no unexpected messages came in | # Make sure no unexpected messages came in | ||||
assert not no_version_disconnect_node.unexpected_msg | assert not no_version_disconnect_peer.unexpected_msg | ||||
assert not no_version_idlenode.unexpected_msg | assert not no_version_idle_peer.unexpected_msg | ||||
assert not no_verack_idlenode.unexpected_msg | assert not no_verack_idle_peer.unexpected_msg | ||||
self.log.info( | self.log.info( | ||||
'Check that the version message does not leak the local address of the node') | 'Check that the version message does not leak the local address of the node') | ||||
p2p_version_store = self.nodes[0].add_p2p_connection(P2PVersionStore()) | p2p_version_store = self.nodes[0].add_p2p_connection(P2PVersionStore()) | ||||
ver = p2p_version_store.version_received | ver = p2p_version_store.version_received | ||||
# Check that received time is within one hour of now | # 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(ver.nTime, time.time() - 3600) | ||||
assert_greater_than_or_equal(time.time() + 3600, ver.nTime) | assert_greater_than_or_equal(time.time() + 3600, ver.nTime) | ||||
assert_equal(ver.addrFrom.port, 0) | assert_equal(ver.addrFrom.port, 0) | ||||
assert_equal(ver.addrFrom.ip, '0.0.0.0') | assert_equal(ver.addrFrom.ip, '0.0.0.0') | ||||
assert_equal(ver.nStartingHeight, 201) | assert_equal(ver.nStartingHeight, 201) | ||||
assert_equal(ver.nRelay, 1) | assert_equal(ver.nRelay, 1) | ||||
self.log.info('Check that old nodes are disconnected') | self.log.info('Check that old peers are disconnected') | ||||
p2p_old_node = self.nodes[0].add_p2p_connection( | p2p_old_peer = self.nodes[0].add_p2p_connection( | ||||
P2PInterface(), send_version=False, wait_for_verack=False) | P2PInterface(), send_version=False, wait_for_verack=False) | ||||
old_version_msg = msg_version() | old_version_msg = msg_version() | ||||
old_version_msg.nVersion = 31799 | old_version_msg.nVersion = 31799 | ||||
with self.nodes[0].assert_debug_log(['peer=4 using obsolete version 31799; disconnecting']): | with self.nodes[0].assert_debug_log(['peer=4 using obsolete version 31799; disconnecting']): | ||||
p2p_old_node.send_message(old_version_msg) | p2p_old_peer.send_message(old_version_msg) | ||||
p2p_old_node.wait_for_disconnect() | p2p_old_peer.wait_for_disconnect() | ||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
P2PLeakTest().main() | P2PLeakTest().main() |