partner_chains_mock_data_sources/
mc_hash.rs

1use crate::Result;
2use crate::block::BlockDataSourceMock;
3use async_trait::async_trait;
4use sidechain_domain::*;
5use sp_timestamp::Timestamp;
6use std::sync::Arc;
7
8/// Mock MC reference hash data source
9///
10/// This source serves synthetic data generated based on inputs
11pub struct McHashDataSourceMock {
12	block_source: Arc<BlockDataSourceMock>,
13}
14
15impl McHashDataSourceMock {
16	/// Creates a new mock MC reference hash data source
17	pub fn new(inner: Arc<BlockDataSourceMock>) -> Self {
18		Self { block_source: inner }
19	}
20}
21
22#[async_trait]
23impl sidechain_mc_hash::McHashDataSource for McHashDataSourceMock {
24	async fn get_latest_stable_block_for(
25		&self,
26		reference_timestamp: sp_timestamp::Timestamp,
27	) -> Result<Option<MainchainBlock>> {
28		Ok(self
29			.block_source
30			.get_latest_stable_block_for(Timestamp::new(reference_timestamp.as_millis()))
31			.await?)
32	}
33
34	async fn get_stable_block_for(
35		&self,
36		hash: McBlockHash,
37		reference_timestamp: sp_timestamp::Timestamp,
38	) -> Result<Option<MainchainBlock>> {
39		Ok(self
40			.block_source
41			.get_stable_block_for(hash, Timestamp::new(reference_timestamp.as_millis()))
42			.await?)
43	}
44
45	async fn get_block_by_hash(&self, hash: McBlockHash) -> Result<Option<MainchainBlock>> {
46		Ok(self.block_source.get_block_by_hash(hash).await?)
47	}
48}