Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(torii-core): engine update cursor when world event #2300

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions crates/torii/core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use tracing::{error, info, trace, warn};

use crate::processors::{BlockProcessor, EventProcessor, TransactionProcessor};
use crate::sql::Sql;

#[allow(missing_debug_implementations)]
pub struct Processors<P: Provider + Sync> {
pub block: Vec<Box<dyn BlockProcessor<P>>>,
Expand Down Expand Up @@ -189,12 +190,14 @@ impl<P: Provider + Sync> Engine<P> {
}
}
}
Ok(true) => {
pending_block_tx = Some(*transaction.transaction_hash());
info!(target: LOG_TARGET, transaction_hash = %format!("{:#x}", transaction.transaction_hash()), "Processed pending world transaction.");
}
Ok(_) => {
info!(target: LOG_TARGET, transaction_hash = %format!("{:#x}", transaction.transaction_hash()), "Processed pending transaction.")
}
}

pending_block_tx = Some(*transaction.transaction_hash());
}

// Set the head to the last processed pending transaction
Expand Down Expand Up @@ -304,17 +307,8 @@ impl<P: Provider + Sync> Engine<P> {
}
}

// Process blocks
for (block_number, block_timestamp) in blocks.iter() {
if let Some(ref block_tx) = self.block_tx {
block_tx.send(*block_number).await?;
}

self.process_block(*block_number, *block_timestamp).await?;
info!(target: LOG_TARGET, block_number = %block_number, "Processed block.");
}

// Process all transactions
let mut last_block = 0;
for (block_number, transaction_hash) in transactions {
// Process transaction
let transaction = self.provider.get_transaction_by_hash(transaction_hash).await?;
Expand All @@ -326,6 +320,16 @@ impl<P: Provider + Sync> Engine<P> {
blocks[&block_number],
)
.await?;

// Process block
if block_number > last_block {
if let Some(ref block_tx) = self.block_tx {
block_tx.send(block_number).await?;
}

self.process_block(block_number, blocks[&block_number]).await?;
last_block = block_number;
}
}

// We return None for the pending_block_tx because our sync_range
Expand All @@ -348,22 +352,24 @@ impl<P: Provider + Sync> Engine<P> {
}
}

// Process a transaction and its receipt.
// Returns whether the transaction has a world event.
async fn process_transaction_and_receipt(
&mut self,
transaction_hash: Felt,
transaction: &Transaction,
block_number: u64,
block_timestamp: u64,
) -> Result<()> {
) -> Result<bool> {
let receipt = self.provider.get_transaction_receipt(transaction_hash).await?;
let events = match &receipt.receipt {
TransactionReceipt::Invoke(receipt) => Some(&receipt.events),
TransactionReceipt::L1Handler(receipt) => Some(&receipt.events),
_ => None,
};

let mut world_event = false;
if let Some(events) = events {
let mut world_event = false;
for (event_idx, event) in events.iter().enumerate() {
if event.from_address != self.world.address {
continue;
Expand Down Expand Up @@ -397,7 +403,7 @@ impl<P: Provider + Sync> Engine<P> {
}
}

Ok(())
Ok(world_event)
}

async fn process_block(&mut self, block_number: u64, block_timestamp: u64) -> Result<()> {
Expand All @@ -406,6 +412,8 @@ impl<P: Provider + Sync> Engine<P> {
.process(&mut self.db, self.provider.as_ref(), block_number, block_timestamp)
.await?
}

info!(target: LOG_TARGET, block_number = %block_number, "Processed block.");
Ok(())
}

Expand Down
Loading