11//! Block storage for persisting full blocks that contain wallet-relevant transactions.
22
3- use std:: collections:: HashSet ;
43use std:: path:: PathBuf ;
54
65use crate :: error:: StorageResult ;
7- use crate :: storage:: segments:: { Persistable , SegmentCache } ;
6+ use crate :: storage:: segments:: SegmentCache ;
87use crate :: storage:: PersistentStorage ;
98use crate :: types:: HashedBlock ;
109use async_trait:: async_trait;
@@ -29,9 +28,6 @@ pub trait BlockStorage: Send + Sync + 'static {
2928pub struct PersistentBlockStorage {
3029 /// Block storage segments.
3130 blocks : RwLock < SegmentCache < HashedBlock > > ,
32- /// Set of available block heights used for fast lookups and to bypass sentinel loading and gap
33- /// detection asserts (in debug builds) in the underlying segment implementation.
34- available_heights : HashSet < CoreBlockHeight > ,
3531}
3632
3733impl PersistentBlockStorage {
@@ -46,24 +42,10 @@ impl PersistentStorage for PersistentBlockStorage {
4642
4743 tracing:: debug!( "Opening PersistentBlockStorage from {:?}" , blocks_folder) ;
4844
49- let mut blocks: SegmentCache < HashedBlock > =
50- SegmentCache :: load_or_new ( & blocks_folder) . await ?;
51-
52- let mut available_heights = HashSet :: new ( ) ;
53-
54- if let ( Some ( start) , Some ( end) ) = ( blocks. start_height ( ) , blocks. tip_height ( ) ) {
55- let hashed_blocks = blocks. get_items ( start..end + 1 ) . await ?;
56- let sentinel = HashedBlock :: sentinel ( ) ;
57- for ( i, hashed_block) in hashed_blocks. iter ( ) . enumerate ( ) {
58- if hashed_block != & sentinel {
59- available_heights. insert ( start + i as CoreBlockHeight ) ;
60- }
61- }
62- }
45+ let blocks: SegmentCache < HashedBlock > = SegmentCache :: load_or_new ( & blocks_folder) . await ?;
6346
6447 Ok ( Self {
6548 blocks : RwLock :: new ( blocks) ,
66- available_heights,
6749 } )
6850 }
6951
@@ -78,17 +60,11 @@ impl PersistentStorage for PersistentBlockStorage {
7860#[ async_trait]
7961impl BlockStorage for PersistentBlockStorage {
8062 async fn store_block ( & mut self , height : u32 , hashed_block : HashedBlock ) -> StorageResult < ( ) > {
81- self . available_heights . insert ( height) ;
8263 self . blocks . write ( ) . await . store_items_at_height ( & [ hashed_block] , height) . await
8364 }
8465
8566 async fn load_block ( & self , height : u32 ) -> StorageResult < Option < HashedBlock > > {
86- // This early return avoids unnecessary disk lookups and bypasses sentinel loading and gap
87- // detection asserts (in debug builds) in the underlying segment implementation.
88- if !self . available_heights . contains ( & height) {
89- return Ok ( None ) ;
90- }
91- Ok ( self . blocks . write ( ) . await . get_items ( height..height + 1 ) . await ?. first ( ) . cloned ( ) )
67+ self . blocks . write ( ) . await . get_item ( height) . await
9268 }
9369}
9470
0 commit comments