Skip to content

Commit 8571854

Browse files
Add a test for what happens when an HTLC is put in the holding cell when a fee update is pending.
1 parent 177200f commit 8571854

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

lightning/src/ln/channel.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2097,9 +2097,11 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
20972097
match err {
20982098
None => {
20992099
if update_add_htlcs.is_empty() && update_fulfill_htlcs.is_empty() && update_fail_htlcs.is_empty() && self.holding_cell_update_fee.is_none() {
2100-
// This should never actually happen and indicates we got some Errs back
2101-
// from update_fulfill_htlc/update_fail_htlc, but we handle it anyway in
2102-
// case there is some strange way to hit duplicate HTLC removes.
2100+
// Reaching this clause is an edge case: a user must (a) be at the edge
2101+
// of their channel send limits and (b) have a race occur where e.g.
2102+
// the remote adds an HTLC while another HTLC is in the holding cell
2103+
// or e.g. a fee update is finalized while an HTLC is in the holding
2104+
// cell.
21032105
return Ok(None);
21042106
}
21052107
let update_fee = if let Some(feerate) = self.holding_cell_update_fee {

lightning/src/ln/functional_tests.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5803,6 +5803,60 @@ fn bolt2_open_channel_sending_node_checks_part2() {
58035803
assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.delayed_payment_basepoint.serialize()).is_ok());
58045804
}
58055805

5806+
#[test]
5807+
fn test_holding_cell_htlc_with_pending_fee_update() {
5808+
let chanmon_cfgs = create_chanmon_cfgs(2);
5809+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5810+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5811+
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5812+
let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::supported(), InitFeatures::supported());
5813+
5814+
// First nodes[0] generates an update_fee, setting the channel's
5815+
// pending_update_fee.
5816+
nodes[0].node.update_fee(chan.2, get_feerate!(nodes[0], chan.2) + 20).unwrap();
5817+
check_added_monitors!(nodes[0], 1);
5818+
5819+
let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
5820+
assert_eq!(events_0.len(), 1);
5821+
let (update_msg, commitment_signed) = match events_0[0] { // (1)
5822+
MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
5823+
(update_fee.as_ref(), commitment_signed)
5824+
},
5825+
_ => panic!("Unexpected event"),
5826+
};
5827+
5828+
nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
5829+
5830+
let mut chan_stat = get_channel_value_stat!(nodes[0], chan.2);
5831+
let their_channel_reserve = chan_stat.channel_reserve_msat;
5832+
let fee_spike_reserve = 2 * chan_stat.commit_tx_fee_per_htlc_msat;
5833+
5834+
let max_can_send = 5000000 - their_channel_reserve - chan_stat.commit_tx_fee_base_msat - chan_stat.commit_tx_fee_per_htlc_msat - fee_spike_reserve;
5835+
let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], max_can_send, TEST_FINAL_CLTV).unwrap();
5836+
let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
5837+
5838+
// Send a payment which passes reserve checks but gets stuck in the holding cell.
5839+
nodes[0].node.send_payment(&route, our_payment_hash, &None).unwrap();
5840+
chan_stat = get_channel_value_stat!(nodes[0], chan.2);
5841+
assert_eq!(chan_stat.holding_cell_outbound_amount_msat, max_can_send);
5842+
5843+
// Flush the pending fee update.
5844+
nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
5845+
let (as_revoke_and_ack, _) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
5846+
check_added_monitors!(nodes[1], 1);
5847+
nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_revoke_and_ack);
5848+
check_added_monitors!(nodes[0], 1);
5849+
5850+
// Upon receipt of the RAA, there will be an attempt to resend the holding cell
5851+
// HTLC, but now that the fee has been raised the payment will now fail, causing
5852+
// it to be put back in the holding cell.
5853+
chan_stat = get_channel_value_stat!(nodes[0], chan.2);
5854+
assert_eq!(chan_stat.holding_cell_outbound_amount_msat, max_can_send);
5855+
nodes[0].logger.assert_log("lightning::ln::channel".to_string(), "Freeing holding cell with 1 HTLC updates".to_string(), 1);
5856+
let failure_log = format!("Failed to send HTLC with payment_hash {} due to Cannot send value that would put us over their reserve value", log_bytes!(our_payment_hash.0));
5857+
nodes[0].logger.assert_log("lightning::ln::channel".to_string(), failure_log.to_string(), 1);
5858+
}
5859+
58065860
// BOLT 2 Requirements for the Sender when constructing and sending an update_add_htlc message.
58075861
// BOLT 2 Requirement: MUST NOT offer amount_msat it cannot pay for in the remote commitment transaction at the current feerate_per_kw (see "Updating Fees") while maintaining its channel reserve.
58085862
//TODO: I don't believe this is explicitly enforced when sending an HTLC but as the Fee aspect of the BOLT specs is in flux leaving this as a TODO.

0 commit comments

Comments
 (0)