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