Skip to content

Commit

Permalink
Load indexer head from db
Browse files Browse the repository at this point in the history
commit-id:7bdcbb32
  • Loading branch information
tarrencev committed May 25, 2023
1 parent 4a1cc78 commit 5f9229e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .spr.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
githubRepoOwner: dojoengine
githubRepoName: dojo
githubHost: github.com
requireChecks: true
requireChecks: false
requireApproval: false
githubRemote: origin
githubBranch: main
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/migrations/20230316154230_setup.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CREATE TABLE indexer (
id INTEGER PRIMARY KEY AUTOINCREMENT,
head BIGINT DEFAULT 0
head BIGINT NOT NULL DEFAULT 0
);

INSERT INTO indexer (head) VALUES (0);
Expand Down
18 changes: 18 additions & 0 deletions crates/torii/sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,24 @@
},
"query": "\n SELECT\n id,\n name,\n address,\n class_hash,\n transaction_hash,\n created_at as \"created_at: _\"\n FROM systems\n "
},
"86da9bb2c020578e868d98cc62fefb4807f6290d669bf9daf5939b48dd8cde51": {
"describe": {
"columns": [
{
"name": "head",
"ordinal": 0,
"type_info": "Int64"
}
],
"nullable": [
true
],
"parameters": {
"Right": 0
}
},
"query": "\n SELECT head\n FROM indexer WHERE id = 0\n "
},
"afb85d9756ae0521332b11ada4482a57f2626e188dce043fd0a4cb456245821d": {
"describe": {
"columns": [
Expand Down
15 changes: 14 additions & 1 deletion crates/torii/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub async fn start_indexer<T: JsonRpcTransport + Sync + Send>(
let transaction_processors: Vec<Arc<dyn TransactionProcessor<T>>> = vec![];
let event_processors: Vec<Arc<dyn EventProcessor<T>>> = vec![];

let mut current_block_number: u64 = 0;
let mut current_block_number = head(pool).await?;

loop {
let block_with_txs =
Expand Down Expand Up @@ -87,6 +87,19 @@ pub async fn start_indexer<T: JsonRpcTransport + Sync + Send>(
}
}

pub async fn head(conn: &Pool<Sqlite>) -> Result<u64, sqlx::Error> {
let indexer = sqlx::query!(
r#"
SELECT head
FROM indexer WHERE id = 0
"#
)
.fetch_one(conn)
.await?;

Ok(indexer.head.unwrap().try_into().expect("value too large for i64"))
}

async fn process_block<T: starknet::providers::jsonrpc::JsonRpcTransport>(
pool: &Pool<Sqlite>,
provider: &JsonRpcClient<T>,
Expand Down
1 change: 1 addition & 0 deletions crates/torii/src/tests/fixtures/seed.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* seed db with mock data, spawning a game and two players */
INSERT INTO indexer (head) VALUES (0);

/* register components and systems */
INSERT INTO components (id, name, properties, address, class_hash, transaction_hash) VALUES ('component_1', 'Game', NULL, '0x0', '0x0', '0x0');
Expand Down

0 comments on commit 5f9229e

Please sign in to comment.