partner_chains_db_sync_data_sources/mc_hash/
mod.rs

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