Skip to content

Commit

Permalink
opt(torii): fetch receipts along with blocks instead of fetching them…
Browse files Browse the repository at this point in the history
… individually

commit-id:b6db4cb5
  • Loading branch information
lambda-0x committed Sep 3, 2024
1 parent 13d8020 commit 6ebd806
Showing 1 changed file with 20 additions and 27 deletions.
47 changes: 20 additions & 27 deletions crates/torii/core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use anyhow::Result;
use dojo_world::contracts::world::WorldContractReader;
use hashlink::LinkedHashMap;
use starknet::core::types::{
BlockId, BlockTag, EmittedEvent, Event, EventFilter, Felt, MaybePendingBlockWithTxHashes,
MaybePendingBlockWithTxs, PendingBlockWithTxs, ReceiptBlock, TransactionReceipt,
TransactionReceiptWithBlockInfo,
BlockId, BlockTag, EmittedEvent, Event, EventFilter, Felt, MaybePendingBlockWithReceipts,
MaybePendingBlockWithTxHashes, PendingBlockWithReceipts, ReceiptBlock, TransactionReceipt,
TransactionReceiptWithBlockInfo, TransactionWithReceipt,
};
use starknet::providers::Provider;
use tokio::sync::broadcast::Sender;
Expand Down Expand Up @@ -79,7 +79,7 @@ pub struct FetchRangeResult {

#[derive(Debug)]
pub struct FetchPendingResult {
pub pending_block: Box<PendingBlockWithTxs>,
pub pending_block: Box<PendingBlockWithReceipts>,
pub pending_block_tx: Option<Felt>,
pub block_number: u64,
}
Expand Down Expand Up @@ -304,8 +304,8 @@ impl<P: Provider + Send + Sync + std::fmt::Debug> Engine<P> {
block_number: u64,
pending_block_tx: Option<Felt>,
) -> Result<Option<FetchPendingResult>> {
let block = if let MaybePendingBlockWithTxs::PendingBlock(pending) =
self.provider.get_block_with_txs(BlockId::Tag(BlockTag::Pending)).await?
let block = if let MaybePendingBlockWithReceipts::PendingBlock(pending) =
self.provider.get_block_with_receipts(BlockId::Tag(BlockTag::Pending)).await?

Check warning on line 308 in crates/torii/core/src/engine.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/engine.rs#L307-L308

Added lines #L307 - L308 were not covered by tests
{
pending
} else {
Expand Down Expand Up @@ -342,47 +342,41 @@ impl<P: Provider + Send + Sync + std::fmt::Debug> Engine<P> {
let mut pending_block_tx_cursor = data.pending_block_tx;
let mut pending_block_tx = data.pending_block_tx;
let timestamp = data.pending_block.timestamp;
for transaction in data.pending_block.transactions {
for t in data.pending_block.transactions {
let transaction_hash = t.transaction.transaction_hash();

Check warning on line 346 in crates/torii/core/src/engine.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/engine.rs#L345-L346

Added lines #L345 - L346 were not covered by tests
if let Some(tx) = pending_block_tx_cursor {
if transaction.transaction_hash() != &tx {
if transaction_hash != &tx {

Check warning on line 348 in crates/torii/core/src/engine.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/engine.rs#L348

Added line #L348 was not covered by tests
continue;
}

pending_block_tx_cursor = None;
continue;
}

match self
.process_transaction_with_receipt(
*transaction.transaction_hash(),
data.block_number,
timestamp,
)
.await
{
match self.process_transaction_with_receipt(&t, data.block_number, timestamp).await {

Check warning on line 356 in crates/torii/core/src/engine.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/engine.rs#L356

Added line #L356 was not covered by tests
Err(e) => {
match e.to_string().as_str() {
"TransactionHashNotFound" => {
// We failed to fetch the transaction, which is because
// the transaction might not have been processed fast enough by the
// provider. So we can fail silently and try
// again in the next iteration.
warn!(target: LOG_TARGET, transaction_hash = %format!("{:#x}", transaction.transaction_hash()), "Retrieving pending transaction receipt.");
warn!(target: LOG_TARGET, transaction_hash = %format!("{:#x}", transaction_hash), "Retrieving pending transaction receipt.");

Check warning on line 364 in crates/torii/core/src/engine.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/engine.rs#L364

Added line #L364 was not covered by tests
self.db.set_head(data.block_number - 1, pending_block_tx);
return Ok(());
}
_ => {
error!(target: LOG_TARGET, error = %e, transaction_hash = %format!("{:#x}", transaction.transaction_hash()), "Processing pending transaction.");
error!(target: LOG_TARGET, error = %e, transaction_hash = %format!("{:#x}", transaction_hash), "Processing pending transaction.");

Check warning on line 369 in crates/torii/core/src/engine.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/engine.rs#L369

Added line #L369 was not covered by tests
return Err(e);
}
}
}
Ok(true) => {
pending_block_tx = Some(*transaction.transaction_hash());
info!(target: LOG_TARGET, transaction_hash = %format!("{:#x}", transaction.transaction_hash()), "Processed pending world transaction.");
pending_block_tx = Some(*transaction_hash);
info!(target: LOG_TARGET, transaction_hash = %format!("{:#x}", transaction_hash), "Processed pending world transaction.");

Check warning on line 376 in crates/torii/core/src/engine.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/engine.rs#L375-L376

Added lines #L375 - L376 were not covered by tests
}
Ok(_) => {
info!(target: LOG_TARGET, transaction_hash = %format!("{:#x}", transaction.transaction_hash()), "Processed pending transaction.")
info!(target: LOG_TARGET, transaction_hash = %format!("{:#x}", transaction_hash), "Processed pending transaction.")

Check warning on line 379 in crates/torii/core/src/engine.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/engine.rs#L379

Added line #L379 was not covered by tests
}
}
}
Expand Down Expand Up @@ -489,13 +483,12 @@ impl<P: Provider + Send + Sync + std::fmt::Debug> Engine<P> {
// Returns whether the transaction has a world event.
async fn process_transaction_with_receipt(
&mut self,
transaction_hash: Felt,
transaction_with_receipt: &TransactionWithReceipt,

Check warning on line 486 in crates/torii/core/src/engine.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/engine.rs#L486

Added line #L486 was not covered by tests
block_number: u64,
block_timestamp: u64,
) -> Result<bool> {
let receipt = self.provider.get_transaction_receipt(transaction_hash).await?;

let events = match &receipt.receipt {
let transaction_hash = transaction_with_receipt.transaction.transaction_hash();
let events = match &transaction_with_receipt.receipt {

Check warning on line 491 in crates/torii/core/src/engine.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/engine.rs#L490-L491

Added lines #L490 - L491 were not covered by tests
TransactionReceipt::Invoke(receipt) => Some(&receipt.events),
TransactionReceipt::L1Handler(receipt) => Some(&receipt.events),
_ => None,
Expand All @@ -510,15 +503,15 @@ impl<P: Provider + Send + Sync + std::fmt::Debug> Engine<P> {

world_event = true;
let event_id =
format!("{:#064x}:{:#x}:{:#04x}", block_number, transaction_hash, event_idx);
format!("{:#064x}:{:#x}:{:#04x}", block_number, *transaction_hash, event_idx);

Check warning on line 506 in crates/torii/core/src/engine.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/engine.rs#L506

Added line #L506 was not covered by tests

Self::process_event(
self,
block_number,
block_timestamp,
&event_id,
event,
transaction_hash,
*transaction_hash,

Check warning on line 514 in crates/torii/core/src/engine.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/engine.rs#L514

Added line #L514 was not covered by tests
)
.await?;
}
Expand Down

0 comments on commit 6ebd806

Please sign in to comment.