Skip to content
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

Add max_no_channel_peers to UserConfig #3382

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
43 changes: 36 additions & 7 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2622,9 +2622,9 @@ const MAX_UNFUNDED_CHANS_PER_PEER: usize = 4;
/// this many peers we reject new (inbound) channels from peers with which we don't have a channel.
const MAX_UNFUNDED_CHANNEL_PEERS: usize = 50;

/// The maximum number of peers which we do not have a (funded) channel with. Once we reach this
/// many peers we reject new (inbound) connections.
const MAX_NO_CHANNEL_PEERS: usize = 250;
/// The default maximum number of peers which we do not have a (funded) channel with. Once we reach
/// this many peers we reject new (inbound) connections.
pub(crate) const DEFAULT_MAX_NO_CHANNEL_PEERS: usize = 250;

/// The maximum expiration from the current time where an [`Offer`] or [`Refund`] is considered
/// short-lived, while anything with a greater expiration is considered long-lived.
Expand Down Expand Up @@ -10776,7 +10776,8 @@ where
// unfunded channels taking up space in memory for disconnected peers, we still let new
// peers connect, but we'll reject new channels from them.
let connected_peers_without_funded_channels = self.peers_without_funded_channels(|node| node.is_connected);
let inbound_peer_limited = inbound && connected_peers_without_funded_channels >= MAX_NO_CHANNEL_PEERS;
let max_no_channel_peers = self.get_current_default_configuration().max_no_channel_peers;
let inbound_peer_limited = inbound && connected_peers_without_funded_channels >= max_no_channel_peers;

{
let mut peer_state_lock = self.per_peer_state.write().unwrap();
Expand Down Expand Up @@ -13124,7 +13125,7 @@ mod tests {
use crate::events::{Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, ClosureReason};
use crate::ln::types::ChannelId;
use crate::types::payment::{PaymentPreimage, PaymentHash, PaymentSecret};
use crate::ln::channelmanager::{create_recv_pending_htlc_info, HTLCForwardInfo, inbound_payment, PaymentId, PaymentSendFailure, RecipientOnionFields, InterceptId};
use crate::ln::channelmanager::{create_recv_pending_htlc_info, HTLCForwardInfo, inbound_payment, PaymentId, PaymentSendFailure, RecipientOnionFields, InterceptId, UserConfig};
use crate::ln::functional_test_utils::*;
use crate::ln::msgs::{self, ErrorAction};
use crate::ln::msgs::ChannelMessageHandler;
Expand Down Expand Up @@ -13995,8 +13996,8 @@ mod tests {
// Further, because all of our channels with nodes[0] are inbound, and none of them funded,
// it doesn't count as a "protected" peer, i.e. it counts towards the MAX_NO_CHANNEL_PEERS
// limit.
let mut peer_pks = Vec::with_capacity(super::MAX_NO_CHANNEL_PEERS);
for _ in 1..super::MAX_NO_CHANNEL_PEERS {
let mut peer_pks = Vec::with_capacity(super::DEFAULT_MAX_NO_CHANNEL_PEERS);
for _ in 1..super::DEFAULT_MAX_NO_CHANNEL_PEERS {
let random_pk = PublicKey::from_secret_key(&nodes[0].node.secp_ctx,
&SecretKey::from_slice(&nodes[1].keys_manager.get_secure_random_bytes()).unwrap());
peer_pks.push(random_pk);
Expand Down Expand Up @@ -14062,6 +14063,34 @@ mod tests {
get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, last_random_pk);
}

#[test]
fn test_custom_max_no_channel_peers_connection_limiting() {
// Test that we limit custom un-channel'd peers properly.
let max_no_channel_peers = 2usize.saturating_mul(super::DEFAULT_MAX_NO_CHANNEL_PEERS);
let chanmon_cfgs = create_chanmon_cfgs(2);
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
let user_cfg = UserConfig { max_no_channel_peers, ..Default::default() };
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(user_cfg), Some(user_cfg)]);
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);

// Note that create_network connects the nodes together for us

let mut peer_pks = Vec::with_capacity(max_no_channel_peers);
for _ in 1..max_no_channel_peers {
let random_pk = PublicKey::from_secret_key(&nodes[0].node.secp_ctx,
&SecretKey::from_slice(&nodes[1].keys_manager.get_secure_random_bytes()).unwrap());
peer_pks.push(random_pk);
nodes[1].node.peer_connected(random_pk, &msgs::Init {
features: nodes[0].node.init_features(), networks: None, remote_network_address: None
}, true).unwrap();
}
let last_random_pk = PublicKey::from_secret_key(&nodes[0].node.secp_ctx,
&SecretKey::from_slice(&nodes[1].keys_manager.get_secure_random_bytes()).unwrap());
nodes[1].node.peer_connected(last_random_pk, &msgs::Init {
features: nodes[0].node.init_features(), networks: None, remote_network_address: None
}, true).unwrap_err();
}

#[test]
fn test_outbound_chans_unlimited() {
// Test that we never refuse an outbound channel even if a peer is unfuned-channel-limited
Expand Down
12 changes: 11 additions & 1 deletion lightning/src/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
//! applies for you.

use crate::ln::channel::MAX_FUNDING_SATOSHIS_NO_WUMBO;
use crate::ln::channelmanager::{BREAKDOWN_TIMEOUT, MAX_LOCAL_BREAKDOWN_TIMEOUT};
use crate::ln::channelmanager::{
BREAKDOWN_TIMEOUT, DEFAULT_MAX_NO_CHANNEL_PEERS, MAX_LOCAL_BREAKDOWN_TIMEOUT,
};

#[cfg(fuzzing)]
use crate::util::ser::Readable;
Expand Down Expand Up @@ -869,6 +871,13 @@ pub struct UserConfig {
/// [`ChannelManager::send_payment_for_bolt12_invoice`]: crate::ln::channelmanager::ChannelManager::send_payment_for_bolt12_invoice
/// [`ChannelManager::abandon_payment`]: crate::ln::channelmanager::ChannelManager::abandon_payment
pub manually_handle_bolt12_invoices: bool,
/// The maximum number of non-channel peers that we will accept. Once this number of non-channel
/// peers is reached, we will not accept any new non-channel peer connectionss. This could mean
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
/// peers is reached, we will not accept any new non-channel peer connectionss. This could mean
/// peers is reached, we will not accept any new non-channel peer connections. This could mean

/// that peers that wish to open a new channel will be unable to connect. Channel counterparties
/// are not checked against this limit.
///
/// Default value: 250
pub max_no_channel_peers: usize,
}

impl Default for UserConfig {
Expand All @@ -883,6 +892,7 @@ impl Default for UserConfig {
accept_intercept_htlcs: false,
accept_mpp_keysend: false,
manually_handle_bolt12_invoices: false,
max_no_channel_peers: DEFAULT_MAX_NO_CHANNEL_PEERS,
}
}
}
Expand Down
Loading