From e36e9486f7555aa178b8a6f7ed7ebdb1af10611a Mon Sep 17 00:00:00 2001 From: "Franz Heinzmann (Frando)" Date: Wed, 19 Jun 2024 14:42:15 +0200 Subject: [PATCH] more cleanup --- iroh/src/node.rs | 1 + iroh/src/node/builder.rs | 155 ++++++++++++++++++++------------------- 2 files changed, 81 insertions(+), 75 deletions(-) diff --git a/iroh/src/node.rs b/iroh/src/node.rs index 5f90f5c48df..9848861a289 100644 --- a/iroh/src/node.rs +++ b/iroh/src/node.rs @@ -212,6 +212,7 @@ impl NodeInner { ipv4, ipv6.map(|addr| format!(" and {addr}")).unwrap_or_default() ); + debug!("rpc listening at: {:?}", external_rpc.local_addr()); let mut join_set = JoinSet::new(); diff --git a/iroh/src/node/builder.rs b/iroh/src/node/builder.rs index 2fadc6c973e..a3719b4503a 100644 --- a/iroh/src/node/builder.rs +++ b/iroh/src/node/builder.rs @@ -102,6 +102,18 @@ pub enum StorageConfig { Persistent(PathBuf), } +impl StorageConfig { + fn default_author_storage(&self) -> DefaultAuthorStorage { + match self { + StorageConfig::Persistent(ref root) => { + let path = IrohPaths::DefaultAuthor.with_root(root); + DefaultAuthorStorage::Persistent(path) + } + StorageConfig::Mem => DefaultAuthorStorage::Mem, + } + } +} + /// Configuration for node discovery. #[derive(Debug, Default)] pub enum DiscoveryConfig { @@ -394,59 +406,60 @@ where trace!("building node"); let lp = LocalPoolHandle::new(num_cpus::get()); - let mut transport_config = quinn::TransportConfig::default(); - transport_config - .max_concurrent_bidi_streams(MAX_STREAMS.try_into()?) - .max_concurrent_uni_streams(0u32.into()); - - let discovery: Option> = match self.node_discovery { - DiscoveryConfig::None => None, - DiscoveryConfig::Custom(discovery) => Some(discovery), - DiscoveryConfig::Default => { - let discovery = ConcurrentDiscovery::from_services(vec![ - // Enable DNS discovery by default - Box::new(DnsDiscovery::n0_dns()), - // Enable pkarr publishing by default - Box::new(PkarrPublisher::n0_dns(self.secret_key.clone())), - ]); - Some(Box::new(discovery)) - } - }; - - let endpoint = Endpoint::builder() - .secret_key(self.secret_key.clone()) - .proxy_from_env() - .keylog(self.keylog) - .transport_config(transport_config) - .concurrent_connections(MAX_CONNECTIONS) - .relay_mode(self.relay_mode); - let endpoint = match discovery { - Some(discovery) => endpoint.discovery(discovery), - None => endpoint, - }; - let endpoint = match self.dns_resolver { - Some(resolver) => endpoint.dns_resolver(resolver), - None => endpoint, - }; - - #[cfg(any(test, feature = "test-utils"))] - let endpoint = - endpoint.insecure_skip_relay_cert_verify(self.insecure_skip_relay_cert_verify); + let endpoint = { + let mut transport_config = quinn::TransportConfig::default(); + transport_config + .max_concurrent_bidi_streams(MAX_STREAMS.try_into()?) + .max_concurrent_uni_streams(0u32.into()); + + let discovery: Option> = match self.node_discovery { + DiscoveryConfig::None => None, + DiscoveryConfig::Custom(discovery) => Some(discovery), + DiscoveryConfig::Default => { + let discovery = ConcurrentDiscovery::from_services(vec![ + // Enable DNS discovery by default + Box::new(DnsDiscovery::n0_dns()), + // Enable pkarr publishing by default + Box::new(PkarrPublisher::n0_dns(self.secret_key.clone())), + ]); + Some(Box::new(discovery)) + } + }; + + let endpoint = Endpoint::builder() + .secret_key(self.secret_key.clone()) + .proxy_from_env() + .keylog(self.keylog) + .transport_config(transport_config) + .concurrent_connections(MAX_CONNECTIONS) + .relay_mode(self.relay_mode); + let endpoint = match discovery { + Some(discovery) => endpoint.discovery(discovery), + None => endpoint, + }; + let endpoint = match self.dns_resolver { + Some(resolver) => endpoint.dns_resolver(resolver), + None => endpoint, + }; - let endpoint = match self.storage { - StorageConfig::Persistent(ref root) => { - let peers_data_path = IrohPaths::PeerData.with_root(root); - endpoint.peers_data_path(peers_data_path) - } - StorageConfig::Mem => endpoint, + #[cfg(any(test, feature = "test-utils"))] + let endpoint = + endpoint.insecure_skip_relay_cert_verify(self.insecure_skip_relay_cert_verify); + + let endpoint = match self.storage { + StorageConfig::Persistent(ref root) => { + let peers_data_path = IrohPaths::PeerData.with_root(root); + endpoint.peers_data_path(peers_data_path) + } + StorageConfig::Mem => endpoint, + }; + let bind_port = self.bind_port.unwrap_or(DEFAULT_BIND_PORT); + endpoint.bind(bind_port).await? }; - let bind_port = self.bind_port.unwrap_or(DEFAULT_BIND_PORT); - let endpoint = endpoint.bind(bind_port).await?; - trace!("created quinn endpoint"); - - let cancel_token = CancellationToken::new(); + trace!("created endpoint"); let addr = endpoint.node_addr().await?; + trace!("endpoint address: {addr:?}"); // initialize the gossip protocol let gossip = Gossip::from_endpoint(endpoint.clone(), Default::default(), &addr.info); @@ -455,44 +468,36 @@ where let downloader = Downloader::new(self.blobs_store.clone(), endpoint.clone(), lp.clone()); // load or create the default author for documents - let default_author_storage = match self.storage { - StorageConfig::Persistent(ref root) => { - let path = IrohPaths::DefaultAuthor.with_root(root); - DefaultAuthorStorage::Persistent(path) - } - StorageConfig::Mem => DefaultAuthorStorage::Mem, - }; - // spawn the docs engine - let sync = Engine::spawn( - endpoint.clone(), - gossip.clone(), - self.docs_store, - self.blobs_store.clone(), - downloader.clone(), - default_author_storage, - ) - .await?; - let sync = DocsEngine(sync); + let docs = DocsEngine( + Engine::spawn( + endpoint.clone(), + gossip.clone(), + self.docs_store, + self.blobs_store.clone(), + downloader.clone(), + self.storage.default_author_storage(), + ) + .await?, + ); // Initialize the internal RPC connection. let (internal_rpc, controller) = quic_rpc::transport::flume::connection(1); let client = crate::client::Iroh::new(quic_rpc::RpcClient::new(controller.clone())); - debug!("rpc listening on: {:?}", self.rpc_endpoint.local_addr()); let inner = Arc::new(NodeInner { - db: self.blobs_store.clone(), - docs: sync, - endpoint: endpoint.clone(), + db: self.blobs_store, + docs, + endpoint, secret_key: self.secret_key, client, - cancel_token, + cancel_token: CancellationToken::new(), rt: lp, downloader, gossip, }); - let node = ProtocolBuilder { + let protocol_builder = ProtocolBuilder { inner, protocols: Default::default(), internal_rpc, @@ -501,9 +506,9 @@ where gc_done_callback: self.gc_done_callback, }; - let node = node.register_iroh_protocols(); + let protocol_builder = protocol_builder.register_iroh_protocols(); - Ok(node) + Ok(protocol_builder) } }