Skip to content
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion migrations/20250505144856_headers.sql
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
6 changes: 6 additions & 0 deletions src/models/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
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<String> {
let raw = query_scalar!("SELECT raw FROM blocks WHERE slot=$1", slot)
Expand Down
9 changes: 9 additions & 0 deletions src/models/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ impl Header {
Ok(data)
}

/// Get the slot id for block by hash
pub async fn hash_to_slot(pool: &PgPool, hash: &str) -> Result<i32> {
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,
Expand Down
18 changes: 15 additions & 3 deletions src/schema/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,22 @@ impl QueryRoot {
}

/// Get the block data from the database.
async fn block(&self, ctx: &Context<'_>, slot: i32) -> Result<Option<Block>> {
async fn block(
&self,
ctx: &Context<'_>,
slot: Option<i32>,
hash: Option<String>,
) -> Result<Option<Block>> {
let pool = &ctx.data::<Manager>()?.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
Expand Down
Loading