Skip to content

Commit fa4c29b

Browse files
author
MarcoFalke
committed
test: Add various low-level p2p tests
1 parent 6ef45bc commit fa4c29b

File tree

5 files changed

+63
-3
lines changed

5 files changed

+63
-3
lines changed

test/functional/p2p_filter.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,13 @@ def skip_test_if_missing_module(self):
6060
self.skip_if_no_wallet()
6161

6262
def run_test(self):
63-
self.log.info('Add filtered P2P connection to the node')
6463
filter_node = self.nodes[0].add_p2p_connection(FilterNode())
64+
65+
self.log.info('Check that too large filter is rejected')
66+
with self.nodes[0].assert_debug_log(['Misbehaving']):
67+
filter_node.send_and_ping(msg_filterload(data=b'\xaa', nHashFuncs=51, nTweak=0, nFlags=1))
68+
69+
self.log.info('Add filtered P2P connection to the node')
6570
filter_node.send_and_ping(filter_node.watch_filter_init)
6671
filter_address = self.nodes[0].decodescript(filter_node.watch_script_pubkey)['addresses'][0]
6772

test/functional/p2p_invalid_messages.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
import sys
99

1010
from test_framework import messages
11-
from test_framework.mininode import P2PDataStore, NetworkThread
11+
from test_framework.mininode import (
12+
NetworkThread,
13+
P2PDataStore,
14+
P2PInterface,
15+
)
1216
from test_framework.test_framework import BitcoinTestFramework
1317

1418

@@ -47,6 +51,7 @@ def run_test(self):
4751
self.test_checksum()
4852
self.test_size()
4953
self.test_command()
54+
self.test_large_inv()
5055

5156
node = self.nodes[0]
5257
self.node = node
@@ -198,6 +203,19 @@ def test_command(self):
198203
conn.sync_with_ping(timeout=1)
199204
self.nodes[0].disconnect_p2ps()
200205

206+
def test_large_inv(self):
207+
conn = self.nodes[0].add_p2p_connection(P2PInterface())
208+
with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=4 (0 -> 20): message inv size() = 50001']):
209+
msg = messages.msg_inv([messages.CInv(1, 1)] * 50001)
210+
conn.send_and_ping(msg)
211+
with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=4 (20 -> 40): message getdata size() = 50001']):
212+
msg = messages.msg_getdata([messages.CInv(1, 1)] * 50001)
213+
conn.send_and_ping(msg)
214+
with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=4 (40 -> 60): headers message size = 2001']):
215+
msg = messages.msg_headers([messages.CBlockHeader()] * 2001)
216+
conn.send_and_ping(msg)
217+
self.nodes[0].disconnect_p2ps()
218+
201219
def _tweak_msg_data_size(self, message, wrong_size):
202220
"""
203221
Return a raw message based on another message but with an incorrect data size in

test/functional/p2p_invalid_tx.py

+17
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def run_test(self):
118118
tx_orphan_2_invalid = CTransaction()
119119
tx_orphan_2_invalid.vin.append(CTxIn(outpoint=COutPoint(tx_orphan_1.sha256, 2)))
120120
tx_orphan_2_invalid.vout.append(CTxOut(nValue=11 * COIN, scriptPubKey=SCRIPT_PUB_KEY_OP_TRUE))
121+
tx_orphan_2_invalid.calc_sha256()
121122

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

152+
self.log.info('Test orphan pool overflow')
153+
orphan_tx_pool = [CTransaction() for _ in range(101)]
154+
for i in range(len(orphan_tx_pool)):
155+
orphan_tx_pool[i].vin.append(CTxIn(outpoint=COutPoint(i, 333)))
156+
orphan_tx_pool[i].vout.append(CTxOut(nValue=11 * COIN, scriptPubKey=SCRIPT_PUB_KEY_OP_TRUE))
157+
158+
with node.assert_debug_log(['mapOrphan overflow, removed 1 tx']):
159+
node.p2p.send_txs_and_test(orphan_tx_pool, node, success=False)
160+
161+
rejected_parent = CTransaction()
162+
rejected_parent.vin.append(CTxIn(outpoint=COutPoint(tx_orphan_2_invalid.sha256, 0)))
163+
rejected_parent.vout.append(CTxOut(nValue=11 * COIN, scriptPubKey=SCRIPT_PUB_KEY_OP_TRUE))
164+
rejected_parent.rehash()
165+
with node.assert_debug_log(['not keeping orphan with rejected parents {}'.format(rejected_parent.hash)]):
166+
node.p2p.send_txs_and_test([rejected_parent], node, success=False)
167+
151168

152169
if __name__ == '__main__':
153170
InvalidTxRequestTest().main()

test/functional/p2p_leak.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212

1313
import time
1414

15-
from test_framework.messages import msg_getaddr, msg_ping, msg_verack
15+
from test_framework.messages import (
16+
msg_getaddr,
17+
msg_ping,
18+
msg_verack,
19+
msg_version,
20+
)
1621
from test_framework.mininode import mininode_lock, P2PInterface
1722
from test_framework.test_framework import BitcoinTestFramework
1823
from test_framework.util import (
@@ -147,6 +152,15 @@ def run_test(self):
147152
assert_equal(ver.nStartingHeight, 201)
148153
assert_equal(ver.nRelay, 1)
149154

155+
self.log.info('Check that old nodes are disconnected')
156+
p2p_old_node = self.nodes[0].add_p2p_connection(P2PInterface(), send_version=False, wait_for_verack=False)
157+
old_version_msg = msg_version()
158+
old_version_msg.nVersion = 31799
159+
wait_until(lambda: p2p_old_node.is_connected)
160+
with self.nodes[0].assert_debug_log(['peer=4 using obsolete version 31799; disconnecting']):
161+
p2p_old_node.send_message(old_version_msg)
162+
p2p_old_node.wait_for_disconnect()
163+
150164

151165
if __name__ == '__main__':
152166
P2PLeakTest().main()

test/functional/p2p_tx_download.py

+6
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ def test_in_flight_max(self):
152152
wait_until(lambda: p.tx_getdata_count == MAX_GETDATA_IN_FLIGHT + 2)
153153
self.nodes[0].setmocktime(0)
154154

155+
def test_spurious_notfound(self):
156+
self.log.info('Check that spurious notfound is ignored')
157+
self.nodes[0].p2ps[0].send_message(msg_notfound(vec=[CInv(1, 1)]))
158+
155159
def run_test(self):
156160
# Setup the p2p connections
157161
self.peers = []
@@ -161,6 +165,8 @@ def run_test(self):
161165

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

168+
self.test_spurious_notfound()
169+
164170
# Test the in-flight max first, because we want no transactions in
165171
# flight ahead of this test.
166172
self.test_in_flight_max()

0 commit comments

Comments
 (0)