Skip to content

Commit

Permalink
test: Add various low-level p2p tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoFalke committed Apr 14, 2020
1 parent 6ef45bc commit fa4c29b
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
7 changes: 6 additions & 1 deletion test/functional/p2p_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ def skip_test_if_missing_module(self):
self.skip_if_no_wallet()

def run_test(self):
self.log.info('Add filtered P2P connection to the node')
filter_node = self.nodes[0].add_p2p_connection(FilterNode())

self.log.info('Check that too large filter is rejected')
with self.nodes[0].assert_debug_log(['Misbehaving']):
filter_node.send_and_ping(msg_filterload(data=b'\xaa', nHashFuncs=51, nTweak=0, nFlags=1))

self.log.info('Add filtered P2P connection to the node')
filter_node.send_and_ping(filter_node.watch_filter_init)
filter_address = self.nodes[0].decodescript(filter_node.watch_script_pubkey)['addresses'][0]

Expand Down
20 changes: 19 additions & 1 deletion test/functional/p2p_invalid_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
import sys

from test_framework import messages
from test_framework.mininode import P2PDataStore, NetworkThread
from test_framework.mininode import (
NetworkThread,
P2PDataStore,
P2PInterface,
)
from test_framework.test_framework import BitcoinTestFramework


Expand Down Expand Up @@ -47,6 +51,7 @@ def run_test(self):
self.test_checksum()
self.test_size()
self.test_command()
self.test_large_inv()

node = self.nodes[0]
self.node = node
Expand Down Expand Up @@ -198,6 +203,19 @@ def test_command(self):
conn.sync_with_ping(timeout=1)
self.nodes[0].disconnect_p2ps()

def test_large_inv(self):
conn = self.nodes[0].add_p2p_connection(P2PInterface())
with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=4 (0 -> 20): message inv size() = 50001']):
msg = messages.msg_inv([messages.CInv(1, 1)] * 50001)
conn.send_and_ping(msg)
with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=4 (20 -> 40): message getdata size() = 50001']):
msg = messages.msg_getdata([messages.CInv(1, 1)] * 50001)
conn.send_and_ping(msg)
with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=4 (40 -> 60): headers message size = 2001']):
msg = messages.msg_headers([messages.CBlockHeader()] * 2001)
conn.send_and_ping(msg)
self.nodes[0].disconnect_p2ps()

def _tweak_msg_data_size(self, message, wrong_size):
"""
Return a raw message based on another message but with an incorrect data size in
Expand Down
17 changes: 17 additions & 0 deletions test/functional/p2p_invalid_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def run_test(self):
tx_orphan_2_invalid = CTransaction()
tx_orphan_2_invalid.vin.append(CTxIn(outpoint=COutPoint(tx_orphan_1.sha256, 2)))
tx_orphan_2_invalid.vout.append(CTxOut(nValue=11 * COIN, scriptPubKey=SCRIPT_PUB_KEY_OP_TRUE))
tx_orphan_2_invalid.calc_sha256()

self.log.info('Send the orphans ... ')
# Send valid orphan txs from p2ps[0]
Expand Down Expand Up @@ -148,6 +149,22 @@ def run_test(self):
wait_until(lambda: 1 == len(node.getpeerinfo()), timeout=12) # p2ps[1] is no longer connected
assert_equal(expected_mempool, set(node.getrawmempool()))

self.log.info('Test orphan pool overflow')
orphan_tx_pool = [CTransaction() for _ in range(101)]
for i in range(len(orphan_tx_pool)):
orphan_tx_pool[i].vin.append(CTxIn(outpoint=COutPoint(i, 333)))
orphan_tx_pool[i].vout.append(CTxOut(nValue=11 * COIN, scriptPubKey=SCRIPT_PUB_KEY_OP_TRUE))

with node.assert_debug_log(['mapOrphan overflow, removed 1 tx']):
node.p2p.send_txs_and_test(orphan_tx_pool, node, success=False)

rejected_parent = CTransaction()
rejected_parent.vin.append(CTxIn(outpoint=COutPoint(tx_orphan_2_invalid.sha256, 0)))
rejected_parent.vout.append(CTxOut(nValue=11 * COIN, scriptPubKey=SCRIPT_PUB_KEY_OP_TRUE))
rejected_parent.rehash()
with node.assert_debug_log(['not keeping orphan with rejected parents {}'.format(rejected_parent.hash)]):
node.p2p.send_txs_and_test([rejected_parent], node, success=False)


if __name__ == '__main__':
InvalidTxRequestTest().main()
16 changes: 15 additions & 1 deletion test/functional/p2p_leak.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@

import time

from test_framework.messages import msg_getaddr, msg_ping, msg_verack
from test_framework.messages import (
msg_getaddr,
msg_ping,
msg_verack,
msg_version,
)
from test_framework.mininode import mininode_lock, P2PInterface
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
Expand Down Expand Up @@ -147,6 +152,15 @@ def run_test(self):
assert_equal(ver.nStartingHeight, 201)
assert_equal(ver.nRelay, 1)

self.log.info('Check that old nodes are disconnected')
p2p_old_node = self.nodes[0].add_p2p_connection(P2PInterface(), send_version=False, wait_for_verack=False)
old_version_msg = msg_version()
old_version_msg.nVersion = 31799
wait_until(lambda: p2p_old_node.is_connected)
with self.nodes[0].assert_debug_log(['peer=4 using obsolete version 31799; disconnecting']):
p2p_old_node.send_message(old_version_msg)
p2p_old_node.wait_for_disconnect()


if __name__ == '__main__':
P2PLeakTest().main()
6 changes: 6 additions & 0 deletions test/functional/p2p_tx_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ def test_in_flight_max(self):
wait_until(lambda: p.tx_getdata_count == MAX_GETDATA_IN_FLIGHT + 2)
self.nodes[0].setmocktime(0)

def test_spurious_notfound(self):
self.log.info('Check that spurious notfound is ignored')
self.nodes[0].p2ps[0].send_message(msg_notfound(vec=[CInv(1, 1)]))

def run_test(self):
# Setup the p2p connections
self.peers = []
Expand All @@ -161,6 +165,8 @@ def run_test(self):

self.log.info("Nodes are setup with {} incoming connections each".format(NUM_INBOUND))

self.test_spurious_notfound()

# Test the in-flight max first, because we want no transactions in
# flight ahead of this test.
self.test_in_flight_max()
Expand Down

0 comments on commit fa4c29b

Please sign in to comment.