Skip to content

Commit 0f6b000

Browse files
committed
Fix two bugs which access the wrong peer's htlc_minimum_msat value
* Channel::get_counterparty_htlc_minimum_msat() returned holder_htlc_minimum_msat, which was obviously incorrect. * ChannelManager::get_channel_update set htlc_minimum_msat to Channel::get_holder_htlc_minimum_msat(), but the spec explicitly states we "MUST set htlc_minimum_msat to the minimum HTLC value (in millisatoshi) that the channel peer will accept." This makes sense because the reason we're rejecting the HTLC is because our counterparty's HTLC minimum value is too small for us to send to them, our own HTLC minimum value plays no role. Further, our router already expects this - looking at the same directional channel info as it does fees. Finally, we add a test in the existing onion router test cases which fails if either of the above is incorrect (the second issue discovered in the process of writing the test).
1 parent a8be9af commit 0f6b000

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

lightning/src/ln/channel.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3125,6 +3125,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
31253125
}
31263126

31273127
/// Allowed in any state (including after shutdown)
3128+
#[cfg(test)]
31283129
pub fn get_holder_htlc_minimum_msat(&self) -> u64 {
31293130
self.holder_htlc_minimum_msat
31303131
}
@@ -3143,7 +3144,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
31433144

31443145
/// Allowed in any state (including after shutdown)
31453146
pub fn get_counterparty_htlc_minimum_msat(&self) -> u64 {
3146-
self.holder_htlc_minimum_msat
3147+
self.counterparty_htlc_minimum_msat
31473148
}
31483149

31493150
pub fn get_value_satoshis(&self) -> u64 {

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
12281228
timestamp: chan.get_update_time_counter(),
12291229
flags: (!were_node_one) as u8 | ((!chan.is_live() as u8) << 1),
12301230
cltv_expiry_delta: CLTV_EXPIRY_DELTA,
1231-
htlc_minimum_msat: chan.get_holder_htlc_minimum_msat(),
1231+
htlc_minimum_msat: chan.get_counterparty_htlc_minimum_msat(),
12321232
htlc_maximum_msat: OptionalField::Present(chan.get_announced_htlc_max_msat()),
12331233
fee_base_msat: chan.get_holder_fee_base_msat(&self.fee_estimator),
12341234
fee_proportional_millionths: chan.get_fee_proportional_millionths(),

lightning/src/ln/onion_route_tests.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use ln::msgs::{ChannelMessageHandler, HTLCFailChannelUpdate, OptionalField};
2121
use util::test_utils;
2222
use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
2323
use util::ser::{Writeable, Writer};
24+
use util::config::UserConfig;
2425

2526
use bitcoin::blockdata::block::BlockHeader;
2627
use bitcoin::hash_types::BlockHash;
@@ -257,9 +258,19 @@ fn test_onion_failure() {
257258
const NODE: u16 = 0x2000;
258259
const UPDATE: u16 = 0x1000;
259260

261+
// When we check for amount_below_minimum below, we want to test that we're using the *right*
262+
// amount, thus we need different htlc_minimum_msat values. We set node[2]'s htlc_minimum_msat
263+
// to 2000, which is above the default value of 1000 set in create_node_chanmgrs.
264+
// This exposed a previous bug because we were using the wrong value all the way down in
265+
// Channel::get_counterparty_htlc_minimum_msat().
266+
let mut node_2_cfg: UserConfig = Default::default();
267+
node_2_cfg.own_channel_config.our_htlc_minimum_msat = 2000;
268+
node_2_cfg.channel_options.announced_channel = true;
269+
node_2_cfg.peer_channel_config_limits.force_announced_channel_preference = false;
270+
260271
let chanmon_cfgs = create_chanmon_cfgs(3);
261272
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
262-
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
273+
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, Some(node_2_cfg)]);
263274
let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
264275
for node in nodes.iter() {
265276
*node.keys_manager.override_session_priv.lock().unwrap() = Some([3; 32]);
@@ -411,6 +422,11 @@ fn test_onion_failure() {
411422
bogus_route.paths[0][route_len-1].fee_msat = amt_to_forward;
412423
run_onion_failure_test("amount_below_minimum", 0, &nodes, &bogus_route, &payment_hash, |_| {}, ||{}, true, Some(UPDATE|11), Some(msgs::HTLCFailChannelUpdate::ChannelUpdateMessage{msg: ChannelUpdate::dummy()}));
413424

425+
// Test a positive test-case with one extra msat, meeting the minimum.
426+
bogus_route.paths[0][route_len-1].fee_msat = amt_to_forward + 1;
427+
let (preimage, _) = send_along_route(&nodes[0], bogus_route, &[&nodes[1], &nodes[2]], amt_to_forward+1);
428+
claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], preimage, amt_to_forward+1);
429+
414430
//TODO: with new config API, we will be able to generate both valid and
415431
//invalid channel_update cases.
416432
run_onion_failure_test("fee_insufficient", 0, &nodes, &route, &payment_hash, |msg| {

0 commit comments

Comments
 (0)