@@ -39,6 +39,7 @@ const_assert!(
3939pub struct Chain {
4040 db : LiveStore ,
4141 idx : LiveIndex ,
42+ ptrs_genesis : ChainAnchor ,
4243}
4344
4445#[ derive( Clone ) ]
@@ -94,8 +95,7 @@ impl Chain {
9495 self . db . pt . state . get_ptr_info ( key)
9596 }
9697
97- pub fn load ( network : Network , genesis : ChainAnchor , dir : & Path , index_spaces : bool , index_ptrs : bool ) -> anyhow:: Result < Self > {
98- let ptrs_genesis = spaces_ptr:: constants:: ptrs_start_height ( & network) ;
98+ pub fn load ( _network : Network , genesis : ChainAnchor , ptrs_genesis : ChainAnchor , dir : & Path , index_spaces : bool , index_ptrs : bool ) -> anyhow:: Result < Self > {
9999 let proto_db_path = dir. join ( "protocol.sdb" ) ;
100100 let ptrs_db_path = dir. join ( "ptrs.sdb" ) ;
101101 let initial_sp_sync = !proto_db_path. exists ( ) ;
@@ -109,7 +109,7 @@ impl Chain {
109109
110110 let pt_store = PtrStore :: open ( ptrs_db_path) ?;
111111 let pt = PtrLiveStore {
112- state : pt_store. begin ( & genesis ) ?,
112+ state : pt_store. begin ( & ptrs_genesis ) ?,
113113 store : pt_store,
114114 } ;
115115
@@ -130,27 +130,23 @@ impl Chain {
130130 let chain = Chain {
131131 db : LiveStore { sp, pt } ,
132132 idx : LiveIndex { sp : sp_idx, pt : pt_idx } ,
133+ ptrs_genesis
133134 } ;
134135
135136 // If spaces synced past the ptrs point, reset the tip
136137 if initial_pt_sync {
137138 let sp_tip = chain. db . sp . state . tip . read ( ) . expect ( "tip" ) . clone ( ) ;
138- if sp_tip. height > ptrs_genesis {
139+ if sp_tip. height > ptrs_genesis. height {
139140 info ! ( "spaces tip = {} > ptrs genesis = {} - rescanning to index ptrs" ,
140- sp_tip. height, ptrs_genesis
141+ sp_tip. height, ptrs_genesis. height
141142 ) ;
142143 assert_eq ! (
143- ptrs_genesis % COMMIT_BLOCK_INTERVAL , 0 ,
144+ ptrs_genesis. height % COMMIT_BLOCK_INTERVAL , 0 ,
144145 "ptrs genesis must align with commit interval"
145146 ) ;
146- chain. restore_spaces ( |height| {
147- if height != ptrs_genesis {
148- // return a dummy hash until we have a checkpoint matching ptrs genesis
149- return Ok ( BlockHash :: from_slice ( & [ 0u8 ; 32 ] ) . expect ( "hash" ) ) ;
150- }
151-
152- Ok ( sp_tip. hash )
153- } ) ?;
147+ chain. restore_spaces ( |_| {
148+ return Ok ( BlockHash :: from_slice ( & [ 0u8 ; 32 ] ) . expect ( "hash" ) ) ;
149+ } , Some ( ptrs_genesis. height ) ) ?;
154150 }
155151 }
156152
@@ -263,6 +259,10 @@ impl Chain {
263259 * self . db . pt . state . tip . read ( ) . expect ( "ptrs tip" )
264260 }
265261
262+ pub fn can_scan_ptrs ( & self , height : u32 ) -> bool {
263+ height > self . ptrs_genesis . height
264+ }
265+
266266 pub fn update_ptrs_tip ( & self , height : u32 , block_hash : BlockHash ) {
267267 let mut tip = self . db . pt . state . tip . write ( ) . expect ( "write tip" ) ;
268268 tip. height = height;
@@ -349,7 +349,7 @@ impl Chain {
349349 where
350350 F : Fn ( u32 ) -> anyhow:: Result < BlockHash > ,
351351 {
352- let point = self . restore_spaces ( get_block_hash) ?;
352+ let point = self . restore_spaces ( get_block_hash, None ) ?;
353353 self . restore_ptrs ( point)
354354 }
355355
@@ -405,22 +405,27 @@ impl Chain {
405405 Ok ( ( ) )
406406 }
407407
408- pub fn restore_spaces < F > ( & self , get_block_hash : F ) -> anyhow:: Result < ChainAnchor >
408+ pub fn restore_spaces < F > ( & self , get_block_hash : F , restore_to_height : Option < u32 > ) -> anyhow:: Result < ChainAnchor >
409409 where
410410 F : Fn ( u32 ) -> anyhow:: Result < BlockHash > ,
411411 {
412412 let chain_iter = self . db . sp . store . iter ( ) ;
413413 for ( snapshot_index, snapshot) in chain_iter. enumerate ( ) {
414414 let chain_snapshot = snapshot?;
415415 let chain_checkpoint: ChainAnchor = chain_snapshot. metadata ( ) . try_into ( ) ?;
416- let required_hash = get_block_hash ( chain_checkpoint. height ) ?;
417-
418- if required_hash != chain_checkpoint. hash {
419- info ! (
420- "Could not restore to block={} height={}" ,
421- chain_checkpoint. hash, chain_checkpoint. height
422- ) ;
423- continue ;
416+ if let Some ( restore_to_height) = restore_to_height {
417+ if restore_to_height != chain_checkpoint. height {
418+ continue ;
419+ }
420+ } else {
421+ let required_hash = get_block_hash ( chain_checkpoint. height ) ?;
422+ if required_hash != chain_checkpoint. hash {
423+ info ! (
424+ "Could not restore to block={} height={}" ,
425+ chain_checkpoint. hash, chain_checkpoint. height
426+ ) ;
427+ continue ;
428+ }
424429 }
425430
426431 info ! (
0 commit comments