Skip to content

Commit

Permalink
refactor: Refactor indexer and add errors
Browse files Browse the repository at this point in the history
  • Loading branch information
luis-herasme committed Jul 26, 2024
1 parent 6128230 commit ab5f9c5
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 45 deletions.
2 changes: 1 addition & 1 deletion ghost-crab-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn block_handler(metadata: TokenStream, input: TokenStream) -> TokenStream {
#fn_body
}

fn name() -> String {
fn name(&self) -> String {
String::from(#name)
}
}
Expand Down
2 changes: 1 addition & 1 deletion ghost-crab/src/block_handler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::cache::manager::CacheProvider;
use crate::indexer::TemplateManager;
use crate::indexer::templates::TemplateManager;
use crate::latest_block_manager::LatestBlockManager;
use alloy::providers::Provider;
use alloy::rpc::types::eth::Block;
Expand Down
2 changes: 1 addition & 1 deletion ghost-crab/src/event_handler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::cache::manager::CacheProvider;
use crate::indexer::TemplateManager;
use crate::indexer::templates::TemplateManager;
use crate::latest_block_manager::LatestBlockManager;
use alloy::eips::BlockNumberOrTag;
use alloy::primitives::Address;
Expand Down
22 changes: 22 additions & 0 deletions ghost-crab/src/indexer/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use core::fmt;

#[derive(Debug)]
pub enum AddHandlerError {
NotFound(String),
NetworkNotFound(String),
}

impl fmt::Display for AddHandlerError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AddHandlerError::NotFound(handler) => {
write!(f, "Handler not found: {}", handler)
}
AddHandlerError::NetworkNotFound(network) => {
write!(f, "Network not found: {}", network)
}
}
}
}

impl std::error::Error for AddHandlerError {}
78 changes: 38 additions & 40 deletions ghost-crab/src/indexer.rs → ghost-crab/src/indexer/indexer.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
use crate::block_handler::{process_blocks, BlockHandlerInstance, ProcessBlocksInput};
use crate::cache::manager::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::error::SendError;
use tokio::sync::mpsc::{self, Receiver, Sender};
use tokio::sync::mpsc::{self, Receiver};

pub struct Template {
pub start_block: u64,
pub address: Address,
pub handler: EventHandlerInstance,
}

#[derive(Clone)]
pub struct TemplateManager {
tx: Sender<Template>,
}

impl TemplateManager {
pub async fn start(&self, template: Template) -> Result<(), SendError<Template>> {
self.tx.send(template).await
}
}
use super::error::AddHandlerError;
use super::templates::{Template, TemplateManager};

pub struct Indexer {
handlers: Vec<ProcessEventsInput>,
Expand All @@ -42,7 +27,7 @@ impl Indexer {
config,
handlers: Vec::new(),
block_handlers: Vec::new(),
templates: TemplateManager { tx },
templates: TemplateManager::new(tx),
rpc_manager: RPCManager::new(),
rx,
})
Expand All @@ -68,26 +53,39 @@ impl Indexer {
});
}

pub async fn load_block_handler(&mut self, handler: BlockHandlerInstance) {
if let Some(block_config) = self.config.block_handlers.remove(&handler.name()) {
if let Some(network) = self.config.networks.get(&block_config.network) {
let provider = self
.rpc_manager
.get_or_create(
block_config.network.clone(),
network.rpc_url.clone(),
network.requests_per_second,
)
.await;

self.block_handlers.push(ProcessBlocksInput {
handler,
templates: self.templates.clone(),
provider,
config: block_config,
});
}
}
pub async fn load_block_handler(
&mut self,
handler: BlockHandlerInstance,
) -> Result<(), AddHandlerError> {
let block_config = self
.config
.block_handlers
.remove(&handler.name())
.ok_or(AddHandlerError::NotFound(handler.name()))?;

let network = self
.config
.networks
.get(&block_config.network)
.ok_or(AddHandlerError::NetworkNotFound(block_config.network.clone()))?;

let provider = self
.rpc_manager
.get_or_create(
block_config.network.clone(),
network.rpc_url.clone(),
network.requests_per_second,
)
.await;

self.block_handlers.push(ProcessBlocksInput {
handler,
templates: self.templates.clone(),
provider,
config: block_config,
});

Ok(())
}

pub async fn start(mut self) {
Expand Down
3 changes: 3 additions & 0 deletions ghost-crab/src/indexer/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod error;
pub mod indexer;
pub mod templates;
26 changes: 26 additions & 0 deletions ghost-crab/src/indexer/templates.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use alloy::primitives::Address;
use tokio::sync::mpsc::error::SendError;
use tokio::sync::mpsc::Sender;

use crate::event_handler::EventHandlerInstance;

pub struct Template {
pub start_block: u64,
pub address: Address,
pub handler: EventHandlerInstance,
}

#[derive(Clone)]
pub struct TemplateManager {
tx: Sender<Template>,
}

impl TemplateManager {
pub fn new(tx: Sender<Template>) -> TemplateManager {
TemplateManager { tx }
}

pub async fn start(&self, template: Template) -> Result<(), SendError<Template>> {
self.tx.send(template).await
}
}
2 changes: 1 addition & 1 deletion ghost-crab/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod indexer;
pub mod prelude;

pub use ghost_crab_common::config;
pub use indexer::Indexer;
pub use indexer::indexer::Indexer;

mod latest_block_manager;
mod rate_limit;
2 changes: 1 addition & 1 deletion ghost-crab/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use crate::block_handler::{BlockContext, BlockHandler};
pub use crate::cache;
pub use crate::config;
pub use crate::indexer;
pub use crate::indexer::Template;
pub use crate::indexer::templates::Template;
pub use alloy::primitives::address;
pub use alloy::primitives::Address;
pub use alloy::providers::Provider;

0 comments on commit ab5f9c5

Please sign in to comment.