@@ -19,8 +19,8 @@ use near_primitives::state::ValueRef;
1919use near_primitives:: state_part:: PartId ;
2020use near_primitives:: types:: { AccountId , BlockHeight , StateRoot } ;
2121use near_store:: flat:: {
22- store_helper, FetchingStateStatus , FlatStateChanges , FlatStorageCreationStatus ,
23- NUM_PARTS_IN_ONE_STEP , STATE_PART_MEMORY_LIMIT ,
22+ store_helper, BlockInfo , FetchingStateStatus , FlatStateChanges , FlatStorageCreationStatus ,
23+ FlatStorageReadyStatus , FlatStorageStatus , NUM_PARTS_IN_ONE_STEP , STATE_PART_MEMORY_LIMIT ,
2424} ;
2525use near_store:: { Store , FLAT_STORAGE_HEAD_HEIGHT } ;
2626use near_store:: { Trie , TrieDBStorage , TrieTraversalItem } ;
@@ -165,10 +165,19 @@ impl FlatStorageShardCreator {
165165 ) -> Result < bool , Error > {
166166 let shard_id = self . shard_uid . shard_id ( ) ;
167167 let current_status =
168- store_helper:: get_flat_storage_creation_status ( chain_store. store ( ) , shard_id ) ;
168+ store_helper:: get_flat_storage_status ( chain_store. store ( ) , self . shard_uid ) ;
169169 self . metrics . status . set ( ( & current_status) . into ( ) ) ;
170170 match & current_status {
171- FlatStorageCreationStatus :: SavingDeltas => {
171+ FlatStorageStatus :: Empty => {
172+ let mut store_update = chain_store. store ( ) . store_update ( ) ;
173+ store_helper:: set_flat_storage_status (
174+ & mut store_update,
175+ self . shard_uid ,
176+ FlatStorageStatus :: Creation ( FlatStorageCreationStatus :: SavingDeltas ) ,
177+ ) ;
178+ store_update. commit ( ) ?;
179+ }
180+ FlatStorageStatus :: Creation ( FlatStorageCreationStatus :: SavingDeltas ) => {
172181 let final_head = chain_store. final_head ( ) ?;
173182 let final_height = final_head. height ;
174183
@@ -220,15 +229,19 @@ impl FlatStorageShardCreator {
220229
221230 let mut store_update = chain_store. store ( ) . store_update ( ) ;
222231 self . metrics . flat_head_height . set ( final_head. height as i64 ) ;
223- store_helper:: set_flat_storage_creation_status (
232+ store_helper:: set_flat_storage_status (
224233 & mut store_update,
225- shard_id,
226- FlatStorageCreationStatus :: FetchingState ( status) ,
234+ self . shard_uid ,
235+ FlatStorageStatus :: Creation ( FlatStorageCreationStatus :: FetchingState (
236+ status,
237+ ) ) ,
227238 ) ;
228239 store_update. commit ( ) ?;
229240 }
230241 }
231- FlatStorageCreationStatus :: FetchingState ( fetching_state_status) => {
242+ FlatStorageStatus :: Creation ( FlatStorageCreationStatus :: FetchingState (
243+ fetching_state_status,
244+ ) ) => {
232245 let store = self . runtime_adapter . store ( ) . clone ( ) ;
233246 let block_hash = fetching_state_status. block_hash ;
234247 let start_part_id = fetching_state_status. part_id ;
@@ -297,26 +310,30 @@ impl FlatStorageShardCreator {
297310 num_parts,
298311 } ;
299312 debug ! ( target: "chain" , %shard_id, %block_hash, ?new_status) ;
300- store_helper:: set_flat_storage_creation_status (
313+ store_helper:: set_flat_storage_status (
301314 & mut store_update,
302- shard_id,
303- FlatStorageCreationStatus :: FetchingState ( new_status) ,
315+ self . shard_uid ,
316+ FlatStorageStatus :: Creation (
317+ FlatStorageCreationStatus :: FetchingState ( new_status) ,
318+ ) ,
304319 ) ;
305320 } else {
306321 // If all parts were fetched, we can start catchup.
307322 info ! ( target: "chain" , %shard_id, %block_hash, "Finished fetching state" ) ;
308323 self . metrics . remaining_state_parts . set ( 0 ) ;
309- store_helper:: set_flat_storage_creation_status (
324+ store_helper:: set_flat_storage_status (
310325 & mut store_update,
311- shard_id,
312- FlatStorageCreationStatus :: CatchingUp ( block_hash) ,
326+ self . shard_uid ,
327+ FlatStorageStatus :: Creation ( FlatStorageCreationStatus :: CatchingUp (
328+ block_hash,
329+ ) ) ,
313330 ) ;
314331 }
315332 store_update. commit ( ) ?;
316333 }
317334 }
318335 }
319- FlatStorageCreationStatus :: CatchingUp ( old_flat_head) => {
336+ FlatStorageStatus :: Creation ( FlatStorageCreationStatus :: CatchingUp ( old_flat_head) ) => {
320337 let store = self . runtime_adapter . store ( ) ;
321338 let mut flat_head = * old_flat_head;
322339 let chain_final_head = chain_store. final_head ( ) ?;
@@ -346,37 +363,46 @@ impl FlatStorageShardCreator {
346363 let epoch_id = self . runtime_adapter . get_epoch_id ( & flat_head) ?;
347364 let shard_uid = self . runtime_adapter . shard_id_to_uid ( shard_id, & epoch_id) ?;
348365 let old_height = chain_store. get_block_height ( & old_flat_head) . unwrap ( ) ;
349- let height = chain_store. get_block_height ( & flat_head) . unwrap ( ) ;
366+ let flat_head_block_header = chain_store. get_block_header ( & flat_head) . unwrap ( ) ;
367+ let height = flat_head_block_header. height ( ) ;
350368 debug ! ( target: "chain" , %shard_id, %old_flat_head, %old_height, %flat_head, %height, "Catching up flat head" ) ;
351369 self . metrics . flat_head_height . set ( height as i64 ) ;
352370 let mut store_update = self . runtime_adapter . store ( ) . store_update ( ) ;
353371 merged_changes. apply_to_flat_state ( & mut store_update, shard_uid) ;
354372 if flat_head == chain_final_head. last_block_hash {
355373 // If we reached chain final head, we can finish catchup and finally create flat storage.
356- store_helper:: remove_flat_storage_creation_status (
374+ store_helper:: set_flat_storage_status (
357375 & mut store_update,
358- shard_id,
376+ self . shard_uid ,
377+ FlatStorageStatus :: Ready ( FlatStorageReadyStatus {
378+ flat_head : BlockInfo {
379+ hash : flat_head,
380+ prev_hash : * flat_head_block_header. prev_hash ( ) ,
381+ height,
382+ } ,
383+ } ) ,
359384 ) ;
360- store_helper:: set_flat_head ( & mut store_update, shard_id, & flat_head) ;
361385 store_update. commit ( ) ?;
362386 self . runtime_adapter . create_flat_storage_for_shard ( shard_uid) ;
363387 info ! ( target: "chain" , %shard_id, %flat_head, %height, "Flat storage creation done" ) ;
364388 } else {
365- store_helper:: set_flat_storage_creation_status (
389+ store_helper:: set_flat_storage_status (
366390 & mut store_update,
367- shard_id,
368- FlatStorageCreationStatus :: CatchingUp ( flat_head) ,
391+ self . shard_uid ,
392+ FlatStorageStatus :: Creation ( FlatStorageCreationStatus :: CatchingUp (
393+ flat_head,
394+ ) ) ,
369395 ) ;
370396 store_update. commit ( ) ?;
371397 }
372398 }
373399 }
374- FlatStorageCreationStatus :: Ready => { }
375- FlatStorageCreationStatus :: DontCreate => {
376- panic ! ( "We initiated flat storage creation for shard {shard_id} but according to flat storage state status in db it cannot be created " ) ;
400+ FlatStorageStatus :: Ready ( _ ) => return Ok ( true ) ,
401+ FlatStorageStatus :: Disabled => {
402+ panic ! ( "initiated flat storage creation for shard {shard_id} while it is disabled " ) ;
377403 }
378404 } ;
379- Ok ( current_status == FlatStorageCreationStatus :: Ready )
405+ Ok ( false )
380406 }
381407}
382408
@@ -402,15 +428,14 @@ impl FlatStorageCreator {
402428 let mut creation_needed = false ;
403429 for shard_id in 0 ..num_shards {
404430 if runtime_adapter. cares_about_shard ( me, & chain_head. prev_block_hash , shard_id, true ) {
405- let status = runtime_adapter. get_flat_storage_creation_status ( shard_id) ;
406431 let shard_uid = runtime_adapter. shard_id_to_uid ( shard_id, & chain_head. epoch_id ) ?;
432+ let status = runtime_adapter. get_flat_storage_status ( shard_uid) ;
407433
408434 match status {
409- FlatStorageCreationStatus :: Ready => {
435+ FlatStorageStatus :: Ready ( _ ) => {
410436 runtime_adapter. create_flat_storage_for_shard ( shard_uid) ;
411437 }
412- FlatStorageCreationStatus :: DontCreate => { }
413- _ => {
438+ FlatStorageStatus :: Empty | FlatStorageStatus :: Creation ( _) => {
414439 creation_needed = true ;
415440 shard_creators. insert (
416441 shard_uid,
@@ -421,6 +446,7 @@ impl FlatStorageCreator {
421446 ) ,
422447 ) ;
423448 }
449+ FlatStorageStatus :: Disabled => { }
424450 }
425451 }
426452 }
0 commit comments