Skip to content

Commit 649af07

Browse files
committed
Store override counterparty handshake limits until we enforce them
We currently allow users to provide an `override_config` in `ChannelManager::create_channel` which it seems should apply to the channel. However, because we don't store any of it, the only parts which we apply to the channel are those which are set in the `Channel` object immediately in `Channel::new_outbound` and used from there. This is great in most cases, however the `UserConfig::peer_channel_config_limits` `ChannelHandshakeLimits` object is used in `accept_channel` to bound what is acceptable in our peer's `AcceptChannel` message. Thus, for outbound channels, we are given a full `UserConfig` object to "override" the default config, but we don't use any of the handshake limits specified in it. Here, we move to storing the `ChannelHandshakeLimits` explicitly and applying it when we receive our peer's `AcceptChannel`. Note that we don't need to store it anywhere because if we haven't received an `AcceptChannel` from our peer when we reload from disk we will forget the channel entirely anyway.
1 parent 482a2b9 commit 649af07

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

lightning/src/ln/channel.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use util::events::ClosureReason;
3939
use util::ser::{Readable, ReadableArgs, Writeable, Writer, VecWriter};
4040
use util::logger::Logger;
4141
use util::errors::APIError;
42-
use util::config::{UserConfig,ChannelConfig};
42+
use util::config::{UserConfig, ChannelConfig, ChannelHandshakeLimits};
4343
use util::scid_utils::scid_from_parts;
4444

4545
use io;
@@ -484,6 +484,8 @@ pub(super) struct Channel<Signer: Sign> {
484484
#[cfg(not(any(test, feature = "_test_utils")))]
485485
config: ChannelConfig,
486486

487+
inbound_handshake_limits_override: Option<ChannelHandshakeLimits>,
488+
487489
user_id: u64,
488490

489491
channel_id: [u8; 32],
@@ -835,6 +837,7 @@ impl<Signer: Sign> Channel<Signer> {
835837
Ok(Channel {
836838
user_id,
837839
config: config.channel_options.clone(),
840+
inbound_handshake_limits_override: Some(config.peer_channel_config_limits.clone()),
838841

839842
channel_id: keys_provider.get_secure_random_bytes(),
840843
channel_state: ChannelState::OurInitSent as u32,
@@ -1134,6 +1137,7 @@ impl<Signer: Sign> Channel<Signer> {
11341137
let chan = Channel {
11351138
user_id,
11361139
config: local_config,
1140+
inbound_handshake_limits_override: None,
11371141

11381142
channel_id: msg.temporary_channel_id,
11391143
channel_state: (ChannelState::OurInitSent as u32) | (ChannelState::TheirInitSent as u32),
@@ -1811,7 +1815,9 @@ impl<Signer: Sign> Channel<Signer> {
18111815

18121816
// Message handlers:
18131817

1814-
pub fn accept_channel(&mut self, msg: &msgs::AcceptChannel, config: &UserConfig, their_features: &InitFeatures) -> Result<(), ChannelError> {
1818+
pub fn accept_channel(&mut self, msg: &msgs::AcceptChannel, default_limits: &ChannelHandshakeLimits, their_features: &InitFeatures) -> Result<(), ChannelError> {
1819+
let peer_limits = if let Some(ref limits) = self.inbound_handshake_limits_override { limits } else { default_limits };
1820+
18151821
// Check sanity of message fields:
18161822
if !self.is_outbound() {
18171823
return Err(ChannelError::Close("Got an accept_channel message from an inbound peer".to_owned()));
@@ -1832,7 +1838,7 @@ impl<Signer: Sign> Channel<Signer> {
18321838
if msg.htlc_minimum_msat >= full_channel_value_msat {
18331839
return Err(ChannelError::Close(format!("Minimum htlc value ({}) is full channel value ({})", msg.htlc_minimum_msat, full_channel_value_msat)));
18341840
}
1835-
let max_delay_acceptable = u16::min(config.peer_channel_config_limits.their_to_self_delay, MAX_LOCAL_BREAKDOWN_TIMEOUT);
1841+
let max_delay_acceptable = u16::min(peer_limits.their_to_self_delay, MAX_LOCAL_BREAKDOWN_TIMEOUT);
18361842
if msg.to_self_delay > max_delay_acceptable {
18371843
return Err(ChannelError::Close(format!("They wanted our payments to be delayed by a needlessly long period. Upper limit: {}. Actual: {}", max_delay_acceptable, msg.to_self_delay)));
18381844
}
@@ -1844,26 +1850,26 @@ impl<Signer: Sign> Channel<Signer> {
18441850
}
18451851

18461852
// Now check against optional parameters as set by config...
1847-
if msg.htlc_minimum_msat > config.peer_channel_config_limits.max_htlc_minimum_msat {
1848-
return Err(ChannelError::Close(format!("htlc_minimum_msat ({}) is higher than the user specified limit ({})", msg.htlc_minimum_msat, config.peer_channel_config_limits.max_htlc_minimum_msat)));
1853+
if msg.htlc_minimum_msat > peer_limits.max_htlc_minimum_msat {
1854+
return Err(ChannelError::Close(format!("htlc_minimum_msat ({}) is higher than the user specified limit ({})", msg.htlc_minimum_msat, peer_limits.max_htlc_minimum_msat)));
18491855
}
1850-
if msg.max_htlc_value_in_flight_msat < config.peer_channel_config_limits.min_max_htlc_value_in_flight_msat {
1851-
return Err(ChannelError::Close(format!("max_htlc_value_in_flight_msat ({}) is less than the user specified limit ({})", msg.max_htlc_value_in_flight_msat, config.peer_channel_config_limits.min_max_htlc_value_in_flight_msat)));
1856+
if msg.max_htlc_value_in_flight_msat < peer_limits.min_max_htlc_value_in_flight_msat {
1857+
return Err(ChannelError::Close(format!("max_htlc_value_in_flight_msat ({}) is less than the user specified limit ({})", msg.max_htlc_value_in_flight_msat, peer_limits.min_max_htlc_value_in_flight_msat)));
18521858
}
1853-
if msg.channel_reserve_satoshis > config.peer_channel_config_limits.max_channel_reserve_satoshis {
1854-
return Err(ChannelError::Close(format!("channel_reserve_satoshis ({}) is higher than the user specified limit ({})", msg.channel_reserve_satoshis, config.peer_channel_config_limits.max_channel_reserve_satoshis)));
1859+
if msg.channel_reserve_satoshis > peer_limits.max_channel_reserve_satoshis {
1860+
return Err(ChannelError::Close(format!("channel_reserve_satoshis ({}) is higher than the user specified limit ({})", msg.channel_reserve_satoshis, peer_limits.max_channel_reserve_satoshis)));
18551861
}
1856-
if msg.max_accepted_htlcs < config.peer_channel_config_limits.min_max_accepted_htlcs {
1857-
return Err(ChannelError::Close(format!("max_accepted_htlcs ({}) is less than the user specified limit ({})", msg.max_accepted_htlcs, config.peer_channel_config_limits.min_max_accepted_htlcs)));
1862+
if msg.max_accepted_htlcs < peer_limits.min_max_accepted_htlcs {
1863+
return Err(ChannelError::Close(format!("max_accepted_htlcs ({}) is less than the user specified limit ({})", msg.max_accepted_htlcs, peer_limits.min_max_accepted_htlcs)));
18581864
}
18591865
if msg.dust_limit_satoshis < MIN_CHAN_DUST_LIMIT_SATOSHIS {
18601866
return Err(ChannelError::Close(format!("dust_limit_satoshis ({}) is less than the implementation limit ({})", msg.dust_limit_satoshis, MIN_CHAN_DUST_LIMIT_SATOSHIS)));
18611867
}
18621868
if msg.dust_limit_satoshis > MAX_CHAN_DUST_LIMIT_SATOSHIS {
18631869
return Err(ChannelError::Close(format!("dust_limit_satoshis ({}) is greater than the implementation limit ({})", msg.dust_limit_satoshis, MAX_CHAN_DUST_LIMIT_SATOSHIS)));
18641870
}
1865-
if msg.minimum_depth > config.peer_channel_config_limits.max_minimum_depth {
1866-
return Err(ChannelError::Close(format!("We consider the minimum depth to be unreasonably large. Expected minimum: ({}). Actual: ({})", config.peer_channel_config_limits.max_minimum_depth, msg.minimum_depth)));
1871+
if msg.minimum_depth > peer_limits.max_minimum_depth {
1872+
return Err(ChannelError::Close(format!("We consider the minimum depth to be unreasonably large. Expected minimum: ({}). Actual: ({})", peer_limits.max_minimum_depth, msg.minimum_depth)));
18671873
}
18681874
if msg.minimum_depth == 0 {
18691875
// Note that if this changes we should update the serialization minimum version to
@@ -1916,6 +1922,7 @@ impl<Signer: Sign> Channel<Signer> {
19161922
self.counterparty_shutdown_scriptpubkey = counterparty_shutdown_scriptpubkey;
19171923

19181924
self.channel_state = ChannelState::OurInitSent as u32 | ChannelState::TheirInitSent as u32;
1925+
self.inbound_handshake_limits_override = None; // We're done enforcing limits on our peer's handshake now.
19191926

19201927
Ok(())
19211928
}
@@ -6002,6 +6009,11 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
60026009
user_id,
60036010

60046011
config: config.unwrap(),
6012+
6013+
// Note that we don't care about serializing handshake limits as we only ever serialize
6014+
// channel data after the handshake has completed.
6015+
inbound_handshake_limits_override: None,
6016+
60056017
channel_id,
60066018
channel_state,
60076019
announcement_sigs_state: announcement_sigs_state.unwrap(),
@@ -6270,7 +6282,7 @@ mod tests {
62706282
// Node B --> Node A: accept channel, explicitly setting B's dust limit.
62716283
let mut accept_channel_msg = node_b_chan.get_accept_channel();
62726284
accept_channel_msg.dust_limit_satoshis = 546;
6273-
node_a_chan.accept_channel(&accept_channel_msg, &config, &InitFeatures::known()).unwrap();
6285+
node_a_chan.accept_channel(&accept_channel_msg, &config.peer_channel_config_limits, &InitFeatures::known()).unwrap();
62746286
node_a_chan.holder_dust_limit_satoshis = 1560;
62756287

62766288
// Put some inbound and outbound HTLCs in A's channel.
@@ -6387,7 +6399,7 @@ mod tests {
63876399

63886400
// Node B --> Node A: accept channel
63896401
let accept_channel_msg = node_b_chan.get_accept_channel();
6390-
node_a_chan.accept_channel(&accept_channel_msg, &config, &InitFeatures::known()).unwrap();
6402+
node_a_chan.accept_channel(&accept_channel_msg, &config.peer_channel_config_limits, &InitFeatures::known()).unwrap();
63916403

63926404
// Node A --> Node B: funding created
63936405
let output_script = node_a_chan.get_funding_redeemscript();

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4117,7 +4117,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
41174117
if chan.get().get_counterparty_node_id() != *counterparty_node_id {
41184118
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.temporary_channel_id));
41194119
}
4120-
try_chan_entry!(self, chan.get_mut().accept_channel(&msg, &self.default_configuration, &their_features), channel_state, chan);
4120+
try_chan_entry!(self, chan.get_mut().accept_channel(&msg, &self.default_configuration.peer_channel_config_limits, &their_features), channel_state, chan);
41214121
(chan.get().get_value_satoshis(), chan.get().get_funding_redeemscript().to_v0_p2wsh(), chan.get().get_user_id())
41224122
},
41234123
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.temporary_channel_id))

0 commit comments

Comments
 (0)