soil_network/gossip/
validator.rs1use soil_network::common::role::ObservedRole;
8use soil_network::types::PeerId;
9use subsoil::runtime::traits::Block as BlockT;
10
11pub trait Validator<B: BlockT>: Send + Sync {
13 fn new_peer(&self, _context: &mut dyn ValidatorContext<B>, _who: &PeerId, _role: ObservedRole) {
15 }
16
17 fn peer_disconnected(&self, _context: &mut dyn ValidatorContext<B>, _who: &PeerId) {}
19
20 fn validate(
22 &self,
23 context: &mut dyn ValidatorContext<B>,
24 sender: &PeerId,
25 data: &[u8],
26 ) -> ValidationResult<B::Hash>;
27
28 fn message_expired<'a>(&'a self) -> Box<dyn FnMut(B::Hash, &[u8]) -> bool + 'a> {
30 Box::new(move |_topic, _data| false)
31 }
32
33 fn message_allowed<'a>(
35 &'a self,
36 ) -> Box<dyn FnMut(&PeerId, MessageIntent, &B::Hash, &[u8]) -> bool + 'a> {
37 Box::new(move |_who, _intent, _topic, _data| true)
38 }
39}
40
41pub trait ValidatorContext<B: BlockT> {
43 fn broadcast_topic(&mut self, topic: B::Hash, force: bool);
45 fn broadcast_message(&mut self, topic: B::Hash, message: Vec<u8>, force: bool);
47 fn send_message(&mut self, who: &PeerId, message: Vec<u8>);
49 fn send_topic(&mut self, who: &PeerId, topic: B::Hash, force: bool);
51}
52
53#[derive(Eq, PartialEq, Copy, Clone)]
55#[cfg_attr(test, derive(Debug))]
56pub enum MessageIntent {
57 Broadcast,
59 ForcedBroadcast,
61 PeriodicRebroadcast,
63}
64
65pub enum ValidationResult<H> {
67 ProcessAndKeep(H),
69 ProcessAndDiscard(H),
71 Discard,
73}
74
75pub struct DiscardAll;
77
78impl<B: BlockT> Validator<B> for DiscardAll {
79 fn validate(
80 &self,
81 _context: &mut dyn ValidatorContext<B>,
82 _sender: &PeerId,
83 _data: &[u8],
84 ) -> ValidationResult<B::Hash> {
85 ValidationResult::Discard
86 }
87
88 fn message_expired<'a>(&'a self) -> Box<dyn FnMut(B::Hash, &[u8]) -> bool + 'a> {
89 Box::new(move |_topic, _data| true)
90 }
91
92 fn message_allowed<'a>(
93 &'a self,
94 ) -> Box<dyn FnMut(&PeerId, MessageIntent, &B::Hash, &[u8]) -> bool + 'a> {
95 Box::new(move |_who, _intent, _topic, _data| false)
96 }
97}