Skip to content

Commit fc984f0

Browse files
Add correct ChannelUpdate htlc_maximum_msat test
1 parent 0c3c62a commit fc984f0

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

lightning/src/ln/functional_tests.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use ln::chan_utils::{htlc_success_tx_weight, htlc_timeout_tx_weight, HTLCOutputI
2626
use routing::router::{PaymentParameters, Route, RouteHop, RouteParameters, find_route, get_route};
2727
use ln::features::{ChannelFeatures, InitFeatures, InvoiceFeatures, NodeFeatures};
2828
use ln::msgs;
29-
use ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, ErrorAction};
29+
use ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, OptionalField, ErrorAction};
3030
use util::enforcing_trait_impls::EnforcingSigner;
3131
use util::{byte_utils, test_utils};
3232
use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, PaymentPurpose, ClosureReason};
@@ -8121,6 +8121,58 @@ fn test_override_0msat_htlc_minimum() {
81218121
assert_eq!(res.htlc_minimum_msat, 1);
81228122
}
81238123

8124+
#[test]
8125+
fn test_channel_update_has_correct_htlc_maximum_msat() {
8126+
// Tests that the `ChannelUpdate` message has the correct values for `htlc_maximum_msat` set.
8127+
// Bolt 7 specifies that the if present, the `htlc_maximum_msat`:
8128+
// 1. MUST be set to less than or equal to the channel capacity. In LDK, this is capped to
8129+
// 90% of the `channel_value`.
8130+
// 2. MUST be set to less than or equal to the `max_htlc_value_in_flight_msat` received from the peer.
8131+
8132+
let mut config_30_percent = UserConfig::default();
8133+
config_30_percent.channel_options.announced_channel = true;
8134+
config_30_percent.channel_options.holder_max_htlc_value_in_flight_msat_channel_value_percent = 30;
8135+
let mut config_50_percent = UserConfig::default();
8136+
config_50_percent.channel_options.announced_channel = true;
8137+
config_50_percent.channel_options.holder_max_htlc_value_in_flight_msat_channel_value_percent = 50;
8138+
let mut config_95_percent = UserConfig::default();
8139+
config_95_percent.channel_options.announced_channel = true;
8140+
config_95_percent.channel_options.holder_max_htlc_value_in_flight_msat_channel_value_percent = 95;
8141+
let mut config_100_percent = UserConfig::default();
8142+
config_100_percent.channel_options.announced_channel = true;
8143+
config_100_percent.channel_options.holder_max_htlc_value_in_flight_msat_channel_value_percent = 100;
8144+
8145+
let chanmon_cfgs = create_chanmon_cfgs(4);
8146+
let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
8147+
let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[Some(config_30_percent), Some(config_50_percent), Some(config_95_percent), Some(config_100_percent)]);
8148+
let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
8149+
8150+
let channel_value_satoshis = 100000;
8151+
let channel_value_msat = channel_value_satoshis * 1000;
8152+
let channel_value_30_percent_msat = (channel_value_msat as f64 * 0.3) as u64;
8153+
let channel_value_50_percent_msat = (channel_value_msat as f64 * 0.5) as u64;
8154+
let channel_value_90_percent_msat = (channel_value_msat as f64 * 0.9) as u64;
8155+
8156+
let (node_0_chan_update, node_1_chan_update, _, _) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, channel_value_satoshis, 10001, InitFeatures::known(), InitFeatures::known());
8157+
let (node_2_chan_update, node_3_chan_update, _, _) = create_announced_chan_between_nodes_with_value(&nodes, 2, 3, channel_value_satoshis, 10001, InitFeatures::known(), InitFeatures::known());
8158+
8159+
// Assert that `node[0]`'s `ChannelUpdate` is capped at 50 percent of the `channel_value`, as
8160+
// that's the value of `node[1]`'s `holder_max_htlc_value_in_flight_msat`.
8161+
assert_eq!(node_0_chan_update.contents.htlc_maximum_msat, OptionalField::Present(channel_value_50_percent_msat));
8162+
// Assert that `node[1]`'s `ChannelUpdate` is capped at 30 percent of the `channel_value`, as
8163+
// that's the value of `node[0]`'s `holder_max_htlc_value_in_flight_msat`.
8164+
assert_eq!(node_1_chan_update.contents.htlc_maximum_msat, OptionalField::Present(channel_value_30_percent_msat));
8165+
8166+
// Assert that `node[2]`'s `ChannelUpdate` is capped at 90 percent of the `channel_value`, as
8167+
// the value of `node[3]`'s `holder_max_htlc_value_in_flight_msat` (100%), exceeds 90% of the
8168+
// `channel_value`.
8169+
assert_eq!(node_2_chan_update.contents.htlc_maximum_msat, OptionalField::Present(channel_value_90_percent_msat));
8170+
// Assert that `node[3]`'s `ChannelUpdate` is capped at 90 percent of the `channel_value`, as
8171+
// the value of `node[2]`'s `holder_max_htlc_value_in_flight_msat` (95%), exceeds 90% of the
8172+
// `channel_value`.
8173+
assert_eq!(node_3_chan_update.contents.htlc_maximum_msat, OptionalField::Present(channel_value_90_percent_msat));
8174+
}
8175+
81248176
#[test]
81258177
fn test_manually_accept_inbound_channel_request() {
81268178
let mut manually_accept_conf = UserConfig::default();

0 commit comments

Comments
 (0)