keri_core/processor/escrow/
duplicitous_events.rs

1use std::sync::Arc;
2
3use crate::{
4    database::{EscrowCreator, EscrowDatabase},
5    error::Error,
6    event_message::signed_event_message::SignedEventMessage,
7    prefix::IdentifierPrefix,
8    processor::notification::{Notification, NotificationBus, Notifier},
9};
10
11pub struct DuplicitousEvents<D: EscrowCreator> {
12    pub(crate) events: D::EscrowDatabaseType,
13}
14
15impl<D: EscrowCreator> DuplicitousEvents<D> {
16    pub fn new(db: Arc<D>) -> Self {
17        let escrow_db = db.create_escrow_db("duplicitous_escrow");
18        Self { events: escrow_db }
19    }
20
21    pub fn get(&self, id: &IdentifierPrefix) -> Result<Vec<SignedEventMessage>, Error> {
22        self.events
23            .get_from_sn(id, 0)
24            .map_err(|_| Error::DbError)
25            .map(|v| v.collect())
26    }
27}
28
29impl<D: EscrowCreator> Notifier for DuplicitousEvents<D> {
30    fn notify(&self, notification: &Notification, _bus: &NotificationBus) -> Result<(), Error> {
31        match notification {
32            Notification::DupliciousEvent(ev_message) => {
33                self.events.insert(ev_message).map_err(|_| Error::DbError)?;
34            }
35            _ => return Err(Error::SemanticError("Wrong notification".into())),
36        }
37
38        Ok(())
39    }
40}