Repository navigation
Add disable_lightning_connections config flag
#1064
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,7 @@ use bitcoin::secp256k1::PublicKey; | |
| use lightning::ln::msgs::SocketAddress; | ||
|
|
||
| use crate::config::TorConfig; | ||
| use crate::logger::{log_debug, log_error, log_info, LdkLogger}; | ||
| use crate::logger::{log_debug, log_error, log_info, log_warn, LdkLogger}; | ||
| use crate::types::{KeysManager, PeerManager}; | ||
| use crate::Error; | ||
|
|
||
|
|
@@ -57,6 +57,7 @@ where | |
| { | ||
| pending_connections: PendingConnections, | ||
| peer_manager: Arc<PeerManager>, | ||
| disable_lightning_connections: bool, | ||
| tor_proxy_config: Option<TorConfig>, | ||
| keys_manager: Arc<KeysManager>, | ||
| logger: L, | ||
|
|
@@ -67,12 +68,19 @@ where | |
| L::Target: LdkLogger, | ||
| { | ||
| pub(crate) fn new( | ||
| peer_manager: Arc<PeerManager>, tor_proxy_config: Option<TorConfig>, | ||
| keys_manager: Arc<KeysManager>, logger: L, | ||
| peer_manager: Arc<PeerManager>, disable_lightning_connections: bool, | ||
| tor_proxy_config: Option<TorConfig>, keys_manager: Arc<KeysManager>, logger: L, | ||
| ) -> Self { | ||
| let pending_connections = Mutex::new(HashMap::new()); | ||
|
|
||
| Self { pending_connections, peer_manager, tor_proxy_config, keys_manager, logger } | ||
| Self { | ||
| pending_connections, | ||
| peer_manager, | ||
| disable_lightning_connections, | ||
| tor_proxy_config, | ||
| keys_manager, | ||
| logger, | ||
| } | ||
| } | ||
|
|
||
| pub(crate) async fn connect_peer_if_necessary( | ||
|
|
@@ -115,6 +123,11 @@ where | |
| async fn do_connect_peer_internal( | ||
| &self, node_id: PublicKey, addr: SocketAddress, | ||
| ) -> Result<(), Error> { | ||
| if self.disable_lightning_connections { | ||
| log_warn!(self.logger, "Lightning peer connections are disabled."); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than logging indefinitely, should we just disable the reconnection loop when the flag is enabled?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks done below, the goal is to remove sources of infinite logs, feel free to use |
||
| return Err(Error::ConnectionFailed); | ||
| } | ||
|
|
||
| log_info!(self.logger, "Connecting to peer: {}@{}", node_id, addr); | ||
|
|
||
| match addr { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -560,41 +560,48 @@ impl Node { | |
| } | ||
| } | ||
|
|
||
| // Regularly reconnect to persisted peers. | ||
| let connect_cm = Arc::clone(&self.connection_manager); | ||
| let connect_pm = Arc::clone(&self.peer_manager); | ||
| let connect_logger = Arc::clone(&self.logger); | ||
| let connect_peer_store = Arc::clone(&self.peer_store); | ||
| let mut stop_connect = self.stop_sender.subscribe(); | ||
| self.runtime.spawn_cancellable_background_task(async move { | ||
| let mut interval = tokio::time::interval(PEER_RECONNECTION_INTERVAL); | ||
| interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip); | ||
| loop { | ||
| tokio::select! { | ||
| _ = stop_connect.changed() => { | ||
| log_debug!( | ||
| connect_logger, | ||
| "Stopping reconnecting known peers." | ||
| ); | ||
| return; | ||
| } | ||
| _ = interval.tick() => { | ||
| let pm_peers = connect_pm | ||
| .list_peers() | ||
| .iter() | ||
| .map(|peer| peer.counterparty_node_id) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| for peer_info in connect_peer_store.list_peers().iter().filter(|info| !pm_peers.contains(&info.node_id)) { | ||
| let _ = connect_cm.do_connect_peer( | ||
| peer_info.node_id, | ||
| peer_info.address.clone(), | ||
| ).await; | ||
| if !self.config.disable_lightning_connections { | ||
| // Regularly reconnect to persisted peers. | ||
| let connect_cm = Arc::clone(&self.connection_manager); | ||
| let connect_pm = Arc::clone(&self.peer_manager); | ||
| let connect_logger = Arc::clone(&self.logger); | ||
| let connect_peer_store = Arc::clone(&self.peer_store); | ||
| let mut stop_connect = self.stop_sender.subscribe(); | ||
| self.runtime.spawn_cancellable_background_task(async move { | ||
| let mut interval = tokio::time::interval(PEER_RECONNECTION_INTERVAL); | ||
| interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip); | ||
| loop { | ||
| tokio::select! { | ||
| _ = stop_connect.changed() => { | ||
| log_debug!( | ||
| connect_logger, | ||
| "Stopping reconnecting known peers." | ||
| ); | ||
| return; | ||
| } | ||
| } | ||
| _ = interval.tick() => { | ||
| let pm_peers = connect_pm | ||
| .list_peers() | ||
| .iter() | ||
| .map(|peer| peer.counterparty_node_id) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| for peer_info in connect_peer_store.list_peers().iter().filter(|info| !pm_peers.contains(&info.node_id)) { | ||
| let _ = connect_cm.do_connect_peer( | ||
| peer_info.node_id, | ||
| peer_info.address.clone(), | ||
| ).await; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
| } else { | ||
| log_info!( | ||
| self.logger, | ||
| "Lightning peer networking is disabled for both inbound and outbound connections." | ||
| ); | ||
| } | ||
|
|
||
| // Regularly broadcast node announcements. | ||
| let bcast_cm = Arc::clone(&self.channel_manager); | ||
|
|
@@ -809,17 +816,20 @@ impl Node { | |
| let liquidity_handler = Arc::clone(&self.liquidity_source); | ||
| let liquidity_logger = Arc::clone(&self.logger); | ||
| let discovery_cm = Arc::clone(&self.connection_manager); | ||
| let disable_lightning_connections = self.config.disable_lightning_connections; | ||
| self.runtime.spawn_background_task(async move { | ||
| // Spawn discovery for configured LSPs in parallel. | ||
| let discovery_logger = Arc::clone(&liquidity_logger); | ||
| let mut discovery_set = tokio::task::JoinSet::new(); | ||
| for (node_id, address) in liquidity_handler.get_all_lsp_details() { | ||
| let cm = Arc::clone(&discovery_cm); | ||
| let logger = Arc::clone(&discovery_logger); | ||
| let ls = Arc::clone(&liquidity_handler); | ||
| discovery_set.spawn(async move { | ||
| connect_and_discover_lsp(&cm, &ls, &logger, node_id, address).await; | ||
| }); | ||
| if !disable_lightning_connections { | ||
| for (node_id, address) in liquidity_handler.get_all_lsp_details() { | ||
| let cm = Arc::clone(&discovery_cm); | ||
| let logger = Arc::clone(&discovery_logger); | ||
| let ls = Arc::clone(&liquidity_handler); | ||
| discovery_set.spawn(async move { | ||
| connect_and_discover_lsp(&cm, &ls, &logger, node_id, address).await; | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| let mut discovery_done = false; | ||
|
|
@@ -848,38 +858,40 @@ impl Node { | |
| } | ||
| }); | ||
|
|
||
| // Retry protocol discovery for any LSPs that failed the startup batch, backing off up to a | ||
| // cap and then retrying at that cap until every configured LSP has been discovered. | ||
| let mut stop_retry = self.stop_sender.subscribe(); | ||
| let retry_ls = Arc::clone(&self.liquidity_source); | ||
| let retry_logger = Arc::clone(&self.logger); | ||
| let retry_cm = Arc::clone(&self.connection_manager); | ||
| self.runtime.spawn_cancellable_background_task(async move { | ||
| let mut backoff = LIQUIDITY_DISCOVERY_RETRY_INITIAL_DELAY; | ||
| loop { | ||
| tokio::select! { | ||
| _ = stop_retry.changed() => return, | ||
| _ = tokio::time::sleep(backoff) => {}, | ||
| } | ||
| if !self.config.disable_lightning_connections { | ||
| // Retry protocol discovery for any LSPs that failed the startup batch, backing off up to a | ||
| // cap and then retrying at that cap until every configured LSP has been discovered. | ||
| let mut stop_retry = self.stop_sender.subscribe(); | ||
| let retry_ls = Arc::clone(&self.liquidity_source); | ||
| let retry_logger = Arc::clone(&self.logger); | ||
| let retry_cm = Arc::clone(&self.connection_manager); | ||
| self.runtime.spawn_cancellable_background_task(async move { | ||
| let mut backoff = LIQUIDITY_DISCOVERY_RETRY_INITIAL_DELAY; | ||
| loop { | ||
| tokio::select! { | ||
| _ = stop_retry.changed() => return, | ||
| _ = tokio::time::sleep(backoff) => {}, | ||
| } | ||
|
|
||
| let undiscovered_lsps = retry_ls.get_undiscovered_lsps(); | ||
| if undiscovered_lsps.is_empty() { | ||
| break; | ||
| } | ||
| let undiscovered_lsps = retry_ls.get_undiscovered_lsps(); | ||
| if undiscovered_lsps.is_empty() { | ||
| break; | ||
| } | ||
|
|
||
| let mut discovery_set = tokio::task::JoinSet::new(); | ||
| for (node_id, address) in undiscovered_lsps { | ||
| let cm = Arc::clone(&retry_cm); | ||
| let ls = Arc::clone(&retry_ls); | ||
| let logger = Arc::clone(&retry_logger); | ||
| discovery_set.spawn(async move { | ||
| connect_and_discover_lsp(&cm, &ls, &logger, node_id, address).await; | ||
| }); | ||
| let mut discovery_set = tokio::task::JoinSet::new(); | ||
| for (node_id, address) in undiscovered_lsps { | ||
| let cm = Arc::clone(&retry_cm); | ||
| let ls = Arc::clone(&retry_ls); | ||
| let logger = Arc::clone(&retry_logger); | ||
| discovery_set.spawn(async move { | ||
| connect_and_discover_lsp(&cm, &ls, &logger, node_id, address).await; | ||
| }); | ||
| } | ||
| discovery_set.join_all().await; | ||
| backoff = (backoff * 2).min(LIQUIDITY_DISCOVERY_RETRY_MAX_DELAY); | ||
| } | ||
| discovery_set.join_all().await; | ||
| backoff = (backoff * 2).min(LIQUIDITY_DISCOVERY_RETRY_MAX_DELAY); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| log_info!(self.logger, "Startup complete."); | ||
| *is_running_lock = true; | ||
|
|
@@ -1322,6 +1334,8 @@ impl Node { | |
| /// Connect to a node on the peer-to-peer network. | ||
| /// | ||
| /// If `persist` is set to `true`, we'll remember the peer and reconnect to it on restart. | ||
| /// Outbound connection attempts fail with [`Error::ConnectionFailed`] if | ||
| /// [`Config::disable_lightning_connections`] is set to `true`. | ||
| pub fn connect( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, wouldn't we still want to allow manual overrides? I.e. allow the user to selectively connect peers, e.g., to trigger the channel-reestablish recovery flow?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the node that reconnects to peers to trigger the force-closes must not have the stale database, it must be launched from the seed alone. That node will be launched with outbound connections enabled. If the node with the stale database tries to reconnect with the peers, the channel-reestablish flow causes the node to panic in rust-lightning. That is why we would like to add this See the full procedure I have in mind here: lightningdevkit/ldk-server#257 (comment)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually I went further and now enforce that inbound connections are disabled as well, see the rationale in the commit message below. |
||
| &self, node_id: PublicKey, address: SocketAddress, persist: bool, | ||
| ) -> Result<(), Error> { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This probably also needs to be exposed in the Uniffi UDL file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed because of these lines further above:
ldk-node/src/builder.rs
Lines 1224 to 1226 in 3bc8309
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, nvm, I forgot that we switched the builder over to macros a while ago 😆