Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(torii): only update curor when required #2438

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

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
lambda-0x marked this conversation as resolved.
Show resolved Hide resolved
} 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 @@
// 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
lambda-0x marked this conversation as resolved.
Show resolved Hide resolved
return Ok(());
}
_ => {
Expand All @@ -426,7 +426,16 @@

// 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 @@
// 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],
lambda-0x marked this conversation as resolved.
Show resolved Hide resolved
QueryType::Other,
);
}
Expand Down
Loading