Skip to content

Commit 9460b07

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 at HTLC routed from a third-party. Fix by enforcing check on both sender and receiver side.
1 parent 80a0d15 commit 9460b07

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
@@ -1652,6 +1652,9 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
16521652
if msg.amount_msat > self.channel_value_satoshis * 1000 {
16531653
return Err(ChannelError::Close("Remote side tried to send more than the total value of the channel"));
16541654
}
1655+
if msg.amount_msat == 0 {
1656+
return Err(ChannelError::Close("Remote side tried to send a 0-msat HTLC"));
1657+
}
16551658
if msg.amount_msat < self.our_htlc_minimum_msat {
16561659
return Err(ChannelError::Close("Remote side tried to send less than our minimum HTLC value"));
16571660
}
@@ -3474,6 +3477,11 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
34743477
if amount_msat > self.channel_value_satoshis * 1000 {
34753478
return Err(ChannelError::Ignore("Cannot send more than the total value of the channel"));
34763479
}
3480+
3481+
if amount_msat == 0 {
3482+
return Err(ChannelError::Ignore("Cannot send 0-msat HTLC"));
3483+
}
3484+
34773485
if amount_msat < self.their_htlc_minimum_msat {
34783486
return Err(ChannelError::Ignore("Cannot send less than their minimum HTLC value"));
34793487
}

lightning/src/ln/functional_tests.rs

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

54965496
#[test]
54975497
fn test_update_add_htlc_bolt2_sender_value_below_minimum_msat() {
5498-
//BOLT2 Requirement: MUST offer amount_msat greater than 0.
54995498
//BOLT2 Requirement: MUST NOT offer amount_msat below the receiving node's htlc_minimum_msat (same validation check catches both of these)
55005499
let chanmon_cfgs = create_chanmon_cfgs(2);
55015500
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
@@ -5505,7 +5504,7 @@ fn test_update_add_htlc_bolt2_sender_value_below_minimum_msat() {
55055504
let mut route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
55065505
let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
55075506

5508-
route.hops[0].fee_msat = 0;
5507+
route.hops[0].fee_msat = 100;
55095508

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

@@ -5518,6 +5517,30 @@ fn test_update_add_htlc_bolt2_sender_value_below_minimum_msat() {
55185517
nodes[0].logger.assert_log("lightning::ln::channelmanager".to_string(), "Cannot send less than their minimum HTLC value".to_string(), 1);
55195518
}
55205519

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

0 commit comments

Comments
 (0)