partner_chains_db_sync_data_sources/mc_hash/
mod.rs

1//! Db-Sync data source used by Partner Chain MC Reference feature
2use crate::block::BlockDataSourceImpl;
3use partner_chains_data_source_metrics::{McFollowerMetrics, observed_async_trait};
4use sidechain_domain::{MainchainBlock, McBlockHash};
5use sidechain_mc_hash::McHashDataSource;
6use sp_timestamp::Timestamp;
7use std::sync::Arc;
8
9/// Db-Sync data source used by the Main Chain Reference feature of Partner Chain toolkit.
10///
11/// See [sidechain_mc_hash] a detailed explanation of its purpose.
12pub struct McHashDataSourceImpl {
13	/// [BlockDataSourceImpl] instance shared with other data sources for cache reuse.
14	inner: Arc<BlockDataSourceImpl>,
15	/// Prometheus metrics client
16	metrics_opt: Option<McFollowerMetrics>,
17}
18
19impl McHashDataSourceImpl {
20	/// Creates a new MC Hash data source by wrapping an instance of [BlockDataSourceImpl]
21	pub fn new(inner: Arc<BlockDataSourceImpl>, metrics_opt: Option<McFollowerMetrics>) -> Self {
22		Self { inner, metrics_opt }
23	}
24}
25
26observed_async_trait!(
27impl McHashDataSource for McHashDataSourceImpl {
28	async fn get_latest_stable_block_for(
29		&self,
30		reference_timestamp: sp_timestamp::Timestamp,
31	) -> std::result::Result<Option<MainchainBlock>, Box<dyn std::error::Error + Send + Sync>> {
32		Ok(self
33			.inner
34			.get_latest_stable_block_for(Timestamp::new(reference_timestamp.as_millis()))
35			.await?)
36	}
37
38	async fn get_stable_block_for(
39		&self,
40		hash: McBlockHash,
41		reference_timestamp: sp_timestamp::Timestamp,
42	) -> std::result::Result<Option<MainchainBlock>, Box<dyn std::error::Error + Send + Sync>> {
43		Ok(self
44			.inner
45			.get_stable_block_for(hash, Timestamp::new(reference_timestamp.as_millis()))
46			.await?)
47	}
48
49	async fn get_block_by_hash(
50		&self,
51		hash: McBlockHash,
52	) -> std::result::Result<Option<MainchainBlock>, Box<dyn std::error::Error + Send + Sync>> {
53		Ok(self.inner.get_block_by_hash(hash).await?)
54	}
55}
56);