Skip to content

Commit

Permalink
feat(storage): add basic return & usage for storage
Browse files Browse the repository at this point in the history
  • Loading branch information
kwiss committed Sep 14, 2023
1 parent 06b146d commit a85c0ca
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 44 deletions.
47 changes: 33 additions & 14 deletions crates/ark-core/src/managers/block_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
12 changes: 10 additions & 2 deletions crates/ark-core/src/managers/collection_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"))
Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 4 additions & 1 deletion crates/ark-core/src/managers/event_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
14 changes: 10 additions & 4 deletions crates/ark-core/src/managers/token_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

Expand Down
74 changes: 51 additions & 23 deletions crates/ark-storage/src/storage_manager.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -16,16 +24,16 @@ pub trait StorageManager {

fn get_contract_info(
&self,
contract_address: &str,
contract_address: &FieldElement,
) -> Result<Option<ContractInfo>, 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>;

Expand All @@ -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<Option<BlockInfo>, 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<Option<ContractInfo>, 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(())
}
}

0 comments on commit a85c0ca

Please sign in to comment.