sidechain_block_search/
impl_find_block.rs

1use sp_api::ApiError;
2
3use super::*;
4
5impl<C, Block, CS> FindSidechainBlock<Block, CS> for C
6where
7	C: Client<Block> + Send + Sync + 'static,
8	Block: BlockT,
9	NumberFor<Block>: From<u32> + Into<u32>,
10	CS: CompareStrategy<Block, Self>,
11{
12	type Error = ApiError;
13
14	fn find_block_number(&self, compare_strategy: CS) -> Result<NumberFor<Block>, Self::Error> {
15		let (left_block, right_block): (u32, u32) = (1u32, self.info().best_number.into());
16		let range = left_block..right_block + 1;
17		let f = |block: &u32| compare_strategy.compare_block((*block).into(), self);
18
19		binary_search_by(range, f)
20			.ok_or(ApiError::Application("Could not find block".to_string().into()))
21			.map(|x| x.into())
22	}
23
24	fn find_block(&self, compare_strategy: CS) -> Result<Block::Hash, Self::Error> {
25		let block_number = self.find_block_number(compare_strategy)?;
26		Ok(self
27			.hash(block_number)?
28			.expect("Block with given number exists, so its hash should exists as well"))
29	}
30}