sc_network_gossip/
validator.rs1use sc_network_common::role::ObservedRole;
20use sc_network_types::PeerId;
21use sp_runtime::traits::Block as BlockT;
22
23pub trait Validator<B: BlockT>: Send + Sync {
25 fn new_peer(&self, _context: &mut dyn ValidatorContext<B>, _who: &PeerId, _role: ObservedRole) {
27 }
28
29 fn peer_disconnected(&self, _context: &mut dyn ValidatorContext<B>, _who: &PeerId) {}
31
32 fn validate(
34 &self,
35 context: &mut dyn ValidatorContext<B>,
36 sender: &PeerId,
37 data: &[u8],
38 ) -> ValidationResult<B::Hash>;
39
40 fn message_expired<'a>(&'a self) -> Box<dyn FnMut(B::Hash, &[u8]) -> bool + 'a> {
42 Box::new(move |_topic, _data| false)
43 }
44
45 fn message_allowed<'a>(
47 &'a self,
48 ) -> Box<dyn FnMut(&PeerId, MessageIntent, &B::Hash, &[u8]) -> bool + 'a> {
49 Box::new(move |_who, _intent, _topic, _data| true)
50 }
51}
52
53pub trait ValidatorContext<B: BlockT> {
55 fn broadcast_topic(&mut self, topic: B::Hash, force: bool);
57 fn broadcast_message(&mut self, topic: B::Hash, message: Vec<u8>, force: bool);
59 fn send_message(&mut self, who: &PeerId, message: Vec<u8>);
61 fn send_topic(&mut self, who: &PeerId, topic: B::Hash, force: bool);
63}
64
65#[derive(Eq, PartialEq, Copy, Clone)]
67#[cfg_attr(test, derive(Debug))]
68pub enum MessageIntent {
69 Broadcast,
71 ForcedBroadcast,
73 PeriodicRebroadcast,
75}
76
77pub enum ValidationResult<H> {
79 ProcessAndKeep(H),
81 ProcessAndDiscard(H),
83 Discard,
85}
86
87pub struct DiscardAll;
89
90impl<B: BlockT> Validator<B> for DiscardAll {
91 fn validate(
92 &self,
93 _context: &mut dyn ValidatorContext<B>,
94 _sender: &PeerId,
95 _data: &[u8],
96 ) -> ValidationResult<B::Hash> {
97 ValidationResult::Discard
98 }
99
100 fn message_expired<'a>(&'a self) -> Box<dyn FnMut(B::Hash, &[u8]) -> bool + 'a> {
101 Box::new(move |_topic, _data| true)
102 }
103
104 fn message_allowed<'a>(
105 &'a self,
106 ) -> Box<dyn FnMut(&PeerId, MessageIntent, &B::Hash, &[u8]) -> bool + 'a> {
107 Box::new(move |_who, _intent, _topic, _data| false)
108 }
109}