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

Expose last synced block to the backend api #218

Closed
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
5 changes: 4 additions & 1 deletion client/api/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -55,6 +55,9 @@ pub trait Backend<Block: BlockT>: Send + Sync {

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

/// Get the block number of the latest synced block.
async fn latest_synced_block(&self) -> Result<<Block::Header as HeaderT>::Number, String>;
}

#[derive(Debug, Eq, PartialEq)]
Expand Down
7 changes: 6 additions & 1 deletion client/db/src/kv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -93,6 +94,10 @@ impl<Block: BlockT, C: HeaderBackend<Block>> fc_api::Backend<Block> for Backend<
async fn latest_block_hash(&self) -> Result<Block::Hash, String> {
Ok(self.client.info().best_hash)
}

async fn latest_synced_block(&self) -> Result<<Block::Header as HeaderT>::Number, String> {
Ok(self.client.info().finalized_number.saturated_into())
}
}

#[derive(Clone, Default)]
Expand Down
11 changes: 11 additions & 0 deletions client/db/src/sql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,17 @@ impl<Block: BlockT<Hash = H256>> fc_api::Backend<Block> for Backend<Block> {
.map(|row| H256::from_slice(&row.get::<Vec<u8>, _>(0)[..]))
.map_err(|e| format!("Failed to fetch best hash: {}", e))
}

async fn latest_synced_block(&self) -> Result<<Block::Header as HeaderT>::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]
Expand Down