From ac6b6d087f6bef3f62e8b0033e118f1128cb26fd Mon Sep 17 00:00:00 2001 From: Tarek Mohamed Abdalla Date: Fri, 20 Dec 2024 07:44:30 +0200 Subject: [PATCH] Add backend method first_block_hash (#1561) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add backend method first_block_hash * fix comment --------- Co-authored-by: Éloïs --- client/api/src/backend.rs | 3 +++ client/db/src/kv/mod.rs | 4 ++++ client/db/src/sql/mod.rs | 9 +++++++++ 3 files changed, 16 insertions(+) diff --git a/client/api/src/backend.rs b/client/api/src/backend.rs index 8a0406883e..e2b200a5d6 100644 --- a/client/api/src/backend.rs +++ b/client/api/src/backend.rs @@ -53,6 +53,9 @@ pub trait Backend: 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; + /// Get the hash of the latest substrate block fully indexed by the backend. async fn latest_block_hash(&self) -> Result; } diff --git a/client/db/src/kv/mod.rs b/client/db/src/kv/mod.rs index af82834e0d..3a39c4c836 100644 --- a/client/db/src/kv/mod.rs +++ b/client/db/src/kv/mod.rs @@ -90,6 +90,10 @@ impl> fc_api::Backend for Backend< &self.log_indexer } + async fn first_block_hash(&self) -> Result { + Ok(self.client.info().genesis_hash) + } + async fn latest_block_hash(&self) -> Result { Ok(self.client.info().best_hash) } diff --git a/client/db/src/sql/mod.rs b/client/db/src/sql/mod.rs index d5c335a954..b05271c76c 100644 --- a/client/db/src/sql/mod.rs +++ b/client/db/src/sql/mod.rs @@ -820,6 +820,15 @@ impl> fc_api::Backend for Backend { self } + async fn first_block_hash(&self) -> Result { + // 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::, _>(0)[..])) + .map_err(|e| format!("Failed to fetch oldest block hash: {}", e)) + } + async fn latest_block_hash(&self) -> Result { // 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")