@@ -15,7 +15,7 @@ use std::ops::DerefMut;
15
15
/// [`ChainPoller`]: ../struct.ChainPoller.html
16
16
pub trait Poll {
17
17
/// Returns a chain tip in terms of its relationship to the provided chain tip.
18
- fn poll_chain_tip < ' a > ( & ' a mut self , best_known_chain_tip : ValidatedBlockHeader ) ->
18
+ fn poll_chain_tip < ' a > ( & ' a mut self , best_known_chain_tip : Option < ValidatedBlockHeader > ) ->
19
19
AsyncBlockSourceResult < ' a , ChainTip > ;
20
20
21
21
/// Returns the header that preceded the given header in the chain.
@@ -174,22 +174,28 @@ impl<B: DerefMut<Target=T> + Sized + Sync + Send, T: BlockSource> ChainPoller<B,
174
174
}
175
175
176
176
impl < B : DerefMut < Target =T > + Sized + Sync + Send , T : BlockSource > Poll for ChainPoller < B , T > {
177
- fn poll_chain_tip < ' a > ( & ' a mut self , best_known_chain_tip : ValidatedBlockHeader ) ->
177
+ fn poll_chain_tip < ' a > ( & ' a mut self , best_known_chain_tip : Option < ValidatedBlockHeader > ) ->
178
178
AsyncBlockSourceResult < ' a , ChainTip >
179
179
{
180
180
Box :: pin ( async move {
181
181
let ( block_hash, height) = self . block_source . get_best_block ( ) . await ?;
182
- if block_hash == best_known_chain_tip. header . block_hash ( ) {
183
- return Ok ( ChainTip :: Common ) ;
182
+ if let Some ( curr_tip) = best_known_chain_tip {
183
+ if block_hash == curr_tip. header . block_hash ( ) {
184
+ return Ok ( ChainTip :: Common ) ;
185
+ }
184
186
}
185
187
186
188
let chain_tip = self . block_source
187
189
. get_header ( & block_hash, height) . await ?
188
190
. validate ( block_hash) ?;
189
- if chain_tip. chainwork > best_known_chain_tip. chainwork {
190
- Ok ( ChainTip :: Better ( chain_tip) )
191
+ if let Some ( curr_tip) = best_known_chain_tip {
192
+ if chain_tip. chainwork > curr_tip. chainwork {
193
+ Ok ( ChainTip :: Better ( chain_tip) )
194
+ } else {
195
+ Ok ( ChainTip :: Worse ( chain_tip) )
196
+ }
191
197
} else {
192
- Ok ( ChainTip :: Worse ( chain_tip) )
198
+ Ok ( ChainTip :: Better ( chain_tip) )
193
199
}
194
200
} )
195
201
}
@@ -234,7 +240,7 @@ mod tests {
234
240
#[ tokio:: test]
235
241
async fn poll_empty_chain ( ) {
236
242
let mut chain = Blockchain :: default ( ) . with_height ( 0 ) ;
237
- let best_known_chain_tip = chain. tip ( ) ;
243
+ let best_known_chain_tip = Some ( chain. tip ( ) ) ;
238
244
chain. disconnect_tip ( ) ;
239
245
240
246
let mut poller = ChainPoller :: new ( & mut chain, Network :: Bitcoin ) ;
@@ -250,7 +256,7 @@ mod tests {
250
256
#[ tokio:: test]
251
257
async fn poll_chain_without_headers ( ) {
252
258
let mut chain = Blockchain :: default ( ) . with_height ( 1 ) . without_headers ( ) ;
253
- let best_known_chain_tip = chain. at_height ( 0 ) ;
259
+ let best_known_chain_tip = Some ( chain. at_height ( 0 ) ) ;
254
260
255
261
let mut poller = ChainPoller :: new ( & mut chain, Network :: Bitcoin ) ;
256
262
match poller. poll_chain_tip ( best_known_chain_tip) . await {
@@ -265,7 +271,7 @@ mod tests {
265
271
#[ tokio:: test]
266
272
async fn poll_chain_with_invalid_pow ( ) {
267
273
let mut chain = Blockchain :: default ( ) . with_height ( 1 ) ;
268
- let best_known_chain_tip = chain. at_height ( 0 ) ;
274
+ let best_known_chain_tip = Some ( chain. at_height ( 0 ) ) ;
269
275
270
276
// Invalidate the tip by changing its target.
271
277
chain. blocks . last_mut ( ) . unwrap ( ) . header . bits =
@@ -284,7 +290,7 @@ mod tests {
284
290
#[ tokio:: test]
285
291
async fn poll_chain_with_malformed_headers ( ) {
286
292
let mut chain = Blockchain :: default ( ) . with_height ( 1 ) . malformed_headers ( ) ;
287
- let best_known_chain_tip = chain. at_height ( 0 ) ;
293
+ let best_known_chain_tip = Some ( chain. at_height ( 0 ) ) ;
288
294
289
295
let mut poller = ChainPoller :: new ( & mut chain, Network :: Bitcoin ) ;
290
296
match poller. poll_chain_tip ( best_known_chain_tip) . await {
@@ -299,7 +305,7 @@ mod tests {
299
305
#[ tokio:: test]
300
306
async fn poll_chain_with_common_tip ( ) {
301
307
let mut chain = Blockchain :: default ( ) . with_height ( 0 ) ;
302
- let best_known_chain_tip = chain. tip ( ) ;
308
+ let best_known_chain_tip = Some ( chain. tip ( ) ) ;
303
309
304
310
let mut poller = ChainPoller :: new ( & mut chain, Network :: Bitcoin ) ;
305
311
match poller. poll_chain_tip ( best_known_chain_tip) . await {
@@ -319,7 +325,7 @@ mod tests {
319
325
assert_eq ! ( best_known_chain_tip. chainwork, worse_chain_tip. chainwork) ;
320
326
321
327
let mut poller = ChainPoller :: new ( & mut chain, Network :: Bitcoin ) ;
322
- match poller. poll_chain_tip ( best_known_chain_tip) . await {
328
+ match poller. poll_chain_tip ( Some ( best_known_chain_tip) ) . await {
323
329
Err ( e) => panic ! ( "Unexpected error: {:?}" , e) ,
324
330
Ok ( tip) => assert_eq ! ( tip, ChainTip :: Worse ( worse_chain_tip) ) ,
325
331
}
@@ -328,7 +334,7 @@ mod tests {
328
334
#[ tokio:: test]
329
335
async fn poll_chain_with_worse_tip ( ) {
330
336
let mut chain = Blockchain :: default ( ) . with_height ( 1 ) ;
331
- let best_known_chain_tip = chain. tip ( ) ;
337
+ let best_known_chain_tip = Some ( chain. tip ( ) ) ;
332
338
333
339
chain. disconnect_tip ( ) ;
334
340
let worse_chain_tip = chain. tip ( ) ;
@@ -348,9 +354,20 @@ mod tests {
348
354
let better_chain_tip = chain. tip ( ) ;
349
355
350
356
let mut poller = ChainPoller :: new ( & mut chain, Network :: Bitcoin ) ;
351
- match poller. poll_chain_tip ( best_known_chain_tip) . await {
357
+ match poller. poll_chain_tip ( Some ( best_known_chain_tip) ) . await {
352
358
Err ( e) => panic ! ( "Unexpected error: {:?}" , e) ,
353
359
Ok ( tip) => assert_eq ! ( tip, ChainTip :: Better ( better_chain_tip) ) ,
354
360
}
355
361
}
362
+
363
+ #[ tokio:: test]
364
+ async fn poll_chain_no_best_tip ( ) {
365
+ let mut chain = Blockchain :: default ( ) . with_height ( 1 ) ;
366
+ let chain_tip = chain. tip ( ) ;
367
+ let mut poller = ChainPoller :: new ( & mut chain, Network :: Bitcoin ) ;
368
+ match poller. poll_chain_tip ( None ) . await {
369
+ Err ( e) => panic ! ( "Unexpected error: {:?}" , e) ,
370
+ Ok ( tip) => assert_eq ! ( tip, ChainTip :: Better ( chain_tip) ) ,
371
+ }
372
+ }
356
373
}
0 commit comments