@@ -15,7 +15,7 @@ use acropolis_common::{
1515 VotingProcedures ,
1616} ;
1717use anyhow:: Result ;
18- use imbl:: { HashMap , OrdMap , Vector } ;
18+ use imbl:: HashMap ;
1919use std:: sync:: { Arc , Mutex } ;
2020use tracing:: { debug, error, info} ;
2121
@@ -41,10 +41,6 @@ pub struct State {
4141 // Keyed by pool_id
4242 total_blocks_minted : HashMap < KeyHash , u64 > ,
4343
44- /// block heights (numbers) keyed pool id and epoch
45- /// <Pool Id, <Epoch Number, Block Height>>
46- blocks : Option < HashMap < KeyHash , OrdMap < u64 , Vector < u64 > > > > ,
47-
4844 /// historical spo state
4945 /// keyed by pool operator id
5046 historical_spos : Option < HashMap < KeyHash , HistoricalSPOState > > ,
@@ -68,11 +64,6 @@ impl State {
6864 } else {
6965 None
7066 } ,
71- blocks : if config. store_blocks {
72- Some ( HashMap :: new ( ) )
73- } else {
74- None
75- } ,
7667 stake_addresses : if config. store_stake_addresses {
7768 Some ( Arc :: new ( Mutex :: new ( StakeAddressMap :: new ( ) ) ) )
7869 } else {
@@ -97,7 +88,7 @@ impl State {
9788 self . store_config . store_votes
9889 }
9990
100- pub fn is_blocks_enabled ( & self ) -> bool {
91+ pub fn is_historical_blocks_enabled ( & self ) -> bool {
10192 self . store_config . store_blocks
10293 }
10394
@@ -125,7 +116,6 @@ impl From<SPOState> for State {
125116 vrf_key_hash_to_pool_id_map,
126117 total_blocks_minted : HashMap :: new ( ) ,
127118 historical_spos : None ,
128- blocks : None ,
129119 stake_addresses : None ,
130120 }
131121 }
@@ -222,28 +212,21 @@ impl State {
222212
223213 /// Get Blocks by Pool
224214 /// Return Vector of block heights
225- /// Return Err when store_blocks not enabled
226- pub fn get_blocks_by_pool ( & self , pool_id : & KeyHash ) -> Result < Vec < u64 > > {
227- let Some ( blocks ) = self . blocks . as_ref ( ) else {
228- return Err ( anyhow :: anyhow! ( "Blocks are not enabled" ) ) ;
215+ /// Return None when store_blocks not enabled
216+ pub fn get_blocks_by_pool ( & self , pool_id : & KeyHash ) -> Option < Vec < u64 > > {
217+ let Some ( historical_spos ) = self . historical_spos . as_ref ( ) else {
218+ return None ;
229219 } ;
230- Ok ( blocks
231- . get ( pool_id)
232- . map ( |epochs| epochs. values ( ) . flatten ( ) . cloned ( ) . collect ( ) )
233- . unwrap_or_default ( ) )
220+ historical_spos. get ( pool_id) . and_then ( |s| s. get_all_blocks ( ) )
234221 }
235222
236223 /// Get Blocks by Pool and Epoch
237- /// Return Err when store_blocks not enabled
238- pub fn get_blocks_by_pool_and_epoch ( & self , pool_id : & KeyHash , epoch : u64 ) -> Result < Vec < u64 > > {
239- let Some ( blocks ) = self . blocks . as_ref ( ) else {
240- return Err ( anyhow :: anyhow! ( "Blocks are not enabled" ) ) ;
224+ /// Return None when store_blocks not enabled
225+ pub fn get_blocks_by_pool_and_epoch ( & self , pool_id : & KeyHash , epoch : u64 ) -> Option < Vec < u64 > > {
226+ let Some ( historical_spos ) = self . historical_spos . as_ref ( ) else {
227+ return None ;
241228 } ;
242- Ok ( blocks
243- . get ( pool_id)
244- . map ( |epochs| epochs. get ( & epoch) . map ( |blocks| blocks. iter ( ) . cloned ( ) . collect ( ) ) )
245- . flatten ( )
246- . unwrap_or_default ( ) )
229+ historical_spos. get ( pool_id) . and_then ( |s| s. get_blocks_by_epoch ( epoch) )
247230 }
248231
249232 /// Get Pool Updates
@@ -299,14 +282,13 @@ impl State {
299282
300283 * ( self . total_blocks_minted . entry ( pool_id. clone ( ) ) . or_insert ( 0 ) ) += 1 ;
301284 // if store_blocks is enabled
302- if let Some ( blocks) = self . blocks . as_mut ( ) {
303- blocks
304- . entry ( pool_id)
305- . or_insert_with ( OrdMap :: new)
306- . entry ( block_info. epoch )
307- . or_insert_with ( Vector :: new)
308- . push_back ( block_info. number ) ;
309- } ;
285+ if self . is_historical_blocks_enabled ( ) {
286+ if let Some ( historical_spos) = self . historical_spos . as_mut ( ) {
287+ if let Some ( historical_spo) = historical_spos. get_mut ( & pool_id) {
288+ historical_spo. add_block ( block_info. epoch , block_info. number ) ;
289+ }
290+ }
291+ }
310292 true
311293 }
312294
@@ -1080,9 +1062,9 @@ mod tests {
10801062 }
10811063
10821064 #[ test]
1083- fn get_blocks_returns_err_when_blocks_not_enabled ( ) {
1065+ fn get_blocks_returns_none_when_blocks_not_enabled ( ) {
10841066 let state = State :: default ( ) ;
1085- assert ! ( state. get_blocks_by_pool( & vec![ 0 ] ) . is_err ( ) ) ;
1067+ assert ! ( state. get_blocks_by_pool( & vec![ 0 ] ) . is_none ( ) ) ;
10861068 }
10871069
10881070 #[ test]
0 commit comments