Skip to content
Open
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
1 change: 1 addition & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2391,6 +2391,7 @@ fn build_with_store_internal(

let connection_manager = Arc::new(ConnectionManager::new(
Arc::clone(&peer_manager),
config.disable_outbound_lightning_connections,
config.tor_config.clone(),
Arc::clone(&keys_manager),
Arc::clone(&logger),
Expand Down
8 changes: 8 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ impl Default for ForwardedPaymentTrackingMode {
/// |----------------------------------------|--------------------------------------|
/// | `storage_dir_path` | /tmp/ldk_node/ |
/// | `network` | Bitcoin |
/// |`disable_outbound_lightning_connections`| false |

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This has missing whitespace (before, after).

/// | `listening_addresses` | None |
/// | `announcement_addresses` | None |
/// | `node_alias` | None |
Expand All @@ -227,6 +228,12 @@ pub struct Config {
pub storage_dir_path: String,
/// The used Bitcoin network.
pub network: Network,
/// Disables all outbound Lightning peer-to-peer (P2P) connections.
///
/// Inbound connections are controlled separately by
/// [`Config::listening_addresses`]; set it to `None` to prevent inbound connections as well.
/// Other networking, including chain synchronization, is unaffected.
pub disable_outbound_lightning_connections: bool,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, maybe this would be better on the builder, rather than a Config field?

/// The addresses on which the node will listen for incoming connections.
///
/// **Note**: We will only allow opening and accepting public channels if the `node_alias` and the
Expand Down Expand Up @@ -304,6 +311,7 @@ impl Default for Config {
Self {
storage_dir_path: DEFAULT_STORAGE_DIR_PATH.to_string(),
network: DEFAULT_NETWORK,
disable_outbound_lightning_connections: false,
listening_addresses: None,
announcement_addresses: None,
trusted_peers_0conf: Vec::new(),
Expand Down
21 changes: 17 additions & 4 deletions src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -57,6 +57,7 @@ where
{
pending_connections: PendingConnections,
peer_manager: Arc<PeerManager>,
disable_outbound_lightning_connections: bool,
tor_proxy_config: Option<TorConfig>,
keys_manager: Arc<KeysManager>,
logger: L,
Expand All @@ -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_outbound_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_outbound_lightning_connections,
tor_proxy_config,
keys_manager,
logger,
}
}

pub(crate) async fn connect_peer_if_necessary(
Expand Down Expand Up @@ -115,6 +123,11 @@ where
async fn do_connect_peer_internal(
&self, node_id: PublicKey, addr: SocketAddress,
) -> Result<(), Error> {
if self.disable_outbound_lightning_connections {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a test for this, i.e., one that sets up some nodes and then restarts with the flag enabled, ensuring that they they don't reconnect?

log_warn!(self.logger, "Outbound Lightning peer connections are disabled.");
return Err(Error::ConnectionFailed);
}

log_info!(self.logger, "Connecting to peer: {}@{}", node_id, addr);

match addr {
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,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_outbound_lightning_connections`] is set.
pub fn connect(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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?

&self, node_id: PublicKey, address: SocketAddress, persist: bool,
) -> Result<(), Error> {
Expand Down
Loading