Skip to content

Commit d1c6f23

Browse files
author
Antoine Riard
committed
BOLT2: Check we don't send and accept 0-msat HTLC
Failing this requirement at sending means a strict receiver would fail our channel while processing a HTLC routed from a third-party. Fix by enforcing check on both sender and receiver side.
1 parent 83c9eb4 commit d1c6f23

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

lightning/src/ln/channel.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,6 +1656,9 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
16561656
if msg.amount_msat > self.channel_value_satoshis * 1000 {
16571657
return Err(ChannelError::Close("Remote side tried to send more than the total value of the channel"));
16581658
}
1659+
if msg.amount_msat == 0 {
1660+
return Err(ChannelError::Close("Remote side tried to send a 0-msat HTLC"));
1661+
}
16591662
if msg.amount_msat < self.our_htlc_minimum_msat {
16601663
return Err(ChannelError::Close("Remote side tried to send less than our minimum HTLC value"));
16611664
}
@@ -3493,6 +3496,11 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
34933496
if amount_msat > self.channel_value_satoshis * 1000 {
34943497
return Err(ChannelError::Ignore("Cannot send more than the total value of the channel"));
34953498
}
3499+
3500+
if amount_msat == 0 {
3501+
return Err(ChannelError::Ignore("Cannot send 0-msat HTLC"));
3502+
}
3503+
34963504
if amount_msat < self.their_htlc_minimum_msat {
34973505
return Err(ChannelError::Ignore("Cannot send less than their minimum HTLC value"));
34983506
}

lightning/src/ln/functional_tests.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5486,7 +5486,6 @@ fn bolt2_open_channel_sending_node_checks_part2() {
54865486

54875487
#[test]
54885488
fn test_update_add_htlc_bolt2_sender_value_below_minimum_msat() {
5489-
//BOLT2 Requirement: MUST offer amount_msat greater than 0.
54905489
//BOLT2 Requirement: MUST NOT offer amount_msat below the receiving node's htlc_minimum_msat (same validation check catches both of these)
54915490
let chanmon_cfgs = create_chanmon_cfgs(2);
54925491
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
@@ -5496,7 +5495,7 @@ fn test_update_add_htlc_bolt2_sender_value_below_minimum_msat() {
54965495
let mut route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
54975496
let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
54985497

5499-
route.hops[0].fee_msat = 0;
5498+
route.hops[0].fee_msat = 100;
55005499

55015500
let err = nodes[0].node.send_payment(route, our_payment_hash);
55025501

@@ -5509,6 +5508,30 @@ fn test_update_add_htlc_bolt2_sender_value_below_minimum_msat() {
55095508
nodes[0].logger.assert_log("lightning::ln::channelmanager".to_string(), "Cannot send less than their minimum HTLC value".to_string(), 1);
55105509
}
55115510

5511+
#[test]
5512+
fn test_update_add_htlc_bolt2_sender_zero_value_msat() {
5513+
//BOLT2 Requirement: MUST offer amount_msat greater than 0.
5514+
let chanmon_cfgs = create_chanmon_cfgs(2);
5515+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5516+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5517+
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5518+
let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::supported(), InitFeatures::supported());
5519+
let mut route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
5520+
let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
5521+
5522+
route.hops[0].fee_msat = 0;
5523+
5524+
let err = nodes[0].node.send_payment(route, our_payment_hash);
5525+
5526+
if let Err(APIError::ChannelUnavailable{err}) = err {
5527+
assert_eq!(err, "Cannot send 0-msat HTLC");
5528+
} else {
5529+
assert!(false);
5530+
}
5531+
assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
5532+
nodes[0].logger.assert_log("lightning::ln::channelmanager".to_string(), "Cannot send 0-msat HTLC".to_string(), 1);
5533+
}
5534+
55125535
#[test]
55135536
fn test_update_add_htlc_bolt2_sender_cltv_expiry_too_high() {
55145537
//BOLT 2 Requirement: MUST set cltv_expiry less than 500000000.

0 commit comments

Comments
 (0)