Skip to content

Commit 9e36067

Browse files
jimpojnewbery
authored andcommitted
[test] Add test for cfilters.
1 parent 11106a4 commit 9e36067

File tree

3 files changed

+127
-7
lines changed

3 files changed

+127
-7
lines changed

test/functional/p2p_blockfilters.py

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
hash256,
1414
msg_getcfcheckpt,
1515
msg_getcfheaders,
16+
msg_getcfilters,
1617
ser_uint256,
1718
uint256_from_str,
1819
)
@@ -25,6 +26,21 @@
2526
wait_until,
2627
)
2728

29+
class CFiltersClient(P2PInterface):
30+
def __init__(self):
31+
super().__init__()
32+
# Store the cfilters received.
33+
self.cfilters = []
34+
35+
def pop_cfilters(self):
36+
cfilters = self.cfilters
37+
self.cfilters = []
38+
return cfilters
39+
40+
def on_cfilter(self, message):
41+
"""Store cfilters received in a list."""
42+
self.cfilters.append(message)
43+
2844
class CompactFiltersTest(BitcoinTestFramework):
2945
def set_test_params(self):
3046
self.setup_clean_chain = True
@@ -37,8 +53,8 @@ def set_test_params(self):
3753

3854
def run_test(self):
3955
# Node 0 supports COMPACT_FILTERS, node 1 does not.
40-
node0 = self.nodes[0].add_p2p_connection(P2PInterface())
41-
node1 = self.nodes[1].add_p2p_connection(P2PInterface())
56+
node0 = self.nodes[0].add_p2p_connection(CFiltersClient())
57+
node1 = self.nodes[1].add_p2p_connection(CFiltersClient())
4258

4359
# Nodes 0 & 1 share the same first 999 blocks in the chain.
4460
self.nodes[0].generate(999)
@@ -112,7 +128,8 @@ def run_test(self):
112128
)
113129
node0.send_and_ping(request)
114130
response = node0.last_message['cfheaders']
115-
assert_equal(len(response.hashes), 1000)
131+
main_cfhashes = response.hashes
132+
assert_equal(len(main_cfhashes), 1000)
116133
assert_equal(
117134
compute_last_header(response.prev_header, response.hashes),
118135
int(main_cfcheckpt, 16)
@@ -126,12 +143,50 @@ def run_test(self):
126143
)
127144
node0.send_and_ping(request)
128145
response = node0.last_message['cfheaders']
129-
assert_equal(len(response.hashes), 1000)
146+
stale_cfhashes = response.hashes
147+
assert_equal(len(stale_cfhashes), 1000)
130148
assert_equal(
131149
compute_last_header(response.prev_header, response.hashes),
132150
int(stale_cfcheckpt, 16)
133151
)
134152

153+
self.log.info("Check that peers can fetch cfilters.")
154+
stop_hash = self.nodes[0].getblockhash(10)
155+
request = msg_getcfilters(
156+
filter_type=FILTER_TYPE_BASIC,
157+
start_height=1,
158+
stop_hash=int(stop_hash, 16)
159+
)
160+
node0.send_message(request)
161+
node0.sync_with_ping()
162+
response = node0.pop_cfilters()
163+
assert_equal(len(response), 10)
164+
165+
self.log.info("Check that cfilter responses are correct.")
166+
for cfilter, cfhash, height in zip(response, main_cfhashes, range(1, 11)):
167+
block_hash = self.nodes[0].getblockhash(height)
168+
assert_equal(cfilter.filter_type, FILTER_TYPE_BASIC)
169+
assert_equal(cfilter.block_hash, int(block_hash, 16))
170+
computed_cfhash = uint256_from_str(hash256(cfilter.filter_data))
171+
assert_equal(computed_cfhash, cfhash)
172+
173+
self.log.info("Check that peers can fetch cfilters for stale blocks.")
174+
request = msg_getcfilters(
175+
filter_type=FILTER_TYPE_BASIC,
176+
start_height=1000,
177+
stop_hash=int(stale_block_hash, 16)
178+
)
179+
node0.send_message(request)
180+
node0.sync_with_ping()
181+
response = node0.pop_cfilters()
182+
assert_equal(len(response), 1)
183+
184+
cfilter = response[0]
185+
assert_equal(cfilter.filter_type, FILTER_TYPE_BASIC)
186+
assert_equal(cfilter.block_hash, int(stale_block_hash, 16))
187+
computed_cfhash = uint256_from_str(hash256(cfilter.filter_data))
188+
assert_equal(computed_cfhash, stale_cfhashes[999])
189+
135190
self.log.info("Requests to node 1 without NODE_COMPACT_FILTERS results in disconnection.")
136191
requests = [
137192
msg_getcfcheckpt(
@@ -143,6 +198,11 @@ def run_test(self):
143198
start_height=1000,
144199
stop_hash=int(main_block_hash, 16)
145200
),
201+
msg_getcfilters(
202+
filter_type=FILTER_TYPE_BASIC,
203+
start_height=1000,
204+
stop_hash=int(main_block_hash, 16)
205+
),
146206
]
147207
for request in requests:
148208
node1 = self.nodes[1].add_p2p_connection(P2PInterface())
@@ -151,6 +211,12 @@ def run_test(self):
151211

152212
self.log.info("Check that invalid requests result in disconnection.")
153213
requests = [
214+
# Requesting too many filters results in disconnection.
215+
msg_getcfilters(
216+
filter_type=FILTER_TYPE_BASIC,
217+
start_height=0,
218+
stop_hash=int(main_block_hash, 16)
219+
),
154220
# Requesting too many filter headers results in disconnection.
155221
msg_getcfheaders(
156222
filter_type=FILTER_TYPE_BASIC,

test/functional/test_framework/messages.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,6 +1516,57 @@ class msg_no_witness_blocktxn(msg_blocktxn):
15161516
def serialize(self):
15171517
return self.block_transactions.serialize(with_witness=False)
15181518

1519+
1520+
class msg_getcfilters:
1521+
__slots__ = ("filter_type", "start_height", "stop_hash")
1522+
msgtype = b"getcfilters"
1523+
1524+
def __init__(self, filter_type, start_height, stop_hash):
1525+
self.filter_type = filter_type
1526+
self.start_height = start_height
1527+
self.stop_hash = stop_hash
1528+
1529+
def deserialize(self, f):
1530+
self.filter_type = struct.unpack("<B", f.read(1))[0]
1531+
self.start_height = struct.unpack("<I", f.read(4))[0]
1532+
self.stop_hash = deser_uint256(f)
1533+
1534+
def serialize(self):
1535+
r = b""
1536+
r += struct.pack("<B", self.filter_type)
1537+
r += struct.pack("<I", self.start_height)
1538+
r += ser_uint256(self.stop_hash)
1539+
return r
1540+
1541+
def __repr__(self):
1542+
return "msg_getcfilters(filter_type={:#x}, start_height={}, stop_hash={:x})".format(
1543+
self.filter_type, self.start_height, self.stop_hash)
1544+
1545+
class msg_cfilter:
1546+
__slots__ = ("filter_type", "block_hash", "filter_data")
1547+
msgtype = b"cfilter"
1548+
1549+
def __init__(self, filter_type=None, block_hash=None, filter_data=None):
1550+
self.filter_type = filter_type
1551+
self.block_hash = block_hash
1552+
self.filter_data = filter_data
1553+
1554+
def deserialize(self, f):
1555+
self.filter_type = struct.unpack("<B", f.read(1))[0]
1556+
self.block_hash = deser_uint256(f)
1557+
self.filter_data = deser_string(f)
1558+
1559+
def serialize(self):
1560+
r = b""
1561+
r += struct.pack("<B", self.filter_type)
1562+
r += ser_uint256(self.block_hash)
1563+
r += ser_string(self.filter_data)
1564+
return r
1565+
1566+
def __repr__(self):
1567+
return "msg_cfilter(filter_type={:#x}, block_hash={:x})".format(
1568+
self.filter_type, self.block_hash)
1569+
15191570
class msg_getcfheaders:
15201571
__slots__ = ("filter_type", "start_height", "stop_hash")
15211572
msgtype = b"getcfheaders"

test/functional/test_framework/mininode.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131
msg_block,
3232
MSG_BLOCK,
3333
msg_blocktxn,
34-
msg_cfheaders,
3534
msg_cfcheckpt,
35+
msg_cfheaders,
36+
msg_cfilter,
3637
msg_cmpctblock,
3738
msg_feefilter,
3839
msg_filteradd,
@@ -69,8 +70,9 @@
6970
b"addr": msg_addr,
7071
b"block": msg_block,
7172
b"blocktxn": msg_blocktxn,
72-
b"cfheaders": msg_cfheaders,
7373
b"cfcheckpt": msg_cfcheckpt,
74+
b"cfheaders": msg_cfheaders,
75+
b"cfilter": msg_cfilter,
7476
b"cmpctblock": msg_cmpctblock,
7577
b"feefilter": msg_feefilter,
7678
b"filteradd": msg_filteradd,
@@ -332,8 +334,9 @@ def on_close(self):
332334
def on_addr(self, message): pass
333335
def on_block(self, message): pass
334336
def on_blocktxn(self, message): pass
335-
def on_cfheaders(self, message): pass
336337
def on_cfcheckpt(self, message): pass
338+
def on_cfheaders(self, message): pass
339+
def on_cfilter(self, message): pass
337340
def on_cmpctblock(self, message): pass
338341
def on_feefilter(self, message): pass
339342
def on_filteradd(self, message): pass

0 commit comments

Comments
 (0)