Skip to content

Commit

Permalink
refactor(torii-core): fix double pending indexing & use btreemap for …
Browse files Browse the repository at this point in the history
…sorted blocks (dojoengine#2009)
  • Loading branch information
Larkooo authored May 27, 2024
1 parent 03aefc3 commit 3a19201
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions crates/torii/core/src/engine.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::BTreeMap;
use std::time::Duration;

use anyhow::Result;
Expand Down Expand Up @@ -207,7 +207,7 @@ impl<P: Provider + Sync> Engine<P> {
&mut self,
from: u64,
to: u64,
mut pending_block_tx: Option<FieldElement>,
pending_block_tx: Option<FieldElement>,
) -> Result<Option<FieldElement>> {
// Process all blocks from current to latest.
let get_events = |token: Option<String>| {
Expand All @@ -232,10 +232,11 @@ impl<P: Provider + Sync> Engine<P> {

// Transactions & blocks to process
let mut last_block = 0_u64;
let mut blocks = HashMap::new();
let mut blocks = BTreeMap::new();

// Flatten events pages and events according to the pending block cursor
// to array of (block_number, transaction_hash)
let mut pending_block_tx_cursor = pending_block_tx;
let mut transactions = vec![];
for events_page in &events_pages {
for event in &events_page.events {
Expand Down Expand Up @@ -269,14 +270,20 @@ impl<P: Provider + Sync> Engine<P> {

// Then we skip all transactions until we reach the last pending processed
// transaction (if any)
if let Some(tx) = pending_block_tx {
if let Some(tx) = pending_block_tx_cursor {
if event.transaction_hash != tx {
continue;
}

// Then we skip that processed transaction
pending_block_tx = None;
continue;
pending_block_tx_cursor = None;
}

// Skip the latest pending block transaction events
// * as we might have multiple events for the same transaction
if let Some(tx) = pending_block_tx {
if event.transaction_hash == tx {
continue;
}
}

if let Some((_, last_tx_hash)) = transactions.last() {
Expand Down

0 comments on commit 3a19201

Please sign in to comment.