Skip to content

Commit

Permalink
Adds step, network, start_block and execution_mode to "block_handler"
Browse files Browse the repository at this point in the history
  • Loading branch information
luis-herasme committed Jul 19, 2024
1 parent fe7c5b6 commit b5517e5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
31 changes: 23 additions & 8 deletions ghost-crab-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
}
}
})
Expand Down
16 changes: 9 additions & 7 deletions ghost-crab/src/block_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,23 @@ pub type BlockHandlerInstance = Arc<Box<(dyn BlockHandler + Send + Sync)>>;
#[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<Http<Client>>,
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());

Expand Down
9 changes: 1 addition & 8 deletions ghost-crab/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
});
}

Expand Down

0 comments on commit b5517e5

Please sign in to comment.