From 441692d512664019dfe3ce14143655d3f81b915f Mon Sep 17 00:00:00 2001 From: Luis Herasme Date: Fri, 26 Jul 2024 18:15:09 -0400 Subject: [PATCH] refactor: Improve indexer errors and file structure --- ghost-crab/src/block_handler.rs | 8 ++-- ghost-crab/src/cache/mod.rs | 3 -- ghost-crab/src/event_handler.rs | 8 ++-- ghost-crab/src/{cache => indexer}/cache.rs | 9 +---- ghost-crab/src/indexer/error.rs | 36 ++++++++++++------ ghost-crab/src/indexer/indexer.rs | 37 ++++++++----------- ghost-crab/src/indexer/mod.rs | 2 + .../manager.rs => indexer/rpc_manager.rs} | 34 ++++++++--------- ghost-crab/src/latest_block_manager.rs | 8 ++-- .../src/{cache => layers}/cache_layer.rs | 0 ghost-crab/src/layers/mod.rs | 2 + .../rate_limit_layer.rs} | 0 ghost-crab/src/lib.rs | 3 +- ghost-crab/src/prelude.rs | 1 - 14 files changed, 74 insertions(+), 77 deletions(-) delete mode 100644 ghost-crab/src/cache/mod.rs rename ghost-crab/src/{cache => indexer}/cache.rs (66%) rename ghost-crab/src/{cache/manager.rs => indexer/rpc_manager.rs} (54%) rename ghost-crab/src/{cache => layers}/cache_layer.rs (100%) create mode 100644 ghost-crab/src/layers/mod.rs rename ghost-crab/src/{rate_limit.rs => layers/rate_limit_layer.rs} (100%) diff --git a/ghost-crab/src/block_handler.rs b/ghost-crab/src/block_handler.rs index a51d8d4..7b6419a 100644 --- a/ghost-crab/src/block_handler.rs +++ b/ghost-crab/src/block_handler.rs @@ -1,7 +1,7 @@ -use crate::cache::manager::CacheProvider; +use crate::indexer::rpc_manager::Provider; use crate::indexer::templates::TemplateManager; use crate::latest_block_manager::LatestBlockManager; -use alloy::providers::Provider; +use alloy::providers::Provider as AlloyProvider; use alloy::rpc::types::eth::Block; use alloy::rpc::types::eth::BlockNumberOrTag; use alloy::transports::TransportError; @@ -12,7 +12,7 @@ use std::sync::Arc; use std::time::Duration; pub struct BlockContext { - pub provider: CacheProvider, + pub provider: Provider, pub templates: TemplateManager, pub block_number: u64, } @@ -35,7 +35,7 @@ pub trait BlockHandler { pub struct ProcessBlocksInput { pub handler: BlockHandlerInstance, pub templates: TemplateManager, - pub provider: CacheProvider, + pub provider: Provider, pub config: BlockHandlerConfig, } diff --git a/ghost-crab/src/cache/mod.rs b/ghost-crab/src/cache/mod.rs deleted file mode 100644 index 8e26f24..0000000 --- a/ghost-crab/src/cache/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub mod cache; -pub mod cache_layer; -pub mod manager; diff --git a/ghost-crab/src/event_handler.rs b/ghost-crab/src/event_handler.rs index 075da6a..bd2f75b 100644 --- a/ghost-crab/src/event_handler.rs +++ b/ghost-crab/src/event_handler.rs @@ -1,9 +1,9 @@ -use crate::cache::manager::CacheProvider; +use crate::indexer::rpc_manager::Provider; use crate::indexer::templates::TemplateManager; use crate::latest_block_manager::LatestBlockManager; use alloy::eips::BlockNumberOrTag; use alloy::primitives::Address; -use alloy::providers::Provider; +use alloy::providers::Provider as AlloyProvider; use alloy::rpc::types::eth::Filter; use alloy::rpc::types::eth::Log; use alloy::rpc::types::Block; @@ -15,7 +15,7 @@ use std::time::Duration; pub struct EventContext { pub log: Log, - pub provider: CacheProvider, + pub provider: Provider, pub templates: TemplateManager, pub contract_address: Address, } @@ -49,7 +49,7 @@ pub struct ProcessEventsInput { pub step: u64, pub handler: EventHandlerInstance, pub templates: TemplateManager, - pub provider: CacheProvider, + pub provider: Provider, pub execution_mode: ExecutionMode, } diff --git a/ghost-crab/src/cache/cache.rs b/ghost-crab/src/indexer/cache.rs similarity index 66% rename from ghost-crab/src/cache/cache.rs rename to ghost-crab/src/indexer/cache.rs index 6d364bb..1dbe029 100644 --- a/ghost-crab/src/cache/cache.rs +++ b/ghost-crab/src/indexer/cache.rs @@ -1,13 +1,6 @@ +use super::error::{Error, Result}; use rocksdb::DB; -#[derive(Debug)] -pub enum Error { - DB(rocksdb::Error), - CacheFileNotFound(std::io::Error), -} - -type Result = core::result::Result; - pub fn load_cache(network: &str) -> Result { let current_dir = std::env::current_dir().map_err(|e| Error::CacheFileNotFound(e))?; let cache_path = current_dir.join("cache").join(network); diff --git a/ghost-crab/src/indexer/error.rs b/ghost-crab/src/indexer/error.rs index 4e22217..018c81d 100644 --- a/ghost-crab/src/indexer/error.rs +++ b/ghost-crab/src/indexer/error.rs @@ -2,26 +2,40 @@ use alloy::hex::FromHexError; use core::fmt; #[derive(Debug)] -pub enum AddHandlerError { +pub enum Error { NotFound(String), + DB(rocksdb::Error), NetworkNotFound(String), - InvalidAddress { address: String, error: FromHexError }, + InvalidAddress(FromHexError), + CacheFileNotFound(std::io::Error), + InvalidRpcUrl(Box), } -impl fmt::Display for AddHandlerError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { +pub type Result = core::result::Result; + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - AddHandlerError::NotFound(handler) => { - write!(f, "Handler not found: {}", handler) + Error::NotFound(handler) => { + writeln!(f, "Handler not found: {}", handler) + } + Error::DB(error) => { + writeln!(f, "Error while loading cache: {}", error) + } + Error::NetworkNotFound(network) => { + writeln!(f, "Network not found: {}", network) + } + Error::InvalidAddress(error) => { + writeln!(f, "Invalid address: {}", error) } - AddHandlerError::NetworkNotFound(network) => { - write!(f, "Network not found: {}", network) + Error::CacheFileNotFound(error) => { + writeln!(f, "Cache file not found: {}", error) } - AddHandlerError::InvalidAddress { address, error } => { - write!(f, "Invalid address: {}.\nError: {}", address, error) + Error::InvalidRpcUrl(error) => { + writeln!(f, "Invalid RPC url: {}", error) } } } } -impl std::error::Error for AddHandlerError {} +impl std::error::Error for Error {} diff --git a/ghost-crab/src/indexer/indexer.rs b/ghost-crab/src/indexer/indexer.rs index 137501f..bf8fc30 100644 --- a/ghost-crab/src/indexer/indexer.rs +++ b/ghost-crab/src/indexer/indexer.rs @@ -1,12 +1,12 @@ +use super::rpc_manager::{Provider, RPCManager}; use crate::block_handler::{process_blocks, BlockHandlerInstance, ProcessBlocksInput}; -use crate::cache::manager::{CacheProvider, RPCManager}; use crate::event_handler::{process_events, EventHandlerInstance, ProcessEventsInput}; use alloy::primitives::Address; use ghost_crab_common::config::{self, Config, ConfigError}; use tokio::sync::mpsc::{self, Receiver}; -use super::error::AddHandlerError; +use super::error::{Error, Result}; use super::templates::{Template, TemplateManager}; pub struct Indexer { @@ -19,8 +19,8 @@ pub struct Indexer { } impl Indexer { - pub fn new() -> Result { - let (tx, rx) = mpsc::channel::