From 2b9f6cc0c81ad1ec5954495563d63c6d966617fb Mon Sep 17 00:00:00 2001 From: Gonza Montiel Date: Thu, 4 Jul 2024 02:06:56 +0200 Subject: [PATCH] expose last synced block on frontiers backend api --- client/api/src/backend.rs | 5 ++++- client/db/src/kv/mod.rs | 7 ++++++- client/db/src/sql/mod.rs | 11 +++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/client/api/src/backend.rs b/client/api/src/backend.rs index 8a0406883e..27bbfad979 100644 --- a/client/api/src/backend.rs +++ b/client/api/src/backend.rs @@ -19,7 +19,7 @@ use scale_codec::{Decode, Encode}; // Substrate use sp_core::{H160, H256}; -use sp_runtime::traits::Block as BlockT; +use sp_runtime::traits::{Block as BlockT, Header as HeaderT}; // Frontier use fp_storage::EthereumStorageSchema; @@ -55,6 +55,9 @@ pub trait Backend: Send + Sync { /// Get the hash of the latest substrate block fully indexed by the backend. async fn latest_block_hash(&self) -> Result; + + /// Get the block number of the latest synced block. + async fn latest_synced_block(&self) -> Result<::Number, String>; } #[derive(Debug, Eq, PartialEq)] diff --git a/client/db/src/kv/mod.rs b/client/db/src/kv/mod.rs index af82834e0d..7c98731048 100644 --- a/client/db/src/kv/mod.rs +++ b/client/db/src/kv/mod.rs @@ -33,7 +33,8 @@ pub use sc_client_db::DatabaseSource; use sp_blockchain::HeaderBackend; use sp_core::{H160, H256}; pub use sp_database::Database; -use sp_runtime::traits::Block as BlockT; +use sp_runtime::traits::{Block as BlockT, Header as HeaderT}; +use sp_runtime::SaturatedConversion; // Frontier use fc_api::{FilteredLog, TransactionMetadata}; use fp_storage::{EthereumStorageSchema, PALLET_ETHEREUM_SCHEMA_CACHE}; @@ -93,6 +94,10 @@ impl> fc_api::Backend for Backend< async fn latest_block_hash(&self) -> Result { Ok(self.client.info().best_hash) } + + async fn latest_synced_block(&self) -> Result<::Number, String> { + Ok(self.client.info().finalized_number.saturated_into()) + } } #[derive(Clone, Default)] diff --git a/client/db/src/sql/mod.rs b/client/db/src/sql/mod.rs index 86cebf59f4..e7e669b597 100644 --- a/client/db/src/sql/mod.rs +++ b/client/db/src/sql/mod.rs @@ -831,6 +831,17 @@ impl> fc_api::Backend for Backend { .map(|row| H256::from_slice(&row.get::, _>(0)[..])) .map_err(|e| format!("Failed to fetch best hash: {}", e)) } + + async fn latest_synced_block(&self) -> Result<::Number, String> { + sqlx::query("SELECT MAX(id) FROM sync_status") + .fetch_one(self.pool()) + .await + .map(|row| { + let block_number: u32 = row.get(0); + return block_number.into() + }) + .map_err(|e| format!("Failed to fetch block number: {}", e)) + } } #[async_trait::async_trait]