From 5f9229ed28d3a3a7f79aab8b72340ee4a0422021 Mon Sep 17 00:00:00 2001 From: Tarrence van As Date: Wed, 24 May 2023 05:14:46 -0400 Subject: [PATCH] Load indexer head from db commit-id:7bdcbb32 --- .spr.yml | 2 +- .../torii/migrations/20230316154230_setup.sql | 2 +- crates/torii/sqlx-data.json | 18 ++++++++++++++++++ crates/torii/src/indexer.rs | 15 ++++++++++++++- crates/torii/src/tests/fixtures/seed.sql | 1 + 5 files changed, 35 insertions(+), 3 deletions(-) diff --git a/.spr.yml b/.spr.yml index 4060e5d1a5..9ab61dbac3 100644 --- a/.spr.yml +++ b/.spr.yml @@ -1,7 +1,7 @@ githubRepoOwner: dojoengine githubRepoName: dojo githubHost: github.com -requireChecks: true +requireChecks: false requireApproval: false githubRemote: origin githubBranch: main diff --git a/crates/torii/migrations/20230316154230_setup.sql b/crates/torii/migrations/20230316154230_setup.sql index 5d7d38bcf6..db3d285509 100644 --- a/crates/torii/migrations/20230316154230_setup.sql +++ b/crates/torii/migrations/20230316154230_setup.sql @@ -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); diff --git a/crates/torii/sqlx-data.json b/crates/torii/sqlx-data.json index de178cea76..a0a5829ff8 100644 --- a/crates/torii/sqlx-data.json +++ b/crates/torii/sqlx-data.json @@ -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": [ diff --git a/crates/torii/src/indexer.rs b/crates/torii/src/indexer.rs index df44225f81..35cfa16c67 100644 --- a/crates/torii/src/indexer.rs +++ b/crates/torii/src/indexer.rs @@ -31,7 +31,7 @@ pub async fn start_indexer( let transaction_processors: Vec>> = vec![]; let event_processors: Vec>> = vec![]; - let mut current_block_number: u64 = 0; + let mut current_block_number = head(pool).await?; loop { let block_with_txs = @@ -87,6 +87,19 @@ pub async fn start_indexer( } } +pub async fn head(conn: &Pool) -> Result { + 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( pool: &Pool, provider: &JsonRpcClient, diff --git a/crates/torii/src/tests/fixtures/seed.sql b/crates/torii/src/tests/fixtures/seed.sql index 09c878011b..c5affd5269 100644 --- a/crates/torii/src/tests/fixtures/seed.sql +++ b/crates/torii/src/tests/fixtures/seed.sql @@ -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');