@@ -67,6 +67,7 @@ const NOT_IBD_BLOCK_FETCH_INTERVAL: Duration = Duration::from_millis(200);
67
67
68
68
#[ derive( Copy , Clone ) ]
69
69
enum CanStart {
70
+ FetchToTarget ( BlockNumber ) ,
70
71
Ready ,
71
72
MinWorkNotReach ,
72
73
AssumeValidNotFound ,
@@ -90,25 +91,34 @@ impl BlockFetchCMD {
90
91
fn process_fetch_cmd ( & mut self , cmd : FetchCMD ) {
91
92
let FetchCMD { peers, ibd_state } : FetchCMD = cmd;
92
93
93
- match self . can_start ( ) {
94
- CanStart :: Ready => {
95
- for peer in peers {
96
- if ckb_stop_handler:: has_received_stop_signal ( ) {
97
- return ;
98
- }
94
+ let fetch_blocks_fn = |cmd : & mut BlockFetchCMD , assume_target : BlockNumber | {
95
+ for peer in peers {
96
+ if ckb_stop_handler:: has_received_stop_signal ( ) {
97
+ return ;
98
+ }
99
99
100
- if let Some ( fetch) =
101
- BlockFetcher :: new ( Arc :: clone ( & self . sync_shared ) , peer, ibd_state) . fetch ( )
102
- {
103
- for item in fetch {
104
- if ckb_stop_handler:: has_received_stop_signal ( ) {
105
- return ;
106
- }
107
- BlockFetchCMD :: send_getblocks ( item, & self . p2p_control , peer) ;
100
+ let mut fetch_end: BlockNumber = u64:: MAX ;
101
+ if assume_target != 0 {
102
+ fetch_end = assume_target
103
+ }
104
+
105
+ if let Some ( fetch) =
106
+ BlockFetcher :: new ( Arc :: clone ( & cmd. sync_shared ) , peer, ibd_state)
107
+ . fetch ( fetch_end)
108
+ {
109
+ for item in fetch {
110
+ if ckb_stop_handler:: has_received_stop_signal ( ) {
111
+ return ;
108
112
}
113
+ BlockFetchCMD :: send_getblocks ( item, & cmd. p2p_control , peer) ;
109
114
}
110
115
}
111
116
}
117
+ } ;
118
+
119
+ match self . can_start ( ) {
120
+ CanStart :: FetchToTarget ( assume_target) => fetch_blocks_fn ( self , assume_target) ,
121
+ CanStart :: Ready => fetch_blocks_fn ( self , BlockNumber :: MAX ) ,
112
122
CanStart :: MinWorkNotReach => {
113
123
let best_known = self . sync_shared . state ( ) . shared_best_header_ref ( ) ;
114
124
let number = best_known. number ( ) ;
@@ -129,8 +139,9 @@ impl BlockFetchCMD {
129
139
let best_known = state. shared_best_header_ref ( ) ;
130
140
let number = best_known. number ( ) ;
131
141
let assume_valid_target: Byte32 = shared
132
- . assume_valid_target ( )
142
+ . assume_valid_targets ( )
133
143
. as_ref ( )
144
+ . and_then ( |targets| targets. first ( ) )
134
145
. map ( Pack :: pack)
135
146
. expect ( "assume valid target must exist" ) ;
136
147
@@ -234,15 +245,23 @@ impl BlockFetchCMD {
234
245
} ;
235
246
236
247
let assume_valid_target_find = |flag : & mut CanStart | {
237
- let mut assume_valid_target = shared. assume_valid_target ( ) ;
238
- if let Some ( ref target) = * assume_valid_target {
239
- match shared. header_map ( ) . get ( & target. pack ( ) ) {
248
+ let mut assume_valid_targets = shared. assume_valid_targets ( ) ;
249
+ if let Some ( ref targets) = * assume_valid_targets {
250
+ if targets. is_empty ( ) {
251
+ assume_valid_targets. take ( ) ;
252
+ * flag = CanStart :: Ready ;
253
+ return ;
254
+ }
255
+ let first_target = targets
256
+ . first ( )
257
+ . expect ( "has checked targets is not empty, assume valid target must exist" ) ;
258
+ match shared. header_map ( ) . get ( & first_target. pack ( ) ) {
240
259
Some ( header) => {
241
- * flag = CanStart :: Ready ;
242
- info ! ( "assume valid target found in header_map; CKB will start fetch blocks now" ) ;
260
+ * flag = CanStart :: FetchToTarget ( header . number ( ) ) ;
261
+ info ! ( "assume valid target found in header_map; CKB will start fetch blocks to {:?} now" , header . number_and_hash ( ) ) ;
243
262
// Blocks that are no longer in the scope of ibd must be forced to verify
244
263
if unix_time_as_millis ( ) . saturating_sub ( header. timestamp ( ) ) < MAX_TIP_AGE {
245
- assume_valid_target . take ( ) ;
264
+ assume_valid_targets . take ( ) ;
246
265
warn ! ( "the duration gap between 'assume valid target' and 'now' is less than 24h; CKB will ignore the specified assume valid target and do full verification from now on" ) ;
247
266
}
248
267
}
@@ -254,7 +273,7 @@ impl BlockFetchCMD {
254
273
{
255
274
warn ! ( "the duration gap between 'shared_best_header' and 'now' is less than 24h, but CKB haven't found the assume valid target in header_map; CKB will ignore the specified assume valid target and do full verification from now on" ) ;
256
275
* flag = CanStart :: Ready ;
257
- assume_valid_target . take ( ) ;
276
+ assume_valid_targets . take ( ) ;
258
277
}
259
278
}
260
279
}
@@ -264,7 +283,7 @@ impl BlockFetchCMD {
264
283
} ;
265
284
266
285
match self . can_start {
267
- CanStart :: Ready => self . can_start ,
286
+ CanStart :: FetchToTarget ( _ ) | CanStart :: Ready => self . can_start ,
268
287
CanStart :: MinWorkNotReach => {
269
288
min_work_reach ( & mut self . can_start ) ;
270
289
if let CanStart :: AssumeValidNotFound = self . can_start {
@@ -453,7 +472,7 @@ impl Synchronizer {
453
472
peer : PeerIndex ,
454
473
ibd : IBDState ,
455
474
) -> Option < Vec < Vec < packed:: Byte32 > > > {
456
- BlockFetcher :: new ( Arc :: clone ( & self . shared ) , peer, ibd) . fetch ( )
475
+ BlockFetcher :: new ( Arc :: clone ( & self . shared ) , peer, ibd) . fetch ( BlockNumber :: MAX )
457
476
}
458
477
459
478
pub ( crate ) fn on_connected ( & self , nc : & dyn CKBProtocolContext , peer : PeerIndex ) {
0 commit comments