1 // This file is Copyright its original authors, visible in version control
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
10 //! Further functional tests which test blockchain reorganizations.
13 use crate::chain::keysinterface::BaseSign;
15 use crate::chain::channelmonitor::LATENCY_GRACE_PERIOD_BLOCKS;
16 use crate::chain::channelmonitor::{ANTI_REORG_DELAY, Balance};
17 use crate::chain::transaction::OutPoint;
18 use crate::chain::chaininterface::LowerBoundedFeeEstimator;
19 use crate::ln::channel;
21 use crate::ln::chan_utils;
22 use crate::ln::channelmanager::{BREAKDOWN_TIMEOUT, PaymentId};
23 use crate::ln::msgs::ChannelMessageHandler;
25 use crate::util::config::UserConfig;
27 use crate::util::events::BumpTransactionEvent;
28 use crate::util::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination};
30 use bitcoin::blockdata::script::Builder;
31 use bitcoin::blockdata::opcodes;
32 use bitcoin::secp256k1::Secp256k1;
34 use bitcoin::{Amount, Script, TxIn, TxOut, PackedLockTime};
35 use bitcoin::Transaction;
37 use crate::prelude::*;
39 use crate::ln::functional_test_utils::*;
42 fn chanmon_fail_from_stale_commitment() {
43 // If we forward an HTLC to our counterparty, but we force-closed the channel before our
44 // counterparty provides us an updated commitment transaction, we'll end up with a commitment
45 // transaction that does not contain the HTLC which we attempted to forward. In this case, we
46 // need to wait `ANTI_REORG_DELAY` blocks and then fail back the HTLC as there is no way for us
47 // to learn the preimage and the confirmed commitment transaction paid us the value of the
50 // However, previously, we did not do this, ignoring the HTLC entirely.
52 // This could lead to channel closure if the sender we received the HTLC from decides to go on
53 // chain to get their HTLC back before it times out.
55 // Here, we check exactly this case, forwarding a payment from A, through B, to C, before B
56 // broadcasts its latest commitment transaction, which should result in it eventually failing
57 // the HTLC back off-chain to A.
58 let chanmon_cfgs = create_chanmon_cfgs(3);
59 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
60 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
61 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
63 create_announced_chan_between_nodes(&nodes, 0, 1);
64 let (update_a, _, chan_id_2, _) = create_announced_chan_between_nodes(&nodes, 1, 2);
66 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 1_000_000);
67 nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap();
68 check_added_monitors!(nodes[0], 1);
70 let bs_txn = get_local_commitment_txn!(nodes[1], chan_id_2);
72 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
73 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
74 commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
76 expect_pending_htlcs_forwardable!(nodes[1]);
77 get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
78 check_added_monitors!(nodes[1], 1);
80 // Don't bother delivering the new HTLC add/commits, instead confirming the pre-HTLC commitment
81 // transaction for nodes[1].
82 mine_transaction(&nodes[1], &bs_txn[0]);
83 check_added_monitors!(nodes[1], 1);
84 check_closed_broadcast!(nodes[1], true);
85 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
86 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
88 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
89 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_id_2 }]);
90 check_added_monitors!(nodes[1], 1);
91 let fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
93 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates.update_fail_htlcs[0]);
94 commitment_signed_dance!(nodes[0], nodes[1], fail_updates.commitment_signed, true, true);
95 expect_payment_failed_with_update!(nodes[0], payment_hash, false, update_a.contents.short_channel_id, true);
98 fn test_spendable_output<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, spendable_tx: &Transaction) {
99 let mut spendable = node.chain_monitor.chain_monitor.get_and_clear_pending_events();
100 assert_eq!(spendable.len(), 1);
101 if let Event::SpendableOutputs { outputs } = spendable.pop().unwrap() {
102 assert_eq!(outputs.len(), 1);
103 let spend_tx = node.keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
104 Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
105 check_spends!(spend_tx, spendable_tx);
110 fn revoked_output_htlc_resolution_timing() {
111 // Tests that HTLCs which were present in a broadcasted remote revoked commitment transaction
112 // are resolved only after a spend of the HTLC output reaches six confirmations. Preivously
113 // they would resolve after the revoked commitment transaction itself reaches six
115 let chanmon_cfgs = create_chanmon_cfgs(2);
116 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
117 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
118 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
120 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000);
122 let payment_hash_1 = route_payment(&nodes[1], &[&nodes[0]], 1_000_000).1;
124 // Get a commitment transaction which contains the HTLC we care about, but which we'll revoke
125 // before forwarding.
126 let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan.2);
127 assert_eq!(revoked_local_txn.len(), 1);
129 // Route a dust payment to revoke the above commitment transaction
130 route_payment(&nodes[0], &[&nodes[1]], 1_000);
132 // Confirm the revoked commitment transaction, closing the channel.
133 mine_transaction(&nodes[1], &revoked_local_txn[0]);
134 check_added_monitors!(nodes[1], 1);
135 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
136 check_closed_broadcast!(nodes[1], true);
138 let bs_spend_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
139 assert_eq!(bs_spend_txn.len(), 1);
140 check_spends!(bs_spend_txn[0], revoked_local_txn[0]);
142 // After the commitment transaction confirms, we should still wait on the HTLC spend
143 // transaction to confirm before resolving the HTLC.
144 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
145 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
146 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
148 // Spend the HTLC output, generating a HTLC failure event after ANTI_REORG_DELAY confirmations.
149 mine_transaction(&nodes[1], &bs_spend_txn[0]);
150 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
151 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
153 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
154 expect_payment_failed!(nodes[1], payment_hash_1, false);
158 fn chanmon_claim_value_coop_close() {
159 // Tests `get_claimable_balances` returns the correct values across a simple cooperative claim.
160 // Specifically, this tests that the channel non-HTLC balances show up in
161 // `get_claimable_balances` until the cooperative claims have confirmed and generated a
162 // `SpendableOutputs` event, and no longer.
163 let chanmon_cfgs = create_chanmon_cfgs(2);
164 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
165 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
166 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
168 let (_, _, chan_id, funding_tx) =
169 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 1_000_000);
170 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
171 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
173 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
174 let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
176 assert_eq!(vec![Balance::ClaimableOnChannelClose {
177 claimable_amount_satoshis: 1_000_000 - 1_000 - chan_feerate * channel::commitment_tx_base_weight(opt_anchors) / 1000
179 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
180 assert_eq!(vec![Balance::ClaimableOnChannelClose { claimable_amount_satoshis: 1_000, }],
181 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
183 nodes[0].node.close_channel(&chan_id, &nodes[1].node.get_our_node_id()).unwrap();
184 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
185 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
186 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
187 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
189 let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
190 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
191 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
192 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
193 let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
194 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
195 let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
196 assert!(node_1_none.is_none());
198 let shutdown_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
199 assert_eq!(shutdown_tx, nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0));
200 assert_eq!(shutdown_tx.len(), 1);
202 mine_transaction(&nodes[0], &shutdown_tx[0]);
203 mine_transaction(&nodes[1], &shutdown_tx[0]);
205 assert!(nodes[0].node.list_channels().is_empty());
206 assert!(nodes[1].node.list_channels().is_empty());
208 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
209 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
211 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
212 claimable_amount_satoshis: 1_000_000 - 1_000 - chan_feerate * channel::commitment_tx_base_weight(opt_anchors) / 1000,
213 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
215 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
216 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
217 claimable_amount_satoshis: 1000,
218 confirmation_height: nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1,
220 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
222 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
223 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
225 assert_eq!(Vec::<Balance>::new(),
226 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
227 assert_eq!(Vec::<Balance>::new(),
228 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
230 test_spendable_output(&nodes[0], &shutdown_tx[0]);
231 test_spendable_output(&nodes[1], &shutdown_tx[0]);
233 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure);
234 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure);
237 fn sorted_vec<T: Ord>(mut v: Vec<T>) -> Vec<T> {
242 /// Asserts that `a` and `b` are close, but maybe off by up to 5.
243 /// This is useful when checking fees and weights on transactions as things may vary by a few based
244 /// on signature size and signature size estimation being non-exact.
245 fn fuzzy_assert_eq<V: core::convert::TryInto<u64>>(a: V, b: V) {
246 let a_u64 = a.try_into().map_err(|_| ()).unwrap();
247 let b_u64 = b.try_into().map_err(|_| ()).unwrap();
248 eprintln!("Checking {} and {} for fuzzy equality", a_u64, b_u64);
249 assert!(a_u64 >= b_u64 - 5);
250 assert!(b_u64 >= a_u64 - 5);
253 fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
254 // Tests `get_claimable_balances` with an HTLC across a force-close.
255 // We build a channel with an HTLC pending, then force close the channel and check that the
256 // `get_claimable_balances` return value is correct as transactions confirm on-chain.
257 let mut chanmon_cfgs = create_chanmon_cfgs(2);
258 if prev_commitment_tx {
259 // We broadcast a second-to-latest commitment transaction, without providing the revocation
260 // secret to the counterparty. However, because we always immediately take the revocation
261 // secret from the keys_manager, we would panic at broadcast as we're trying to sign a
262 // transaction which, from the point of view of our keys_manager, is revoked.
263 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
265 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
266 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
267 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
269 let (_, _, chan_id, funding_tx) =
270 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 1_000_000);
271 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
272 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
274 // This HTLC is immediately claimed, giving node B the preimage
275 let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 3_000_000);
276 // This HTLC is allowed to time out, letting A claim it. However, in order to test claimable
277 // balances more fully we also give B the preimage for this HTLC.
278 let (timeout_payment_preimage, timeout_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 4_000_000);
279 // This HTLC will be dust, and not be claimable at all:
280 let (dust_payment_preimage, dust_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 3_000);
282 let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
284 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
285 let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
287 let remote_txn = get_local_commitment_txn!(nodes[1], chan_id);
288 // Before B receives the payment preimage, it only suggests the push_msat value of 1_000 sats
289 // as claimable. A lists both its to-self balance and the (possibly-claimable) HTLCs.
290 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
291 claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
292 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
293 }, Balance::MaybeTimeoutClaimableHTLC {
294 claimable_amount_satoshis: 3_000,
295 claimable_height: htlc_cltv_timeout,
296 }, Balance::MaybeTimeoutClaimableHTLC {
297 claimable_amount_satoshis: 4_000,
298 claimable_height: htlc_cltv_timeout,
300 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
301 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
302 claimable_amount_satoshis: 1_000,
303 }, Balance::MaybePreimageClaimableHTLC {
304 claimable_amount_satoshis: 3_000,
305 expiry_height: htlc_cltv_timeout,
306 }, Balance::MaybePreimageClaimableHTLC {
307 claimable_amount_satoshis: 4_000,
308 expiry_height: htlc_cltv_timeout,
310 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
312 nodes[1].node.claim_funds(payment_preimage);
313 check_added_monitors!(nodes[1], 1);
314 expect_payment_claimed!(nodes[1], payment_hash, 3_000_000);
316 let b_htlc_msgs = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
317 // We claim the dust payment here as well, but it won't impact our claimable balances as its
318 // dust and thus doesn't appear on chain at all.
319 nodes[1].node.claim_funds(dust_payment_preimage);
320 check_added_monitors!(nodes[1], 1);
321 expect_payment_claimed!(nodes[1], dust_payment_hash, 3_000);
323 nodes[1].node.claim_funds(timeout_payment_preimage);
324 check_added_monitors!(nodes[1], 1);
325 expect_payment_claimed!(nodes[1], timeout_payment_hash, 4_000_000);
327 if prev_commitment_tx {
328 // To build a previous commitment transaction, deliver one round of commitment messages.
329 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &b_htlc_msgs.update_fulfill_htlcs[0]);
330 expect_payment_sent_without_paths!(nodes[0], payment_preimage);
331 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &b_htlc_msgs.commitment_signed);
332 check_added_monitors!(nodes[0], 1);
333 let (as_raa, as_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
334 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
335 let _htlc_updates = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
336 check_added_monitors!(nodes[1], 1);
337 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs);
338 let _bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
339 check_added_monitors!(nodes[1], 1);
342 // Once B has received the payment preimage, it includes the value of the HTLC in its
343 // "claimable if you were to close the channel" balance.
344 let mut a_expected_balances = vec![Balance::ClaimableOnChannelClose {
345 claimable_amount_satoshis: 1_000_000 - // Channel funding value in satoshis
346 4_000 - // The to-be-failed HTLC value in satoshis
347 3_000 - // The claimed HTLC value in satoshis
348 1_000 - // The push_msat value in satoshis
349 3 - // The dust HTLC value in satoshis
350 // The commitment transaction fee with two HTLC outputs:
351 chan_feerate * (channel::commitment_tx_base_weight(opt_anchors) +
352 if prev_commitment_tx { 1 } else { 2 } *
353 channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
354 }, Balance::MaybeTimeoutClaimableHTLC {
355 claimable_amount_satoshis: 4_000,
356 claimable_height: htlc_cltv_timeout,
358 if !prev_commitment_tx {
359 a_expected_balances.push(Balance::MaybeTimeoutClaimableHTLC {
360 claimable_amount_satoshis: 3_000,
361 claimable_height: htlc_cltv_timeout,
364 assert_eq!(sorted_vec(a_expected_balances),
365 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
366 assert_eq!(vec![Balance::ClaimableOnChannelClose {
367 claimable_amount_satoshis: 1_000 + 3_000 + 4_000,
369 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
371 // Broadcast the closing transaction (which has both pending HTLCs in it) and get B's
372 // broadcasted HTLC claim transaction with preimage.
373 let node_b_commitment_claimable = nodes[1].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
374 mine_transaction(&nodes[0], &remote_txn[0]);
375 mine_transaction(&nodes[1], &remote_txn[0]);
377 let b_broadcast_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
378 assert_eq!(b_broadcast_txn.len(), 2);
379 // b_broadcast_txn should spend the HTLCs output of the commitment tx for 3_000 and 4_000 sats
380 check_spends!(b_broadcast_txn[0], remote_txn[0]);
381 check_spends!(b_broadcast_txn[1], remote_txn[0]);
382 assert_eq!(b_broadcast_txn[0].input.len(), 1);
383 assert_eq!(b_broadcast_txn[1].input.len(), 1);
384 assert_eq!(remote_txn[0].output[b_broadcast_txn[0].input[0].previous_output.vout as usize].value, 3_000);
385 assert_eq!(remote_txn[0].output[b_broadcast_txn[1].input[0].previous_output.vout as usize].value, 4_000);
387 assert!(nodes[0].node.list_channels().is_empty());
388 check_closed_broadcast!(nodes[0], true);
389 check_added_monitors!(nodes[0], 1);
390 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
391 assert!(nodes[1].node.list_channels().is_empty());
392 check_closed_broadcast!(nodes[1], true);
393 check_added_monitors!(nodes[1], 1);
394 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
395 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
396 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
398 // Once the commitment transaction confirms, we will wait until ANTI_REORG_DELAY until we
399 // generate any `SpendableOutputs` events. Thus, the same balances will still be listed
400 // available in `get_claimable_balances`. However, both will swap from `ClaimableOnClose` to
401 // other Balance variants, as close has already happened.
402 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
403 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
405 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
406 claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
407 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
408 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
409 }, Balance::MaybeTimeoutClaimableHTLC {
410 claimable_amount_satoshis: 3_000,
411 claimable_height: htlc_cltv_timeout,
412 }, Balance::MaybeTimeoutClaimableHTLC {
413 claimable_amount_satoshis: 4_000,
414 claimable_height: htlc_cltv_timeout,
416 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
417 // The main non-HTLC balance is just awaiting confirmations, but the claimable height is the
418 // CSV delay, not ANTI_REORG_DELAY.
419 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
420 claimable_amount_satoshis: 1_000,
421 confirmation_height: node_b_commitment_claimable,
423 // Both HTLC balances are "contentious" as our counterparty could claim them if we wait too
425 Balance::ContentiousClaimable {
426 claimable_amount_satoshis: 3_000,
427 timeout_height: htlc_cltv_timeout,
428 }, Balance::ContentiousClaimable {
429 claimable_amount_satoshis: 4_000,
430 timeout_height: htlc_cltv_timeout,
432 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
434 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
435 expect_payment_failed!(nodes[0], dust_payment_hash, false);
436 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
438 // After ANTI_REORG_DELAY, A will consider its balance fully spendable and generate a
439 // `SpendableOutputs` event. However, B still has to wait for the CSV delay.
440 assert_eq!(sorted_vec(vec![Balance::MaybeTimeoutClaimableHTLC {
441 claimable_amount_satoshis: 3_000,
442 claimable_height: htlc_cltv_timeout,
443 }, Balance::MaybeTimeoutClaimableHTLC {
444 claimable_amount_satoshis: 4_000,
445 claimable_height: htlc_cltv_timeout,
447 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
448 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
449 claimable_amount_satoshis: 1_000,
450 confirmation_height: node_b_commitment_claimable,
451 }, Balance::ContentiousClaimable {
452 claimable_amount_satoshis: 3_000,
453 timeout_height: htlc_cltv_timeout,
454 }, Balance::ContentiousClaimable {
455 claimable_amount_satoshis: 4_000,
456 timeout_height: htlc_cltv_timeout,
458 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
460 test_spendable_output(&nodes[0], &remote_txn[0]);
461 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
463 // After broadcasting the HTLC claim transaction, node A will still consider the HTLC
464 // possibly-claimable up to ANTI_REORG_DELAY, at which point it will drop it.
465 mine_transaction(&nodes[0], &b_broadcast_txn[0]);
466 if prev_commitment_tx {
467 expect_payment_path_successful!(nodes[0]);
469 expect_payment_sent!(nodes[0], payment_preimage);
471 assert_eq!(sorted_vec(vec![Balance::MaybeTimeoutClaimableHTLC {
472 claimable_amount_satoshis: 3_000,
473 claimable_height: htlc_cltv_timeout,
474 }, Balance::MaybeTimeoutClaimableHTLC {
475 claimable_amount_satoshis: 4_000,
476 claimable_height: htlc_cltv_timeout,
478 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
479 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
480 assert_eq!(vec![Balance::MaybeTimeoutClaimableHTLC {
481 claimable_amount_satoshis: 4_000,
482 claimable_height: htlc_cltv_timeout,
484 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
486 // When the HTLC timeout output is spendable in the next block, A should broadcast it
487 connect_blocks(&nodes[0], htlc_cltv_timeout - nodes[0].best_block_info().1 - 1);
488 let a_broadcast_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
489 assert_eq!(a_broadcast_txn.len(), 2);
490 assert_eq!(a_broadcast_txn[0].input.len(), 1);
491 check_spends!(a_broadcast_txn[0], remote_txn[0]);
492 assert_eq!(a_broadcast_txn[1].input.len(), 1);
493 check_spends!(a_broadcast_txn[1], remote_txn[0]);
494 assert_ne!(a_broadcast_txn[0].input[0].previous_output.vout,
495 a_broadcast_txn[1].input[0].previous_output.vout);
496 // a_broadcast_txn [0] and [1] should spend the HTLC outputs of the commitment tx
497 assert_eq!(remote_txn[0].output[a_broadcast_txn[0].input[0].previous_output.vout as usize].value, 3_000);
498 assert_eq!(remote_txn[0].output[a_broadcast_txn[1].input[0].previous_output.vout as usize].value, 4_000);
500 // Once the HTLC-Timeout transaction confirms, A will no longer consider the HTLC
501 // "MaybeClaimable", but instead move it to "AwaitingConfirmations".
502 mine_transaction(&nodes[0], &a_broadcast_txn[1]);
503 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
504 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
505 claimable_amount_satoshis: 4_000,
506 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
508 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
509 // After ANTI_REORG_DELAY, A will generate a SpendableOutputs event and drop the claimable
511 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
512 assert_eq!(Vec::<Balance>::new(),
513 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
514 expect_payment_failed!(nodes[0], timeout_payment_hash, false);
516 test_spendable_output(&nodes[0], &a_broadcast_txn[1]);
518 // Node B will no longer consider the HTLC "contentious" after the HTLC claim transaction
519 // confirms, and consider it simply "awaiting confirmations". Note that it has to wait for the
520 // standard revocable transaction CSV delay before receiving a `SpendableOutputs`.
521 let node_b_htlc_claimable = nodes[1].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
522 mine_transaction(&nodes[1], &b_broadcast_txn[0]);
524 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
525 claimable_amount_satoshis: 1_000,
526 confirmation_height: node_b_commitment_claimable,
527 }, Balance::ClaimableAwaitingConfirmations {
528 claimable_amount_satoshis: 3_000,
529 confirmation_height: node_b_htlc_claimable,
530 }, Balance::ContentiousClaimable {
531 claimable_amount_satoshis: 4_000,
532 timeout_height: htlc_cltv_timeout,
534 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
536 // After reaching the commitment output CSV, we'll get a SpendableOutputs event for it and have
537 // only the HTLCs claimable on node B.
538 connect_blocks(&nodes[1], node_b_commitment_claimable - nodes[1].best_block_info().1);
539 test_spendable_output(&nodes[1], &remote_txn[0]);
541 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
542 claimable_amount_satoshis: 3_000,
543 confirmation_height: node_b_htlc_claimable,
544 }, Balance::ContentiousClaimable {
545 claimable_amount_satoshis: 4_000,
546 timeout_height: htlc_cltv_timeout,
548 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
550 // After reaching the claimed HTLC output CSV, we'll get a SpendableOutptus event for it and
551 // have only one HTLC output left spendable.
552 connect_blocks(&nodes[1], node_b_htlc_claimable - nodes[1].best_block_info().1);
553 test_spendable_output(&nodes[1], &b_broadcast_txn[0]);
555 assert_eq!(vec![Balance::ContentiousClaimable {
556 claimable_amount_satoshis: 4_000,
557 timeout_height: htlc_cltv_timeout,
559 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
561 // Finally, mine the HTLC timeout transaction that A broadcasted (even though B should be able
562 // to claim this HTLC with the preimage it knows!). It will remain listed as a claimable HTLC
563 // until ANTI_REORG_DELAY confirmations on the spend.
564 mine_transaction(&nodes[1], &a_broadcast_txn[1]);
565 assert_eq!(vec![Balance::ContentiousClaimable {
566 claimable_amount_satoshis: 4_000,
567 timeout_height: htlc_cltv_timeout,
569 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
570 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
571 assert_eq!(Vec::<Balance>::new(),
572 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
574 // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
575 // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
576 // monitor events or claimable balances.
577 for node in nodes.iter() {
578 connect_blocks(node, 6);
579 connect_blocks(node, 6);
580 assert!(node.chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
581 assert!(node.chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
586 fn test_claim_value_force_close() {
587 do_test_claim_value_force_close(true);
588 do_test_claim_value_force_close(false);
592 fn test_balances_on_local_commitment_htlcs() {
593 // Previously, when handling the broadcast of a local commitment transactions (with associated
594 // CSV delays prior to spendability), we incorrectly handled the CSV delays on HTLC
595 // transactions. This caused us to miss spendable outputs for HTLCs which were awaiting a CSV
596 // delay prior to spendability.
598 // Further, because of this, we could hit an assertion as `get_claimable_balances` asserted
599 // that HTLCs were resolved after the funding spend was resolved, which was not true if the
600 // HTLC did not have a CSV delay attached (due to the above bug or due to it being an HTLC
601 // claim by our counterparty).
602 let chanmon_cfgs = create_chanmon_cfgs(2);
603 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
604 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
605 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
607 // Create a single channel with two pending HTLCs from nodes[0] to nodes[1], one which nodes[1]
608 // knows the preimage for, one which it does not.
609 let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
610 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
612 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 10_000_000);
613 let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
614 nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap();
615 check_added_monitors!(nodes[0], 1);
617 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
618 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
619 commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
621 expect_pending_htlcs_forwardable!(nodes[1]);
622 expect_payment_claimable!(nodes[1], payment_hash, payment_secret, 10_000_000);
624 let (route_2, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 20_000_000);
625 nodes[0].node.send_payment(&route_2, payment_hash_2, &Some(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
626 check_added_monitors!(nodes[0], 1);
628 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
629 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
630 commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
632 expect_pending_htlcs_forwardable!(nodes[1]);
633 expect_payment_claimable!(nodes[1], payment_hash_2, payment_secret_2, 20_000_000);
634 nodes[1].node.claim_funds(payment_preimage_2);
635 get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
636 check_added_monitors!(nodes[1], 1);
637 expect_payment_claimed!(nodes[1], payment_hash_2, 20_000_000);
639 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
640 let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
642 // Get nodes[0]'s commitment transaction and HTLC-Timeout transactions
643 let as_txn = get_local_commitment_txn!(nodes[0], chan_id);
644 assert_eq!(as_txn.len(), 3);
645 check_spends!(as_txn[1], as_txn[0]);
646 check_spends!(as_txn[2], as_txn[0]);
647 check_spends!(as_txn[0], funding_tx);
649 // First confirm the commitment transaction on nodes[0], which should leave us with three
650 // claimable balances.
651 let node_a_commitment_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
652 mine_transaction(&nodes[0], &as_txn[0]);
653 check_added_monitors!(nodes[0], 1);
654 check_closed_broadcast!(nodes[0], true);
655 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
657 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
658 claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
659 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
660 confirmation_height: node_a_commitment_claimable,
661 }, Balance::MaybeTimeoutClaimableHTLC {
662 claimable_amount_satoshis: 10_000,
663 claimable_height: htlc_cltv_timeout,
664 }, Balance::MaybeTimeoutClaimableHTLC {
665 claimable_amount_satoshis: 20_000,
666 claimable_height: htlc_cltv_timeout,
668 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
670 // Get nodes[1]'s HTLC claim tx for the second HTLC
671 mine_transaction(&nodes[1], &as_txn[0]);
672 check_added_monitors!(nodes[1], 1);
673 check_closed_broadcast!(nodes[1], true);
674 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
675 let bs_htlc_claim_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
676 assert_eq!(bs_htlc_claim_txn.len(), 1);
677 check_spends!(bs_htlc_claim_txn[0], as_txn[0]);
679 // Connect blocks until the HTLCs expire, allowing us to (validly) broadcast the HTLC-Timeout
681 connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1);
682 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
683 claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
684 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
685 confirmation_height: node_a_commitment_claimable,
686 }, Balance::MaybeTimeoutClaimableHTLC {
687 claimable_amount_satoshis: 10_000,
688 claimable_height: htlc_cltv_timeout,
689 }, Balance::MaybeTimeoutClaimableHTLC {
690 claimable_amount_satoshis: 20_000,
691 claimable_height: htlc_cltv_timeout,
693 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
694 assert_eq!(as_txn[1].lock_time.0, nodes[0].best_block_info().1 + 1); // as_txn[1] can be included in the next block
696 // Now confirm nodes[0]'s HTLC-Timeout transaction, which changes the claimable balance to an
697 // "awaiting confirmations" one.
698 let node_a_htlc_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
699 mine_transaction(&nodes[0], &as_txn[1]);
700 // Note that prior to the fix in the commit which introduced this test, this (and the next
701 // balance) check failed. With this check removed, the code panicked in the `connect_blocks`
702 // call, as described, two hunks down.
703 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
704 claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
705 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
706 confirmation_height: node_a_commitment_claimable,
707 }, Balance::ClaimableAwaitingConfirmations {
708 claimable_amount_satoshis: 10_000,
709 confirmation_height: node_a_htlc_claimable,
710 }, Balance::MaybeTimeoutClaimableHTLC {
711 claimable_amount_satoshis: 20_000,
712 claimable_height: htlc_cltv_timeout,
714 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
716 // Now confirm nodes[1]'s HTLC claim, giving nodes[0] the preimage. Note that the "maybe
717 // claimable" balance remains until we see ANTI_REORG_DELAY blocks.
718 mine_transaction(&nodes[0], &bs_htlc_claim_txn[0]);
719 expect_payment_sent!(nodes[0], payment_preimage_2);
720 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
721 claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
722 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
723 confirmation_height: node_a_commitment_claimable,
724 }, Balance::ClaimableAwaitingConfirmations {
725 claimable_amount_satoshis: 10_000,
726 confirmation_height: node_a_htlc_claimable,
727 }, Balance::MaybeTimeoutClaimableHTLC {
728 claimable_amount_satoshis: 20_000,
729 claimable_height: htlc_cltv_timeout,
731 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
733 // Finally make the HTLC transactions have ANTI_REORG_DELAY blocks. This call previously
734 // panicked as described in the test introduction. This will remove the "maybe claimable"
735 // spendable output as nodes[1] has fully claimed the second HTLC.
736 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
737 expect_payment_failed!(nodes[0], payment_hash, false);
739 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
740 claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
741 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
742 confirmation_height: node_a_commitment_claimable,
743 }, Balance::ClaimableAwaitingConfirmations {
744 claimable_amount_satoshis: 10_000,
745 confirmation_height: node_a_htlc_claimable,
747 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
749 // Connect blocks until the commitment transaction's CSV expires, providing us the relevant
750 // `SpendableOutputs` event and removing the claimable balance entry.
751 connect_blocks(&nodes[0], node_a_commitment_claimable - nodes[0].best_block_info().1);
752 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
753 claimable_amount_satoshis: 10_000,
754 confirmation_height: node_a_htlc_claimable,
756 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
757 test_spendable_output(&nodes[0], &as_txn[0]);
759 // Connect blocks until the HTLC-Timeout's CSV expires, providing us the relevant
760 // `SpendableOutputs` event and removing the claimable balance entry.
761 connect_blocks(&nodes[0], node_a_htlc_claimable - nodes[0].best_block_info().1);
762 assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
763 test_spendable_output(&nodes[0], &as_txn[1]);
765 // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
766 // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
767 // monitor events or claimable balances.
768 connect_blocks(&nodes[0], 6);
769 connect_blocks(&nodes[0], 6);
770 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
771 assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
775 fn test_no_preimage_inbound_htlc_balances() {
776 // Tests that MaybePreimageClaimableHTLC are generated for inbound HTLCs for which we do not
778 let chanmon_cfgs = create_chanmon_cfgs(2);
779 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
780 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
781 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
783 let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000);
784 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
786 // Send two HTLCs, one from A to B, and one from B to A.
787 let to_b_failed_payment_hash = route_payment(&nodes[0], &[&nodes[1]], 10_000_000).1;
788 let to_a_failed_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 20_000_000).1;
789 let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
791 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
792 let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
794 // Both A and B will have an HTLC that's claimable on timeout and one that's claimable if they
795 // receive the preimage. These will remain the same through the channel closure and until the
796 // HTLC output is spent.
798 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
799 claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
800 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
801 }, Balance::MaybePreimageClaimableHTLC {
802 claimable_amount_satoshis: 20_000,
803 expiry_height: htlc_cltv_timeout,
804 }, Balance::MaybeTimeoutClaimableHTLC {
805 claimable_amount_satoshis: 10_000,
806 claimable_height: htlc_cltv_timeout,
808 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
810 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
811 claimable_amount_satoshis: 500_000 - 20_000,
812 }, Balance::MaybePreimageClaimableHTLC {
813 claimable_amount_satoshis: 10_000,
814 expiry_height: htlc_cltv_timeout,
815 }, Balance::MaybeTimeoutClaimableHTLC {
816 claimable_amount_satoshis: 20_000,
817 claimable_height: htlc_cltv_timeout,
819 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
821 // Get nodes[0]'s commitment transaction and HTLC-Timeout transaction
822 let as_txn = get_local_commitment_txn!(nodes[0], chan_id);
823 assert_eq!(as_txn.len(), 2);
824 check_spends!(as_txn[1], as_txn[0]);
825 check_spends!(as_txn[0], funding_tx);
827 // Now close the channel by confirming A's commitment transaction on both nodes, checking the
828 // claimable balances remain the same except for the non-HTLC balance changing variant.
829 let node_a_commitment_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
830 let as_pre_spend_claims = sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
831 claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
832 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
833 confirmation_height: node_a_commitment_claimable,
834 }, Balance::MaybePreimageClaimableHTLC {
835 claimable_amount_satoshis: 20_000,
836 expiry_height: htlc_cltv_timeout,
837 }, Balance::MaybeTimeoutClaimableHTLC {
838 claimable_amount_satoshis: 10_000,
839 claimable_height: htlc_cltv_timeout,
842 mine_transaction(&nodes[0], &as_txn[0]);
843 nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
844 check_added_monitors!(nodes[0], 1);
845 check_closed_broadcast!(nodes[0], true);
846 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
848 assert_eq!(as_pre_spend_claims,
849 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
851 mine_transaction(&nodes[1], &as_txn[0]);
852 check_added_monitors!(nodes[1], 1);
853 check_closed_broadcast!(nodes[1], true);
854 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
856 let node_b_commitment_claimable = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
857 let mut bs_pre_spend_claims = sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
858 claimable_amount_satoshis: 500_000 - 20_000,
859 confirmation_height: node_b_commitment_claimable,
860 }, Balance::MaybePreimageClaimableHTLC {
861 claimable_amount_satoshis: 10_000,
862 expiry_height: htlc_cltv_timeout,
863 }, Balance::MaybeTimeoutClaimableHTLC {
864 claimable_amount_satoshis: 20_000,
865 claimable_height: htlc_cltv_timeout,
867 assert_eq!(bs_pre_spend_claims,
868 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
870 // We'll broadcast the HTLC-Timeout transaction one block prior to the htlc's expiration (as it
871 // is confirmable in the next block), but will still include the same claimable balances as no
872 // HTLC has been spent, even after the HTLC expires. We'll also fail the inbound HTLC, but it
873 // won't do anything as the channel is already closed.
875 connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1);
876 let as_htlc_timeout_claim = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
877 assert_eq!(as_htlc_timeout_claim.len(), 1);
878 check_spends!(as_htlc_timeout_claim[0], as_txn[0]);
879 expect_pending_htlcs_forwardable_conditions!(nodes[0],
880 [HTLCDestination::FailedPayment { payment_hash: to_a_failed_payment_hash }]);
882 assert_eq!(as_pre_spend_claims,
883 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
885 connect_blocks(&nodes[0], 1);
886 assert_eq!(as_pre_spend_claims,
887 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
889 // For node B, we'll get the non-HTLC funds claimable after ANTI_REORG_DELAY confirmations
890 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
891 test_spendable_output(&nodes[1], &as_txn[0]);
892 bs_pre_spend_claims.retain(|e| if let Balance::ClaimableAwaitingConfirmations { .. } = e { false } else { true });
894 // The next few blocks for B look the same as for A, though for the opposite HTLC
895 nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
896 connect_blocks(&nodes[1], TEST_FINAL_CLTV - (ANTI_REORG_DELAY - 1) - 1);
897 expect_pending_htlcs_forwardable_conditions!(nodes[1],
898 [HTLCDestination::FailedPayment { payment_hash: to_b_failed_payment_hash }]);
899 let bs_htlc_timeout_claim = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
900 assert_eq!(bs_htlc_timeout_claim.len(), 1);
901 check_spends!(bs_htlc_timeout_claim[0], as_txn[0]);
903 assert_eq!(bs_pre_spend_claims,
904 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
906 connect_blocks(&nodes[1], 1);
907 assert_eq!(bs_pre_spend_claims,
908 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
910 // Now confirm the two HTLC timeout transactions for A, checking that the inbound HTLC resolves
911 // after ANTI_REORG_DELAY confirmations and the other takes BREAKDOWN_TIMEOUT confirmations.
912 mine_transaction(&nodes[0], &as_htlc_timeout_claim[0]);
913 let as_timeout_claimable_height = nodes[0].best_block_info().1 + (BREAKDOWN_TIMEOUT as u32) - 1;
914 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
915 claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
916 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
917 confirmation_height: node_a_commitment_claimable,
918 }, Balance::MaybePreimageClaimableHTLC {
919 claimable_amount_satoshis: 20_000,
920 expiry_height: htlc_cltv_timeout,
921 }, Balance::ClaimableAwaitingConfirmations {
922 claimable_amount_satoshis: 10_000,
923 confirmation_height: as_timeout_claimable_height,
925 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
927 mine_transaction(&nodes[0], &bs_htlc_timeout_claim[0]);
928 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
929 claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
930 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
931 confirmation_height: node_a_commitment_claimable,
932 }, Balance::MaybePreimageClaimableHTLC {
933 claimable_amount_satoshis: 20_000,
934 expiry_height: htlc_cltv_timeout,
935 }, Balance::ClaimableAwaitingConfirmations {
936 claimable_amount_satoshis: 10_000,
937 confirmation_height: as_timeout_claimable_height,
939 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
941 // Once as_htlc_timeout_claim[0] reaches ANTI_REORG_DELAY confirmations, we should get a
942 // payment failure event.
943 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 2);
944 expect_payment_failed!(nodes[0], to_b_failed_payment_hash, false);
946 connect_blocks(&nodes[0], 1);
947 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
948 claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
949 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
950 confirmation_height: node_a_commitment_claimable,
951 }, Balance::ClaimableAwaitingConfirmations {
952 claimable_amount_satoshis: 10_000,
953 confirmation_height: core::cmp::max(as_timeout_claimable_height, htlc_cltv_timeout),
955 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
957 connect_blocks(&nodes[0], node_a_commitment_claimable - nodes[0].best_block_info().1);
958 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
959 claimable_amount_satoshis: 10_000,
960 confirmation_height: core::cmp::max(as_timeout_claimable_height, htlc_cltv_timeout),
962 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
963 test_spendable_output(&nodes[0], &as_txn[0]);
965 connect_blocks(&nodes[0], as_timeout_claimable_height - nodes[0].best_block_info().1);
966 assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
967 test_spendable_output(&nodes[0], &as_htlc_timeout_claim[0]);
969 // The process for B should be completely identical as well, noting that the non-HTLC-balance
970 // was already claimed.
971 mine_transaction(&nodes[1], &bs_htlc_timeout_claim[0]);
972 let bs_timeout_claimable_height = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
973 assert_eq!(sorted_vec(vec![Balance::MaybePreimageClaimableHTLC {
974 claimable_amount_satoshis: 10_000,
975 expiry_height: htlc_cltv_timeout,
976 }, Balance::ClaimableAwaitingConfirmations {
977 claimable_amount_satoshis: 20_000,
978 confirmation_height: bs_timeout_claimable_height,
980 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
982 mine_transaction(&nodes[1], &as_htlc_timeout_claim[0]);
983 assert_eq!(sorted_vec(vec![Balance::MaybePreimageClaimableHTLC {
984 claimable_amount_satoshis: 10_000,
985 expiry_height: htlc_cltv_timeout,
986 }, Balance::ClaimableAwaitingConfirmations {
987 claimable_amount_satoshis: 20_000,
988 confirmation_height: bs_timeout_claimable_height,
990 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
992 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 2);
993 expect_payment_failed!(nodes[1], to_a_failed_payment_hash, false);
995 assert_eq!(vec![Balance::MaybePreimageClaimableHTLC {
996 claimable_amount_satoshis: 10_000,
997 expiry_height: htlc_cltv_timeout,
999 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
1000 test_spendable_output(&nodes[1], &bs_htlc_timeout_claim[0]);
1002 connect_blocks(&nodes[1], 1);
1003 assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1005 // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
1006 // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
1007 // monitor events or claimable balances.
1008 connect_blocks(&nodes[1], 6);
1009 connect_blocks(&nodes[1], 6);
1010 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1011 assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1014 fn sorted_vec_with_additions<T: Ord + Clone>(v_orig: &Vec<T>, extra_ts: &[&T]) -> Vec<T> {
1015 let mut v = v_orig.clone();
1017 v.push((*t).clone());
1023 fn do_test_revoked_counterparty_commitment_balances(confirm_htlc_spend_first: bool) {
1024 // Tests `get_claimable_balances` for revoked counterparty commitment transactions.
1025 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1026 // We broadcast a second-to-latest commitment transaction, without providing the revocation
1027 // secret to the counterparty. However, because we always immediately take the revocation
1028 // secret from the keys_manager, we would panic at broadcast as we're trying to sign a
1029 // transaction which, from the point of view of our keys_manager, is revoked.
1030 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
1031 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1032 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1033 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1035 let (_, _, chan_id, funding_tx) =
1036 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 100_000_000);
1037 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
1038 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
1040 // We create five HTLCs for B to claim against A's revoked commitment transaction:
1042 // (1) one for which A is the originator and B knows the preimage
1043 // (2) one for which B is the originator where the HTLC has since timed-out
1044 // (3) one for which B is the originator but where the HTLC has not yet timed-out
1045 // (4) one dust HTLC which is lost in the channel closure
1046 // (5) one that actually isn't in the revoked commitment transaction at all, but was added in
1047 // later commitment transaction updates
1049 // Though they could all be claimed in a single claim transaction, due to CLTV timeouts they
1050 // are all currently claimed in separate transactions, which helps us test as we can claim
1051 // HTLCs individually.
1053 let (claimed_payment_preimage, claimed_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 3_000_000);
1054 let timeout_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 4_000_000).1;
1055 let dust_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 3_000).1;
1057 let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
1059 connect_blocks(&nodes[0], 10);
1060 connect_blocks(&nodes[1], 10);
1062 let live_htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
1063 let live_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 5_000_000).1;
1065 // Get the latest commitment transaction from A and then update the fee to revoke it
1066 let as_revoked_txn = get_local_commitment_txn!(nodes[0], chan_id);
1067 let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
1069 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
1071 let missing_htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
1072 let missing_htlc_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 2_000_000).1;
1074 nodes[1].node.claim_funds(claimed_payment_preimage);
1075 expect_payment_claimed!(nodes[1], claimed_payment_hash, 3_000_000);
1076 check_added_monitors!(nodes[1], 1);
1077 let _b_htlc_msgs = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
1079 connect_blocks(&nodes[0], htlc_cltv_timeout + 1 - 10);
1080 check_closed_broadcast!(nodes[0], true);
1081 check_added_monitors!(nodes[0], 1);
1083 let mut events = nodes[0].node.get_and_clear_pending_events();
1084 assert_eq!(events.len(), 6);
1085 let mut failed_payments: HashSet<_> =
1086 [timeout_payment_hash, dust_payment_hash, live_payment_hash, missing_htlc_payment_hash]
1087 .iter().map(|a| *a).collect();
1088 events.retain(|ev| {
1090 Event::HTLCHandlingFailed { failed_next_destination: HTLCDestination::NextHopChannel { node_id, channel_id }, .. } => {
1091 assert_eq!(*channel_id, chan_id);
1092 assert_eq!(*node_id, Some(nodes[1].node.get_our_node_id()));
1095 Event::HTLCHandlingFailed { failed_next_destination: HTLCDestination::FailedPayment { payment_hash }, .. } => {
1096 assert!(failed_payments.remove(payment_hash));
1102 assert!(failed_payments.is_empty());
1103 if let Event::PendingHTLCsForwardable { .. } = events[0] {} else { panic!(); }
1105 Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {},
1109 connect_blocks(&nodes[1], htlc_cltv_timeout + 1 - 10);
1110 check_closed_broadcast!(nodes[1], true);
1111 check_added_monitors!(nodes[1], 1);
1112 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
1114 // Prior to channel closure, B considers the preimage HTLC as its own, and otherwise only
1115 // lists the two on-chain timeout-able HTLCs as claimable balances.
1116 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
1117 claimable_amount_satoshis: 100_000 - 5_000 - 4_000 - 3 - 2_000 + 3_000,
1118 }, Balance::MaybeTimeoutClaimableHTLC {
1119 claimable_amount_satoshis: 2_000,
1120 claimable_height: missing_htlc_cltv_timeout,
1121 }, Balance::MaybeTimeoutClaimableHTLC {
1122 claimable_amount_satoshis: 4_000,
1123 claimable_height: htlc_cltv_timeout,
1124 }, Balance::MaybeTimeoutClaimableHTLC {
1125 claimable_amount_satoshis: 5_000,
1126 claimable_height: live_htlc_cltv_timeout,
1128 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1130 mine_transaction(&nodes[1], &as_revoked_txn[0]);
1131 let mut claim_txn: Vec<_> = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().drain(..).filter(|tx| tx.input.iter().any(|inp| inp.previous_output.txid == as_revoked_txn[0].txid())).collect();
1132 // Currently the revoked commitment is claimed in four transactions as the HTLCs all expire
1134 assert_eq!(claim_txn.len(), 4);
1135 claim_txn.sort_unstable_by_key(|tx| tx.output.iter().map(|output| output.value).sum::<u64>());
1137 // The following constants were determined experimentally
1138 const BS_TO_SELF_CLAIM_EXP_WEIGHT: usize = 483;
1139 const OUTBOUND_HTLC_CLAIM_EXP_WEIGHT: usize = 571;
1140 const INBOUND_HTLC_CLAIM_EXP_WEIGHT: usize = 578;
1142 // Check that the weight is close to the expected weight. Note that signature sizes vary
1143 // somewhat so it may not always be exact.
1144 fuzzy_assert_eq(claim_txn[0].weight(), OUTBOUND_HTLC_CLAIM_EXP_WEIGHT);
1145 fuzzy_assert_eq(claim_txn[1].weight(), INBOUND_HTLC_CLAIM_EXP_WEIGHT);
1146 fuzzy_assert_eq(claim_txn[2].weight(), INBOUND_HTLC_CLAIM_EXP_WEIGHT);
1147 fuzzy_assert_eq(claim_txn[3].weight(), BS_TO_SELF_CLAIM_EXP_WEIGHT);
1149 // The expected balance for the next three checks, with the largest-HTLC and to_self output
1150 // claim balances separated out.
1151 let expected_balance = vec![Balance::ClaimableAwaitingConfirmations {
1152 // to_remote output in A's revoked commitment
1153 claimable_amount_satoshis: 100_000 - 5_000 - 4_000 - 3,
1154 confirmation_height: nodes[1].best_block_info().1 + 5,
1155 }, Balance::CounterpartyRevokedOutputClaimable {
1156 claimable_amount_satoshis: 3_000,
1157 }, Balance::CounterpartyRevokedOutputClaimable {
1158 claimable_amount_satoshis: 4_000,
1161 let to_self_unclaimed_balance = Balance::CounterpartyRevokedOutputClaimable {
1162 claimable_amount_satoshis: 1_000_000 - 100_000 - 3_000 - chan_feerate *
1163 (channel::commitment_tx_base_weight(opt_anchors) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1165 let to_self_claimed_avail_height;
1166 let largest_htlc_unclaimed_balance = Balance::CounterpartyRevokedOutputClaimable {
1167 claimable_amount_satoshis: 5_000,
1169 let largest_htlc_claimed_avail_height;
1171 // Once the channel has been closed by A, B now considers all of the commitment transactions'
1172 // outputs as `CounterpartyRevokedOutputClaimable`.
1173 assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_unclaimed_balance, &largest_htlc_unclaimed_balance]),
1174 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1176 if confirm_htlc_spend_first {
1177 mine_transaction(&nodes[1], &claim_txn[2]);
1178 largest_htlc_claimed_avail_height = nodes[1].best_block_info().1 + 5;
1179 to_self_claimed_avail_height = nodes[1].best_block_info().1 + 6; // will be claimed in the next block
1181 // Connect the to_self output claim, taking all of A's non-HTLC funds
1182 mine_transaction(&nodes[1], &claim_txn[3]);
1183 to_self_claimed_avail_height = nodes[1].best_block_info().1 + 5;
1184 largest_htlc_claimed_avail_height = nodes[1].best_block_info().1 + 6; // will be claimed in the next block
1187 let largest_htlc_claimed_balance = Balance::ClaimableAwaitingConfirmations {
1188 claimable_amount_satoshis: 5_000 - chan_feerate * INBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
1189 confirmation_height: largest_htlc_claimed_avail_height,
1191 let to_self_claimed_balance = Balance::ClaimableAwaitingConfirmations {
1192 claimable_amount_satoshis: 1_000_000 - 100_000 - 3_000 - chan_feerate *
1193 (channel::commitment_tx_base_weight(opt_anchors) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
1194 - chan_feerate * claim_txn[3].weight() as u64 / 1000,
1195 confirmation_height: to_self_claimed_avail_height,
1198 if confirm_htlc_spend_first {
1199 assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_unclaimed_balance, &largest_htlc_claimed_balance]),
1200 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1202 assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_claimed_balance, &largest_htlc_unclaimed_balance]),
1203 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1206 if confirm_htlc_spend_first {
1207 mine_transaction(&nodes[1], &claim_txn[3]);
1209 mine_transaction(&nodes[1], &claim_txn[2]);
1211 assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_claimed_balance, &largest_htlc_claimed_balance]),
1212 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1214 // Finally, connect the last two remaining HTLC spends and check that they move to
1215 // `ClaimableAwaitingConfirmations`
1216 mine_transaction(&nodes[1], &claim_txn[0]);
1217 mine_transaction(&nodes[1], &claim_txn[1]);
1219 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1220 // to_remote output in A's revoked commitment
1221 claimable_amount_satoshis: 100_000 - 5_000 - 4_000 - 3,
1222 confirmation_height: nodes[1].best_block_info().1 + 1,
1223 }, Balance::ClaimableAwaitingConfirmations {
1224 claimable_amount_satoshis: 1_000_000 - 100_000 - 3_000 - chan_feerate *
1225 (channel::commitment_tx_base_weight(opt_anchors) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
1226 - chan_feerate * claim_txn[3].weight() as u64 / 1000,
1227 confirmation_height: to_self_claimed_avail_height,
1228 }, Balance::ClaimableAwaitingConfirmations {
1229 claimable_amount_satoshis: 3_000 - chan_feerate * OUTBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
1230 confirmation_height: nodes[1].best_block_info().1 + 4,
1231 }, Balance::ClaimableAwaitingConfirmations {
1232 claimable_amount_satoshis: 4_000 - chan_feerate * INBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
1233 confirmation_height: nodes[1].best_block_info().1 + 5,
1234 }, Balance::ClaimableAwaitingConfirmations {
1235 claimable_amount_satoshis: 5_000 - chan_feerate * INBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
1236 confirmation_height: largest_htlc_claimed_avail_height,
1238 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1240 connect_blocks(&nodes[1], 1);
1241 test_spendable_output(&nodes[1], &as_revoked_txn[0]);
1243 let mut payment_failed_events = nodes[1].node.get_and_clear_pending_events();
1244 expect_payment_failed_conditions_event(payment_failed_events[..2].to_vec(),
1245 missing_htlc_payment_hash, false, PaymentFailedConditions::new());
1246 expect_payment_failed_conditions_event(payment_failed_events[2..].to_vec(),
1247 dust_payment_hash, false, PaymentFailedConditions::new());
1249 connect_blocks(&nodes[1], 1);
1250 test_spendable_output(&nodes[1], &claim_txn[if confirm_htlc_spend_first { 2 } else { 3 }]);
1251 connect_blocks(&nodes[1], 1);
1252 test_spendable_output(&nodes[1], &claim_txn[if confirm_htlc_spend_first { 3 } else { 2 }]);
1253 expect_payment_failed!(nodes[1], live_payment_hash, false);
1254 connect_blocks(&nodes[1], 1);
1255 test_spendable_output(&nodes[1], &claim_txn[0]);
1256 connect_blocks(&nodes[1], 1);
1257 test_spendable_output(&nodes[1], &claim_txn[1]);
1258 expect_payment_failed!(nodes[1], timeout_payment_hash, false);
1259 assert_eq!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances(), Vec::new());
1261 // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
1262 // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
1263 // monitor events or claimable balances.
1264 connect_blocks(&nodes[1], 6);
1265 connect_blocks(&nodes[1], 6);
1266 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1267 assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1271 fn test_revoked_counterparty_commitment_balances() {
1272 do_test_revoked_counterparty_commitment_balances(true);
1273 do_test_revoked_counterparty_commitment_balances(false);
1277 fn test_revoked_counterparty_htlc_tx_balances() {
1278 // Tests `get_claimable_balances` for revocation spends of HTLC transactions.
1279 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1280 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
1281 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1282 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1283 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1285 // Create some initial channels
1286 let (_, _, chan_id, funding_tx) =
1287 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 11_000_000);
1288 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
1289 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
1291 let payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 3_000_000).0;
1292 let failed_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 1_000_000).1;
1293 let revoked_local_txn = get_local_commitment_txn!(nodes[1], chan_id);
1294 assert_eq!(revoked_local_txn[0].input.len(), 1);
1295 assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, funding_tx.txid());
1297 // The to-be-revoked commitment tx should have two HTLCs and an output for both sides
1298 assert_eq!(revoked_local_txn[0].output.len(), 4);
1300 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
1302 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
1303 let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
1305 // B will generate an HTLC-Success from its revoked commitment tx
1306 mine_transaction(&nodes[1], &revoked_local_txn[0]);
1307 check_closed_broadcast!(nodes[1], true);
1308 check_added_monitors!(nodes[1], 1);
1309 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
1310 let revoked_htlc_success_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1312 assert_eq!(revoked_htlc_success_txn.len(), 1);
1313 assert_eq!(revoked_htlc_success_txn[0].input.len(), 1);
1314 assert_eq!(revoked_htlc_success_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
1315 check_spends!(revoked_htlc_success_txn[0], revoked_local_txn[0]);
1317 connect_blocks(&nodes[1], TEST_FINAL_CLTV);
1318 let revoked_htlc_timeout_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1319 assert_eq!(revoked_htlc_timeout_txn.len(), 1);
1320 check_spends!(revoked_htlc_timeout_txn[0], revoked_local_txn[0]);
1321 assert_ne!(revoked_htlc_success_txn[0].input[0].previous_output, revoked_htlc_timeout_txn[0].input[0].previous_output);
1322 assert_eq!(revoked_htlc_success_txn[0].lock_time.0, 0);
1323 assert_ne!(revoked_htlc_timeout_txn[0].lock_time.0, 0);
1325 // A will generate justice tx from B's revoked commitment/HTLC tx
1326 mine_transaction(&nodes[0], &revoked_local_txn[0]);
1327 check_closed_broadcast!(nodes[0], true);
1328 check_added_monitors!(nodes[0], 1);
1329 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
1330 let to_remote_conf_height = nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1;
1332 let as_commitment_claim_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1333 assert_eq!(as_commitment_claim_txn.len(), 1);
1334 check_spends!(as_commitment_claim_txn[0], revoked_local_txn[0]);
1336 // The next two checks have the same balance set for A - even though we confirm a revoked HTLC
1337 // transaction our balance tracking doesn't use the on-chain value so the
1338 // `CounterpartyRevokedOutputClaimable` entry doesn't change.
1339 let as_balances = sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1340 // to_remote output in B's revoked commitment
1341 claimable_amount_satoshis: 1_000_000 - 11_000 - 3_000 - chan_feerate *
1342 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1343 confirmation_height: to_remote_conf_height,
1344 }, Balance::CounterpartyRevokedOutputClaimable {
1345 // to_self output in B's revoked commitment
1346 claimable_amount_satoshis: 10_000,
1347 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1348 claimable_amount_satoshis: 3_000,
1349 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1350 claimable_amount_satoshis: 1_000,
1352 assert_eq!(as_balances,
1353 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1355 mine_transaction(&nodes[0], &revoked_htlc_success_txn[0]);
1356 let as_htlc_claim_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1357 assert_eq!(as_htlc_claim_tx.len(), 2);
1358 check_spends!(as_htlc_claim_tx[0], revoked_htlc_success_txn[0]);
1359 check_spends!(as_htlc_claim_tx[1], revoked_local_txn[0]); // A has to generate a new claim for the remaining revoked
1360 // outputs (which no longer includes the spent HTLC output)
1362 assert_eq!(as_balances,
1363 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1365 assert_eq!(as_htlc_claim_tx[0].output.len(), 1);
1366 fuzzy_assert_eq(as_htlc_claim_tx[0].output[0].value,
1367 3_000 - chan_feerate * (revoked_htlc_success_txn[0].weight() + as_htlc_claim_tx[0].weight()) as u64 / 1000);
1369 mine_transaction(&nodes[0], &as_htlc_claim_tx[0]);
1370 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1371 // to_remote output in B's revoked commitment
1372 claimable_amount_satoshis: 1_000_000 - 11_000 - 3_000 - chan_feerate *
1373 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1374 confirmation_height: to_remote_conf_height,
1375 }, Balance::CounterpartyRevokedOutputClaimable {
1376 // to_self output in B's revoked commitment
1377 claimable_amount_satoshis: 10_000,
1378 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1379 claimable_amount_satoshis: 1_000,
1380 }, Balance::ClaimableAwaitingConfirmations {
1381 claimable_amount_satoshis: as_htlc_claim_tx[0].output[0].value,
1382 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
1384 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1386 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 3);
1387 test_spendable_output(&nodes[0], &revoked_local_txn[0]);
1388 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1389 // to_self output to B
1390 claimable_amount_satoshis: 10_000,
1391 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1392 claimable_amount_satoshis: 1_000,
1393 }, Balance::ClaimableAwaitingConfirmations {
1394 claimable_amount_satoshis: as_htlc_claim_tx[0].output[0].value,
1395 confirmation_height: nodes[0].best_block_info().1 + 2,
1397 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1399 connect_blocks(&nodes[0], 2);
1400 test_spendable_output(&nodes[0], &as_htlc_claim_tx[0]);
1401 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1402 // to_self output in B's revoked commitment
1403 claimable_amount_satoshis: 10_000,
1404 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1405 claimable_amount_satoshis: 1_000,
1407 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1409 connect_blocks(&nodes[0], revoked_htlc_timeout_txn[0].lock_time.0 - nodes[0].best_block_info().1);
1410 expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(&nodes[0],
1411 [HTLCDestination::FailedPayment { payment_hash: failed_payment_hash }]);
1412 // As time goes on A may split its revocation claim transaction into multiple.
1413 let as_fewer_input_rbf = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1414 for tx in as_fewer_input_rbf.iter() {
1415 check_spends!(tx, revoked_local_txn[0]);
1418 // Connect a number of additional blocks to ensure we don't forget the HTLC output needs
1420 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
1421 let as_fewer_input_rbf = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1422 for tx in as_fewer_input_rbf.iter() {
1423 check_spends!(tx, revoked_local_txn[0]);
1426 mine_transaction(&nodes[0], &revoked_htlc_timeout_txn[0]);
1427 let as_second_htlc_claim_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1428 assert_eq!(as_second_htlc_claim_tx.len(), 2);
1430 check_spends!(as_second_htlc_claim_tx[0], revoked_htlc_timeout_txn[0]);
1431 check_spends!(as_second_htlc_claim_tx[1], revoked_local_txn[0]);
1433 // Connect blocks to finalize the HTLC resolution with the HTLC-Timeout transaction. In a
1434 // previous iteration of the revoked balance handling this would result in us "forgetting" that
1435 // the revoked HTLC output still needed to be claimed.
1436 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
1437 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1438 // to_self output in B's revoked commitment
1439 claimable_amount_satoshis: 10_000,
1440 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1441 claimable_amount_satoshis: 1_000,
1443 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1445 mine_transaction(&nodes[0], &as_second_htlc_claim_tx[0]);
1446 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1447 // to_self output in B's revoked commitment
1448 claimable_amount_satoshis: 10_000,
1449 }, Balance::ClaimableAwaitingConfirmations {
1450 claimable_amount_satoshis: as_second_htlc_claim_tx[0].output[0].value,
1451 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
1453 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1455 mine_transaction(&nodes[0], &as_second_htlc_claim_tx[1]);
1456 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1457 // to_self output in B's revoked commitment
1458 claimable_amount_satoshis: as_second_htlc_claim_tx[1].output[0].value,
1459 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
1460 }, Balance::ClaimableAwaitingConfirmations {
1461 claimable_amount_satoshis: as_second_htlc_claim_tx[0].output[0].value,
1462 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 2,
1464 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1466 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 2);
1467 test_spendable_output(&nodes[0], &as_second_htlc_claim_tx[0]);
1468 connect_blocks(&nodes[0], 1);
1469 test_spendable_output(&nodes[0], &as_second_htlc_claim_tx[1]);
1471 assert_eq!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances(), Vec::new());
1473 // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
1474 // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
1475 // monitor events or claimable balances.
1476 connect_blocks(&nodes[0], 6);
1477 connect_blocks(&nodes[0], 6);
1478 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1479 assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1483 fn test_revoked_counterparty_aggregated_claims() {
1484 // Tests `get_claimable_balances` for revoked counterparty commitment transactions when
1485 // claiming with an aggregated claim transaction.
1486 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1487 // We broadcast a second-to-latest commitment transaction, without providing the revocation
1488 // secret to the counterparty. However, because we always immediately take the revocation
1489 // secret from the keys_manager, we would panic at broadcast as we're trying to sign a
1490 // transaction which, from the point of view of our keys_manager, is revoked.
1491 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
1492 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1493 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1494 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1496 let (_, _, chan_id, funding_tx) =
1497 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 100_000_000);
1498 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
1499 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
1501 // We create two HTLCs, one which we will give A the preimage to to generate an HTLC-Success
1502 // transaction, and one which we will not, allowing B to claim the HTLC output in an aggregated
1503 // revocation-claim transaction.
1505 let (claimed_payment_preimage, claimed_payment_hash, ..) = route_payment(&nodes[1], &[&nodes[0]], 3_000_000);
1506 let revoked_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 4_000_000).1;
1508 let htlc_cltv_timeout = nodes[1].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
1510 // Cheat by giving A's ChannelMonitor the preimage to the to-be-claimed HTLC so that we have an
1511 // HTLC-claim transaction on the to-be-revoked state.
1512 get_monitor!(nodes[0], chan_id).provide_payment_preimage(&claimed_payment_hash, &claimed_payment_preimage,
1513 &node_cfgs[0].tx_broadcaster, &LowerBoundedFeeEstimator::new(node_cfgs[0].fee_estimator), &nodes[0].logger);
1515 // Now get the latest commitment transaction from A and then update the fee to revoke it
1516 let as_revoked_txn = get_local_commitment_txn!(nodes[0], chan_id);
1518 assert_eq!(as_revoked_txn.len(), 2);
1519 check_spends!(as_revoked_txn[0], funding_tx);
1520 check_spends!(as_revoked_txn[1], as_revoked_txn[0]); // The HTLC-Claim transaction
1522 let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
1523 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
1526 let mut feerate = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
1529 nodes[0].node.timer_tick_occurred();
1530 check_added_monitors!(nodes[0], 1);
1532 let fee_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1533 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &fee_update.update_fee.unwrap());
1534 commitment_signed_dance!(nodes[1], nodes[0], fee_update.commitment_signed, false);
1536 nodes[0].node.claim_funds(claimed_payment_preimage);
1537 expect_payment_claimed!(nodes[0], claimed_payment_hash, 3_000_000);
1538 check_added_monitors!(nodes[0], 1);
1539 let _a_htlc_msgs = get_htlc_update_msgs!(&nodes[0], nodes[1].node.get_our_node_id());
1541 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
1542 claimable_amount_satoshis: 100_000 - 4_000 - 3_000,
1543 }, Balance::MaybeTimeoutClaimableHTLC {
1544 claimable_amount_satoshis: 4_000,
1545 claimable_height: htlc_cltv_timeout,
1546 }, Balance::MaybeTimeoutClaimableHTLC {
1547 claimable_amount_satoshis: 3_000,
1548 claimable_height: htlc_cltv_timeout,
1550 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1552 mine_transaction(&nodes[1], &as_revoked_txn[0]);
1553 check_closed_broadcast!(nodes[1], true);
1554 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
1555 check_added_monitors!(nodes[1], 1);
1557 let mut claim_txn: Vec<_> = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().drain(..).filter(|tx| tx.input.iter().any(|inp| inp.previous_output.txid == as_revoked_txn[0].txid())).collect();
1558 // Currently the revoked commitment outputs are all claimed in one aggregated transaction
1559 assert_eq!(claim_txn.len(), 1);
1560 assert_eq!(claim_txn[0].input.len(), 3);
1561 check_spends!(claim_txn[0], as_revoked_txn[0]);
1563 let to_remote_maturity = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
1565 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1566 // to_remote output in A's revoked commitment
1567 claimable_amount_satoshis: 100_000 - 4_000 - 3_000,
1568 confirmation_height: to_remote_maturity,
1569 }, Balance::CounterpartyRevokedOutputClaimable {
1570 // to_self output in A's revoked commitment
1571 claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1572 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1573 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1574 claimable_amount_satoshis: 4_000,
1575 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1576 claimable_amount_satoshis: 3_000,
1578 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1580 // Confirm A's HTLC-Success tranasction which presumably raced B's claim, causing B to create a
1582 mine_transaction(&nodes[1], &as_revoked_txn[1]);
1583 expect_payment_sent!(nodes[1], claimed_payment_preimage);
1584 let mut claim_txn_2: Vec<_> = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
1585 claim_txn_2.sort_unstable_by_key(|tx| if tx.input.iter().any(|inp| inp.previous_output.txid == as_revoked_txn[0].txid()) { 0 } else { 1 });
1586 // Once B sees the HTLC-Success transaction it splits its claim transaction into two, though in
1587 // theory it could re-aggregate the claims as well.
1588 assert_eq!(claim_txn_2.len(), 2);
1589 assert_eq!(claim_txn_2[0].input.len(), 2);
1590 check_spends!(claim_txn_2[0], as_revoked_txn[0]);
1591 assert_eq!(claim_txn_2[1].input.len(), 1);
1592 check_spends!(claim_txn_2[1], as_revoked_txn[1]);
1594 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1595 // to_remote output in A's revoked commitment
1596 claimable_amount_satoshis: 100_000 - 4_000 - 3_000,
1597 confirmation_height: to_remote_maturity,
1598 }, Balance::CounterpartyRevokedOutputClaimable {
1599 // to_self output in A's revoked commitment
1600 claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1601 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1602 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1603 claimable_amount_satoshis: 4_000,
1604 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1605 // The amount here is a bit of a misnomer, really its been reduced by the HTLC
1606 // transaction fee, but the claimable amount is always a bit of an overshoot for HTLCs
1607 // anyway, so its not a big change.
1608 claimable_amount_satoshis: 3_000,
1610 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1612 connect_blocks(&nodes[1], 5);
1613 test_spendable_output(&nodes[1], &as_revoked_txn[0]);
1615 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1616 // to_self output in A's revoked commitment
1617 claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1618 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1619 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1620 claimable_amount_satoshis: 4_000,
1621 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1622 // The amount here is a bit of a misnomer, really its been reduced by the HTLC
1623 // transaction fee, but the claimable amount is always a bit of an overshoot for HTLCs
1624 // anyway, so its not a big change.
1625 claimable_amount_satoshis: 3_000,
1627 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1629 mine_transaction(&nodes[1], &claim_txn_2[1]);
1630 let htlc_2_claim_maturity = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
1632 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1633 // to_self output in A's revoked commitment
1634 claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1635 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1636 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1637 claimable_amount_satoshis: 4_000,
1638 }, Balance::ClaimableAwaitingConfirmations { // HTLC 2
1639 claimable_amount_satoshis: claim_txn_2[1].output[0].value,
1640 confirmation_height: htlc_2_claim_maturity,
1642 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1644 connect_blocks(&nodes[1], 5);
1645 test_spendable_output(&nodes[1], &claim_txn_2[1]);
1647 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1648 // to_self output in A's revoked commitment
1649 claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1650 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1651 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1652 claimable_amount_satoshis: 4_000,
1654 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1656 mine_transaction(&nodes[1], &claim_txn_2[0]);
1657 let rest_claim_maturity = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
1659 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
1660 claimable_amount_satoshis: claim_txn_2[0].output[0].value,
1661 confirmation_height: rest_claim_maturity,
1663 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
1665 assert!(nodes[1].node.get_and_clear_pending_events().is_empty()); // We shouldn't fail the payment until we spend the output
1667 connect_blocks(&nodes[1], 5);
1668 expect_payment_failed!(nodes[1], revoked_payment_hash, false);
1669 test_spendable_output(&nodes[1], &claim_txn_2[0]);
1670 assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1672 // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
1673 // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
1674 // monitor events or claimable balances.
1675 connect_blocks(&nodes[1], 6);
1676 connect_blocks(&nodes[1], 6);
1677 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1678 assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1683 fn test_yield_anchors_events() {
1684 // Tests that two parties supporting anchor outputs can open a channel, route payments over
1685 // it, and finalize its resolution uncooperatively. Once the HTLCs are locked in, one side will
1686 // force close once the HTLCs expire. The force close should stem from an event emitted by LDK,
1687 // allowing the consumer to provide additional fees to the commitment transaction to be
1688 // broadcast. Once the commitment transaction confirms, events for the HTLC resolution should be
1689 // emitted by LDK, such that the consumer can attach fees to the zero fee HTLC transactions.
1690 let secp = Secp256k1::new();
1691 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1692 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1693 let mut anchors_config = UserConfig::default();
1694 anchors_config.channel_handshake_config.announced_channel = true;
1695 anchors_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
1696 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(anchors_config), Some(anchors_config)]);
1697 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1699 let chan_id = create_announced_chan_between_nodes_with_value(
1700 &nodes, 0, 1, 1_000_000, 500_000_000
1702 route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
1703 let (payment_preimage, payment_hash, _) = route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1705 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
1707 connect_blocks(&nodes[0], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1);
1708 check_closed_broadcast!(&nodes[0], true);
1709 assert!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().is_empty());
1711 get_monitor!(nodes[0], chan_id).provide_payment_preimage(
1712 &payment_hash, &payment_preimage, &node_cfgs[0].tx_broadcaster,
1713 &LowerBoundedFeeEstimator::new(node_cfgs[0].fee_estimator), &nodes[0].logger
1716 let mut holder_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
1717 assert_eq!(holder_events.len(), 1);
1718 let (commitment_tx, anchor_tx) = match holder_events.pop().unwrap() {
1719 Event::BumpTransaction(BumpTransactionEvent::ChannelClose { commitment_tx, anchor_descriptor, .. }) => {
1720 assert_eq!(commitment_tx.input.len(), 1);
1721 assert_eq!(commitment_tx.output.len(), 6);
1722 let mut anchor_tx = Transaction {
1724 lock_time: PackedLockTime::ZERO,
1726 TxIn { previous_output: anchor_descriptor.outpoint, ..Default::default() },
1727 TxIn { ..Default::default() },
1729 output: vec![TxOut {
1730 value: Amount::ONE_BTC.to_sat(),
1731 script_pubkey: Script::new_op_return(&[]),
1734 let signer = nodes[0].keys_manager.derive_channel_keys(
1735 anchor_descriptor.channel_value_satoshis, &anchor_descriptor.channel_keys_id,
1737 let funding_sig = signer.sign_holder_anchor_input(&mut anchor_tx, 0, &secp).unwrap();
1738 anchor_tx.input[0].witness = chan_utils::build_anchor_input_witness(
1739 &signer.pubkeys().funding_pubkey, &funding_sig
1741 (commitment_tx, anchor_tx)
1743 _ => panic!("Unexpected event"),
1746 mine_transactions(&nodes[0], &[&commitment_tx, &anchor_tx]);
1747 check_added_monitors!(nodes[0], 1);
1749 let mut holder_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
1750 // Certain block `ConnectStyle`s cause an extra `ChannelClose` event to be emitted since the
1751 // best block is being updated prior to the confirmed transactions.
1752 match *nodes[0].connect_style.borrow() {
1753 ConnectStyle::BestBlockFirst|ConnectStyle::BestBlockFirstReorgsOnlyTip|ConnectStyle::BestBlockFirstSkippingBlocks => {
1754 assert_eq!(holder_events.len(), 3);
1755 if let Event::BumpTransaction(BumpTransactionEvent::ChannelClose { .. }) = holder_events.remove(0) {}
1756 else { panic!("unexpected event"); }
1759 _ => assert_eq!(holder_events.len(), 2),
1761 let mut htlc_txs = Vec::with_capacity(2);
1762 for event in holder_events {
1764 Event::BumpTransaction(BumpTransactionEvent::HTLCResolution { htlc_descriptors, .. }) => {
1765 assert_eq!(htlc_descriptors.len(), 1);
1766 let htlc_descriptor = &htlc_descriptors[0];
1767 let signer = nodes[0].keys_manager.derive_channel_keys(
1768 htlc_descriptor.channel_value_satoshis, &htlc_descriptor.channel_keys_id
1770 let per_commitment_point = signer.get_per_commitment_point(htlc_descriptor.per_commitment_number, &secp);
1771 let mut htlc_tx = Transaction {
1773 lock_time: if htlc_descriptor.htlc.offered {
1774 PackedLockTime(htlc_descriptor.htlc.cltv_expiry)
1776 PackedLockTime::ZERO
1779 htlc_descriptor.unsigned_tx_input(), // HTLC input
1780 TxIn { ..Default::default() } // Fee input
1783 htlc_descriptor.tx_output(&per_commitment_point, &secp), // HTLC output
1784 TxOut { // Fee input change
1785 value: Amount::ONE_BTC.to_sat(),
1786 script_pubkey: Script::new_op_return(&[]),
1790 let our_sig = signer.sign_holder_htlc_transaction(&mut htlc_tx, 0, htlc_descriptor, &secp).unwrap();
1791 let witness_script = htlc_descriptor.witness_script(&per_commitment_point, &secp);
1792 htlc_tx.input[0].witness = htlc_descriptor.tx_input_witness(&our_sig, &witness_script);
1793 htlc_txs.push(htlc_tx);
1795 _ => panic!("Unexpected event"),
1799 mine_transactions(&nodes[0], &[&htlc_txs[0], &htlc_txs[1]]);
1800 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
1802 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1804 connect_blocks(&nodes[0], BREAKDOWN_TIMEOUT as u32);
1806 let holder_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
1807 assert_eq!(holder_events.len(), 3);
1808 for event in holder_events {
1810 Event::SpendableOutputs { .. } => {},
1811 _ => panic!("Unexpected event"),
1815 // Clear the remaining events as they're not relevant to what we're testing.
1816 nodes[0].node.get_and_clear_pending_events();