From b5517e581723bbd7b3a12782da079ce56af67bbb Mon Sep 17 00:00:00 2001 From: Luis Herasme Date: Fri, 19 Jul 2024 12:43:19 -0400 Subject: [PATCH] Adds step, network, start_block and execution_mode to "block_handler" --- ghost-crab-macros/src/lib.rs | 31 +++++++++++++++++++++++-------- ghost-crab/src/block_handler.rs | 16 +++++++++------- ghost-crab/src/indexer.rs | 9 +-------- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/ghost-crab-macros/src/lib.rs b/ghost-crab-macros/src/lib.rs index bcd8be0..eb9c9dd 100644 --- a/ghost-crab-macros/src/lib.rs +++ b/ghost-crab-macros/src/lib.rs @@ -26,19 +26,22 @@ pub fn block_handler(metadata: TokenStream, input: TokenStream) -> TokenStream { } let config = get_config(); - let source = config.block_handlers.get(name); + let source = config.block_handlers.get(name).expect("Source not found."); - if source.is_none() { - panic!("Source '{}' not found.", name); - } + let step = Literal::u64_suffixed(source.step); + let start_block = Literal::u64_suffixed(source.start_block); + let network = Literal::string(&source.network); + + let execution_mode = match source.execution_mode { + Some(ExecutionMode::Serial) => quote! { ExecutionMode::Serial }, + _ => quote! { ExecutionMode::Parallel }, + }; let parsed = parse_macro_input!(input as ItemFn); let fn_name = parsed.sig.ident.clone(); let fn_body = parsed.block; let fn_args = parsed.sig.inputs.clone(); - let data_source = Literal::string(name); - TokenStream::from(quote! { pub struct #fn_name; @@ -54,8 +57,20 @@ pub fn block_handler(metadata: TokenStream, input: TokenStream) -> TokenStream { #fn_body } - fn get_source(&self) -> String { - String::from(#data_source) + fn step(&self) -> u64 { + #step + } + + fn network(&self) -> String { + #network + } + + fn start_block(&self) -> u64 { + #start_block + } + + fn execution_mode(&self) -> ExecutionMode { + #execution_mode } } }) diff --git a/ghost-crab/src/block_handler.rs b/ghost-crab/src/block_handler.rs index 9102624..c689353 100644 --- a/ghost-crab/src/block_handler.rs +++ b/ghost-crab/src/block_handler.rs @@ -20,21 +20,23 @@ pub type BlockHandlerInstance = Arc>; #[async_trait] pub trait BlockHandler { async fn handle(&self, params: BlockContext); - fn get_source(&self) -> String; + fn step(&self) -> u64; + fn network(&self) -> String; + fn start_block(&self) -> u64; + fn execution_mode(&self) -> ExecutionMode; } pub struct BlockConfig { - pub start_block: u64, pub handler: BlockHandlerInstance, pub provider: RootProvider>, pub templates: TemplateManager, - pub step: u64, - pub execution_mode: ExecutionMode, } -pub async fn process_logs_block( - BlockConfig { start_block, handler, provider, templates, step, execution_mode }: BlockConfig, -) { +pub async fn process_logs_block(BlockConfig { handler, provider, templates }: BlockConfig) { + let step = handler.step(); + let start_block = handler.start_block(); + let execution_mode = handler.execution_mode(); + let mut current_block = start_block; let mut latest_block_manager = LatestBlockManager::new(1000, provider.clone()); diff --git a/ghost-crab/src/indexer.rs b/ghost-crab/src/indexer.rs index 499d91c..e75919f 100644 --- a/ghost-crab/src/indexer.rs +++ b/ghost-crab/src/indexer.rs @@ -4,7 +4,6 @@ use crate::config; use crate::handler::{HandleInstance, HandlerConfig}; use crate::process_logs::process_logs; use crate::server::Server; -use ghost_crab_common::config::ExecutionMode; use tokio::sync::mpsc::{self, Receiver, Sender}; #[derive(Clone)] @@ -88,18 +87,12 @@ impl Indexer { } pub async fn load_block_handler(&mut self, handler: BlockHandlerInstance) { - let source = self.config.block_handlers.get(&handler.get_source()).unwrap(); - - let provider = RPC_MANAGER.lock().await.get(source.network.clone()).await; - let execution_mode = source.execution_mode.unwrap_or(ExecutionMode::Parallel); + let provider = RPC_MANAGER.lock().await.get(handler.network()).await; self.block_handlers.push(BlockConfig { - start_block: source.start_block, handler, provider, templates: self.templates.clone(), - step: source.step, - execution_mode, }); }