Skip to content

Commit 7581b8e

Browse files
author
Antoine Riard
committed
Fail back dust HTLC of local commitment tx after enough confirmations
Add test_failure_delay_htlc_local_commitment Move some bits of check_spend_remote as we need to fail dust HTLCs which can be spread on both prev/lastest local commitment tx
1 parent 4bc89a6 commit 7581b8e

File tree

2 files changed

+135
-13
lines changed

2 files changed

+135
-13
lines changed

src/ln/channelmonitor.rs

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,42 +1637,75 @@ impl ChannelMonitor {
16371637
/// Attempts to claim any claimable HTLCs in a commitment transaction which was not (yet)
16381638
/// revoked using data in local_claimable_outpoints.
16391639
/// Should not be used if check_spend_revoked_transaction succeeds.
1640-
fn check_spend_local_transaction(&self, tx: &Transaction, _height: u32) -> (Vec<Transaction>, Vec<SpendableOutputDescriptor>, (Sha256dHash, Vec<TxOut>)) {
1640+
fn check_spend_local_transaction(&mut self, tx: &Transaction, height: u32) -> (Vec<Transaction>, Vec<SpendableOutputDescriptor>, (Sha256dHash, Vec<TxOut>)) {
16411641
let commitment_txid = tx.txid();
1642-
// TODO: If we find a match here we need to fail back HTLCs that weren't included in the
1643-
// broadcast commitment transaction, either because they didn't meet dust or because they
1644-
// weren't yet included in our commitment transaction(s).
1642+
let mut local_txn = Vec::new();
1643+
let mut spendable_outputs = Vec::new();
1644+
let mut watch_outputs = Vec::new();
1645+
1646+
macro_rules! wait_threshold_conf {
1647+
($height: expr, $update: expr, $commitment_tx: expr, $payment_hash: expr) => {
1648+
log_trace!(self, "Failing HTLC with payment_hash {} from {} local commitment tx due to broadcast of transaction, waiting confirmation until {} height", log_bytes!($payment_hash.0), $commitment_tx, height + HTLC_FAIL_ANTI_REORG_DELAY - 1);
1649+
match self.htlc_updated_waiting_threshold_conf.entry($height + HTLC_FAIL_ANTI_REORG_DELAY - 1) {
1650+
hash_map::Entry::Occupied(mut entry) => {
1651+
entry.get_mut().push($update);
1652+
}
1653+
hash_map::Entry::Vacant(entry) => {
1654+
entry.insert(vec![($update)]);
1655+
}
1656+
}
1657+
}
1658+
}
1659+
1660+
macro_rules! append_onchain_update {
1661+
($updates: expr) => {
1662+
local_txn.append(&mut $updates.0);
1663+
spendable_outputs.append(&mut $updates.1);
1664+
watch_outputs.append(&mut $updates.2);
1665+
}
1666+
}
1667+
16451668
if let &Some(ref local_tx) = &self.current_local_signed_commitment_tx {
1669+
for &(ref htlc, _, ref source) in &local_tx.htlc_outputs {
1670+
if htlc.transaction_output_index.is_none() {
1671+
if let &Some(ref source) = source {
1672+
wait_threshold_conf!(height, (source.clone(), None, htlc.payment_hash.clone()), "lastest", htlc.payment_hash);
1673+
}
1674+
}
1675+
}
16461676
if local_tx.txid == commitment_txid {
16471677
log_trace!(self, "Got latest local commitment tx broadcast, searching for available HTLCs to claim");
16481678
match self.key_storage {
16491679
Storage::Local { ref delayed_payment_base_key, ref latest_per_commitment_point, .. } => {
1650-
let (local_txn, spendable_outputs, watch_outputs) = self.broadcast_by_local_state(local_tx, latest_per_commitment_point, &Some(*delayed_payment_base_key));
1651-
return (local_txn, spendable_outputs, (commitment_txid, watch_outputs));
1680+
append_onchain_update!(self.broadcast_by_local_state(local_tx, latest_per_commitment_point, &Some(*delayed_payment_base_key)));
16521681
},
16531682
Storage::Watchtower { .. } => {
1654-
let (local_txn, spendable_outputs, watch_outputs) = self.broadcast_by_local_state(local_tx, &None, &None);
1655-
return (local_txn, spendable_outputs, (commitment_txid, watch_outputs));
1683+
append_onchain_update!(self.broadcast_by_local_state(local_tx, &None, &None));
16561684
}
16571685
}
16581686
}
16591687
}
16601688
if let &Some(ref local_tx) = &self.prev_local_signed_commitment_tx {
1689+
for &(ref htlc, _, ref source) in &local_tx.htlc_outputs {
1690+
if htlc.transaction_output_index.is_none() {
1691+
if let &Some(ref source) = source {
1692+
wait_threshold_conf!(height, (source.clone(), None, htlc.payment_hash.clone()), "previous", htlc.payment_hash);
1693+
}
1694+
}
1695+
}
16611696
if local_tx.txid == commitment_txid {
16621697
log_trace!(self, "Got previous local commitment tx broadcast, searching for available HTLCs to claim");
16631698
match self.key_storage {
16641699
Storage::Local { ref delayed_payment_base_key, ref prev_latest_per_commitment_point, .. } => {
1665-
let (local_txn, spendable_outputs, watch_outputs) = self.broadcast_by_local_state(local_tx, prev_latest_per_commitment_point, &Some(*delayed_payment_base_key));
1666-
return (local_txn, spendable_outputs, (commitment_txid, watch_outputs));
1700+
append_onchain_update!(self.broadcast_by_local_state(local_tx, prev_latest_per_commitment_point, &Some(*delayed_payment_base_key)));
16671701
},
16681702
Storage::Watchtower { .. } => {
1669-
let (local_txn, spendable_outputs, watch_outputs) = self.broadcast_by_local_state(local_tx, &None, &None);
1670-
return (local_txn, spendable_outputs, (commitment_txid, watch_outputs));
1703+
append_onchain_update!(self.broadcast_by_local_state(local_tx, &None, &None));
16711704
}
16721705
}
16731706
}
16741707
}
1675-
(Vec::new(), Vec::new(), (commitment_txid, Vec::new()))
1708+
(local_txn, spendable_outputs, (commitment_txid, watch_outputs))
16761709
}
16771710

16781711
/// Generate a spendable output event when closing_transaction get registered onchain.

src/ln/functional_tests.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5065,3 +5065,92 @@ fn test_update_add_htlc_bolt2_receiver_check_repeated_id_ignore() {
50655065
assert!(nodes[1].node.list_channels().is_empty());
50665066
check_closed_broadcast!(nodes[1]);
50675067
}
5068+
5069+
fn do_test_failure_delay_dust_htlc_local_commitment(announce_latest: bool) {
5070+
// Dust-HTLC failure updates must be delayed until failure-trigger tx (in this case local commitment) reach HTLC_FAIL_ANTI_REORG_DELAY
5071+
// We can have at most two valid local commitment tx, so both cases must be covered, and both txs must be checked to get them all as
5072+
// HTLC could have been removed from lastest local commitment tx but still valid until we get remote RAA
5073+
5074+
let nodes = create_network(2);
5075+
let chan =create_announced_chan_between_nodes(&nodes, 0, 1);
5076+
5077+
let bs_dust_limit = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().our_dust_limit_satoshis;
5078+
5079+
// We route 2 dust-HTLCs between A and B
5080+
let (_, payment_hash_1) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
5081+
let (_, payment_hash_2) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
5082+
route_payment(&nodes[0], &[&nodes[1]], 1000000);
5083+
5084+
// Cache one local commitment tx as previous
5085+
let as_prev_commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
5086+
5087+
// Fail one HTLC to prune it in the will-be-latest-local commitment tx
5088+
assert!(nodes[1].node.fail_htlc_backwards(&payment_hash_2));
5089+
check_added_monitors!(nodes[1], 0);
5090+
expect_pending_htlcs_forwardable!(nodes[1]);
5091+
check_added_monitors!(nodes[1], 1);
5092+
5093+
let remove = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
5094+
nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &remove.update_fail_htlcs[0]).unwrap();
5095+
nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &remove.commitment_signed).unwrap();
5096+
check_added_monitors!(nodes[0], 1);
5097+
5098+
// Cache one local commitment tx as lastest
5099+
let as_last_commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
5100+
5101+
let events = nodes[0].node.get_and_clear_pending_msg_events();
5102+
match events[0] {
5103+
MessageSendEvent::SendRevokeAndACK { node_id, .. } => {
5104+
assert_eq!(node_id, nodes[1].node.get_our_node_id());
5105+
},
5106+
_ => panic!("Unexpected event"),
5107+
}
5108+
match events[1] {
5109+
MessageSendEvent::UpdateHTLCs { node_id, .. } => {
5110+
assert_eq!(node_id, nodes[1].node.get_our_node_id());
5111+
},
5112+
_ => panic!("Unexpected event"),
5113+
}
5114+
5115+
assert_ne!(as_prev_commitment_tx, as_last_commitment_tx);
5116+
// Fail the 2 dust-HTLCs, move their failure in maturation buffer (htlc_updated_waiting_threshold_conf)
5117+
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
5118+
if announce_latest {
5119+
nodes[0].chain_monitor.block_connected_checked(&header, 1, &[&as_last_commitment_tx[0]], &[1; 1]);
5120+
} else {
5121+
nodes[0].chain_monitor.block_connected_checked(&header, 1, &[&as_prev_commitment_tx[0]], &[1; 1]);
5122+
}
5123+
5124+
let events = nodes[0].node.get_and_clear_pending_msg_events();
5125+
assert_eq!(events.len(), 1);
5126+
match events[0] {
5127+
MessageSendEvent::BroadcastChannelUpdate { .. } => {},
5128+
_ => panic!("Unexpected event"),
5129+
}
5130+
5131+
assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
5132+
connect_blocks(&nodes[0].chain_monitor, HTLC_FAIL_ANTI_REORG_DELAY, 1, true, header.bitcoin_hash());
5133+
let events = nodes[0].node.get_and_clear_pending_events();
5134+
// Only 2 PaymentFailed events should show up, over-dust HTLC has to be failed by timeout tx
5135+
assert_eq!(events.len(), 2);
5136+
let mut first_failed = false;
5137+
for event in events {
5138+
match event {
5139+
Event::PaymentFailed { payment_hash, .. } => {
5140+
if payment_hash == payment_hash_1 {
5141+
assert!(!first_failed);
5142+
first_failed = true;
5143+
} else {
5144+
assert_eq!(payment_hash, payment_hash_2);
5145+
}
5146+
}
5147+
_ => panic!("Unexpected event"),
5148+
}
5149+
}
5150+
}
5151+
5152+
#[test]
5153+
fn test_failure_delay_dust_htlc_local_commitment() {
5154+
do_test_failure_delay_dust_htlc_local_commitment(true);
5155+
do_test_failure_delay_dust_htlc_local_commitment(false);
5156+
}

0 commit comments

Comments
 (0)