Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions pkgs/dht/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions pkgs/dht/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
13 changes: 13 additions & 0 deletions pkgs/dht/dht-node/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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" }
84 changes: 84 additions & 0 deletions pkgs/dht/dht-node/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<Node> {
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,
})
}
57 changes: 6 additions & 51 deletions pkgs/dht/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<()> {
Expand All @@ -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 {
Expand Down