Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add backend method first_block_hash #1561

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
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
Loading