Skip to main content

rings_node/mail/
registration.rs

1//! The heartbeat that keeps a recipient's mail descriptor in the DHT.
2//!
3//! The descriptor is a registry element on the recipient's own
4//! [`descriptor_topic`]: a signed lease whose bytes each heartbeat re-appends (the registry
5//! publisher's heartbeat law, #918), so the topic holds
6//! one live descriptor per recipient, written without a remove, and a gateway stops accepting
7//! mail for it within its freshness window once the recipient stops.
8
9use std::time::Duration;
10
11use async_trait::async_trait;
12
13use super::descriptor::descriptor_topic;
14use super::descriptor::MailDescriptor;
15use super::descriptor::MailDescriptorBody;
16use crate::error::Error;
17use crate::error::Result;
18use crate::prelude::rings_core::dht::Did;
19use crate::registration::validate_schedule;
20use crate::registration::DhtRegistrationPublisher;
21use crate::registration::LeaseTerms;
22use crate::registration::RegistrationContext;
23use crate::registration::RegistrationTask;
24use crate::registration::TimingKeys;
25
26/// The default interval between heartbeats of a mail descriptor.
27pub const DEFAULT_MAIL_HEARTBEAT_INTERVAL: Duration = Duration::from_secs(30);
28/// The default freshness window of a mail descriptor: three heartbeats.
29pub const DEFAULT_MAIL_DESCRIPTOR_TTL: Duration = Duration::from_secs(90);
30
31/// The schedule of a recipient's mail descriptor: how often it is published and how long each
32/// publication is live.
33#[derive(Clone, Copy, Debug, Eq, PartialEq)]
34pub struct MailOffer {
35    heartbeat_interval: Duration,
36    ttl: Duration,
37}
38
39impl MailOffer {
40    /// The offer published every `heartbeat_interval` and live for `ttl`.
41    ///
42    /// Post: the schedule satisfies the registry's timing law (`validate_schedule`): the interval
43    /// is below the window and below half the registry lease, and the window is at most the
44    /// lease.
45    pub fn new(heartbeat_interval: Duration, ttl: Duration) -> Result<Self> {
46        let keys = TimingKeys {
47            heartbeat_interval: "mail_heartbeat_interval",
48            ttl: "mail_ttl",
49            enabled_by: None,
50        };
51        validate_schedule(&keys, heartbeat_interval, ttl)?;
52        Ok(Self {
53            heartbeat_interval,
54            ttl,
55        })
56    }
57
58    /// How often the descriptor is published.
59    pub fn heartbeat_interval(&self) -> Duration {
60        self.heartbeat_interval
61    }
62
63    /// How long each published descriptor is live.
64    pub fn ttl(&self) -> Duration {
65        self.ttl
66    }
67}
68
69impl Default for MailOffer {
70    /// [`DEFAULT_MAIL_HEARTBEAT_INTERVAL`] and [`DEFAULT_MAIL_DESCRIPTOR_TTL`], which satisfy
71    /// the timing law.
72    fn default() -> Self {
73        Self {
74            heartbeat_interval: DEFAULT_MAIL_HEARTBEAT_INTERVAL,
75            ttl: DEFAULT_MAIL_DESCRIPTOR_TTL,
76        }
77    }
78}
79
80/// The registration task publishing a recipient's mail descriptor.
81#[derive(Clone, Debug)]
82pub(crate) struct MailRegistration {
83    offer: MailOffer,
84    publisher: DhtRegistrationPublisher<[MailDescriptor; 1]>,
85}
86
87impl MailRegistration {
88    /// The registration of `offer` for the recipient `did`.
89    pub(crate) fn new(did: Did, offer: MailOffer) -> Self {
90        Self {
91            publisher: DhtRegistrationPublisher::new(
92                descriptor_topic(did),
93                offer.heartbeat_interval,
94                offer.ttl,
95            ),
96            offer,
97        }
98    }
99
100    /// This recipient's signed descriptor under the lease `terms`, signed by its delegatee,
101    /// whose key mail is sealed to.
102    pub(crate) fn descriptor_at(
103        &self,
104        context: &RegistrationContext<'_>,
105        terms: LeaseTerms,
106    ) -> Result<MailDescriptor> {
107        let body = MailDescriptorBody {
108            did: context.did(),
109            public_key: context.delegator_verification_pubkey()?,
110            network_id: context.network_id(),
111            fresh_for_ms: terms.fresh_for_ms,
112            expires_at_ms: terms.expires_at_ms,
113        };
114        MailDescriptor::new_signed(body, context.message_signer()).map_err(Error::CoreError)
115    }
116}
117
118#[cfg_attr(rings_browser, async_trait(?Send))]
119#[cfg_attr(rings_native, async_trait)]
120impl RegistrationTask for MailRegistration {
121    fn name(&self) -> &'static str {
122        "mail-descriptor"
123    }
124
125    fn interval(&self) -> Duration {
126        self.offer.heartbeat_interval
127    }
128
129    async fn register_once(&self, context: &RegistrationContext<'_>) -> Result<()> {
130        self.publisher
131            .publish_lease(context, context.now_ms(), |terms| {
132                self.descriptor_at(context, terms)
133                    .map(|descriptor| [descriptor])
134            })
135            .await
136            .map(drop)
137    }
138}