24
24
assert_equal ,
25
25
)
26
26
27
- class CFiltersClient (P2PInterface ):
27
+ class FiltersClient (P2PInterface ):
28
28
def __init__ (self ):
29
29
super ().__init__ ()
30
30
# Store the cfilters received.
@@ -39,6 +39,7 @@ def on_cfilter(self, message):
39
39
"""Store cfilters received in a list."""
40
40
self .cfilters .append (message )
41
41
42
+
42
43
class CompactFiltersTest (BitcoinTestFramework ):
43
44
def set_test_params (self ):
44
45
self .setup_clean_chain = True
@@ -51,8 +52,8 @@ def set_test_params(self):
51
52
52
53
def run_test (self ):
53
54
# Node 0 supports COMPACT_FILTERS, node 1 does not.
54
- node0 = self .nodes [0 ].add_p2p_connection (CFiltersClient ())
55
- node1 = self .nodes [1 ].add_p2p_connection (CFiltersClient ())
55
+ peer_0 = self .nodes [0 ].add_p2p_connection (FiltersClient ())
56
+ peer_1 = self .nodes [1 ].add_p2p_connection (FiltersClient ())
56
57
57
58
# Nodes 0 & 1 share the same first 999 blocks in the chain.
58
59
self .nodes [0 ].generate (999 )
@@ -61,16 +62,16 @@ def run_test(self):
61
62
# Stale blocks by disconnecting nodes 0 & 1, mining, then reconnecting
62
63
self .disconnect_nodes (0 , 1 )
63
64
64
- self .nodes [0 ].generate (1 )
65
- self .wait_until ( lambda : self . nodes [0 ].getblockcount () == 1000 )
66
- stale_block_hash = self .nodes [0 ].getblockhash ( 1000 )
65
+ stale_block_hash = self .nodes [0 ].generate (1 )[ 0 ]
66
+ self .nodes [0 ].syncwithvalidationinterfacequeue ( )
67
+ assert_equal ( self .nodes [0 ].getblockcount (), 1000 )
67
68
68
69
self .nodes [1 ].generate (1001 )
69
- self . wait_until ( lambda : self .nodes [1 ].getblockcount () == 2000 )
70
+ assert_equal ( self .nodes [1 ].getblockcount (), 2000 )
70
71
71
72
# Check that nodes have signalled NODE_COMPACT_FILTERS correctly.
72
- assert node0 .nServices & NODE_COMPACT_FILTERS != 0
73
- assert node1 .nServices & NODE_COMPACT_FILTERS == 0
73
+ assert peer_0 .nServices & NODE_COMPACT_FILTERS != 0
74
+ assert peer_1 .nServices & NODE_COMPACT_FILTERS == 0
74
75
75
76
# Check that the localservices is as expected.
76
77
assert int (self .nodes [0 ].getnetworkinfo ()['localservices' ], 16 ) & NODE_COMPACT_FILTERS != 0
@@ -79,17 +80,18 @@ def run_test(self):
79
80
self .log .info ("get cfcheckpt on chain to be re-orged out." )
80
81
request = msg_getcfcheckpt (
81
82
filter_type = FILTER_TYPE_BASIC ,
82
- stop_hash = int (stale_block_hash , 16 )
83
+ stop_hash = int (stale_block_hash , 16 ),
83
84
)
84
- node0 .send_and_ping (message = request )
85
- response = node0 .last_message ['cfcheckpt' ]
85
+ peer_0 .send_and_ping (message = request )
86
+ response = peer_0 .last_message ['cfcheckpt' ]
86
87
assert_equal (response .filter_type , request .filter_type )
87
88
assert_equal (response .stop_hash , request .stop_hash )
88
89
assert_equal (len (response .headers ), 1 )
89
90
90
91
self .log .info ("Reorg node 0 to a new chain." )
91
92
self .connect_nodes (0 , 1 )
92
93
self .sync_blocks (timeout = 600 )
94
+ self .nodes [0 ].syncwithvalidationinterfacequeue ()
93
95
94
96
main_block_hash = self .nodes [0 ].getblockhash (1000 )
95
97
assert main_block_hash != stale_block_hash , "node 0 chain did not reorganize"
@@ -98,74 +100,73 @@ def run_test(self):
98
100
tip_hash = self .nodes [0 ].getbestblockhash ()
99
101
request = msg_getcfcheckpt (
100
102
filter_type = FILTER_TYPE_BASIC ,
101
- stop_hash = int (tip_hash , 16 )
103
+ stop_hash = int (tip_hash , 16 ),
102
104
)
103
- node0 .send_and_ping (request )
104
- response = node0 .last_message ['cfcheckpt' ]
105
+ peer_0 .send_and_ping (request )
106
+ response = peer_0 .last_message ['cfcheckpt' ]
105
107
assert_equal (response .filter_type , request .filter_type )
106
108
assert_equal (response .stop_hash , request .stop_hash )
107
109
108
110
main_cfcheckpt = self .nodes [0 ].getblockfilter (main_block_hash , 'basic' )['header' ]
109
111
tip_cfcheckpt = self .nodes [0 ].getblockfilter (tip_hash , 'basic' )['header' ]
110
112
assert_equal (
111
113
response .headers ,
112
- [int (header , 16 ) for header in (main_cfcheckpt , tip_cfcheckpt )]
114
+ [int (header , 16 ) for header in (main_cfcheckpt , tip_cfcheckpt )],
113
115
)
114
116
115
117
self .log .info ("Check that peers can fetch cfcheckpt on stale chain." )
116
118
request = msg_getcfcheckpt (
117
119
filter_type = FILTER_TYPE_BASIC ,
118
- stop_hash = int (stale_block_hash , 16 )
120
+ stop_hash = int (stale_block_hash , 16 ),
119
121
)
120
- node0 .send_and_ping (request )
121
- response = node0 .last_message ['cfcheckpt' ]
122
+ peer_0 .send_and_ping (request )
123
+ response = peer_0 .last_message ['cfcheckpt' ]
122
124
123
125
stale_cfcheckpt = self .nodes [0 ].getblockfilter (stale_block_hash , 'basic' )['header' ]
124
126
assert_equal (
125
127
response .headers ,
126
- [int (header , 16 ) for header in (stale_cfcheckpt ,)]
128
+ [int (header , 16 ) for header in (stale_cfcheckpt , )],
127
129
)
128
130
129
131
self .log .info ("Check that peers can fetch cfheaders on active chain." )
130
132
request = msg_getcfheaders (
131
133
filter_type = FILTER_TYPE_BASIC ,
132
134
start_height = 1 ,
133
- stop_hash = int (main_block_hash , 16 )
135
+ stop_hash = int (main_block_hash , 16 ),
134
136
)
135
- node0 .send_and_ping (request )
136
- response = node0 .last_message ['cfheaders' ]
137
+ peer_0 .send_and_ping (request )
138
+ response = peer_0 .last_message ['cfheaders' ]
137
139
main_cfhashes = response .hashes
138
140
assert_equal (len (main_cfhashes ), 1000 )
139
141
assert_equal (
140
142
compute_last_header (response .prev_header , response .hashes ),
141
- int (main_cfcheckpt , 16 )
143
+ int (main_cfcheckpt , 16 ),
142
144
)
143
145
144
146
self .log .info ("Check that peers can fetch cfheaders on stale chain." )
145
147
request = msg_getcfheaders (
146
148
filter_type = FILTER_TYPE_BASIC ,
147
149
start_height = 1 ,
148
- stop_hash = int (stale_block_hash , 16 )
150
+ stop_hash = int (stale_block_hash , 16 ),
149
151
)
150
- node0 .send_and_ping (request )
151
- response = node0 .last_message ['cfheaders' ]
152
+ peer_0 .send_and_ping (request )
153
+ response = peer_0 .last_message ['cfheaders' ]
152
154
stale_cfhashes = response .hashes
153
155
assert_equal (len (stale_cfhashes ), 1000 )
154
156
assert_equal (
155
157
compute_last_header (response .prev_header , response .hashes ),
156
- int (stale_cfcheckpt , 16 )
158
+ int (stale_cfcheckpt , 16 ),
157
159
)
158
160
159
161
self .log .info ("Check that peers can fetch cfilters." )
160
162
stop_hash = self .nodes [0 ].getblockhash (10 )
161
163
request = msg_getcfilters (
162
164
filter_type = FILTER_TYPE_BASIC ,
163
165
start_height = 1 ,
164
- stop_hash = int (stop_hash , 16 )
166
+ stop_hash = int (stop_hash , 16 ),
165
167
)
166
- node0 .send_message (request )
167
- node0 .sync_with_ping ()
168
- response = node0 .pop_cfilters ()
168
+ peer_0 .send_and_ping (request )
169
+ response = peer_0 .pop_cfilters ()
169
170
assert_equal (len (response ), 10 )
170
171
171
172
self .log .info ("Check that cfilter responses are correct." )
@@ -180,11 +181,10 @@ def run_test(self):
180
181
request = msg_getcfilters (
181
182
filter_type = FILTER_TYPE_BASIC ,
182
183
start_height = 1000 ,
183
- stop_hash = int (stale_block_hash , 16 )
184
+ stop_hash = int (stale_block_hash , 16 ),
184
185
)
185
- node0 .send_message (request )
186
- node0 .sync_with_ping ()
187
- response = node0 .pop_cfilters ()
186
+ peer_0 .send_and_ping (request )
187
+ response = peer_0 .pop_cfilters ()
188
188
assert_equal (len (response ), 1 )
189
189
190
190
cfilter = response [0 ]
@@ -197,42 +197,42 @@ def run_test(self):
197
197
requests = [
198
198
msg_getcfcheckpt (
199
199
filter_type = FILTER_TYPE_BASIC ,
200
- stop_hash = int (main_block_hash , 16 )
200
+ stop_hash = int (main_block_hash , 16 ),
201
201
),
202
202
msg_getcfheaders (
203
203
filter_type = FILTER_TYPE_BASIC ,
204
204
start_height = 1000 ,
205
- stop_hash = int (main_block_hash , 16 )
205
+ stop_hash = int (main_block_hash , 16 ),
206
206
),
207
207
msg_getcfilters (
208
208
filter_type = FILTER_TYPE_BASIC ,
209
209
start_height = 1000 ,
210
- stop_hash = int (main_block_hash , 16 )
210
+ stop_hash = int (main_block_hash , 16 ),
211
211
),
212
212
]
213
213
for request in requests :
214
- node1 = self .nodes [1 ].add_p2p_connection (P2PInterface ())
215
- node1 .send_message (request )
216
- node1 .wait_for_disconnect ()
214
+ peer_1 = self .nodes [1 ].add_p2p_connection (P2PInterface ())
215
+ peer_1 .send_message (request )
216
+ peer_1 .wait_for_disconnect ()
217
217
218
218
self .log .info ("Check that invalid requests result in disconnection." )
219
219
requests = [
220
220
# Requesting too many filters results in disconnection.
221
221
msg_getcfilters (
222
222
filter_type = FILTER_TYPE_BASIC ,
223
223
start_height = 0 ,
224
- stop_hash = int (main_block_hash , 16 )
224
+ stop_hash = int (main_block_hash , 16 ),
225
225
),
226
226
# Requesting too many filter headers results in disconnection.
227
227
msg_getcfheaders (
228
228
filter_type = FILTER_TYPE_BASIC ,
229
229
start_height = 0 ,
230
- stop_hash = int (tip_hash , 16 )
230
+ stop_hash = int (tip_hash , 16 ),
231
231
),
232
232
# Requesting unknown filter type results in disconnection.
233
233
msg_getcfcheckpt (
234
234
filter_type = 255 ,
235
- stop_hash = int (main_block_hash , 16 )
235
+ stop_hash = int (main_block_hash , 16 ),
236
236
),
237
237
# Requesting unknown hash results in disconnection.
238
238
msg_getcfcheckpt (
@@ -241,9 +241,10 @@ def run_test(self):
241
241
),
242
242
]
243
243
for request in requests :
244
- node0 = self .nodes [0 ].add_p2p_connection (P2PInterface ())
245
- node0 .send_message (request )
246
- node0 .wait_for_disconnect ()
244
+ peer_0 = self .nodes [0 ].add_p2p_connection (P2PInterface ())
245
+ peer_0 .send_message (request )
246
+ peer_0 .wait_for_disconnect ()
247
+
247
248
248
249
def compute_last_header (prev_header , hashes ):
249
250
"""Compute the last filter header from a starting header and a sequence of filter hashes."""
@@ -252,5 +253,6 @@ def compute_last_header(prev_header, hashes):
252
253
header = hash256 (ser_uint256 (filter_hash ) + header )
253
254
return uint256_from_str (header )
254
255
256
+
255
257
if __name__ == '__main__' :
256
258
CompactFiltersTest ().main ()
0 commit comments