From a85c0ca9614899930335292a5ecf4dc630d4647b Mon Sep 17 00:00:00 2001 From: kwiss Date: Thu, 14 Sep 2023 22:08:38 +0200 Subject: [PATCH] feat(storage): add basic return & usage for storage --- crates/ark-core/src/managers/block_manager.rs | 47 ++++++++---- .../src/managers/collection_manager.rs | 12 ++- crates/ark-core/src/managers/event_manager.rs | 5 +- crates/ark-core/src/managers/token_manager.rs | 14 +++- crates/ark-storage/src/storage_manager.rs | 74 +++++++++++++------ 5 files changed, 108 insertions(+), 44 deletions(-) diff --git a/crates/ark-core/src/managers/block_manager.rs b/crates/ark-core/src/managers/block_manager.rs index 9610d30bd..39956c001 100644 --- a/crates/ark-core/src/managers/block_manager.rs +++ b/crates/ark-core/src/managers/block_manager.rs @@ -56,28 +56,47 @@ impl<'a, T: StorageManager, C: StarknetClient> BlockManager<'a, T, C> { .unwrap_or(false); if *do_force { - log::debug!("Block #{} forced", block_number); - // TODO: self.storage.clean_block(block_number); + match self.storage.clean_block(block_number) { + Ok(_) => println!("Block cleaned successfully!"), + Err(e) => println!("Error cleaning block: {:?}", e), + } return true; } - // TODO: self.storage.get_block_info(...); - let info = BlockInfo { - indexer_version: 0, - status: BlockIndexingStatus::None, + let info = match self.storage.get_block_info(block_number) { + Ok(Some(block_info)) => { + println!("Retrieved block info: {:?}", block_info); + Some(block_info) // Assign the value of block_info to `info` + } + Ok(None) => { + println!("No info found for block."); + None // Assigns None to `info` + } + Err(e) => { + println!("Error retrieving block info: {:?}", e); + None // Assigns None to `info` in case of error + } }; - if info.status == BlockIndexingStatus::None { - return true; - } + // Use the retrieved info to determine some actions + if let Some(actual_info) = info { + if actual_info.status == BlockIndexingStatus::None { + return true; + } - if info.indexer_version > self.indexer_version { - log::debug!("Block #{} new version", block_number); - // TODO: self.storage.clean_block(block_number); - return true; + if actual_info.indexer_version > self.indexer_version { + log::debug!("Block #{} new version", block_number); + match self.storage.clean_block(block_number) { + Ok(_) => println!("Block cleaned successfully!"), + Err(e) => println!("Error cleaning block: {:?}", e), + } + return true; + } + } else { + log::debug!("Info is not available for the block."); } - log::debug!("Block #{} not candidate", block_number); + // If no conditions are met, return false or whatever default you want false } } diff --git a/crates/ark-core/src/managers/collection_manager.rs b/crates/ark-core/src/managers/collection_manager.rs index e1cf95530..8bf7427a6 100644 --- a/crates/ark-core/src/managers/collection_manager.rs +++ b/crates/ark-core/src/managers/collection_manager.rs @@ -29,7 +29,11 @@ impl<'a, T: StorageManager, C: StarknetClient> CollectionManager<'a, T, C> { Some(info) => Ok(info.clone()), None => { log::trace!("Cache miss for contract {address}"); - // TODO: self.storage.get_contract_info(); + match self.storage.get_contract_info(&address) { + Ok(Some(info)) => println!("Retrieved contract info: {:?}", info), + Ok(None) => println!("No info found for contract."), + Err(e) => println!("Error retrieving contract info: {:?}", e), + } // If no info available -> return error. // For now, return error to simulate it's not available. Err(anyhow!("Info not found in storage for contract {address}")) @@ -59,7 +63,11 @@ impl<'a, T: StorageManager, C: StarknetClient> CollectionManager<'a, T, C> { }; self.cache.insert(address, info.clone()); - // TODO: self.storage.register_contract_info(...); + + match self.storage.register_contract_info(&address, &info) { + Ok(_) => println!("Contract info registered successfully!"), + Err(e) => println!("Error registering contract info: {:?}", e), + } Ok(info) } diff --git a/crates/ark-core/src/managers/event_manager.rs b/crates/ark-core/src/managers/event_manager.rs index eea1d7309..a77836d1b 100644 --- a/crates/ark-core/src/managers/event_manager.rs +++ b/crates/ark-core/src/managers/event_manager.rs @@ -73,7 +73,10 @@ impl<'a, T: StorageManager, C: StarknetClient> EventManager<'a, T, C> { info!("Event identified: {:?}", self.token_event.event_type); - // TODO: self.storage.register_event(self.token_event); + match self.storage.register_event(&self.token_event) { + Ok(_) => println!("Event registered successfully!"), + Err(e) => println!("Error registering event: {:?}", e), + } // TODO: check depending on event type if it's a create/update etc...? Ok(self.token_event.clone()) diff --git a/crates/ark-core/src/managers/token_manager.rs b/crates/ark-core/src/managers/token_manager.rs index e4464e307..c6c00c2f6 100644 --- a/crates/ark-core/src/managers/token_manager.rs +++ b/crates/ark-core/src/managers/token_manager.rs @@ -63,12 +63,18 @@ impl<'a, T: StorageManager, C: StarknetClient> TokenManager<'a, T, C> { event.contract_address ); - // TODO: self.storage.register_token(self.token.clone()).await?; + match self.storage.register_token(&self.token) { + Ok(_) => println!("Token registered successfully!"), + Err(e) => println!("Error registering token: {:?}", e), + } - if (event.event_type == EventType::Mint) { - // self.storage.register_mint(self.token.clone()).await?; + if event.event_type == EventType::Mint { + match self.storage.register_mint(&self.token) { + Ok(_) => println!("Mint registered successfully!"), + Err(e) => println!("Error registering mint: {:?}", e), + } } - + Ok(()) } diff --git a/crates/ark-storage/src/storage_manager.rs b/crates/ark-storage/src/storage_manager.rs index 3df57ac22..052a323bd 100644 --- a/crates/ark-storage/src/storage_manager.rs +++ b/crates/ark-storage/src/storage_manager.rs @@ -1,8 +1,16 @@ -use crate::types::{BlockIndexing, BlockInfo, ContractInfo, TokenEvent, TokenFromEvent}; +use crate::types::{ + BlockIndexing, BlockIndexingStatus, BlockInfo, ContractInfo, ContractType, TokenEvent, + TokenFromEvent, +}; use log; +use starknet::core::types::FieldElement; +#[derive(Debug)] pub enum StorageError { - // TODO + DatabaseError, + NotFound, + DuplicateToken, + InvalidMintData, } pub trait StorageManager { @@ -16,16 +24,16 @@ pub trait StorageManager { fn get_contract_info( &self, - contract_address: &str, + contract_address: &FieldElement, ) -> Result, StorageError>; fn register_contract_info( &self, - contract_address: &str, - info: ContractInfo, + contract_address: &FieldElement, + info: &ContractInfo, ) -> Result<(), StorageError>; - fn register_event(&self, event: TokenEvent) -> Result<(), StorageError>; + fn register_event(&self, event: &TokenEvent) -> Result<(), StorageError>; fn set_block_info(&self, block_number: u64, info: BlockInfo) -> Result<(), StorageError>; @@ -43,66 +51,86 @@ impl DefaultStorage { impl StorageManager for DefaultStorage { fn register_token(&self, token: &TokenFromEvent) -> Result<(), StorageError> { log::debug!("Registering token {:?}", token); - // ... Logic here + // TODO: In future, handle and return potential errors + // Err(StorageError::DuplicateToken) Ok(()) } fn register_mint(&self, token: &TokenFromEvent) -> Result<(), StorageError> { log::debug!("Registering mint {:?}", token); - // ... Logic here + // TODO: In future, handle and return potential errors + // Err(StorageError::InvalidMintData) Ok(()) } fn clean_block(&self, block_number: u64) -> Result<(), StorageError> { log::debug!("Cleaning block #{}", block_number); - // ... Logic here + // TODO: In future, handle and return potential errors + // Err(StorageError::DatabaseError) Ok(()) } fn get_block_info(&self, block_number: u64) -> Result, StorageError> { log::debug!("Getting block info for block #{}", block_number); - // ... Logic here - Ok(None) + // TODO: In future, handle and return potential errors + // Err(StorageError::NotFound) + Ok(Some(BlockInfo { + indexer_version: 0, + indexer_indentifier: "42".to_string(), + status: BlockIndexingStatus::None, + })) } fn get_contract_info( &self, - contract_address: &str, + contract_address: &FieldElement, ) -> Result, StorageError> { log::debug!("Getting contract info for contract {}", contract_address); - // ... Logic here - Ok(None) + // TODO: In future, handle and return potential errors + // Err(StorageError::NotFound) + Ok(Some(ContractInfo { + name: "Dummy".to_string(), + symbol: "DUM".to_string(), + r#type: ContractType::Other, + })) } fn register_contract_info( &self, - contract_address: &str, - info: ContractInfo, + contract_address: &FieldElement, + info: &ContractInfo, ) -> Result<(), StorageError> { log::debug!( "Registering contract info {:?} for contract {}", info, contract_address ); - // ... Logic here + // TODO: In future, handle and return potential errors + // Err(StorageError::DuplicateToken) Ok(()) } - fn register_event(&self, event: TokenEvent) -> Result<(), StorageError> { + fn register_event(&self, event: &TokenEvent) -> Result<(), StorageError> { log::debug!("Registering event {:?}", event); - // ... Logic here + // TODO: In future, handle and return potential errors + // Err(StorageError::DatabaseError) Ok(()) } fn set_block_info(&self, block_number: u64, info: BlockInfo) -> Result<(), StorageError> { log::debug!("Setting block info {:?} for block #{}", info, block_number); - // ... Logic here + // TODO: In future, handle and return potential errors + // Err(StorageError::DatabaseError) Ok(()) } - fn set_indexer_progress(&self, progress: BlockIndexing) -> Result<(), StorageError> { - log::debug!("Setting indexer progress to block #{}", progress.percentage); - // ... Logic here + fn set_indexer_progress(&self, indexer_progress: BlockIndexing) -> Result<(), StorageError> { + log::debug!( + "Setting indexer progress to block #{}", + indexer_progress.percentage + ); + // TODO: In future, handle and return potential errors + // Err(StorageError::DatabaseError) Ok(()) } }