Skip to content

Commit

Permalink
feat: fallback for non block number events & flag for indx pending bl…
Browse files Browse the repository at this point in the history
…ocks
  • Loading branch information
Larkooo committed Apr 27, 2024
1 parent 6b6219f commit 8dc9949
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
5 changes: 5 additions & 0 deletions bin/torii/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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(),
Expand Down
27 changes: 24 additions & 3 deletions crates/torii/core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}

Expand Down Expand Up @@ -123,7 +129,7 @@ impl<P: Provider + Sync> Engine<P> {
// 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?;
}

Expand Down Expand Up @@ -235,7 +241,22 @@ impl<P: Provider + Sync> Engine<P> {
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
Expand Down

0 comments on commit 8dc9949

Please sign in to comment.