diff --git a/pkgs/dht/Cargo.lock b/pkgs/dht/Cargo.lock index dbc4a93602..e9add7a132 100644 --- a/pkgs/dht/Cargo.lock +++ b/pkgs/dht/Cargo.lock @@ -699,17 +699,26 @@ dependencies = [ [[package]] name = "dht" version = "0.1.0" +dependencies = [ + "anyhow", + "dht-node", + "futures-lite", + "iroh-gossip", + "tokio", + "tracing-subscriber", +] + +[[package]] +name = "dht-node" +version = "0.1.0" dependencies = [ "anyhow", "dht-rotation", "distributed-topic-tracker", "ed25519-dalek", - "futures-lite", "iroh", "iroh-gossip", "rand 0.9.5", - "tokio", - "tracing-subscriber", ] [[package]] diff --git a/pkgs/dht/Cargo.toml b/pkgs/dht/Cargo.toml index 088fda981c..1063ce0618 100644 --- a/pkgs/dht/Cargo.toml +++ b/pkgs/dht/Cargo.toml @@ -4,17 +4,13 @@ version = "0.1.0" edition = "2024" [workspace] -members = [".", "dht-rotation"] +members = [".", "dht-rotation", "dht-node"] resolver = "3" [dependencies] anyhow = "1.0.100" -distributed-topic-tracker = { git = "https://github.com/rustonbsd/distributed-topic-tracker", version = "0.2.5" } -ed25519-dalek = "3.0.0-pre.1" futures-lite = "2" -iroh = "0.95" iroh-gossip = "0.95" -rand = "0.9.2" tokio = { version = "1.48.0", features = ["full"] } tracing-subscriber = { version = "0.3.20", features = ["env-filter"] } -dht-rotation = { path = "dht-rotation" } +dht-node = { path = "dht-node" } diff --git a/pkgs/dht/dht-node/Cargo.toml b/pkgs/dht/dht-node/Cargo.toml new file mode 100644 index 0000000000..5ab5844a39 --- /dev/null +++ b/pkgs/dht/dht-node/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "dht-node" +version = "0.1.0" +edition = "2024" + +[dependencies] +anyhow = "1.0.100" +distributed-topic-tracker = { git = "https://github.com/rustonbsd/distributed-topic-tracker", version = "0.2.5" } +ed25519-dalek = "3.0.0-pre.1" +iroh = "0.95" +iroh-gossip = "0.95" +rand = "0.9.2" +dht-rotation = { path = "../dht-rotation" } diff --git a/pkgs/dht/dht-node/src/lib.rs b/pkgs/dht/dht-node/src/lib.rs new file mode 100644 index 0000000000..44ad55ec14 --- /dev/null +++ b/pkgs/dht/dht-node/src/lib.rs @@ -0,0 +1,84 @@ +//! Endpoint, router, and record-publisher setup for a DHT gossip node. + +use anyhow::Result; +use distributed_topic_tracker::{ + AutoDiscoveryGossip, GossipReceiver, GossipSender, RecordPublisher, RotationHandle, + SecretRotation, TopicId, +}; +use ed25519_dalek::SigningKey; +use iroh::protocol::Router; +use iroh::{Endpoint, SecretKey}; +use iroh_gossip::net::Gossip; + +struct MySecretRotation; + +impl SecretRotation for MySecretRotation { + fn derive( + &self, + topic_hash: [u8; 32], + unix_minute: u64, + initial_secret_hash: [u8; 32], + ) -> [u8; 32] { + dht_rotation::derive(topic_hash, unix_minute, initial_secret_hash) + } +} + +/// The joined topic and its handles. Keep the node alive while using the +/// gossip handles so it retains the endpoint, router, and gossip protocol. +pub struct Node { + pub topic_id: TopicId, + pub sender: GossipSender, + pub receiver: GossipReceiver, + // Keep the original reverse-local drop order: router, gossip, endpoint. + _router: Router, + _gossip: Gossip, + endpoint: Endpoint, +} + +impl Node { + pub fn node_id(&self) -> String { + self.endpoint.id().to_string() + } +} + +/// Build a node and join its topic through the existing auto-discovery path. +pub async fn join() -> Result { + let secret_key = SecretKey::generate(&mut rand::rng()); + let signing_key = SigningKey::from_bytes(&secret_key.to_bytes()); + + let endpoint = Endpoint::builder() + .secret_key(secret_key.clone()) + .bind() + .await?; + + let gossip = Gossip::builder().spawn(endpoint.clone()); + let router = Router::builder(endpoint.clone()) + .accept(iroh_gossip::ALPN, gossip.clone()) + .spawn(); + + let topic_id = TopicId::new("my-iroh-gossip-topic".to_string()); + let initial_secret = b"my-initial-secret".to_vec(); + + let record_publisher = RecordPublisher::new( + topic_id.clone(), + signing_key.verifying_key(), + signing_key.clone(), + Some(RotationHandle::new(MySecretRotation)), + initial_secret, + ); + + let (gossip_sender, gossip_receiver) = gossip + .subscribe_and_join_with_auto_discovery(record_publisher) + .await? + .split() + .await?; + + Ok(Node { + topic_id, + sender: gossip_sender, + receiver: gossip_receiver, + _router: router, + _gossip: gossip, + endpoint, + }) +} diff --git a/pkgs/dht/src/main.rs b/pkgs/dht/src/main.rs index fbc9fb237f..6619a36598 100644 --- a/pkgs/dht/src/main.rs +++ b/pkgs/dht/src/main.rs @@ -1,23 +1,5 @@ use anyhow::Result; -use distributed_topic_tracker::{ - AutoDiscoveryGossip, RecordPublisher, RotationHandle, SecretRotation, TopicId, -}; -use ed25519_dalek::SigningKey; -use iroh::{Endpoint, SecretKey}; -use iroh_gossip::{api::Event, net::Gossip}; - -struct MySecretRotation; - -impl SecretRotation for MySecretRotation { - fn derive( - &self, - topic_hash: [u8; 32], - unix_minute: u64, - initial_secret_hash: [u8; 32], - ) -> [u8; 32] { - dht_rotation::derive(topic_hash, unix_minute, initial_secret_hash) - } -} +use iroh_gossip::api::Event; #[tokio::main] async fn main() -> Result<()> { @@ -30,40 +12,13 @@ async fn main() -> Result<()> { ) .init(); - let secret_key = SecretKey::generate(&mut rand::rng()); - let signing_key = SigningKey::from_bytes(&secret_key.to_bytes()); - - let endpoint = Endpoint::builder() - .secret_key(secret_key.clone()) - .bind() - .await?; - - let gossip = Gossip::builder().spawn(endpoint.clone()); - let _router = iroh::protocol::Router::builder(endpoint.clone()) - .accept(iroh_gossip::ALPN, gossip.clone()) - .spawn(); - - let topic_id = TopicId::new("my-iroh-gossip-topic".to_string()); - let initial_secret = b"my-initial-secret".to_vec(); - - let record_publisher = RecordPublisher::new( - topic_id.clone(), - signing_key.verifying_key(), - signing_key.clone(), - Some(RotationHandle::new(MySecretRotation)), - initial_secret, - ); - - let (gossip_sender, gossip_receiver) = gossip - .subscribe_and_join_with_auto_discovery(record_publisher) - .await? - .split() - .await?; - - println!("joined topic: {}", topic_id.raw()); - let node_id = endpoint.id().to_string(); + let node = dht_node::join().await?; + println!("joined topic: {}", node.topic_id.raw()); + let node_id = node.node_id(); println!("node id: {}", &node_id[..8]); + let gossip_receiver = node.receiver; + let gossip_sender = node.sender; tokio::spawn(async move { while let Some(event) = gossip_receiver.next().await { match event {