Skip to content

Commit

Permalink
Add backend method first_block_hash (#1561)
Browse files Browse the repository at this point in the history
* add backend method first_block_hash

* fix comment

---------

Co-authored-by: Éloïs <[email protected]>
  • Loading branch information
TarekkMA and librelois authored Dec 20, 2024
1 parent 347155a commit ac6b6d0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions client/api/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ pub trait Backend<Block: BlockT>: Send + Sync {
self.log_indexer().is_indexed()
}

/// Get the hash of the oldest substrate block fully indexed by the backend.
async fn first_block_hash(&self) -> Result<Block::Hash, String>;

/// Get the hash of the latest substrate block fully indexed by the backend.
async fn latest_block_hash(&self) -> Result<Block::Hash, String>;
}
Expand Down
4 changes: 4 additions & 0 deletions client/db/src/kv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ impl<Block: BlockT, C: HeaderBackend<Block>> fc_api::Backend<Block> for Backend<
&self.log_indexer
}

async fn first_block_hash(&self) -> Result<Block::Hash, String> {
Ok(self.client.info().genesis_hash)
}

async fn latest_block_hash(&self) -> Result<Block::Hash, String> {
Ok(self.client.info().best_hash)
}
Expand Down
9 changes: 9 additions & 0 deletions client/db/src/sql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,15 @@ impl<Block: BlockT<Hash = H256>> fc_api::Backend<Block> for Backend<Block> {
self
}

async fn first_block_hash(&self) -> Result<Block::Hash, String> {
// Retrieves the block hash for the earliest indexed block, maybe it's not canon.
sqlx::query("SELECT substrate_block_hash FROM blocks ORDER BY block_number ASC LIMIT 1")
.fetch_one(self.pool())
.await
.map(|row| H256::from_slice(&row.get::<Vec<u8>, _>(0)[..]))
.map_err(|e| format!("Failed to fetch oldest block hash: {}", e))
}

async fn latest_block_hash(&self) -> Result<Block::Hash, String> {
// Retrieves the block hash for the latest indexed block, maybe it's not canon.
sqlx::query("SELECT substrate_block_hash FROM blocks ORDER BY block_number DESC LIMIT 1")
Expand Down

0 comments on commit ac6b6d0

Please sign in to comment.