Skip to content

Commit 9e31403

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 9e31403

File tree

2 files changed

+144
-13
lines changed

2 files changed

+144
-13
lines changed

src/ln/channel.rs

Lines changed: 18 additions & 13 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> {
@@ -1863,6 +1872,12 @@ impl Channel {
18631872
}
18641873
if err.is_some() {
18651874
self.holding_cell_htlc_updates.push(htlc_update);
1875+
if let Some(ChannelError::Ignore(e)) = err {
1876+
// If we failed to add the HTLC, but got an Ignore error, we should
1877+
// still send the new commitment_signed, so reset the err to None.
1878+
log_info!(self, "Failed to forward HTLC due to {}", e);
1879+
err = None;
1880+
}
18661881
}
18671882
}
18681883
}
@@ -3157,19 +3172,9 @@ impl Channel {
31573172
return Err(ChannelError::Ignore("Cannot send value that would put us over our max HTLC value in flight"));
31583173
}
31593174

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-
}
3168-
}
3169-
31703175
// Check self.their_channel_reserve_satoshis (the amount we must keep as
31713176
// 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 {
3177+
if self.value_to_self_msat < self.their_channel_reserve_satoshis * 1000 + amount_msat + htlc_outbound_value_msat {
31733178
return Err(ChannelError::Ignore("Cannot send value that would put us over our reserve value"));
31743179
}
31753180

src/ln/functional_tests.rs

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)