Skip to content

Commit cfdb6ba

Browse files
committed
test: use MiniWallet for p2p_filter.py
This test can now be run even with the Bitcoin Core wallet disabled.
1 parent 6fc2cd3 commit cfdb6ba

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

test/functional/p2p_filter.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from test_framework.messages import (
1010
CInv,
11+
COIN,
1112
MAX_BLOOM_FILTER_SIZE,
1213
MAX_BLOOM_HASH_FUNCS,
1314
MSG_BLOCK,
@@ -28,11 +29,15 @@
2829
)
2930
from test_framework.script import MAX_SCRIPT_ELEMENT_SIZE
3031
from test_framework.test_framework import BitcoinTestFramework
32+
from test_framework.wallet import (
33+
MiniWallet,
34+
random_p2wpkh,
35+
)
3136

3237

3338
class P2PBloomFilter(P2PInterface):
3439
# This is a P2SH watch-only wallet
35-
watch_script_pubkey = 'a914ffffffffffffffffffffffffffffffffffffffff87'
40+
watch_script_pubkey = bytes.fromhex('a914ffffffffffffffffffffffffffffffffffffffff87')
3641
# The initial filter (n=10, fp=0.000001) with just the above scriptPubKey added
3742
watch_filter_init = msg_filterload(
3843
data=
@@ -93,8 +98,9 @@ def set_test_params(self):
9398
'[email protected]', # immediate tx relay
9499
]]
95100

96-
def skip_test_if_missing_module(self):
97-
self.skip_if_no_wallet()
101+
def generatetoscriptpubkey(self, scriptpubkey):
102+
"""Helper to generate a single block to the given scriptPubKey."""
103+
return self.generatetodescriptor(self.nodes[0], 1, f'raw({scriptpubkey.hex()})')[0]
98104

99105
def test_size_limits(self, filter_peer):
100106
self.log.info('Check that too large filter is rejected')
@@ -130,8 +136,7 @@ def test_msg_mempool(self):
130136
filter_peer = P2PBloomFilter()
131137

132138
self.log.debug("Create a tx relevant to the peer before connecting")
133-
filter_address = self.nodes[0].decodescript(filter_peer.watch_script_pubkey)['address']
134-
txid = self.nodes[0].sendtoaddress(filter_address, 90)
139+
txid, _ = self.wallet.send_to(from_node=self.nodes[0], scriptPubKey=filter_peer.watch_script_pubkey, amount=9 * COIN)
135140

136141
self.log.debug("Send a mempool msg after connecting and check that the tx is received")
137142
self.nodes[0].add_p2p_connection(filter_peer)
@@ -142,8 +147,7 @@ def test_msg_mempool(self):
142147
def test_frelay_false(self, filter_peer):
143148
self.log.info("Check that a node with fRelay set to false does not receive invs until the filter is set")
144149
filter_peer.tx_received = False
145-
filter_address = self.nodes[0].decodescript(filter_peer.watch_script_pubkey)['address']
146-
self.nodes[0].sendtoaddress(filter_address, 90)
150+
self.wallet.send_to(from_node=self.nodes[0], scriptPubKey=filter_peer.watch_script_pubkey, amount=9 * COIN)
147151
# Sync to make sure the reason filter_peer doesn't receive the tx is not p2p delays
148152
filter_peer.sync_with_ping()
149153
assert not filter_peer.tx_received
@@ -156,45 +160,44 @@ def test_filter(self, filter_peer):
156160
filter_peer.send_and_ping(filter_peer.watch_filter_init)
157161
# If fRelay is not already True, sending filterload sets it to True
158162
assert self.nodes[0].getpeerinfo()[0]['relaytxes']
159-
filter_address = self.nodes[0].decodescript(filter_peer.watch_script_pubkey)['address']
160163

161164
self.log.info('Check that we receive merkleblock and tx if the filter matches a tx in a block')
162-
block_hash = self.generatetoaddress(self.nodes[0], 1, filter_address)[0]
165+
block_hash = self.generatetoscriptpubkey(filter_peer.watch_script_pubkey)
163166
txid = self.nodes[0].getblock(block_hash)['tx'][0]
164167
filter_peer.wait_for_merkleblock(block_hash)
165168
filter_peer.wait_for_tx(txid)
166169

167170
self.log.info('Check that we only receive a merkleblock if the filter does not match a tx in a block')
168171
filter_peer.tx_received = False
169-
block_hash = self.generatetoaddress(self.nodes[0], 1, self.nodes[0].getnewaddress())[0]
172+
block_hash = self.generatetoscriptpubkey(random_p2wpkh())
170173
filter_peer.wait_for_merkleblock(block_hash)
171174
assert not filter_peer.tx_received
172175

173176
self.log.info('Check that we not receive a tx if the filter does not match a mempool tx')
174177
filter_peer.merkleblock_received = False
175178
filter_peer.tx_received = False
176-
self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 90)
179+
self.wallet.send_to(from_node=self.nodes[0], scriptPubKey=random_p2wpkh(), amount=7 * COIN)
177180
filter_peer.sync_send_with_ping()
178181
assert not filter_peer.merkleblock_received
179182
assert not filter_peer.tx_received
180183

181184
self.log.info('Check that we receive a tx if the filter matches a mempool tx')
182185
filter_peer.merkleblock_received = False
183-
txid = self.nodes[0].sendtoaddress(filter_address, 90)
186+
txid, _ = self.wallet.send_to(from_node=self.nodes[0], scriptPubKey=filter_peer.watch_script_pubkey, amount=9 * COIN)
184187
filter_peer.wait_for_tx(txid)
185188
assert not filter_peer.merkleblock_received
186189

187190
self.log.info('Check that after deleting filter all txs get relayed again')
188191
filter_peer.send_and_ping(msg_filterclear())
189192
for _ in range(5):
190-
txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 7)
193+
txid, _ = self.wallet.send_to(from_node=self.nodes[0], scriptPubKey=random_p2wpkh(), amount=7 * COIN)
191194
filter_peer.wait_for_tx(txid)
192195

193196
self.log.info('Check that request for filtered blocks is ignored if no filter is set')
194197
filter_peer.merkleblock_received = False
195198
filter_peer.tx_received = False
196199
with self.nodes[0].assert_debug_log(expected_msgs=['received getdata']):
197-
block_hash = self.generatetoaddress(self.nodes[0], 1, self.nodes[0].getnewaddress())[0]
200+
block_hash = self.generatetoscriptpubkey(random_p2wpkh())
198201
filter_peer.wait_for_inv([CInv(MSG_BLOCK, int(block_hash, 16))])
199202
filter_peer.sync_with_ping()
200203
assert not filter_peer.merkleblock_received
@@ -210,6 +213,9 @@ def test_filter(self, filter_peer):
210213
self.nodes[0].disconnect_p2ps()
211214

212215
def run_test(self):
216+
self.wallet = MiniWallet(self.nodes[0])
217+
self.wallet.rescan_utxos()
218+
213219
filter_peer = self.nodes[0].add_p2p_connection(P2PBloomFilter())
214220
self.log.info('Test filter size limits')
215221
self.test_size_limits(filter_peer)

0 commit comments

Comments
 (0)