Skip to content

Commit

Permalink
fix(torii): only update curor when required (#2438)
Browse files Browse the repository at this point in the history
  • Loading branch information
lambda-0x authored Sep 17, 2024
1 parent b391948 commit 21ec45e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
38 changes: 25 additions & 13 deletions crates/torii/core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,9 @@ impl<P: Provider + Send + Sync + std::fmt::Debug + 'static> Engine<P> {

pub async fn start(&mut self) -> Result<()> {
// use the start block provided by user if head is 0
let (head, last_pending_block_world_tx, last_pending_block_tx) = self.db.head().await?;
let (head, _, _) = self.db.head().await?;
if head == 0 {
self.db.set_head(
self.config.start_block,
last_pending_block_world_tx,
last_pending_block_tx,
);
self.db.set_head(self.config.start_block);
} else if self.config.start_block != 0 {
warn!(target: LOG_TARGET, "Start block ignored, stored head exists and will be used instead.");
}
Expand Down Expand Up @@ -396,11 +392,15 @@ impl<P: Provider + Send + Sync + std::fmt::Debug + 'static> Engine<P> {
// provider. So we can fail silently and try
// again in the next iteration.
warn!(target: LOG_TARGET, transaction_hash = %format!("{:#x}", transaction_hash), "Retrieving pending transaction receipt.");
self.db.set_head(
data.block_number - 1,
last_pending_block_world_tx,
last_pending_block_tx,
);
self.db.set_head(data.block_number - 1);
if let Some(tx) = last_pending_block_tx {
self.db.set_last_pending_block_tx(Some(tx));
}

if let Some(tx) = last_pending_block_world_tx {
self.db.set_last_pending_block_world_tx(Some(tx));
}
self.db.execute().await?;
return Ok(());
}
_ => {
Expand All @@ -426,7 +426,16 @@ impl<P: Provider + Send + Sync + std::fmt::Debug + 'static> Engine<P> {

// Set the head to the last processed pending transaction
// Head block number should still be latest block number
self.db.set_head(data.block_number - 1, last_pending_block_world_tx, last_pending_block_tx);
self.db.set_head(data.block_number - 1);

if let Some(tx) = last_pending_block_tx {
self.db.set_last_pending_block_tx(Some(tx));
}

if let Some(tx) = last_pending_block_world_tx {
self.db.set_last_pending_block_world_tx(Some(tx));
}

self.db.execute().await?;

Ok(())
Expand Down Expand Up @@ -466,7 +475,10 @@ impl<P: Provider + Send + Sync + std::fmt::Debug + 'static> Engine<P> {
// Process parallelized events
self.process_tasks().await?;

self.db.set_head(data.latest_block_number, None, None);
self.db.set_head(data.latest_block_number);
self.db.set_last_pending_block_world_tx(None);
self.db.set_last_pending_block_tx(None);

self.db.execute().await?;

Ok(())
Expand Down
32 changes: 23 additions & 9 deletions crates/torii/core/src/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,29 +110,43 @@ impl Sql {
))
}

pub fn set_head(
&mut self,
head: u64,
last_pending_block_world_tx: Option<Felt>,
last_pending_block_tx: Option<Felt>,
) {
pub fn set_head(&mut self, head: u64) {
let head = Argument::Int(head.try_into().expect("doesn't fit in u64"));
let id = Argument::FieldElement(self.world_address);
self.query_queue.enqueue(
"UPDATE contracts SET head = ? WHERE id = ?",
vec![head, id],
QueryType::Other,
);
}

pub fn set_last_pending_block_world_tx(&mut self, last_pending_block_world_tx: Option<Felt>) {
let last_pending_block_world_tx = if let Some(f) = last_pending_block_world_tx {
Argument::String(format!("{:#x}", f))
} else {
Argument::Null
};

let id = Argument::FieldElement(self.world_address);

self.query_queue.enqueue(
"UPDATE contracts SET last_pending_block_world_tx = ? WHERE id = ?",
vec![last_pending_block_world_tx, id],
QueryType::Other,
);
}

pub fn set_last_pending_block_tx(&mut self, last_pending_block_tx: Option<Felt>) {
let last_pending_block_tx = if let Some(f) = last_pending_block_tx {
Argument::String(format!("{:#x}", f))
} else {
Argument::Null
};
let id = Argument::FieldElement(self.world_address);

self.query_queue.enqueue(
"UPDATE contracts SET head = ?, last_pending_block_world_tx = ?, \
last_pending_block_tx = ? WHERE id = ?",
vec![head, last_pending_block_world_tx, last_pending_block_tx, id],
"UPDATE contracts SET last_pending_block_tx = ? WHERE id = ?",
vec![last_pending_block_tx, id],
QueryType::Other,
);
}
Expand Down

0 comments on commit 21ec45e

Please sign in to comment.