From d6ebe9ee7a466250e69bb1e40483094d80645d59 Mon Sep 17 00:00:00 2001 From: lambda-0x <0xlambda@protonmail.com> Date: Tue, 17 Sep 2024 14:10:12 +0530 Subject: [PATCH] fix(torii): only update curor when required commit-id:6ff455c9 --- crates/torii/core/src/engine.rs | 38 ++++++++++++++++++++++----------- crates/torii/core/src/sql.rs | 32 +++++++++++++++++++-------- 2 files changed, 48 insertions(+), 22 deletions(-) diff --git a/crates/torii/core/src/engine.rs b/crates/torii/core/src/engine.rs index 8b2f5685e9..2330091a4d 100644 --- a/crates/torii/core/src/engine.rs +++ b/crates/torii/core/src/engine.rs @@ -138,13 +138,9 @@ impl Engine

{ 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."); } @@ -396,11 +392,15 @@ impl Engine

{ // 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(()); } _ => { @@ -426,7 +426,16 @@ impl Engine

{ // 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(()) @@ -466,7 +475,10 @@ impl Engine

{ // 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(()) diff --git a/crates/torii/core/src/sql.rs b/crates/torii/core/src/sql.rs index bb518b1bc2..ccca4f4c7d 100644 --- a/crates/torii/core/src/sql.rs +++ b/crates/torii/core/src/sql.rs @@ -110,29 +110,43 @@ impl Sql { )) } - pub fn set_head( - &mut self, - head: u64, - last_pending_block_world_tx: Option, - last_pending_block_tx: Option, - ) { + 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) { 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) { 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, ); }