Skip to content

Commit ac6b6d0

Browse files
TarekkMAlibrelois
andauthored
Add backend method first_block_hash (polkadot-evm#1561)
* add backend method first_block_hash * fix comment --------- Co-authored-by: Éloïs <c@elo.tf>
1 parent 347155a commit ac6b6d0

3 files changed

Lines changed: 16 additions & 0 deletions

File tree

client/api/src/backend.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ pub trait Backend<Block: BlockT>: Send + Sync {
5353
self.log_indexer().is_indexed()
5454
}
5555

56+
/// Get the hash of the oldest substrate block fully indexed by the backend.
57+
async fn first_block_hash(&self) -> Result<Block::Hash, String>;
58+
5659
/// Get the hash of the latest substrate block fully indexed by the backend.
5760
async fn latest_block_hash(&self) -> Result<Block::Hash, String>;
5861
}

client/db/src/kv/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ impl<Block: BlockT, C: HeaderBackend<Block>> fc_api::Backend<Block> for Backend<
9090
&self.log_indexer
9191
}
9292

93+
async fn first_block_hash(&self) -> Result<Block::Hash, String> {
94+
Ok(self.client.info().genesis_hash)
95+
}
96+
9397
async fn latest_block_hash(&self) -> Result<Block::Hash, String> {
9498
Ok(self.client.info().best_hash)
9599
}

client/db/src/sql/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,15 @@ impl<Block: BlockT<Hash = H256>> fc_api::Backend<Block> for Backend<Block> {
820820
self
821821
}
822822

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

0 commit comments

Comments
 (0)