From 8dc99497b09ae5ff6fd292c527d65604da2e9444 Mon Sep 17 00:00:00 2001 From: Nasr Date: Sat, 27 Apr 2024 15:06:03 -0400 Subject: [PATCH] feat: fallback for non block number events & flag for indx pending blocks --- bin/torii/src/main.rs | 5 +++++ crates/torii/core/src/engine.rs | 27 ++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/bin/torii/src/main.rs b/bin/torii/src/main.rs index a14ff3d4c0..5ed50ee1e1 100644 --- a/bin/torii/src/main.rs +++ b/bin/torii/src/main.rs @@ -109,6 +109,10 @@ struct Args { /// Chunk size of the events page when indexing using events #[arg(long, default_value = "1000")] events_chunk_size: u64, + + /// Enable indexing pending blocks + #[arg(long)] + index_pending: bool, } #[tokio::main] @@ -179,6 +183,7 @@ async fn main() -> anyhow::Result<()> { EngineConfig { start_block: args.start_block, events_chunk_size: args.events_chunk_size, + index_pending: args.index_pending, ..Default::default() }, shutdown_tx.clone(), diff --git a/crates/torii/core/src/engine.rs b/crates/torii/core/src/engine.rs index a49b9c1220..8c4cf37549 100644 --- a/crates/torii/core/src/engine.rs +++ b/crates/torii/core/src/engine.rs @@ -37,11 +37,17 @@ pub struct EngineConfig { pub block_time: Duration, pub start_block: u64, pub events_chunk_size: u64, + pub index_pending: bool, } impl Default for EngineConfig { fn default() -> Self { - Self { block_time: Duration::from_secs(1), start_block: 0, events_chunk_size: 1000 } + Self { + block_time: Duration::from_secs(1), + start_block: 0, + events_chunk_size: 1000, + index_pending: false, + } } } @@ -123,7 +129,7 @@ impl Engine

{ // if `from` == 0, then the block may or may not be processed yet. let from = if from == 0 { from } else { from + 1 }; pending_block_tx = self.sync_range(from, latest_block_number, pending_block_tx).await?; - } else { + } else if self.config.index_pending { pending_block_tx = self.sync_pending(latest_block_number + 1, pending_block_tx).await?; } @@ -235,7 +241,22 @@ impl Engine

{ for event in &events_page.events { let block_number = match event.block_number { Some(block_number) => block_number, - None => return Err(anyhow::anyhow!("Event without block number.")), + None => { + match self.provider.get_transaction_receipt(event.transaction_hash).await? { + MaybePendingTransactionReceipt::Receipt( + TransactionReceipt::Invoke(receipt), + ) => receipt.block_number, + MaybePendingTransactionReceipt::Receipt( + TransactionReceipt::L1Handler(receipt), + ) => receipt.block_number, + _ => { + error!(target: LOG_TARGET, transaction_hash = %format!("{:#x}", event.transaction_hash), "Invalid transaction state. Could not fetch block number for event."); + return Err(anyhow::anyhow!( + "Expected finalized transaction receipt for event." + )); + } + } + } }; // Keep track of last block number and fetch block timestamp