From 76a84ec9b16189fede1b3555e3bd457b75281add Mon Sep 17 00:00:00 2001 From: link2xt Date: Fri, 21 Jun 2024 07:57:33 +0000 Subject: [PATCH] refactor: store public key instead of secret key for peer channels We only need public key, so there is no need to derive it from secret key every time. --- src/peer_channels.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/peer_channels.rs b/src/peer_channels.rs index a481596b00..d90a2da41f 100644 --- a/src/peer_channels.rs +++ b/src/peer_channels.rs @@ -27,8 +27,9 @@ use anyhow::{anyhow, Context as _, Result}; use email::Header; use iroh_gossip::net::{Gossip, JoinTopicFut, GOSSIP_ALPN}; use iroh_gossip::proto::{Event as IrohEvent, TopicId}; +use iroh_net::key::{PublicKey, SecretKey}; use iroh_net::relay::{RelayMap, RelayUrl}; -use iroh_net::{key::SecretKey, relay::RelayMode, Endpoint}; +use iroh_net::{relay::RelayMode, Endpoint}; use iroh_net::{NodeAddr, NodeId}; use std::collections::{BTreeSet, HashMap}; use std::env; @@ -60,8 +61,10 @@ pub struct Iroh { /// Topics for which an advertisement has already been sent. pub(crate) iroh_channels: RwLock>, - /// Currently used Iroh secret key - pub(crate) secret_key: SecretKey, + /// Currently used Iroh public key. + /// + /// This is attached to every message to work around `iroh_gossip` deduplication. + pub(crate) public_key: PublicKey, } impl Iroh { @@ -154,7 +157,7 @@ impl Iroh { let seq_num = self.get_and_incr(&topic).await; data.extend(seq_num.to_le_bytes()); - data.extend(self.secret_key.public().as_bytes()); + data.extend(self.public_key.as_bytes()); self.gossip.broadcast(topic, data.into()).await?; @@ -214,7 +217,8 @@ impl ChannelState { impl Context { /// Create magic endpoint and gossip. async fn init_peer_channels(&self) -> Result { - let secret_key: SecretKey = SecretKey::generate(); + let secret_key = SecretKey::generate(); + let public_key = secret_key.public(); let relay_mode = if let Some(relay_url) = self .metadata @@ -231,7 +235,7 @@ impl Context { }; let endpoint = Endpoint::builder() - .secret_key(secret_key.clone()) + .secret_key(secret_key) .alpns(vec![GOSSIP_ALPN.to_vec()]) .relay_mode(relay_mode) .bind(0) @@ -251,7 +255,7 @@ impl Context { endpoint, gossip, iroh_channels: RwLock::new(HashMap::new()), - secret_key, + public_key, }) }