use soil_network::common::role::ObservedRole;
use soil_network::types::PeerId;
use subsoil::runtime::traits::Block as BlockT;
pub trait Validator<B: BlockT>: Send + Sync {
fn new_peer(&self, _context: &mut dyn ValidatorContext<B>, _who: &PeerId, _role: ObservedRole) {
}
fn peer_disconnected(&self, _context: &mut dyn ValidatorContext<B>, _who: &PeerId) {}
fn validate(
&self,
context: &mut dyn ValidatorContext<B>,
sender: &PeerId,
data: &[u8],
) -> ValidationResult<B::Hash>;
fn message_expired<'a>(&'a self) -> Box<dyn FnMut(B::Hash, &[u8]) -> bool + 'a> {
Box::new(move |_topic, _data| false)
}
fn message_allowed<'a>(
&'a self,
) -> Box<dyn FnMut(&PeerId, MessageIntent, &B::Hash, &[u8]) -> bool + 'a> {
Box::new(move |_who, _intent, _topic, _data| true)
}
}
pub trait ValidatorContext<B: BlockT> {
fn broadcast_topic(&mut self, topic: B::Hash, force: bool);
fn broadcast_message(&mut self, topic: B::Hash, message: Vec<u8>, force: bool);
fn send_message(&mut self, who: &PeerId, message: Vec<u8>);
fn send_topic(&mut self, who: &PeerId, topic: B::Hash, force: bool);
}
#[derive(Eq, PartialEq, Copy, Clone)]
#[cfg_attr(test, derive(Debug))]
pub enum MessageIntent {
Broadcast,
ForcedBroadcast,
PeriodicRebroadcast,
}
pub enum ValidationResult<H> {
ProcessAndKeep(H),
ProcessAndDiscard(H),
Discard,
}
pub struct DiscardAll;
impl<B: BlockT> Validator<B> for DiscardAll {
fn validate(
&self,
_context: &mut dyn ValidatorContext<B>,
_sender: &PeerId,
_data: &[u8],
) -> ValidationResult<B::Hash> {
ValidationResult::Discard
}
fn message_expired<'a>(&'a self) -> Box<dyn FnMut(B::Hash, &[u8]) -> bool + 'a> {
Box::new(move |_topic, _data| true)
}
fn message_allowed<'a>(
&'a self,
) -> Box<dyn FnMut(&PeerId, MessageIntent, &B::Hash, &[u8]) -> bool + 'a> {
Box::new(move |_who, _intent, _topic, _data| false)
}
}