Skip to content

Commit 3f209b7

Browse files
Add test for configurable in-flight limit
1 parent b9baa45 commit 3f209b7

File tree

1 file changed

+94
-1
lines changed

1 file changed

+94
-1
lines changed

lightning/src/ln/channel.rs

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6348,7 +6348,7 @@ mod tests {
63486348
use hex;
63496349
use ln::PaymentHash;
63506350
use ln::channelmanager::{HTLCSource, PaymentId};
6351-
use ln::channel::{Channel, InboundHTLCOutput, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator};
6351+
use ln::channel::{Channel, InboundHTLCOutput, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, ChannelError};
63526352
use ln::channel::MAX_FUNDING_SATOSHIS;
63536353
use ln::features::InitFeatures;
63546354
use ln::msgs::{ChannelUpdate, DataLossProtect, DecodeError, OptionalField, UnsignedChannelUpdate};
@@ -6665,6 +6665,99 @@ mod tests {
66656665
}
66666666
}
66676667

6668+
#[test]
6669+
fn test_configured_holder_max_htlc_value_in_flight() {
6670+
let feeest = TestFeeEstimator{fee_est: 15000};
6671+
let logger = test_utils::TestLogger::new();
6672+
let secp_ctx = Secp256k1::new();
6673+
let seed = [42; 32];
6674+
let network = Network::Testnet;
6675+
let keys_provider = test_utils::TestKeysInterface::new(&seed, network);
6676+
let outbound_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
6677+
let inbound_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[7; 32]).unwrap());
6678+
6679+
let mut config_1_percent = UserConfig::default();
6680+
config_1_percent.channel_options.holder_max_htlc_value_in_flight_msat_channel_value_percent = 1;
6681+
let mut config_100_percent = UserConfig::default();
6682+
config_100_percent.channel_options.holder_max_htlc_value_in_flight_msat_channel_value_percent = 100;
6683+
let mut config_0_percent = UserConfig::default();
6684+
config_0_percent.channel_options.holder_max_htlc_value_in_flight_msat_channel_value_percent = 0;
6685+
let mut config_101_percent = UserConfig::default();
6686+
config_101_percent.channel_options.holder_max_htlc_value_in_flight_msat_channel_value_percent = 101;
6687+
6688+
// Test that `new_outbound` creates a channel with the correct value for
6689+
// `holder_max_htlc_value_in_flight_msat`, when configured with a valid percentage value,
6690+
// which is set to the lower bound (1%) of the `channel_value`.
6691+
let chan_1 = Channel::<EnforcingSigner>::new_outbound(&&feeest, &&keys_provider, outbound_node_id, &InitFeatures::known(), 10000000, 100000, 42, &config_1_percent, 0, 42).unwrap();
6692+
let chan_1_value_msat = chan_1.channel_value_satoshis * 1000;
6693+
assert_eq!(chan_1.holder_max_htlc_value_in_flight_msat, (chan_1_value_msat as f64 * 0.01) as u64);
6694+
6695+
// Test with the upper bound of valid values (100%).
6696+
let chan_2 = Channel::<EnforcingSigner>::new_outbound(&&feeest, &&keys_provider, outbound_node_id, &InitFeatures::known(), 10000000, 100000, 42, &config_100_percent, 0, 42).unwrap();
6697+
let chan_2_value_msat = chan_2.channel_value_satoshis * 1000;
6698+
assert_eq!(chan_2.holder_max_htlc_value_in_flight_msat, chan_2_value_msat);
6699+
6700+
let chan_1_open_channel_msg = chan_1.get_open_channel(genesis_block(network).header.block_hash());
6701+
6702+
// Test that `new_from_req` creates a channel with the correct value for
6703+
// `holder_max_htlc_value_in_flight_msat`, when configured with a valid percentage value,
6704+
// which is set to the lower bound (1%) of the `channel_value`.
6705+
let chan_3 = Channel::<EnforcingSigner>::new_from_req(&&feeest, &&keys_provider, inbound_node_id, &InitFeatures::known(), &chan_1_open_channel_msg, 7, &config_1_percent, 0, &&logger, 42).unwrap();
6706+
let chan_3_value_msat = chan_3.channel_value_satoshis * 1000;
6707+
assert_eq!(chan_3.holder_max_htlc_value_in_flight_msat, (chan_3_value_msat as f64 * 0.01) as u64);
6708+
6709+
// Test with the upper bound of valid values (100%).
6710+
let chan_4 = Channel::<EnforcingSigner>::new_from_req(&&feeest, &&keys_provider, inbound_node_id, &InitFeatures::known(), &chan_1_open_channel_msg, 7, &config_100_percent, 0, &&logger, 42).unwrap();
6711+
let chan_4_value_msat = chan_4.channel_value_satoshis * 1000;
6712+
assert_eq!(chan_4.holder_max_htlc_value_in_flight_msat, chan_4_value_msat);
6713+
6714+
// Test invalid values
6715+
let get_error_string = | percentage_value | { format!(
6716+
"UserConfig::channel_options::holder_max_htlc_value_in_flight_msat_channel_value_percent must be set to a value between 1-100. Current value set ({})",
6717+
percentage_value)
6718+
};
6719+
6720+
// Test that `new_outbound` fails when trying to create a channel invalid percentage value
6721+
// set for `holder_max_htlc_value_in_flight_msat_channel_value_percent` (less than 1).
6722+
let err_1 = Channel::<EnforcingSigner>::new_outbound(&&feeest, &&keys_provider, outbound_node_id, &InitFeatures::known(), 10000000, 100000, 42, &config_0_percent, 0, 42);
6723+
match err_1 {
6724+
Err(APIError::APIMisuseError { err }) => {
6725+
assert_eq!(err, get_error_string(0));
6726+
}
6727+
_ => { panic!("new_outbound should have resulted in APIError::APIMisuseError"); }
6728+
}
6729+
6730+
// Test that `new_outbound` fails when trying to create a channel invalid percentage value
6731+
// set for `holder_max_htlc_value_in_flight_msat_channel_value_percent` (larger than 100).
6732+
let err_2 = Channel::<EnforcingSigner>::new_outbound(&&feeest, &&keys_provider, outbound_node_id, &InitFeatures::known(), 10000000, 100000, 42, &config_101_percent, 0, 42);
6733+
match err_2 {
6734+
Err(APIError::APIMisuseError { err }) => {
6735+
assert_eq!(err, get_error_string(101));
6736+
}
6737+
_ => { panic!("new_outbound should have resulted in APIError::APIMisuseError"); }
6738+
}
6739+
6740+
// Test that `new_from_req` fails when trying to create a channel invalid percentage value
6741+
// set for `holder_max_htlc_value_in_flight_msat_channel_value_percent` (less than 1).
6742+
let err_3 = Channel::<EnforcingSigner>::new_from_req(&&feeest, &&keys_provider, inbound_node_id, &InitFeatures::known(), &chan_1_open_channel_msg, 7, &config_0_percent, 0, &&logger, 42);
6743+
match err_3 {
6744+
Err(ChannelError::Close(err)) => {
6745+
assert_eq!(err, get_error_string(0));
6746+
}
6747+
_ => { panic!("new_from_req should have resulted in ChannelError::Close"); }
6748+
}
6749+
6750+
// Test that `new_from_req` fails when trying to create a channel invalid percentage value
6751+
// set for `holder_max_htlc_value_in_flight_msat_channel_value_percent` (larger than 100).
6752+
let err_4 = Channel::<EnforcingSigner>::new_from_req(&&feeest, &&keys_provider, inbound_node_id, &InitFeatures::known(), &chan_1_open_channel_msg, 7, &config_101_percent, 0, &&logger, 42);
6753+
match err_4 {
6754+
Err(ChannelError::Close(err)) => {
6755+
assert_eq!(err, get_error_string(101));
6756+
}
6757+
_ => { panic!("new_from_req should have resulted in ChannelError::Close"); }
6758+
}
6759+
}
6760+
66686761
#[test]
66696762
fn channel_update() {
66706763
let feeest = TestFeeEstimator{fee_est: 15000};

0 commit comments

Comments
 (0)