keri_core/processor/escrow/
mod.rs

1pub mod delegation_escrow;
2pub mod duplicitous_events;
3pub mod maybe_out_of_order_escrow;
4pub mod partially_signed_escrow;
5pub mod partially_witnessed_escrow;
6#[cfg(feature = "query")]
7pub mod reply_escrow;
8
9use std::{fmt::Debug, sync::Arc, time::Duration};
10
11use delegation_escrow::DelegationEscrow;
12use duplicitous_events::DuplicitousEvents;
13use maybe_out_of_order_escrow::MaybeOutOfOrderEscrow;
14use partially_signed_escrow::PartiallySignedEscrow;
15use partially_witnessed_escrow::PartiallyWitnessedEscrow;
16
17use super::notification::{JustNotification, NotificationBus};
18use crate::database::{EscrowCreator, EventDatabase};
19
20#[derive(Debug, Clone)]
21pub struct EscrowConfig {
22    pub out_of_order_timeout: Duration,
23    pub partially_signed_timeout: Duration,
24    pub partially_witnessed_timeout: Duration,
25    pub trans_receipt_timeout: Duration,
26    pub delegation_timeout: Duration,
27}
28
29impl Default for EscrowConfig {
30    fn default() -> Self {
31        Self {
32            out_of_order_timeout: Duration::from_secs(60),
33            partially_signed_timeout: Duration::from_secs(60),
34            partially_witnessed_timeout: Duration::from_secs(60),
35            trans_receipt_timeout: Duration::from_secs(60),
36            delegation_timeout: Duration::from_secs(60),
37        }
38    }
39}
40
41pub fn default_escrow_bus<D>(
42    event_db: Arc<D>,
43    escrow_config: EscrowConfig,
44) -> (
45    NotificationBus,
46    (
47        Arc<MaybeOutOfOrderEscrow<D>>,
48        Arc<PartiallySignedEscrow<D>>,
49        Arc<PartiallyWitnessedEscrow<D>>,
50        Arc<DelegationEscrow<D>>,
51        Arc<DuplicitousEvents<D>>,
52    ),
53)
54where
55    D: EventDatabase + EscrowCreator + Sync + Send + 'static,
56{
57    let mut bus = NotificationBus::new();
58
59    // Register out of order escrow, to save and reprocess out of order events
60    let ooo_escrow = Arc::new(MaybeOutOfOrderEscrow::new(
61        event_db.clone(),
62        escrow_config.out_of_order_timeout,
63    ));
64    println!(
65        "Registering out of order escrow with timeout: {:?}",
66        escrow_config.out_of_order_timeout
67    );
68    bus.register_observer(
69        ooo_escrow.clone(),
70        vec![
71            JustNotification::OutOfOrder,
72            JustNotification::KeyEventAdded,
73        ],
74    );
75
76    let ps_escrow = Arc::new(PartiallySignedEscrow::new(
77        event_db.clone(),
78        escrow_config.partially_signed_timeout,
79    ));
80    bus.register_observer(ps_escrow.clone(), vec![JustNotification::PartiallySigned]);
81
82    let pw_escrow = Arc::new(PartiallyWitnessedEscrow::new(
83        event_db.clone(),
84        event_db.get_log_db(),
85        escrow_config.partially_witnessed_timeout,
86    ));
87    bus.register_observer(
88        pw_escrow.clone(),
89        vec![
90            JustNotification::PartiallyWitnessed,
91            JustNotification::ReceiptOutOfOrder,
92        ],
93    );
94
95    let delegation_escrow = Arc::new(DelegationEscrow::new(
96        event_db.clone(),
97        escrow_config.delegation_timeout,
98    ));
99    bus.register_observer(
100        delegation_escrow.clone(),
101        vec![
102            JustNotification::MissingDelegatingEvent,
103            JustNotification::KeyEventAdded,
104        ],
105    );
106
107    let dup = Arc::new(DuplicitousEvents::new(event_db));
108    bus.register_observer(dup.clone(), vec![JustNotification::DuplicitousEvent]);
109
110    (
111        bus,
112        (ooo_escrow, ps_escrow, pw_escrow, delegation_escrow, dup),
113    )
114}