mirror of
https://github.com/dashpay/dash.git
synced 2024-12-26 12:32:48 +01:00
8f0c22f93b
9a40cfc558b3f7fa4fff1270f969582af17479a5 [refactor] use waiting inside disconnect_p2ps (gzhao408) aeb9fb414e2d000830287d9dd3fed7fc2eb570d2 [test] wait for disconnect_p2ps to be reflected in getpeerinfo (gzhao408) e81942d2e1288367e8da94adb2b2a88be99e4751 [test] logging and style followups for bloomfilter tests (gzhao408) Pull request description: Followup to #19083 which adds bloomfilter-related tests. 1. Make test_node `disconnect_p2ps` wait until disconnection is complete to avoid race conditions (and not place the burden on tests) from MarcoFalke's [comment](https://github.com/bitcoin/bitcoin/pull/19083#discussion_r437383989). And clean up any redundant `wait_until`s in the functional tests. 2. Clean up style + logging in p2p_filter.py and p2p_nobloomfilter_messages.py and jonatack's other [comments](https://github.com/bitcoin/bitcoin/pull/19083#pullrequestreview-428955784) ACKs for top commit: jonatack: Code review ACK 9a40cfc from re-reviewing the diff and `git range-diff 5cafb46 8386ad5 9a40cfc` MarcoFalke: ACK 9a40cfc558b3f7fa4fff1270f969582af17479a5 🐂 Tree-SHA512: 2e14b1c12fc08a355bd5ccad7a2a734a4ccda4bc7dc7bac171cb57359819fc1599d764290729af74832fac3e2be258c5d406c701e78ab6d7262835859b9a7d87
153 lines
6.0 KiB
Python
Executable File
153 lines
6.0 KiB
Python
Executable File
#!/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.
|
|
"""Test message sending before handshake completion.
|
|
|
|
A node should never send anything other than VERSION/VERACK until it's
|
|
received a VERACK.
|
|
|
|
This test connects to a node and sends it a few messages, trying to entice it
|
|
into sending us something it shouldn't.
|
|
"""
|
|
|
|
import time
|
|
|
|
from test_framework.messages import msg_getaddr, msg_ping, msg_verack
|
|
from test_framework.mininode import mininode_lock, P2PInterface
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
from test_framework.util import (
|
|
assert_equal,
|
|
assert_greater_than_or_equal,
|
|
wait_until,
|
|
)
|
|
|
|
banscore = 10
|
|
|
|
|
|
class CLazyNode(P2PInterface):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.unexpected_msg = False
|
|
self.ever_connected = False
|
|
|
|
def bad_message(self, message):
|
|
self.unexpected_msg = True
|
|
self.log.info("should not have received message: %s" % message.command)
|
|
|
|
def on_open(self):
|
|
self.ever_connected = True
|
|
|
|
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_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)
|
|
|
|
# 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 dashd ban behavior changes
|
|
def on_open(self):
|
|
super().on_open()
|
|
for i in range(banscore):
|
|
self.send_message(msg_verack())
|
|
|
|
# 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_verack(self, 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, message):
|
|
self.version_received = True
|
|
self.send_message(msg_ping())
|
|
self.send_message(msg_getaddr())
|
|
|
|
|
|
class P2PVersionStore(P2PInterface):
|
|
version_received = None
|
|
|
|
def on_version(self, msg):
|
|
super().on_version(msg)
|
|
self.version_received = msg
|
|
|
|
|
|
class P2PLeakTest(BitcoinTestFramework):
|
|
def set_test_params(self):
|
|
self.num_nodes = 1
|
|
self.extra_args = [['-banscore=' + str(banscore)]]
|
|
|
|
def setup_network(self):
|
|
self.disable_mocktime()
|
|
self.setup_nodes()
|
|
|
|
def run_test(self):
|
|
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)
|
|
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()
|
|
|
|
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)
|
|
|
|
# Mine a block and make sure that it's not sent to the connected nodes
|
|
self.nodes[0].generatetoaddress(1, self.nodes[0].get_deterministic_priv_key().address)
|
|
|
|
#Give the node enough time to possibly leak out a message
|
|
time.sleep(5)
|
|
|
|
#This node should have been banned
|
|
assert not no_version_bannode.is_connected
|
|
|
|
self.nodes[0].disconnect_p2ps()
|
|
|
|
# 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
|
|
|
|
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
|
|
# 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)
|
|
assert_equal(ver.addrFrom.port, 0)
|
|
assert_equal(ver.addrFrom.ip, '0.0.0.0')
|
|
assert_equal(ver.nStartingHeight, 201)
|
|
assert_equal(ver.nRelay, 1)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
P2PLeakTest().main()
|