1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
#![allow(unused_imports)] use log::{error, info, debug}; use std::sync::Arc; use fxhash::FxHashSet; use crate::crypto::{Hash}; use super::validator::EventValidator; use super::lint::EventMetadataLinter; use super::transform::EventDataTransformer; use super::sink::{EventSink}; use super::event::*; use super::error::*; use super::transaction::ConversationSession; use super::plugin::*; use super::validator::ValidationResult; #[derive(Debug, Default, Clone)] pub struct AntiReplayPlugin { seen: FxHashSet<Hash>, } impl AntiReplayPlugin { pub fn new() -> AntiReplayPlugin { AntiReplayPlugin { seen: FxHashSet::default(), } } } impl EventSink for AntiReplayPlugin { fn feed(&mut self, header: &EventHeader, _conversation: Option<&Arc<ConversationSession>>) -> Result<(), SinkError> { self.seen.insert(header.raw.event_hash); Ok(()) } fn reset(&mut self) { self.seen.clear(); } } impl EventValidator for AntiReplayPlugin { fn validate(&self, header: &EventHeader, _conversation: Option<&Arc<ConversationSession>>) -> Result<ValidationResult, ValidationError> { match self.seen.contains(&header.raw.event_hash) { true => { debug!("rejected event as it is a duplicate - {}", header.raw.event_hash); Ok(ValidationResult::Deny) }, false => Ok(ValidationResult::Abstain), } } fn clone_validator(&self) -> Box<dyn EventValidator> { Box::new(self.clone()) } } impl EventMetadataLinter for AntiReplayPlugin { fn clone_linter(&self) -> Box<dyn EventMetadataLinter> { Box::new(self.clone()) } } impl EventDataTransformer for AntiReplayPlugin { fn clone_transformer(&self) -> Box<dyn EventDataTransformer> { Box::new(self.clone()) } } impl EventPlugin for AntiReplayPlugin { fn clone_plugin(&self) -> Box<dyn EventPlugin> { Box::new(self.clone()) } }