diff --git a/crates/torii/core/src/engine.rs b/crates/torii/core/src/engine.rs index fed0bb3d95..cd3995055a 100644 --- a/crates/torii/core/src/engine.rs +++ b/crates/torii/core/src/engine.rs @@ -158,7 +158,7 @@ impl Engine

{ // use the start block provided by user if head is 0 let (head, _, _) = self.db.head().await?; if head == 0 { - self.db.set_head(self.config.start_block, 0, 0).await?; + self.db.set_head(self.config.start_block).await?; } else if self.config.start_block != 0 { warn!(target: LOG_TARGET, "Start block ignored, stored head exists and will be used instead."); } @@ -410,7 +410,8 @@ 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, timestamp, world_txns_count).await?; + self.db.set_head(data.block_number - 1).await?; + self.db.set_tps(world_txns_count, timestamp).await?; if let Some(tx) = last_pending_block_tx { self.db.set_last_pending_block_tx(Some(tx))?; } @@ -448,7 +449,8 @@ 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, timestamp, world_txns_count).await?; + self.db.set_head(data.block_number - 1).await?; + self.db.set_tps(world_txns_count, timestamp).await?; if let Some(tx) = last_pending_block_tx { self.db.set_last_pending_block_tx(Some(tx))?; @@ -502,9 +504,10 @@ impl Engine

{ self.process_tasks().await?; let last_block_timestamp = self.get_block_timestamp(data.latest_block_number).await?; - self.db.set_head(data.latest_block_number, last_block_timestamp, transactions_count as u64).await?; - self.db.set_last_pending_block_world_tx(None); - self.db.set_last_pending_block_tx(None); + self.db.set_head(data.latest_block_number).await?; + self.db.set_tps(transactions_count as u64, last_block_timestamp).await?; + self.db.set_last_pending_block_world_tx(None)?; + self.db.set_last_pending_block_tx(None)?; Ok(EngineHead { block_number: data.latest_block_number, diff --git a/crates/torii/core/src/sql.rs b/crates/torii/core/src/sql.rs index 59730c402f..8001333599 100644 --- a/crates/torii/core/src/sql.rs +++ b/crates/torii/core/src/sql.rs @@ -89,12 +89,21 @@ impl Sql { pub async fn set_head( &mut self, head: u64, - last_block_timestamp: u64, - txns_count: u64, ) -> Result<()> { let head = Argument::Int(head.try_into().map_err(|_| anyhow!("Head value {} doesn't fit in u64", head))?); let id = Argument::FieldElement(self.world_address); + self.executor.send(QueryMessage::other( + "UPDATE contracts SET head = ? WHERE id = ?".to_string(), + vec![head, id], + ))?; + + Ok(()) + } + + pub async fn set_tps(&mut self, txns_count: u64, last_block_timestamp: u64) -> Result<()> { + let id = Argument::FieldElement(self.world_address); + let mut conn = self.pool.acquire().await?; let previous_block_timestamp: u64 = sqlx::query_scalar::<_, i64>("SELECT last_block_timestamp FROM contracts WHERE id = ?") @@ -108,16 +117,15 @@ impl Sql { let tps: u64 = if last_block_timestamp - previous_block_timestamp != 0 { txns_count / (last_block_timestamp - previous_block_timestamp) } else { - 0 + txns_count }; - let tps = Argument::Int(tps.try_into().map_err(|_| anyhow!("Tps value {} doesn't fit in u64", tps))?); - let last_block_timestamp = - Argument::Int(last_block_timestamp.try_into().map_err(|_| anyhow!("Last block timestamp value {} doesn't fit in u64", last_block_timestamp))?); + let tps = Argument::Int(tps.try_into().map_err(|_| anyhow!("Tps value {} doesn't fit in u64", tps))?); + let last_block_timestamp = Argument::Int(last_block_timestamp.try_into().map_err(|_| anyhow!("Last block timestamp value {} doesn't fit in u64", last_block_timestamp))?); self.executor.send(QueryMessage::other( - "UPDATE contracts SET head = ?, tps = ?, last_block_timestamp = ? WHERE id = ?".to_string(), - vec![head, tps, last_block_timestamp, id], + "UPDATE contracts SET tps = ?, last_block_timestamp = ? WHERE id = ?".to_string(), + vec![tps, last_block_timestamp, id], ))?; Ok(())