@@ -11,6 +11,7 @@ use mp_block::{
1111 BlockId , MadaraBlock , MadaraBlockInfo , MadaraBlockInner , MadaraMaybePendingBlock , MadaraMaybePendingBlockInfo ,
1212 MadaraPendingBlock , MadaraPendingBlockInfo ,
1313} ;
14+ use mp_rpc:: BlockTag ;
1415use mp_state_update:: StateDiff ;
1516use rocksdb:: { Direction , IteratorMode } ;
1617use starknet_api:: core:: ChainId ;
@@ -128,7 +129,7 @@ impl MadaraBackend {
128129 let Some ( res) = self . db . get_cf ( & col, ROW_PENDING_INFO ) ? else {
129130 // See pending block quirk
130131
131- let Some ( latest_block_id) = self . get_block_n_latest ( ) else {
132+ let Some ( latest_block_id) = self . get_block_n_latest_db ( ) ? else {
132133 // Second quirk: if there is not even a genesis block in db, make up the gas prices and everything else
133134 return Ok ( MadaraPendingBlockInfo {
134135 header : PendingHeader {
@@ -347,6 +348,26 @@ impl MadaraBackend {
347348 self . watch_blocks . latest_pending_block ( ) . header . parent_block_number
348349 }
349350
351+ /// This is necessary since other block_n methods will rely on the pending cache in ram to
352+ /// retrieve the latest block number. Since we need the latest block_n to initialize the pending
353+ /// cache, we use this to retrieve the block_n when the ram state has not yet been initialized.
354+ #[ tracing:: instrument( skip( self ) , fields( module = "BlockDB" ) ) ]
355+ pub fn get_block_n_latest_db ( & self ) -> Result < Option < u64 > > {
356+ let col = self . db . get_column ( Column :: BlockNToBlockInfo ) ;
357+ let iter_mode = IteratorMode :: End ;
358+ let mut iter = self . db . iterator_cf ( & col, iter_mode) ;
359+
360+ iter. next ( )
361+ . map ( |kvs| {
362+ kvs. map_err ( MadaraStorageError :: from) . map ( |( key, _) | {
363+ let mut bytes = [ 0u8 ; 8 ] ;
364+ bytes. copy_from_slice ( & key) ;
365+ u64:: from_be_bytes ( bytes)
366+ } )
367+ } )
368+ . transpose ( )
369+ }
370+
350371 #[ tracing:: instrument( skip( self ) , fields( module = "BlockDB" ) ) ]
351372 pub fn get_block_n_next ( & self ) -> u64 {
352373 self . watch_blocks . latest_pending_block ( ) . header . parent_block_number . map ( |n| n. saturating_add ( 1 ) ) . unwrap_or ( 0 )
0 commit comments