Skip to content

Commit 8b98ace

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 946b89f commit 8b98ace

File tree

2 files changed

+144
-14
lines changed

2 files changed

+144
-14
lines changed

src/ln/channel.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ pub(super) struct Channel {
337337
logger: Arc<Logger>,
338338
}
339339

340-
const OUR_MAX_HTLCS: u16 = 50; //TODO
340+
pub const OUR_MAX_HTLCS: u16 = 50; //TODO
341341
/// Confirmation count threshold at which we close a channel. Ideally we'd keep the channel around
342342
/// on ice until the funding transaction gets more confirmations, but the LN protocol doesn't
343343
/// really allow for this, so instead we're stuck closing it out at that point.
@@ -1534,14 +1534,23 @@ impl Channel {
15341534
(self.pending_inbound_htlcs.len() as u32, htlc_inbound_value_msat)
15351535
}
15361536

1537-
/// Returns (outbound_htlc_count, htlc_outbound_value_msat)
1537+
/// Returns (outbound_htlc_count, htlc_outbound_value_msat) *including* pending adds in our
1538+
/// holding cell.
15381539
fn get_outbound_pending_htlc_stats(&self) -> (u32, u64) {
15391540
let mut htlc_outbound_value_msat = 0;
15401541
for ref htlc in self.pending_outbound_htlcs.iter() {
15411542
htlc_outbound_value_msat += htlc.amount_msat;
15421543
}
15431544

1544-
(self.pending_outbound_htlcs.len() as u32, htlc_outbound_value_msat)
1545+
let mut htlc_outbound_count = self.pending_outbound_htlcs.len();
1546+
for ref update in self.holding_cell_htlc_updates.iter() {
1547+
if let &HTLCUpdateAwaitingACK::AddHTLC { ref amount_msat, .. } = update {
1548+
htlc_outbound_count += 1;
1549+
htlc_outbound_value_msat += amount_msat;
1550+
}
1551+
}
1552+
1553+
(htlc_outbound_count as u32, htlc_outbound_value_msat)
15451554
}
15461555

15471556
pub fn update_add_htlc(&mut self, msg: &msgs::UpdateAddHTLC, pending_forward_state: PendingHTLCStatus) -> Result<(), ChannelError> {
@@ -1858,6 +1867,11 @@ impl Channel {
18581867
}
18591868
if err.is_some() {
18601869
self.holding_cell_htlc_updates.push(htlc_update);
1870+
if let Some(ChannelError::Ignore(_)) = err {
1871+
// If we failed to add the HTLC, but got an Ignore error, we should
1872+
// still send the new commitment_signed, so reset the err to None.
1873+
err = None;
1874+
}
18611875
}
18621876
}
18631877
}
@@ -3152,19 +3166,9 @@ impl Channel {
31523166
return Err(ChannelError::Ignore("Cannot send value that would put us over our max HTLC value in flight"));
31533167
}
31543168

3155-
let mut holding_cell_outbound_amount_msat = 0;
3156-
for holding_htlc in self.holding_cell_htlc_updates.iter() {
3157-
match holding_htlc {
3158-
&HTLCUpdateAwaitingACK::AddHTLC { ref amount_msat, .. } => {
3159-
holding_cell_outbound_amount_msat += *amount_msat;
3160-
}
3161-
_ => {}
3162-
}
3163-
}
3164-
31653169
// Check self.their_channel_reserve_satoshis (the amount we must keep as
31663170
// reserve for them to have something to claim if we misbehave)
3167-
if self.value_to_self_msat < self.their_channel_reserve_satoshis * 1000 + amount_msat + holding_cell_outbound_amount_msat + htlc_outbound_value_msat {
3171+
if self.value_to_self_msat < self.their_channel_reserve_satoshis * 1000 + amount_msat + htlc_outbound_value_msat {
31683172
return Err(ChannelError::Ignore("Cannot send value that would put us over our reserve value"));
31693173
}
31703174

src/ln/functional_tests.rs

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,6 +1823,132 @@ fn fake_network_test() {
18231823
close_channel(&nodes[1], &nodes[3], &chan_5.2, chan_5.3, false);
18241824
}
18251825

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

0 commit comments

Comments
 (0)