Skip to content

Commit b33136b

Browse files
author
MarcoFalke
committed
Merge #19083: test: msg_mempool, fRelay, and other bloomfilter tests
dca7394 scripted-diff: rename node to peer for mininodes (gzhao408) 0474ea2 [test] fix race conditions and test in p2p_filter (gzhao408) 4ef80f0 [test] sending invalid msgs to node with bloomfilters=0 causes disconnect (gzhao408) 497a619 [test] add BIP 37 test for node with fRelay=false (gzhao408) e8acc60 [test] add mempool msg test for node with bloomfilter enabled (gzhao408) Pull request description: This PR adds a few tests that are bloomfilter-related, including behavior for when bloomfilters are turned _off_: 1. Tests p2p message `msg_mempool`: a node that has `peerbloomfilters` enabled should send its mempool (disabled behavior already tested [here](https://github.com/bitcoin/bitcoin/blob/master/test/functional/p2p_mempool.py)). 2. Tests that bloomfilter peers with [`fRelay=False`](https://github.com/bitcoin/bips/blob/master/bip-0037.mediawiki#extensions-to-existing-messages) in the `version` message should not receive any invs until they set the filter. The rest is the same as what’s already tested in `p2p_filter.py`. 3. Tests that peers get disconnected if they send `filterload` or `filteradd` p2p messages to a node with bloom filters disabled. 4. Refactor: renames p2p_mempool.py to p2p_nobloomfilter_messages.py. 5. Fixes race conditions in p2p_filter.py ACKs for top commit: MarcoFalke: ACK dca7394 only changes is restoring accidentally deleted test 🍮 jonatack: ACK dca7394 modulo a few nits if you retouch, happy to re-ACK if you take any of them but don't feel obliged to. Tree-SHA512: 442aeab0755cb8b830251ea170d1d5e6da8ac9029b3276d407a20ee3d588cc61b77b8842368de18c244056316b8c63b911776d6e106bc7c023439ab915b27ad3
2 parents 8682414 + dca7394 commit b33136b

File tree

4 files changed

+165
-80
lines changed

4 files changed

+165
-80
lines changed

test/functional/p2p_filter.py

Lines changed: 121 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
msg_filterclear,
1717
msg_filterload,
1818
msg_getdata,
19+
msg_mempool,
20+
msg_version,
1921
)
20-
from test_framework.mininode import P2PInterface
22+
from test_framework.mininode import P2PInterface, mininode_lock
2123
from test_framework.script import MAX_SCRIPT_ELEMENT_SIZE
2224
from test_framework.test_framework import BitcoinTestFramework
2325

2426

25-
class FilterNode(P2PInterface):
27+
class P2PBloomFilter(P2PInterface):
2628
# This is a P2SH watch-only wallet
2729
watch_script_pubkey = 'a914ffffffffffffffffffffffffffffffffffffffff87'
2830
# The initial filter (n=10, fp=0.000001) with just the above scriptPubKey added
@@ -34,6 +36,11 @@ class FilterNode(P2PInterface):
3436
nFlags=1,
3537
)
3638

39+
def __init__(self):
40+
super().__init__()
41+
self._tx_received = False
42+
self._merkleblock_received = False
43+
3744
def on_inv(self, message):
3845
want = msg_getdata()
3946
for i in message.inv:
@@ -46,10 +53,30 @@ def on_inv(self, message):
4653
self.send_message(want)
4754

4855
def on_merkleblock(self, message):
49-
self.merkleblock_received = True
56+
self._merkleblock_received = True
5057

5158
def on_tx(self, message):
52-
self.tx_received = True
59+
self._tx_received = True
60+
61+
@property
62+
def tx_received(self):
63+
with mininode_lock:
64+
return self._tx_received
65+
66+
@tx_received.setter
67+
def tx_received(self, value):
68+
with mininode_lock:
69+
self._tx_received = value
70+
71+
@property
72+
def merkleblock_received(self):
73+
with mininode_lock:
74+
return self._merkleblock_received
75+
76+
@merkleblock_received.setter
77+
def merkleblock_received(self, value):
78+
with mininode_lock:
79+
self._merkleblock_received = value
5380

5481

5582
class FilterTest(BitcoinTestFramework):
@@ -64,95 +91,144 @@ def set_test_params(self):
6491
def skip_test_if_missing_module(self):
6592
self.skip_if_no_wallet()
6693

67-
def test_size_limits(self, filter_node):
94+
def test_size_limits(self, filter_peer):
6895
self.log.info('Check that too large filter is rejected')
6996
with self.nodes[0].assert_debug_log(['Misbehaving']):
70-
filter_node.send_and_ping(msg_filterload(data=b'\xbb'*(MAX_BLOOM_FILTER_SIZE+1)))
97+
filter_peer.send_and_ping(msg_filterload(data=b'\xbb'*(MAX_BLOOM_FILTER_SIZE+1)))
7198

7299
self.log.info('Check that max size filter is accepted')
73100
with self.nodes[0].assert_debug_log([], unexpected_msgs=['Misbehaving']):
74-
filter_node.send_and_ping(msg_filterload(data=b'\xbb'*(MAX_BLOOM_FILTER_SIZE)))
75-
filter_node.send_and_ping(msg_filterclear())
101+
filter_peer.send_and_ping(msg_filterload(data=b'\xbb'*(MAX_BLOOM_FILTER_SIZE)))
102+
filter_peer.send_and_ping(msg_filterclear())
76103

77104
self.log.info('Check that filter with too many hash functions is rejected')
78105
with self.nodes[0].assert_debug_log(['Misbehaving']):
79-
filter_node.send_and_ping(msg_filterload(data=b'\xaa', nHashFuncs=MAX_BLOOM_HASH_FUNCS+1))
106+
filter_peer.send_and_ping(msg_filterload(data=b'\xaa', nHashFuncs=MAX_BLOOM_HASH_FUNCS+1))
80107

81108
self.log.info('Check that filter with max hash functions is accepted')
82109
with self.nodes[0].assert_debug_log([], unexpected_msgs=['Misbehaving']):
83-
filter_node.send_and_ping(msg_filterload(data=b'\xaa', nHashFuncs=MAX_BLOOM_HASH_FUNCS))
110+
filter_peer.send_and_ping(msg_filterload(data=b'\xaa', nHashFuncs=MAX_BLOOM_HASH_FUNCS))
84111
# Don't send filterclear until next two filteradd checks are done
85112

86113
self.log.info('Check that max size data element to add to the filter is accepted')
87114
with self.nodes[0].assert_debug_log([], unexpected_msgs=['Misbehaving']):
88-
filter_node.send_and_ping(msg_filteradd(data=b'\xcc'*(MAX_SCRIPT_ELEMENT_SIZE)))
115+
filter_peer.send_and_ping(msg_filteradd(data=b'\xcc'*(MAX_SCRIPT_ELEMENT_SIZE)))
89116

90117
self.log.info('Check that too large data element to add to the filter is rejected')
91118
with self.nodes[0].assert_debug_log(['Misbehaving']):
92-
filter_node.send_and_ping(msg_filteradd(data=b'\xcc'*(MAX_SCRIPT_ELEMENT_SIZE+1)))
119+
filter_peer.send_and_ping(msg_filteradd(data=b'\xcc'*(MAX_SCRIPT_ELEMENT_SIZE+1)))
93120

94-
filter_node.send_and_ping(msg_filterclear())
121+
filter_peer.send_and_ping(msg_filterclear())
95122

96-
def run_test(self):
97-
filter_node = self.nodes[0].add_p2p_connection(FilterNode())
123+
def test_msg_mempool(self):
124+
self.log.info("Check that a node with bloom filters enabled services p2p mempool messages")
125+
filter_peer = P2PBloomFilter()
98126

99-
self.test_size_limits(filter_node)
127+
self.log.info("Create a tx relevant to the peer before connecting")
128+
filter_address = self.nodes[0].decodescript(filter_peer.watch_script_pubkey)['addresses'][0]
129+
txid = self.nodes[0].sendtoaddress(filter_address, 90)
100130

101-
self.log.info('Add filtered P2P connection to the node')
102-
filter_node.send_and_ping(filter_node.watch_filter_init)
103-
filter_address = self.nodes[0].decodescript(filter_node.watch_script_pubkey)['addresses'][0]
131+
self.log.info("Send a mempool msg after connecting and check that the tx is received")
132+
self.nodes[0].add_p2p_connection(filter_peer)
133+
filter_peer.send_and_ping(filter_peer.watch_filter_init)
134+
self.nodes[0].p2p.send_message(msg_mempool())
135+
filter_peer.wait_for_tx(txid)
136+
137+
def test_frelay_false(self, filter_peer):
138+
self.log.info("Check that a node with fRelay set to false does not receive invs until the filter is set")
139+
filter_peer.tx_received = False
140+
filter_address = self.nodes[0].decodescript(filter_peer.watch_script_pubkey)['addresses'][0]
141+
self.nodes[0].sendtoaddress(filter_address, 90)
142+
# Sync to make sure the reason filter_peer doesn't receive the tx is not p2p delays
143+
filter_peer.sync_with_ping()
144+
assert not filter_peer.tx_received
145+
146+
# Clear the mempool so that this transaction does not impact subsequent tests
147+
self.nodes[0].generate(1)
148+
149+
def test_filter(self, filter_peer):
150+
# Set the bloomfilter using filterload
151+
filter_peer.send_and_ping(filter_peer.watch_filter_init)
152+
# If fRelay is not already True, sending filterload sets it to True
153+
assert self.nodes[0].getpeerinfo()[0]['relaytxes']
154+
filter_address = self.nodes[0].decodescript(filter_peer.watch_script_pubkey)['addresses'][0]
104155

105156
self.log.info('Check that we receive merkleblock and tx if the filter matches a tx in a block')
106157
block_hash = self.nodes[0].generatetoaddress(1, filter_address)[0]
107158
txid = self.nodes[0].getblock(block_hash)['tx'][0]
108-
filter_node.wait_for_merkleblock(block_hash)
109-
filter_node.wait_for_tx(txid)
159+
filter_peer.wait_for_merkleblock(block_hash)
160+
filter_peer.wait_for_tx(txid)
110161

111162
self.log.info('Check that we only receive a merkleblock if the filter does not match a tx in a block')
112-
filter_node.tx_received = False
163+
filter_peer.tx_received = False
113164
block_hash = self.nodes[0].generatetoaddress(1, self.nodes[0].getnewaddress())[0]
114-
filter_node.wait_for_merkleblock(block_hash)
115-
assert not filter_node.tx_received
165+
filter_peer.wait_for_merkleblock(block_hash)
166+
assert not filter_peer.tx_received
116167

117168
self.log.info('Check that we not receive a tx if the filter does not match a mempool tx')
118-
filter_node.merkleblock_received = False
119-
filter_node.tx_received = False
169+
filter_peer.merkleblock_received = False
170+
filter_peer.tx_received = False
120171
self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 90)
121-
filter_node.sync_with_ping()
122-
filter_node.sync_with_ping()
123-
assert not filter_node.merkleblock_received
124-
assert not filter_node.tx_received
172+
filter_peer.sync_with_ping()
173+
filter_peer.sync_with_ping()
174+
assert not filter_peer.merkleblock_received
175+
assert not filter_peer.tx_received
125176

126-
self.log.info('Check that we receive a tx in reply to a mempool msg if the filter matches a mempool tx')
127-
filter_node.merkleblock_received = False
177+
self.log.info('Check that we receive a tx if the filter matches a mempool tx')
178+
filter_peer.merkleblock_received = False
128179
txid = self.nodes[0].sendtoaddress(filter_address, 90)
129-
filter_node.wait_for_tx(txid)
130-
assert not filter_node.merkleblock_received
180+
filter_peer.wait_for_tx(txid)
181+
assert not filter_peer.merkleblock_received
131182

132183
self.log.info('Check that after deleting filter all txs get relayed again')
133-
filter_node.send_and_ping(msg_filterclear())
184+
filter_peer.send_and_ping(msg_filterclear())
134185
for _ in range(5):
135186
txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 7)
136-
filter_node.wait_for_tx(txid)
187+
filter_peer.wait_for_tx(txid)
137188

138189
self.log.info('Check that request for filtered blocks is ignored if no filter is set')
139-
filter_node.merkleblock_received = False
140-
filter_node.tx_received = False
190+
filter_peer.merkleblock_received = False
191+
filter_peer.tx_received = False
141192
with self.nodes[0].assert_debug_log(expected_msgs=['received getdata']):
142193
block_hash = self.nodes[0].generatetoaddress(1, self.nodes[0].getnewaddress())[0]
143-
filter_node.wait_for_inv([CInv(MSG_BLOCK, int(block_hash, 16))])
144-
filter_node.sync_with_ping()
145-
assert not filter_node.merkleblock_received
146-
assert not filter_node.tx_received
194+
filter_peer.wait_for_inv([CInv(MSG_BLOCK, int(block_hash, 16))])
195+
filter_peer.sync_with_ping()
196+
assert not filter_peer.merkleblock_received
197+
assert not filter_peer.tx_received
147198

148199
self.log.info('Check that sending "filteradd" if no filter is set is treated as misbehavior')
149200
with self.nodes[0].assert_debug_log(['Misbehaving']):
150-
filter_node.send_and_ping(msg_filteradd(data=b'letsmisbehave'))
201+
filter_peer.send_and_ping(msg_filteradd(data=b'letsmisbehave'))
151202

152203
self.log.info("Check that division-by-zero remote crash bug [CVE-2013-5700] is fixed")
153-
filter_node.send_and_ping(msg_filterload(data=b'', nHashFuncs=1))
154-
filter_node.send_and_ping(msg_filteradd(data=b'letstrytocrashthisnode'))
204+
filter_peer.send_and_ping(msg_filterload(data=b'', nHashFuncs=1))
205+
filter_peer.send_and_ping(msg_filteradd(data=b'letstrytocrashthisnode'))
206+
self.nodes[0].disconnect_p2ps()
155207

208+
def run_test(self):
209+
filter_peer = self.nodes[0].add_p2p_connection(P2PBloomFilter())
210+
self.log.info('Test filter size limits')
211+
self.test_size_limits(filter_peer)
212+
213+
self.log.info('Test BIP 37 for a node with fRelay = True (default)')
214+
self.test_filter(filter_peer)
215+
self.nodes[0].disconnect_p2ps()
216+
217+
self.log.info('Test BIP 37 for a node with fRelay = False')
218+
# Add peer but do not send version yet
219+
filter_peer_without_nrelay = self.nodes[0].add_p2p_connection(P2PBloomFilter(), send_version=False, wait_for_verack=False)
220+
# Send version with fRelay=False
221+
filter_peer_without_nrelay.wait_until(lambda: filter_peer_without_nrelay.is_connected, timeout=10)
222+
version_without_fRelay = msg_version()
223+
version_without_fRelay.nRelay = 0
224+
filter_peer_without_nrelay.send_message(version_without_fRelay)
225+
filter_peer_without_nrelay.wait_for_verack()
226+
assert not self.nodes[0].getpeerinfo()[0]['relaytxes']
227+
self.test_frelay_false(filter_peer_without_nrelay)
228+
self.test_filter(filter_peer_without_nrelay)
229+
230+
self.log.info('Test msg_mempool')
231+
self.test_msg_mempool()
156232

157233
if __name__ == '__main__':
158234
FilterTest().main()

test/functional/p2p_mempool.py

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2015-2018 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Test invalid p2p messages for nodes with bloom filters disabled.
6+
7+
Test that, when bloom filters are not enabled, nodes are disconnected if:
8+
1. They send a p2p mempool message
9+
2. They send a p2p filterload message
10+
3. They send a p2p filteradd message
11+
"""
12+
13+
from test_framework.messages import msg_mempool, msg_filteradd, msg_filterload
14+
from test_framework.mininode import P2PInterface
15+
from test_framework.test_framework import BitcoinTestFramework
16+
from test_framework.util import assert_equal
17+
18+
19+
class P2PNobloomfilterMessages(BitcoinTestFramework):
20+
def set_test_params(self):
21+
self.setup_clean_chain = True
22+
self.num_nodes = 1
23+
self.extra_args = [["-peerbloomfilters=0"]]
24+
25+
def test_message_causes_disconnect(self, message):
26+
# Add a p2p connection that sends a message and check that it disconnects
27+
peer = self.nodes[0].add_p2p_connection(P2PInterface())
28+
peer.send_message(message)
29+
peer.wait_for_disconnect()
30+
assert_equal(len(self.nodes[0].getpeerinfo()), 0)
31+
32+
def run_test(self):
33+
self.log.info("Test that node is disconnected if it sends mempool message")
34+
self.test_message_causes_disconnect(msg_mempool())
35+
36+
self.log.info("Test that node is disconnected if it sends filterload message")
37+
self.test_message_causes_disconnect(msg_filterload())
38+
39+
self.log.info("Test that node is disconnected if it sends filteradd message")
40+
self.test_message_causes_disconnect(msg_filteradd(data=b'\xcc'))
41+
42+
if __name__ == '__main__':
43+
P2PNobloomfilterMessages().main()

test/functional/test_runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@
164164
'wallet_keypool.py',
165165
'wallet_keypool.py --descriptors',
166166
'wallet_descriptor.py',
167-
'p2p_mempool.py',
167+
'p2p_nobloomfilter_messages.py',
168168
'p2p_filter.py',
169169
'rpc_setban.py',
170170
'p2p_blocksonly.py',

0 commit comments

Comments
 (0)