distributed_topic_tracker/gossip/
mod.rs

1//! Decentralized bootstrap for iroh-gossip topics via DHT auto-discovery.
2//!
3//! Combines iroh-gossip with mainline DHT to enable automatic peer discovery
4//! and topic joining without prior knowledge of peers. Includes bubble detection
5//! and message overlap merging for cluster topology optimization.
6
7mod merge;
8mod receiver;
9mod sender;
10mod topic;
11
12pub use merge::{BubbleMerge, MessageOverlapMerge};
13pub use receiver::GossipReceiver;
14pub use sender::GossipSender;
15use serde::{Deserialize, Serialize};
16pub use topic::{Bootstrap, Publisher, Topic, TopicId};
17
18use crate::RecordPublisher;
19
20/// Record content for peer discovery.
21///
22/// Stored in DHT records to advertise this node's active peers
23/// and recently seen message hashes for cluster merging.
24#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct GossipRecordContent {
26    /// Fixed array of 5 peer node IDs (as 32-byte arrays, empty slots are zero-filled)
27    pub active_peers: [[u8; 32]; 5],
28    /// Fixed array of 5 recent message hashes for overlap detection (empty slots are zero-filled)
29    pub last_message_hashes: [[u8; 32]; 5],
30}
31
32/// Extension trait for iroh Gossip enabling auto-discovery.
33pub trait AutoDiscoveryGossip {
34    /// Subscribe to a topic and bootstrap with DHT peer discovery.
35    ///
36    /// Starts bootstrap and waits until at least one neighbor connection is established.
37    /// Returns a `Topic` for sending/receiving messages.
38    #[allow(async_fn_in_trait)]
39    async fn subscribe_and_join_with_auto_discovery(
40        &self,
41        record_publisher: RecordPublisher,
42    ) -> anyhow::Result<Topic>;
43
44    /// Subscribe to a topic and bootstrap asynchronously.
45    ///
46    /// Returns immediately with a `Topic` while bootstrap proceeds in background.
47    #[allow(async_fn_in_trait)]
48    async fn subscribe_and_join_with_auto_discovery_no_wait(
49        &self,
50        record_publisher: RecordPublisher,
51    ) -> anyhow::Result<Topic>;
52}
53
54impl AutoDiscoveryGossip for iroh_gossip::net::Gossip {
55    async fn subscribe_and_join_with_auto_discovery(
56        &self,
57        record_publisher: RecordPublisher,
58    ) -> anyhow::Result<Topic> {
59        Topic::new(record_publisher, self.clone(), false).await
60    }
61
62    async fn subscribe_and_join_with_auto_discovery_no_wait(
63        &self,
64        record_publisher: RecordPublisher,
65    ) -> anyhow::Result<Topic> {
66        Topic::new(record_publisher, self.clone(), true).await
67    }
68}