Skip to content

Commit 1a718e5

Browse files
committed
Fix holding cell freeing in case we fail to add some HTLC
Previously, if we went to free the holding cell HTLC updates, and adding one failed as we hit our outbound HTLC limit (or in-flight value limit), we would not send a commitment_signed, leaving us in an invalid state. We first fix that bug, and then refuse to add things to our holding cell once we reach our limits considering the holding cell, as we shouldn't have multiple commitment dance rounds worth of HTLCs in the holding cell anyway.
1 parent 4cc7d5d commit 1a718e5

File tree

2 files changed

+159
-22
lines changed

2 files changed

+159
-22
lines changed

src/ln/channel.rs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,14 +1540,23 @@ impl Channel {
15401540
(self.pending_inbound_htlcs.len() as u32, htlc_inbound_value_msat)
15411541
}
15421542

1543-
/// Returns (outbound_htlc_count, htlc_outbound_value_msat)
1543+
/// Returns (outbound_htlc_count, htlc_outbound_value_msat) *including* pending adds in our
1544+
/// holding cell.
15441545
fn get_outbound_pending_htlc_stats(&self) -> (u32, u64) {
15451546
let mut htlc_outbound_value_msat = 0;
15461547
for ref htlc in self.pending_outbound_htlcs.iter() {
15471548
htlc_outbound_value_msat += htlc.amount_msat;
15481549
}
15491550

1550-
(self.pending_outbound_htlcs.len() as u32, htlc_outbound_value_msat)
1551+
let mut htlc_outbound_count = self.pending_outbound_htlcs.len();
1552+
for update in self.holding_cell_htlc_updates.iter() {
1553+
if let &HTLCUpdateAwaitingACK::AddHTLC { ref amount_msat, .. } = update {
1554+
htlc_outbound_count += 1;
1555+
htlc_outbound_value_msat += amount_msat;
1556+
}
1557+
}
1558+
1559+
(htlc_outbound_count as u32, htlc_outbound_value_msat)
15511560
}
15521561

15531562
pub fn update_add_htlc(&mut self, msg: &msgs::UpdateAddHTLC, pending_forward_state: PendingHTLCStatus) -> Result<(), ChannelError> {
@@ -1834,6 +1843,14 @@ impl Channel {
18341843
match self.send_htlc(amount_msat, *payment_hash, cltv_expiry, source.clone(), onion_routing_packet.clone()) {
18351844
Ok(update_add_msg_option) => update_add_htlcs.push(update_add_msg_option.unwrap()),
18361845
Err(e) => {
1846+
match e {
1847+
ChannelError::Ignore(ref msg) => {
1848+
log_info!(self, "Failed to send HTLC with payment_hash {} due to {}", log_bytes!(payment_hash.0), msg);
1849+
},
1850+
_ => {
1851+
log_info!(self, "Failed to send HTLC with payment_hash {} resulting in a channel closure during holding_cell freeing", log_bytes!(payment_hash.0));
1852+
},
1853+
}
18371854
err = Some(e);
18381855
}
18391856
}
@@ -1863,6 +1880,11 @@ impl Channel {
18631880
}
18641881
if err.is_some() {
18651882
self.holding_cell_htlc_updates.push(htlc_update);
1883+
if let Some(ChannelError::Ignore(_)) = err {
1884+
// If we failed to add the HTLC, but got an Ignore error, we should
1885+
// still send the new commitment_signed, so reset the err to None.
1886+
err = None;
1887+
}
18661888
}
18671889
}
18681890
}
@@ -3154,30 +3176,19 @@ impl Channel {
31543176
//TODO: Spec is unclear if this is per-direction or in total (I assume per direction):
31553177
// Check their_max_htlc_value_in_flight_msat
31563178
if htlc_outbound_value_msat + amount_msat > self.their_max_htlc_value_in_flight_msat {
3157-
return Err(ChannelError::Ignore("Cannot send value that would put us over our max HTLC value in flight"));
3158-
}
3159-
3160-
let mut holding_cell_outbound_amount_msat = 0;
3161-
for holding_htlc in self.holding_cell_htlc_updates.iter() {
3162-
match holding_htlc {
3163-
&HTLCUpdateAwaitingACK::AddHTLC { ref amount_msat, .. } => {
3164-
holding_cell_outbound_amount_msat += *amount_msat;
3165-
}
3166-
_ => {}
3167-
}
3179+
return Err(ChannelError::Ignore("Cannot send value that would put us over the max HTLC value in flight"));
31683180
}
31693181

31703182
// Check self.their_channel_reserve_satoshis (the amount we must keep as
31713183
// reserve for them to have something to claim if we misbehave)
3172-
if self.value_to_self_msat < self.their_channel_reserve_satoshis * 1000 + amount_msat + holding_cell_outbound_amount_msat + htlc_outbound_value_msat {
3173-
return Err(ChannelError::Ignore("Cannot send value that would put us over our reserve value"));
3184+
if self.value_to_self_msat < self.their_channel_reserve_satoshis * 1000 + amount_msat + htlc_outbound_value_msat {
3185+
return Err(ChannelError::Ignore("Cannot send value that would put us over the reserve value"));
31743186
}
31753187

31763188
//TODO: Check cltv_expiry? Do this in channel manager?
31773189

31783190
// Now update local state:
31793191
if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == (ChannelState::AwaitingRemoteRevoke as u32) {
3180-
//TODO: Check the limits *including* other pending holding cell HTLCs!
31813192
self.holding_cell_htlc_updates.push(HTLCUpdateAwaitingACK::AddHTLC {
31823193
amount_msat: amount_msat,
31833194
payment_hash: payment_hash,

src/ln/functional_tests.rs

Lines changed: 132 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ fn route_over_limit(origin_node: &Node, expected_route: &[&Node], recv_value: u6
718718

719719
let err = origin_node.node.send_payment(route, our_payment_hash).err().unwrap();
720720
match err {
721-
APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over our max HTLC value in flight"),
721+
APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight"),
722722
_ => panic!("Unknown error variants"),
723723
};
724724
}
@@ -1824,6 +1824,132 @@ fn fake_network_test() {
18241824
close_channel(&nodes[1], &nodes[3], &chan_5.2, chan_5.3, false);
18251825
}
18261826

1827+
#[test]
1828+
fn holding_cell_htlc_counting() {
1829+
// Tests that HTLCs in the holding cell count towards the pending HTLC limits on outbound HTLCs
1830+
// to ensure we don't end up with HTLCs sitting around in our holding cell for several
1831+
// commitment dance rounds.
1832+
let mut nodes = create_network(3);
1833+
create_announced_chan_between_nodes(&nodes, 0, 1);
1834+
let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
1835+
1836+
let mut payments = Vec::new();
1837+
for _ in 0..::ln::channel::OUR_MAX_HTLCS {
1838+
let route = nodes[1].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV).unwrap();
1839+
let (payment_preimage, payment_hash) = get_payment_preimage_hash!(nodes[0]);
1840+
nodes[1].node.send_payment(route, payment_hash).unwrap();
1841+
payments.push((payment_preimage, payment_hash));
1842+
}
1843+
check_added_monitors!(nodes[1], 1);
1844+
1845+
let mut events = nodes[1].node.get_and_clear_pending_msg_events();
1846+
assert_eq!(events.len(), 1);
1847+
let initial_payment_event = SendEvent::from_event(events.pop().unwrap());
1848+
assert_eq!(initial_payment_event.node_id, nodes[2].node.get_our_node_id());
1849+
1850+
// There is now one HTLC in an outbound commitment transaction and (OUR_MAX_HTLCS - 1) HTLCs in
1851+
// the holding cell waiting on B's RAA to send. At this point we should not be able to add
1852+
// another HTLC.
1853+
let route = nodes[1].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV).unwrap();
1854+
let (_, payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
1855+
if let APIError::ChannelUnavailable { err } = nodes[1].node.send_payment(route, payment_hash_1).unwrap_err() {
1856+
assert_eq!(err, "Cannot push more than their max accepted HTLCs");
1857+
} else { panic!("Unexpected event"); }
1858+
1859+
// This should also be true if we try to forward a payment.
1860+
let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV).unwrap();
1861+
let (_, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
1862+
nodes[0].node.send_payment(route, payment_hash_2).unwrap();
1863+
check_added_monitors!(nodes[0], 1);
1864+
1865+
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1866+
assert_eq!(events.len(), 1);
1867+
let payment_event = SendEvent::from_event(events.pop().unwrap());
1868+
assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
1869+
1870+
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
1871+
commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
1872+
// We have to forward pending HTLCs twice - once tries to forward the payment forward (and
1873+
// fails), the second will process the resulting failure and fail the HTLC backward.
1874+
expect_pending_htlcs_forwardable!(nodes[1]);
1875+
expect_pending_htlcs_forwardable!(nodes[1]);
1876+
check_added_monitors!(nodes[1], 1);
1877+
1878+
let bs_fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1879+
nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_fail_updates.update_fail_htlcs[0]).unwrap();
1880+
commitment_signed_dance!(nodes[0], nodes[1], bs_fail_updates.commitment_signed, false, true);
1881+
1882+
let events = nodes[0].node.get_and_clear_pending_msg_events();
1883+
assert_eq!(events.len(), 1);
1884+
match events[0] {
1885+
MessageSendEvent::PaymentFailureNetworkUpdate { update: msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg }} => {
1886+
assert_eq!(msg.contents.short_channel_id, chan_2.0.contents.short_channel_id);
1887+
},
1888+
_ => panic!("Unexpected event"),
1889+
}
1890+
1891+
let events = nodes[0].node.get_and_clear_pending_events();
1892+
assert_eq!(events.len(), 1);
1893+
match events[0] {
1894+
Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
1895+
assert_eq!(payment_hash, payment_hash_2);
1896+
assert!(!rejected_by_dest);
1897+
},
1898+
_ => panic!("Unexpected event"),
1899+
}
1900+
1901+
// Now forward all the pending HTLCs and claim them back
1902+
nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &initial_payment_event.msgs[0]).unwrap();
1903+
nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &initial_payment_event.commitment_msg).unwrap();
1904+
check_added_monitors!(nodes[2], 1);
1905+
1906+
let (bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
1907+
nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
1908+
check_added_monitors!(nodes[1], 1);
1909+
let as_updates = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
1910+
1911+
nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &bs_commitment_signed).unwrap();
1912+
check_added_monitors!(nodes[1], 1);
1913+
let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
1914+
1915+
for ref update in as_updates.update_add_htlcs.iter() {
1916+
nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), update).unwrap();
1917+
}
1918+
nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &as_updates.commitment_signed).unwrap();
1919+
check_added_monitors!(nodes[2], 1);
1920+
nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa).unwrap();
1921+
check_added_monitors!(nodes[2], 1);
1922+
let (bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
1923+
1924+
nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
1925+
check_added_monitors!(nodes[1], 1);
1926+
nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &bs_commitment_signed).unwrap();
1927+
check_added_monitors!(nodes[1], 1);
1928+
let as_final_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
1929+
1930+
nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_final_raa).unwrap();
1931+
check_added_monitors!(nodes[2], 1);
1932+
1933+
expect_pending_htlcs_forwardable!(nodes[2]);
1934+
1935+
let events = nodes[2].node.get_and_clear_pending_events();
1936+
assert_eq!(events.len(), payments.len());
1937+
for (event, &(_, ref hash)) in events.iter().zip(payments.iter()) {
1938+
match event {
1939+
&Event::PaymentReceived { ref payment_hash, .. } => {
1940+
assert_eq!(*payment_hash, *hash);
1941+
},
1942+
_ => panic!("Unexpected event"),
1943+
};
1944+
}
1945+
1946+
for (preimage, _) in payments.drain(..) {
1947+
claim_payment(&nodes[1], &[&nodes[2]], preimage);
1948+
}
1949+
1950+
send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
1951+
}
1952+
18271953
#[test]
18281954
fn duplicate_htlc_test() {
18291955
// Test that we accept duplicate payment_hash HTLCs across the network and that
@@ -2035,7 +2161,7 @@ fn do_channel_reserve_test(test_recv: bool) {
20352161
assert!(route.hops.iter().rev().skip(1).all(|h| h.fee_msat == feemsat));
20362162
let err = nodes[0].node.send_payment(route, our_payment_hash).err().unwrap();
20372163
match err {
2038-
APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over our max HTLC value in flight"),
2164+
APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight"),
20392165
_ => panic!("Unknown error variants"),
20402166
}
20412167
}
@@ -2071,7 +2197,7 @@ fn do_channel_reserve_test(test_recv: bool) {
20712197
let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value + 1);
20722198
let err = nodes[0].node.send_payment(route.clone(), our_payment_hash).err().unwrap();
20732199
match err {
2074-
APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over our reserve value"),
2200+
APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the reserve value"),
20752201
_ => panic!("Unknown error variants"),
20762202
}
20772203
}
@@ -2096,7 +2222,7 @@ fn do_channel_reserve_test(test_recv: bool) {
20962222
{
20972223
let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value_2 + 1);
20982224
match nodes[0].node.send_payment(route, our_payment_hash).err().unwrap() {
2099-
APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over our reserve value"),
2225+
APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the reserve value"),
21002226
_ => panic!("Unknown error variants"),
21012227
}
21022228
}
@@ -2159,7 +2285,7 @@ fn do_channel_reserve_test(test_recv: bool) {
21592285
{
21602286
let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value_22+1);
21612287
match nodes[0].node.send_payment(route, our_payment_hash).err().unwrap() {
2162-
APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over our reserve value"),
2288+
APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the reserve value"),
21632289
_ => panic!("Unknown error variants"),
21642290
}
21652291
}
@@ -6704,7 +6830,7 @@ fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_value_in_flight() {
67046830
let err = nodes[0].node.send_payment(route, our_payment_hash);
67056831

67066832
if let Err(APIError::ChannelUnavailable{err}) = err {
6707-
assert_eq!(err, "Cannot send value that would put us over our max HTLC value in flight");
6833+
assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight");
67086834
} else {
67096835
assert!(false);
67106836
}

0 commit comments

Comments
 (0)