Skip to content

Commit 3117e18

Browse files
authored
Merge pull request #295 from TheBlueMatt/2019-01-holding-cell-limits
Fix holding cell freeing in case we fail to add some HTLC
2 parents a6f0281 + bf26056 commit 3117e18

File tree

3 files changed

+159
-22
lines changed

3 files changed

+159
-22
lines changed

src/ln/channel.rs

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

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

1553-
(self.pending_outbound_htlcs.len() as u32, htlc_outbound_value_msat)
1554+
let mut htlc_outbound_count = self.pending_outbound_htlcs.len();
1555+
for update in self.holding_cell_htlc_updates.iter() {
1556+
if let &HTLCUpdateAwaitingACK::AddHTLC { ref amount_msat, .. } = update {
1557+
htlc_outbound_count += 1;
1558+
htlc_outbound_value_msat += amount_msat;
1559+
}
1560+
}
1561+
1562+
(htlc_outbound_count as u32, htlc_outbound_value_msat)
15541563
}
15551564

15561565
pub fn update_add_htlc(&mut self, msg: &msgs::UpdateAddHTLC, pending_forward_state: PendingHTLCStatus) -> Result<(), ChannelError> {
@@ -1853,6 +1862,14 @@ impl Channel {
18531862
match self.send_htlc(amount_msat, *payment_hash, cltv_expiry, source.clone(), onion_routing_packet.clone()) {
18541863
Ok(update_add_msg_option) => update_add_htlcs.push(update_add_msg_option.unwrap()),
18551864
Err(e) => {
1865+
match e {
1866+
ChannelError::Ignore(ref msg) => {
1867+
log_info!(self, "Failed to send HTLC with payment_hash {} due to {}", log_bytes!(payment_hash.0), msg);
1868+
},
1869+
_ => {
1870+
log_info!(self, "Failed to send HTLC with payment_hash {} resulting in a channel closure during holding_cell freeing", log_bytes!(payment_hash.0));
1871+
},
1872+
}
18561873
err = Some(e);
18571874
}
18581875
}
@@ -1882,6 +1899,11 @@ impl Channel {
18821899
}
18831900
if err.is_some() {
18841901
self.holding_cell_htlc_updates.push(htlc_update);
1902+
if let Some(ChannelError::Ignore(_)) = err {
1903+
// If we failed to add the HTLC, but got an Ignore error, we should
1904+
// still send the new commitment_signed, so reset the err to None.
1905+
err = None;
1906+
}
18851907
}
18861908
}
18871909
}
@@ -3166,30 +3188,19 @@ impl Channel {
31663188
//TODO: Spec is unclear if this is per-direction or in total (I assume per direction):
31673189
// Check their_max_htlc_value_in_flight_msat
31683190
if htlc_outbound_value_msat + amount_msat > self.their_max_htlc_value_in_flight_msat {
3169-
return Err(ChannelError::Ignore("Cannot send value that would put us over our max HTLC value in flight"));
3170-
}
3171-
3172-
let mut holding_cell_outbound_amount_msat = 0;
3173-
for holding_htlc in self.holding_cell_htlc_updates.iter() {
3174-
match holding_htlc {
3175-
&HTLCUpdateAwaitingACK::AddHTLC { ref amount_msat, .. } => {
3176-
holding_cell_outbound_amount_msat += *amount_msat;
3177-
}
3178-
_ => {}
3179-
}
3191+
return Err(ChannelError::Ignore("Cannot send value that would put us over the max HTLC value in flight"));
31803192
}
31813193

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

31883200
//TODO: Check cltv_expiry? Do this in channel manager?
31893201

31903202
// Now update local state:
31913203
if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == (ChannelState::AwaitingRemoteRevoke as u32) {
3192-
//TODO: Check the limits *including* other pending holding cell HTLCs!
31933204
self.holding_cell_htlc_updates.push(HTLCUpdateAwaitingACK::AddHTLC {
31943205
amount_msat: amount_msat,
31953206
payment_hash: payment_hash,

src/ln/functional_test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ pub fn route_over_limit(origin_node: &Node, expected_route: &[&Node], recv_value
702702

703703
let err = origin_node.node.send_payment(route, our_payment_hash).err().unwrap();
704704
match err {
705-
APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over our max HTLC value in flight"),
705+
APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight"),
706706
_ => panic!("Unknown error variants"),
707707
};
708708
}

src/ln/functional_tests.rs

Lines changed: 131 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,132 @@ fn fake_network_test() {
10391039
close_channel(&nodes[1], &nodes[3], &chan_5.2, chan_5.3, false);
10401040
}
10411041

1042+
#[test]
1043+
fn holding_cell_htlc_counting() {
1044+
// Tests that HTLCs in the holding cell count towards the pending HTLC limits on outbound HTLCs
1045+
// to ensure we don't end up with HTLCs sitting around in our holding cell for several
1046+
// commitment dance rounds.
1047+
let mut nodes = create_network(3);
1048+
create_announced_chan_between_nodes(&nodes, 0, 1);
1049+
let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
1050+
1051+
let mut payments = Vec::new();
1052+
for _ in 0..::ln::channel::OUR_MAX_HTLCS {
1053+
let route = nodes[1].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV).unwrap();
1054+
let (payment_preimage, payment_hash) = get_payment_preimage_hash!(nodes[0]);
1055+
nodes[1].node.send_payment(route, payment_hash).unwrap();
1056+
payments.push((payment_preimage, payment_hash));
1057+
}
1058+
check_added_monitors!(nodes[1], 1);
1059+
1060+
let mut events = nodes[1].node.get_and_clear_pending_msg_events();
1061+
assert_eq!(events.len(), 1);
1062+
let initial_payment_event = SendEvent::from_event(events.pop().unwrap());
1063+
assert_eq!(initial_payment_event.node_id, nodes[2].node.get_our_node_id());
1064+
1065+
// There is now one HTLC in an outbound commitment transaction and (OUR_MAX_HTLCS - 1) HTLCs in
1066+
// the holding cell waiting on B's RAA to send. At this point we should not be able to add
1067+
// another HTLC.
1068+
let route = nodes[1].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV).unwrap();
1069+
let (_, payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
1070+
if let APIError::ChannelUnavailable { err } = nodes[1].node.send_payment(route, payment_hash_1).unwrap_err() {
1071+
assert_eq!(err, "Cannot push more than their max accepted HTLCs");
1072+
} else { panic!("Unexpected event"); }
1073+
1074+
// This should also be true if we try to forward a payment.
1075+
let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV).unwrap();
1076+
let (_, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
1077+
nodes[0].node.send_payment(route, payment_hash_2).unwrap();
1078+
check_added_monitors!(nodes[0], 1);
1079+
1080+
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1081+
assert_eq!(events.len(), 1);
1082+
let payment_event = SendEvent::from_event(events.pop().unwrap());
1083+
assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
1084+
1085+
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
1086+
commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
1087+
// We have to forward pending HTLCs twice - once tries to forward the payment forward (and
1088+
// fails), the second will process the resulting failure and fail the HTLC backward.
1089+
expect_pending_htlcs_forwardable!(nodes[1]);
1090+
expect_pending_htlcs_forwardable!(nodes[1]);
1091+
check_added_monitors!(nodes[1], 1);
1092+
1093+
let bs_fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1094+
nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_fail_updates.update_fail_htlcs[0]).unwrap();
1095+
commitment_signed_dance!(nodes[0], nodes[1], bs_fail_updates.commitment_signed, false, true);
1096+
1097+
let events = nodes[0].node.get_and_clear_pending_msg_events();
1098+
assert_eq!(events.len(), 1);
1099+
match events[0] {
1100+
MessageSendEvent::PaymentFailureNetworkUpdate { update: msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg }} => {
1101+
assert_eq!(msg.contents.short_channel_id, chan_2.0.contents.short_channel_id);
1102+
},
1103+
_ => panic!("Unexpected event"),
1104+
}
1105+
1106+
let events = nodes[0].node.get_and_clear_pending_events();
1107+
assert_eq!(events.len(), 1);
1108+
match events[0] {
1109+
Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
1110+
assert_eq!(payment_hash, payment_hash_2);
1111+
assert!(!rejected_by_dest);
1112+
},
1113+
_ => panic!("Unexpected event"),
1114+
}
1115+
1116+
// Now forward all the pending HTLCs and claim them back
1117+
nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &initial_payment_event.msgs[0]).unwrap();
1118+
nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &initial_payment_event.commitment_msg).unwrap();
1119+
check_added_monitors!(nodes[2], 1);
1120+
1121+
let (bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
1122+
nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
1123+
check_added_monitors!(nodes[1], 1);
1124+
let as_updates = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
1125+
1126+
nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &bs_commitment_signed).unwrap();
1127+
check_added_monitors!(nodes[1], 1);
1128+
let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
1129+
1130+
for ref update in as_updates.update_add_htlcs.iter() {
1131+
nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), update).unwrap();
1132+
}
1133+
nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &as_updates.commitment_signed).unwrap();
1134+
check_added_monitors!(nodes[2], 1);
1135+
nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa).unwrap();
1136+
check_added_monitors!(nodes[2], 1);
1137+
let (bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
1138+
1139+
nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
1140+
check_added_monitors!(nodes[1], 1);
1141+
nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &bs_commitment_signed).unwrap();
1142+
check_added_monitors!(nodes[1], 1);
1143+
let as_final_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
1144+
1145+
nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_final_raa).unwrap();
1146+
check_added_monitors!(nodes[2], 1);
1147+
1148+
expect_pending_htlcs_forwardable!(nodes[2]);
1149+
1150+
let events = nodes[2].node.get_and_clear_pending_events();
1151+
assert_eq!(events.len(), payments.len());
1152+
for (event, &(_, ref hash)) in events.iter().zip(payments.iter()) {
1153+
match event {
1154+
&Event::PaymentReceived { ref payment_hash, .. } => {
1155+
assert_eq!(*payment_hash, *hash);
1156+
},
1157+
_ => panic!("Unexpected event"),
1158+
};
1159+
}
1160+
1161+
for (preimage, _) in payments.drain(..) {
1162+
claim_payment(&nodes[1], &[&nodes[2]], preimage);
1163+
}
1164+
1165+
send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
1166+
}
1167+
10421168
#[test]
10431169
fn duplicate_htlc_test() {
10441170
// Test that we accept duplicate payment_hash HTLCs across the network and that
@@ -1109,7 +1235,7 @@ fn do_channel_reserve_test(test_recv: bool) {
11091235
assert!(route.hops.iter().rev().skip(1).all(|h| h.fee_msat == feemsat));
11101236
let err = nodes[0].node.send_payment(route, our_payment_hash).err().unwrap();
11111237
match err {
1112-
APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over our max HTLC value in flight"),
1238+
APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight"),
11131239
_ => panic!("Unknown error variants"),
11141240
}
11151241
}
@@ -1145,7 +1271,7 @@ fn do_channel_reserve_test(test_recv: bool) {
11451271
let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value + 1);
11461272
let err = nodes[0].node.send_payment(route.clone(), our_payment_hash).err().unwrap();
11471273
match err {
1148-
APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over our reserve value"),
1274+
APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the reserve value"),
11491275
_ => panic!("Unknown error variants"),
11501276
}
11511277
}
@@ -1170,7 +1296,7 @@ fn do_channel_reserve_test(test_recv: bool) {
11701296
{
11711297
let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value_2 + 1);
11721298
match nodes[0].node.send_payment(route, our_payment_hash).err().unwrap() {
1173-
APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over our reserve value"),
1299+
APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the reserve value"),
11741300
_ => panic!("Unknown error variants"),
11751301
}
11761302
}
@@ -1233,7 +1359,7 @@ fn do_channel_reserve_test(test_recv: bool) {
12331359
{
12341360
let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value_22+1);
12351361
match nodes[0].node.send_payment(route, our_payment_hash).err().unwrap() {
1236-
APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over our reserve value"),
1362+
APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the reserve value"),
12371363
_ => panic!("Unknown error variants"),
12381364
}
12391365
}
@@ -4726,7 +4852,7 @@ fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_value_in_flight() {
47264852
let err = nodes[0].node.send_payment(route, our_payment_hash);
47274853

47284854
if let Err(APIError::ChannelUnavailable{err}) = err {
4729-
assert_eq!(err, "Cannot send value that would put us over our max HTLC value in flight");
4855+
assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight");
47304856
} else {
47314857
assert!(false);
47324858
}

0 commit comments

Comments
 (0)