Skip to content

Commit

Permalink
tps
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Oct 4, 2024
1 parent e35d72a commit 244c277
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
15 changes: 9 additions & 6 deletions crates/torii/core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl<P: Provider + Send + Sync + std::fmt::Debug + 'static> Engine<P> {
// 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.");
}
Expand Down Expand Up @@ -410,7 +410,8 @@ 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, 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))?;
}
Expand Down Expand Up @@ -448,7 +449,8 @@ 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, 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))?;
Expand Down Expand Up @@ -502,9 +504,10 @@ impl<P: Provider + Send + Sync + std::fmt::Debug + 'static> Engine<P> {
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,
Expand Down
24 changes: 16 additions & 8 deletions crates/torii/core/src/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ?")
Expand All @@ -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(())
Expand Down

0 comments on commit 244c277

Please sign in to comment.