From 8225049f565862d1212a381b231082925d6b72dd Mon Sep 17 00:00:00 2001 From: Neo Sun Date: Sat, 13 Sep 2025 22:42:28 +1200 Subject: [PATCH 1/2] feat(query): add block by hash query --- migrations/20250505144856_headers.sql | 2 +- src/models/block.rs | 6 ++++++ src/models/header.rs | 9 +++++++++ src/schema/root.rs | 18 +++++++++++++++--- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/migrations/20250505144856_headers.sql b/migrations/20250505144856_headers.sql index aa76fd3..d4da6d5 100644 --- a/migrations/20250505144856_headers.sql +++ b/migrations/20250505144856_headers.sql @@ -1,7 +1,7 @@ -- Add migration script here CREATE TABLE IF NOT EXISTS headers ( slot INT PRIMARY KEY, - hash VARCHAR NOT NULL, + hash VARCHAR NOT NULL UNIQUE, parent VARCHAR NOT NULL, parent_state_root VARCHAR NOT NULL, extrinsic_hash VARCHAR NOT NULL, diff --git a/src/models/block.rs b/src/models/block.rs index 5bc3568..90ab3e8 100644 --- a/src/models/block.rs +++ b/src/models/block.rs @@ -127,6 +127,12 @@ impl Block { Ok(Block { slot }) } + /// Get the block data by hash from the database. + pub async fn get_by_hash(pool: &PgPool, hash: &str) -> Result { + let slot = Header::hash_to_slot(pool, hash).await?; + Ok(Block { slot }) + } + /// Get the raw block data from the database. pub async fn raw(pool: &PgPool, slot: i32) -> Result { let raw = query_scalar!("SELECT raw FROM blocks WHERE slot=$1", slot) diff --git a/src/models/header.rs b/src/models/header.rs index 0584725..32594d2 100644 --- a/src/models/header.rs +++ b/src/models/header.rs @@ -79,6 +79,15 @@ impl Header { Ok(data) } + /// Get the slot id for block by hash + pub async fn hash_to_slot(pool: &PgPool, hash: &str) -> Result { + let data = query_scalar!("SELECT slot FROM headers WHERE hash=$1", hash) + .fetch_one(pool) + .await?; + + Ok(data) + } + pub async fn insert( pool: &PgPool, slot: i32, diff --git a/src/schema/root.rs b/src/schema/root.rs index 819f439..f5c58fd 100644 --- a/src/schema/root.rs +++ b/src/schema/root.rs @@ -53,10 +53,22 @@ impl QueryRoot { } /// Get the block data from the database. - async fn block(&self, ctx: &Context<'_>, slot: i32) -> Result> { + async fn block( + &self, + ctx: &Context<'_>, + slot: Option, + hash: Option, + ) -> Result> { let pool = &ctx.data::()?.pg; - let block = Block::get(pool, slot).await.ok(); - Ok(block) + if let Some(slot) = slot { + return Ok(Block::get(pool, slot).await.ok()); + } + + if let Some(hash) = hash { + return Ok(Block::get_by_hash(pool, &hash).await.ok()); + } + + Ok(None) } /// Get the epoch by id From a18a1ac358be748856a35ba01cb5c6d61e4a86a6 Mon Sep 17 00:00:00 2001 From: Neo Sun Date: Sun, 14 Sep 2025 20:23:50 +1200 Subject: [PATCH 2/2] chore(sqlx): sqlx prepare --- ...62de12045f0caa9045a15b243da87fc0ecc31.json | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 .sqlx/query-d01854aab0bb5bd11776dd4987f62de12045f0caa9045a15b243da87fc0ecc31.json diff --git a/.sqlx/query-d01854aab0bb5bd11776dd4987f62de12045f0caa9045a15b243da87fc0ecc31.json b/.sqlx/query-d01854aab0bb5bd11776dd4987f62de12045f0caa9045a15b243da87fc0ecc31.json new file mode 100644 index 0000000..e837da1 --- /dev/null +++ b/.sqlx/query-d01854aab0bb5bd11776dd4987f62de12045f0caa9045a15b243da87fc0ecc31.json @@ -0,0 +1,22 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT slot FROM headers WHERE hash=$1", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "slot", + "type_info": "Int4" + } + ], + "parameters": { + "Left": [ + "Text" + ] + }, + "nullable": [ + false + ] + }, + "hash": "d01854aab0bb5bd11776dd4987f62de12045f0caa9045a15b243da87fc0ecc31" +}