Skip to main content

rings_node/dns_relay/directory/
mod.rs

1//! The relay's directory: an owner's announcement, read from its descriptor in the DHT.
2//!
3//! ```text
4//!   announcement(d) = announcement(σ)    σ = the newest descriptor on service_topic(d) that is
5//!                                            signed for d, verifies and is live
6//!                   = ⊥                  no such descriptor, or it does not validate
7//!                   = Err                the DHT fetch failed
8//! ```
9//!
10//! Each call is one fetch: the relay keeps its own memory of answers in front of it
11//! ([`Recall`](rings_dns_relay::Recall)), so a query or a connection rarely reaches the DHT.
12
13use std::sync::Arc;
14
15use async_trait::async_trait;
16use rings_core::dht::entry::Entry;
17#[cfg(test)]
18use rings_core::utils::get_epoch_ms;
19use rings_dns_relay::Announcement;
20use rings_dns_relay::Directory;
21use thiserror::Error;
22
23use super::descriptor::service_topic;
24use super::descriptor::ServiceDescriptor;
25use super::DidOwner;
26use crate::descriptor::heard_descriptors;
27use crate::prelude::rings_core::dht::Did;
28use crate::processor::Processor;
29
30/// Why the DHT could not tell what an owner announces.
31#[derive(Debug, Error)]
32pub enum DirectoryError {
33    /// The DHT key of the owner's service topic cannot be derived.
34    #[error("no DHT key for {did}'s service topic: {source}")]
35    Key {
36        /// The owner.
37        did: Did,
38        /// Why.
39        source: rings_core::error::Error,
40    },
41    /// The fetch of the owner's service topic failed.
42    #[error("cannot fetch {did}'s service descriptor: {source}")]
43    Fetch {
44        /// The owner.
45        did: Did,
46        /// Why.
47        source: crate::error::Error,
48    },
49}
50
51/// The announcement the newest valid descriptor among `descriptors`, each heard at its
52/// element's dot, makes for `did`, at `now_ms` in overlay `network_id`.
53fn announcement_in(
54    descriptors: Vec<ServiceDescriptor>,
55    did: Did,
56    now_ms: u128,
57    network_id: u32,
58) -> Option<Announcement> {
59    let latest = ServiceDescriptor::latest_valid_for(descriptors, did, now_ms, network_id)?;
60    match latest.announcement() {
61        Ok(announcement) => Some(announcement),
62        Err(error) => {
63            tracing::debug!("dns relay: {did} announces an invalid descriptor: {error}");
64            None
65        }
66    }
67}
68
69/// The directory over a node's DHT.
70pub struct DhtDirectory {
71    processor: Arc<Processor>,
72}
73
74impl DhtDirectory {
75    /// The directory reading `processor`'s DHT.
76    pub fn new(processor: Arc<Processor>) -> Self {
77        Self { processor }
78    }
79}
80
81#[async_trait]
82impl Directory for DhtDirectory {
83    type Error = DirectoryError;
84    type Owner = DidOwner;
85
86    async fn announcement(&self, owner: DidOwner) -> Result<Option<Announcement>, DirectoryError> {
87        let did = owner.0;
88        let key = Entry::gen_did(&service_topic(did))
89            .map_err(|source| DirectoryError::Key { did, source })?;
90        let entry = self
91            .processor
92            .fetch_storage_entry(key)
93            .await
94            .map_err(|source| DirectoryError::Fetch { did, source })?;
95        let now_ms = self.processor.clock().now_ms();
96        let network_id = self.processor.swarm.network_id();
97        Ok(entry
98            .and_then(|entry| announcement_in(heard_descriptors(&entry), did, now_ms, network_id)))
99    }
100}
101
102#[cfg(test)]
103mod test_directory;