Skip to content

Commit

Permalink
fix(torii): only update curor when required
Browse files Browse the repository at this point in the history
commit-id:6ff455c9
  • Loading branch information
lambda-0x committed Sep 17, 2024
1 parent b391948 commit d6ebe9e
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?;

Check warning on line 141 in crates/torii/core/src/engine.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/engine.rs#L141

Added line #L141 was not covered by tests
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);

Check warning on line 143 in crates/torii/core/src/engine.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/engine.rs#L143

Added line #L143 was not covered by tests
} 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));
}

Check warning on line 398 in crates/torii/core/src/engine.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/engine.rs#L395-L398

Added lines #L395 - L398 were not covered by tests

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

Check warning on line 403 in crates/torii/core/src/engine.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/engine.rs#L400-L403

Added lines #L400 - L403 were not covered by tests
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);

Check warning on line 429 in crates/torii/core/src/engine.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/engine.rs#L429

Added line #L429 was not covered by tests

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

Check warning on line 433 in crates/torii/core/src/engine.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/engine.rs#L431-L433

Added lines #L431 - L433 were not covered by tests

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

Check warning on line 437 in crates/torii/core/src/engine.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/engine.rs#L435-L437

Added lines #L435 - L437 were not covered by tests

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 d6ebe9e

Please sign in to comment.