diff --git a/indexer/core/src/block_store.rs b/indexer/core/src/block_store.rs index ce3881bd..beed8e6d 100644 --- a/indexer/core/src/block_store.rs +++ b/indexer/core/src/block_store.rs @@ -12,6 +12,7 @@ use storage::indexer::RocksDBIO; #[derive(Clone)] pub struct IndexerStore { dbio: Arc, + final_state: V02State, } impl IndexerStore { @@ -24,9 +25,11 @@ impl IndexerStore { start_data: Option<(Block, V02State)>, ) -> Result { let dbio = RocksDBIO::open_or_create(location, start_data)?; + let final_state = dbio.final_state()?; Ok(Self { dbio: Arc::new(dbio), + final_state, }) } @@ -95,22 +98,30 @@ impl IndexerStore { Ok(self.dbio.calculate_state_for_id(block_id)?) } - pub fn final_state(&self) -> Result { + pub fn final_state(&self) -> &V02State { + &self.final_state + } + + pub fn final_state_mut(&mut self) -> &mut V02State { + &mut self.final_state + } + + pub fn final_state_db(&self) -> Result { Ok(self.dbio.final_state()?) } pub fn get_account_final(&self, account_id: &AccountId) -> Result { - Ok(self.final_state()?.get_account_by_id(*account_id)) + Ok(self.final_state().get_account_by_id(*account_id)) } - pub fn put_block(&self, mut block: Block, l1_header: HeaderId) -> Result<()> { - let mut final_state = self.dbio.final_state()?; + pub fn put_block(&mut self, mut block: Block, l1_header: HeaderId) -> Result<()> { + let final_state = self.final_state_mut(); for transaction in &block.body.transactions { transaction .clone() .transaction_stateless_check()? - .execute_check_on_state(&mut final_state)?; + .execute_check_on_state(final_state)?; } // ToDo: Currently we are fetching only finalized blocks diff --git a/indexer/core/src/lib.rs b/indexer/core/src/lib.rs index 6d56eb18..5037ae20 100644 --- a/indexer/core/src/lib.rs +++ b/indexer/core/src/lib.rs @@ -98,7 +98,9 @@ impl IndexerCore { }) } - pub async fn subscribe_parse_block_stream(&self) -> impl futures::Stream> { + pub async fn subscribe_parse_block_stream( + &mut self, + ) -> impl futures::Stream> { async_stream::stream! { info!("Searching for initial header"); diff --git a/indexer/service/src/service.rs b/indexer/service/src/service.rs index 13ec2ea8..e02b6c3b 100644 --- a/indexer/service/src/service.rs +++ b/indexer/service/src/service.rs @@ -127,7 +127,7 @@ impl indexer_service_rpc::RpcServer for IndexerService { async fn healthcheck(&self) -> Result<(), ErrorObjectOwned> { // Checking, that indexer can calculate last state - let _ = self.indexer.store.final_state().map_err(db_error)?; + let _ = self.indexer.store.final_state_db().map_err(db_error)?; Ok(()) } @@ -179,7 +179,7 @@ impl SubscriptionService { Ok(()) } - fn spawn_respond_subscribers_loop(indexer: IndexerCore) -> SubscriptionLoopParts { + fn spawn_respond_subscribers_loop(mut indexer: IndexerCore) -> SubscriptionLoopParts { let (new_subscription_sender, mut sub_receiver) = tokio::sync::mpsc::unbounded_channel::>();