soil_client/consensus/
block_validation.rs1use super::BlockStatus;
10use futures::FutureExt as _;
11use std::{error::Error, future::Future, pin::Pin, sync::Arc};
12use subsoil::runtime::traits::Block;
13
14pub trait Chain<B: Block> {
16 fn block_status(&self, hash: B::Hash) -> Result<BlockStatus, Box<dyn Error + Send>>;
18}
19
20impl<T: Chain<B>, B: Block> Chain<B> for Arc<T> {
21 fn block_status(&self, hash: B::Hash) -> Result<BlockStatus, Box<dyn Error + Send>> {
22 (&**self).block_status(hash)
23 }
24}
25
26#[derive(Debug, PartialEq, Eq)]
28pub enum Validation {
29 Success {
31 is_new_best: bool,
33 },
34 Failure {
36 disconnect: bool,
40 },
41}
42
43pub trait BlockAnnounceValidator<B: Block> {
45 fn validate(
56 &mut self,
57 header: &B::Header,
58 data: &[u8],
59 ) -> Pin<Box<dyn Future<Output = Result<Validation, Box<dyn Error + Send>>> + Send>>;
60}
61
62#[derive(Debug)]
64pub struct DefaultBlockAnnounceValidator;
65
66impl<B: Block> BlockAnnounceValidator<B> for DefaultBlockAnnounceValidator {
67 fn validate(
68 &mut self,
69 _: &B::Header,
70 data: &[u8],
71 ) -> Pin<Box<dyn Future<Output = Result<Validation, Box<dyn Error + Send>>> + Send>> {
72 let is_empty = data.is_empty();
73
74 async move {
75 if !is_empty {
76 log::debug!(
77 target: "sync",
78 "Received unknown data alongside the block announcement.",
79 );
80 Ok(Validation::Failure { disconnect: true })
81 } else {
82 Ok(Validation::Success { is_new_best: false })
83 }
84 }
85 .boxed()
86 }
87}