Merge pull request #2101 from TheBlueMatt/2023-03-one-less-sig
[rust-lightning] / lightning / src / ln / monitor_tests.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
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
8 // licenses.
9
10 //! Further functional tests which test blockchain reorganizations.
11
12 #[cfg(anchors)]
13 use crate::chain::keysinterface::{ChannelSigner, EcdsaChannelSigner};
14 #[cfg(anchors)]
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 #[cfg(anchors)]
20 use crate::events::bump_transaction::BumpTransactionEvent;
21 use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination};
22 use crate::ln::channel;
23 #[cfg(anchors)]
24 use crate::ln::chan_utils;
25 #[cfg(anchors)]
26 use crate::ln::channelmanager::ChannelManager;
27 use crate::ln::channelmanager::{BREAKDOWN_TIMEOUT, PaymentId};
28 use crate::ln::msgs::ChannelMessageHandler;
29 #[cfg(anchors)]
30 use crate::util::config::UserConfig;
31 #[cfg(anchors)]
32 use crate::util::crypto::sign;
33 #[cfg(anchors)]
34 use crate::util::ser::Writeable;
35 #[cfg(anchors)]
36 use crate::util::test_utils;
37
38 #[cfg(anchors)]
39 use bitcoin::blockdata::transaction::EcdsaSighashType;
40 use bitcoin::blockdata::script::Builder;
41 use bitcoin::blockdata::opcodes;
42 use bitcoin::secp256k1::Secp256k1;
43 #[cfg(anchors)]
44 use bitcoin::secp256k1::SecretKey;
45 #[cfg(anchors)]
46 use bitcoin::{Amount, PublicKey, Script, TxIn, TxOut, PackedLockTime, Witness};
47 use bitcoin::Transaction;
48 #[cfg(anchors)]
49 use bitcoin::util::sighash::SighashCache;
50
51 use crate::prelude::*;
52
53 use crate::ln::functional_test_utils::*;
54
55 #[test]
56 fn chanmon_fail_from_stale_commitment() {
57         // If we forward an HTLC to our counterparty, but we force-closed the channel before our
58         // counterparty provides us an updated commitment transaction, we'll end up with a commitment
59         // transaction that does not contain the HTLC which we attempted to forward. In this case, we
60         // need to wait `ANTI_REORG_DELAY` blocks and then fail back the HTLC as there is no way for us
61         // to learn the preimage and the confirmed commitment transaction paid us the value of the
62         // HTLC.
63         //
64         // However, previously, we did not do this, ignoring the HTLC entirely.
65         //
66         // This could lead to channel closure if the sender we received the HTLC from decides to go on
67         // chain to get their HTLC back before it times out.
68         //
69         // Here, we check exactly this case, forwarding a payment from A, through B, to C, before B
70         // broadcasts its latest commitment transaction, which should result in it eventually failing
71         // the HTLC back off-chain to A.
72         let chanmon_cfgs = create_chanmon_cfgs(3);
73         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
74         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
75         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
76
77         create_announced_chan_between_nodes(&nodes, 0, 1);
78         let (update_a, _, chan_id_2, _) = create_announced_chan_between_nodes(&nodes, 1, 2);
79
80         let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 1_000_000);
81         nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap();
82         check_added_monitors!(nodes[0], 1);
83
84         let bs_txn = get_local_commitment_txn!(nodes[1], chan_id_2);
85
86         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
87         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
88         commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
89
90         expect_pending_htlcs_forwardable!(nodes[1]);
91         get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
92         check_added_monitors!(nodes[1], 1);
93
94         // Don't bother delivering the new HTLC add/commits, instead confirming the pre-HTLC commitment
95         // transaction for nodes[1].
96         mine_transaction(&nodes[1], &bs_txn[0]);
97         check_added_monitors!(nodes[1], 1);
98         check_closed_broadcast!(nodes[1], true);
99         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
100         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
101
102         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
103         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 }]);
104         check_added_monitors!(nodes[1], 1);
105         let fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
106
107         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates.update_fail_htlcs[0]);
108         commitment_signed_dance!(nodes[0], nodes[1], fail_updates.commitment_signed, true, true);
109         expect_payment_failed_with_update!(nodes[0], payment_hash, false, update_a.contents.short_channel_id, true);
110 }
111
112 fn test_spendable_output<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, spendable_tx: &Transaction) {
113         let mut spendable = node.chain_monitor.chain_monitor.get_and_clear_pending_events();
114         assert_eq!(spendable.len(), 1);
115         if let Event::SpendableOutputs { outputs } = spendable.pop().unwrap() {
116                 assert_eq!(outputs.len(), 1);
117                 let spend_tx = node.keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
118                         Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
119                 check_spends!(spend_tx, spendable_tx);
120         } else { panic!(); }
121 }
122
123 #[test]
124 fn revoked_output_htlc_resolution_timing() {
125         // Tests that HTLCs which were present in a broadcasted remote revoked commitment transaction
126         // are resolved only after a spend of the HTLC output reaches six confirmations. Preivously
127         // they would resolve after the revoked commitment transaction itself reaches six
128         // confirmations.
129         let chanmon_cfgs = create_chanmon_cfgs(2);
130         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
131         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
132         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
133
134         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000);
135
136         let payment_hash_1 = route_payment(&nodes[1], &[&nodes[0]], 1_000_000).1;
137
138         // Get a commitment transaction which contains the HTLC we care about, but which we'll revoke
139         // before forwarding.
140         let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan.2);
141         assert_eq!(revoked_local_txn.len(), 1);
142
143         // Route a dust payment to revoke the above commitment transaction
144         route_payment(&nodes[0], &[&nodes[1]], 1_000);
145
146         // Confirm the revoked commitment transaction, closing the channel.
147         mine_transaction(&nodes[1], &revoked_local_txn[0]);
148         check_added_monitors!(nodes[1], 1);
149         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
150         check_closed_broadcast!(nodes[1], true);
151
152         let bs_spend_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
153         assert_eq!(bs_spend_txn.len(), 1);
154         check_spends!(bs_spend_txn[0], revoked_local_txn[0]);
155
156         // After the commitment transaction confirms, we should still wait on the HTLC spend
157         // transaction to confirm before resolving the HTLC.
158         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
159         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
160         assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
161
162         // Spend the HTLC output, generating a HTLC failure event after ANTI_REORG_DELAY confirmations.
163         mine_transaction(&nodes[1], &bs_spend_txn[0]);
164         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
165         assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
166
167         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
168         expect_payment_failed!(nodes[1], payment_hash_1, false);
169 }
170
171 #[test]
172 fn chanmon_claim_value_coop_close() {
173         // Tests `get_claimable_balances` returns the correct values across a simple cooperative claim.
174         // Specifically, this tests that the channel non-HTLC balances show up in
175         // `get_claimable_balances` until the cooperative claims have confirmed and generated a
176         // `SpendableOutputs` event, and no longer.
177         let chanmon_cfgs = create_chanmon_cfgs(2);
178         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
179         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
180         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
181
182         let (_, _, chan_id, funding_tx) =
183                 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 1_000_000);
184         let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
185         assert_eq!(funding_outpoint.to_channel_id(), chan_id);
186
187         let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
188         let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
189
190         assert_eq!(vec![Balance::ClaimableOnChannelClose {
191                         claimable_amount_satoshis: 1_000_000 - 1_000 - chan_feerate * channel::commitment_tx_base_weight(opt_anchors) / 1000
192                 }],
193                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
194         assert_eq!(vec![Balance::ClaimableOnChannelClose { claimable_amount_satoshis: 1_000, }],
195                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
196
197         nodes[0].node.close_channel(&chan_id, &nodes[1].node.get_our_node_id()).unwrap();
198         let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
199         nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
200         let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
201         nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
202
203         let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
204         nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
205         let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
206         nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
207         let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
208         nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
209         let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
210         assert!(node_1_none.is_none());
211
212         let shutdown_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
213         assert_eq!(shutdown_tx, nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0));
214         assert_eq!(shutdown_tx.len(), 1);
215
216         mine_transaction(&nodes[0], &shutdown_tx[0]);
217         mine_transaction(&nodes[1], &shutdown_tx[0]);
218
219         assert!(nodes[0].node.list_channels().is_empty());
220         assert!(nodes[1].node.list_channels().is_empty());
221
222         assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
223         assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
224
225         assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
226                         claimable_amount_satoshis: 1_000_000 - 1_000 - chan_feerate * channel::commitment_tx_base_weight(opt_anchors) / 1000,
227                         confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
228                 }],
229                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
230         assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
231                         claimable_amount_satoshis: 1000,
232                         confirmation_height: nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1,
233                 }],
234                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
235
236         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
237         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
238
239         assert_eq!(Vec::<Balance>::new(),
240                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
241         assert_eq!(Vec::<Balance>::new(),
242                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
243
244         test_spendable_output(&nodes[0], &shutdown_tx[0]);
245         test_spendable_output(&nodes[1], &shutdown_tx[0]);
246
247         check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure);
248         check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure);
249 }
250
251 fn sorted_vec<T: Ord>(mut v: Vec<T>) -> Vec<T> {
252         v.sort_unstable();
253         v
254 }
255
256 /// Asserts that `a` and `b` are close, but maybe off by up to 5.
257 /// This is useful when checking fees and weights on transactions as things may vary by a few based
258 /// on signature size and signature size estimation being non-exact.
259 fn fuzzy_assert_eq<V: core::convert::TryInto<u64>>(a: V, b: V) {
260         let a_u64 = a.try_into().map_err(|_| ()).unwrap();
261         let b_u64 = b.try_into().map_err(|_| ()).unwrap();
262         eprintln!("Checking {} and {} for fuzzy equality", a_u64, b_u64);
263         assert!(a_u64 >= b_u64 - 5);
264         assert!(b_u64 >= a_u64 - 5);
265 }
266
267 fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
268         // Tests `get_claimable_balances` with an HTLC across a force-close.
269         // We build a channel with an HTLC pending, then force close the channel and check that the
270         // `get_claimable_balances` return value is correct as transactions confirm on-chain.
271         let mut chanmon_cfgs = create_chanmon_cfgs(2);
272         if prev_commitment_tx {
273                 // We broadcast a second-to-latest commitment transaction, without providing the revocation
274                 // secret to the counterparty. However, because we always immediately take the revocation
275                 // secret from the keys_manager, we would panic at broadcast as we're trying to sign a
276                 // transaction which, from the point of view of our keys_manager, is revoked.
277                 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
278         }
279         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
280         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
281         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
282
283         let (_, _, chan_id, funding_tx) =
284                 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 1_000_000);
285         let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
286         assert_eq!(funding_outpoint.to_channel_id(), chan_id);
287
288         // This HTLC is immediately claimed, giving node B the preimage
289         let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 3_000_000);
290         // This HTLC is allowed to time out, letting A claim it. However, in order to test claimable
291         // balances more fully we also give B the preimage for this HTLC.
292         let (timeout_payment_preimage, timeout_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 4_000_000);
293         // This HTLC will be dust, and not be claimable at all:
294         let (dust_payment_preimage, dust_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 3_000);
295
296         let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
297
298         let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
299         let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
300
301         let remote_txn = get_local_commitment_txn!(nodes[1], chan_id);
302         // Before B receives the payment preimage, it only suggests the push_msat value of 1_000 sats
303         // as claimable. A lists both its to-self balance and the (possibly-claimable) HTLCs.
304         assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
305                         claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
306                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
307                 }, Balance::MaybeTimeoutClaimableHTLC {
308                         claimable_amount_satoshis: 3_000,
309                         claimable_height: htlc_cltv_timeout,
310                 }, Balance::MaybeTimeoutClaimableHTLC {
311                         claimable_amount_satoshis: 4_000,
312                         claimable_height: htlc_cltv_timeout,
313                 }]),
314                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
315         assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
316                         claimable_amount_satoshis: 1_000,
317                 }, Balance::MaybePreimageClaimableHTLC {
318                         claimable_amount_satoshis: 3_000,
319                         expiry_height: htlc_cltv_timeout,
320                 }, Balance::MaybePreimageClaimableHTLC {
321                         claimable_amount_satoshis: 4_000,
322                         expiry_height: htlc_cltv_timeout,
323                 }]),
324                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
325
326         nodes[1].node.claim_funds(payment_preimage);
327         check_added_monitors!(nodes[1], 1);
328         expect_payment_claimed!(nodes[1], payment_hash, 3_000_000);
329
330         let b_htlc_msgs = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
331         // We claim the dust payment here as well, but it won't impact our claimable balances as its
332         // dust and thus doesn't appear on chain at all.
333         nodes[1].node.claim_funds(dust_payment_preimage);
334         check_added_monitors!(nodes[1], 1);
335         expect_payment_claimed!(nodes[1], dust_payment_hash, 3_000);
336
337         nodes[1].node.claim_funds(timeout_payment_preimage);
338         check_added_monitors!(nodes[1], 1);
339         expect_payment_claimed!(nodes[1], timeout_payment_hash, 4_000_000);
340
341         if prev_commitment_tx {
342                 // To build a previous commitment transaction, deliver one round of commitment messages.
343                 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &b_htlc_msgs.update_fulfill_htlcs[0]);
344                 expect_payment_sent_without_paths!(nodes[0], payment_preimage);
345                 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &b_htlc_msgs.commitment_signed);
346                 check_added_monitors!(nodes[0], 1);
347                 let (as_raa, as_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
348                 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
349                 let _htlc_updates = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
350                 check_added_monitors!(nodes[1], 1);
351                 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs);
352                 let _bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
353                 check_added_monitors!(nodes[1], 1);
354         }
355
356         // Once B has received the payment preimage, it includes the value of the HTLC in its
357         // "claimable if you were to close the channel" balance.
358         let mut a_expected_balances = vec![Balance::ClaimableOnChannelClose {
359                         claimable_amount_satoshis: 1_000_000 - // Channel funding value in satoshis
360                                 4_000 - // The to-be-failed HTLC value in satoshis
361                                 3_000 - // The claimed HTLC value in satoshis
362                                 1_000 - // The push_msat value in satoshis
363                                 3 - // The dust HTLC value in satoshis
364                                 // The commitment transaction fee with two HTLC outputs:
365                                 chan_feerate * (channel::commitment_tx_base_weight(opt_anchors) +
366                                                                 if prev_commitment_tx { 1 } else { 2 } *
367                                                                 channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
368                 }, Balance::MaybeTimeoutClaimableHTLC {
369                         claimable_amount_satoshis: 4_000,
370                         claimable_height: htlc_cltv_timeout,
371                 }];
372         if !prev_commitment_tx {
373                 a_expected_balances.push(Balance::MaybeTimeoutClaimableHTLC {
374                         claimable_amount_satoshis: 3_000,
375                         claimable_height: htlc_cltv_timeout,
376                 });
377         }
378         assert_eq!(sorted_vec(a_expected_balances),
379                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
380         assert_eq!(vec![Balance::ClaimableOnChannelClose {
381                         claimable_amount_satoshis: 1_000 + 3_000 + 4_000,
382                 }],
383                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
384
385         // Broadcast the closing transaction (which has both pending HTLCs in it) and get B's
386         // broadcasted HTLC claim transaction with preimage.
387         let node_b_commitment_claimable = nodes[1].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
388         mine_transaction(&nodes[0], &remote_txn[0]);
389         mine_transaction(&nodes[1], &remote_txn[0]);
390
391         let b_broadcast_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
392         assert_eq!(b_broadcast_txn.len(), 2);
393         // b_broadcast_txn should spend the HTLCs output of the commitment tx for 3_000 and 4_000 sats
394         check_spends!(b_broadcast_txn[0], remote_txn[0]);
395         check_spends!(b_broadcast_txn[1], remote_txn[0]);
396         assert_eq!(b_broadcast_txn[0].input.len(), 1);
397         assert_eq!(b_broadcast_txn[1].input.len(), 1);
398         assert_eq!(remote_txn[0].output[b_broadcast_txn[0].input[0].previous_output.vout as usize].value, 3_000);
399         assert_eq!(remote_txn[0].output[b_broadcast_txn[1].input[0].previous_output.vout as usize].value, 4_000);
400
401         assert!(nodes[0].node.list_channels().is_empty());
402         check_closed_broadcast!(nodes[0], true);
403         check_added_monitors!(nodes[0], 1);
404         check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
405         assert!(nodes[1].node.list_channels().is_empty());
406         check_closed_broadcast!(nodes[1], true);
407         check_added_monitors!(nodes[1], 1);
408         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
409         assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
410         assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
411
412         // Once the commitment transaction confirms, we will wait until ANTI_REORG_DELAY until we
413         // generate any `SpendableOutputs` events. Thus, the same balances will still be listed
414         // available in `get_claimable_balances`. However, both will swap from `ClaimableOnClose` to
415         // other Balance variants, as close has already happened.
416         assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
417         assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
418
419         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
420                         claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
421                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
422                         confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
423                 }, Balance::MaybeTimeoutClaimableHTLC {
424                         claimable_amount_satoshis: 3_000,
425                         claimable_height: htlc_cltv_timeout,
426                 }, Balance::MaybeTimeoutClaimableHTLC {
427                         claimable_amount_satoshis: 4_000,
428                         claimable_height: htlc_cltv_timeout,
429                 }]),
430                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
431         // The main non-HTLC balance is just awaiting confirmations, but the claimable height is the
432         // CSV delay, not ANTI_REORG_DELAY.
433         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
434                         claimable_amount_satoshis: 1_000,
435                         confirmation_height: node_b_commitment_claimable,
436                 },
437                 // Both HTLC balances are "contentious" as our counterparty could claim them if we wait too
438                 // long.
439                 Balance::ContentiousClaimable {
440                         claimable_amount_satoshis: 3_000,
441                         timeout_height: htlc_cltv_timeout,
442                 }, Balance::ContentiousClaimable {
443                         claimable_amount_satoshis: 4_000,
444                         timeout_height: htlc_cltv_timeout,
445                 }]),
446                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
447
448         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
449         expect_payment_failed!(nodes[0], dust_payment_hash, false);
450         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
451
452         // After ANTI_REORG_DELAY, A will consider its balance fully spendable and generate a
453         // `SpendableOutputs` event. However, B still has to wait for the CSV delay.
454         assert_eq!(sorted_vec(vec![Balance::MaybeTimeoutClaimableHTLC {
455                         claimable_amount_satoshis: 3_000,
456                         claimable_height: htlc_cltv_timeout,
457                 }, Balance::MaybeTimeoutClaimableHTLC {
458                         claimable_amount_satoshis: 4_000,
459                         claimable_height: htlc_cltv_timeout,
460                 }]),
461                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
462         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
463                         claimable_amount_satoshis: 1_000,
464                         confirmation_height: node_b_commitment_claimable,
465                 }, Balance::ContentiousClaimable {
466                         claimable_amount_satoshis: 3_000,
467                         timeout_height: htlc_cltv_timeout,
468                 }, Balance::ContentiousClaimable {
469                         claimable_amount_satoshis: 4_000,
470                         timeout_height: htlc_cltv_timeout,
471                 }]),
472                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
473
474         test_spendable_output(&nodes[0], &remote_txn[0]);
475         assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
476
477         // After broadcasting the HTLC claim transaction, node A will still consider the HTLC
478         // possibly-claimable up to ANTI_REORG_DELAY, at which point it will drop it.
479         mine_transaction(&nodes[0], &b_broadcast_txn[0]);
480         if prev_commitment_tx {
481                 expect_payment_path_successful!(nodes[0]);
482         } else {
483                 expect_payment_sent!(nodes[0], payment_preimage);
484         }
485         assert_eq!(sorted_vec(vec![Balance::MaybeTimeoutClaimableHTLC {
486                         claimable_amount_satoshis: 3_000,
487                         claimable_height: htlc_cltv_timeout,
488                 }, Balance::MaybeTimeoutClaimableHTLC {
489                         claimable_amount_satoshis: 4_000,
490                         claimable_height: htlc_cltv_timeout,
491                 }]),
492                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
493         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
494         assert_eq!(vec![Balance::MaybeTimeoutClaimableHTLC {
495                         claimable_amount_satoshis: 4_000,
496                         claimable_height: htlc_cltv_timeout,
497                 }],
498                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
499
500         // When the HTLC timeout output is spendable in the next block, A should broadcast it
501         connect_blocks(&nodes[0], htlc_cltv_timeout - nodes[0].best_block_info().1 - 1);
502         let a_broadcast_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
503         assert_eq!(a_broadcast_txn.len(), 2);
504         assert_eq!(a_broadcast_txn[0].input.len(), 1);
505         check_spends!(a_broadcast_txn[0], remote_txn[0]);
506         assert_eq!(a_broadcast_txn[1].input.len(), 1);
507         check_spends!(a_broadcast_txn[1], remote_txn[0]);
508         assert_ne!(a_broadcast_txn[0].input[0].previous_output.vout,
509                    a_broadcast_txn[1].input[0].previous_output.vout);
510         // a_broadcast_txn [0] and [1] should spend the HTLC outputs of the commitment tx
511         assert_eq!(remote_txn[0].output[a_broadcast_txn[0].input[0].previous_output.vout as usize].value, 3_000);
512         assert_eq!(remote_txn[0].output[a_broadcast_txn[1].input[0].previous_output.vout as usize].value, 4_000);
513
514         // Once the HTLC-Timeout transaction confirms, A will no longer consider the HTLC
515         // "MaybeClaimable", but instead move it to "AwaitingConfirmations".
516         mine_transaction(&nodes[0], &a_broadcast_txn[1]);
517         assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
518         assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
519                         claimable_amount_satoshis: 4_000,
520                         confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
521                 }],
522                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
523         // After ANTI_REORG_DELAY, A will generate a SpendableOutputs event and drop the claimable
524         // balance entry.
525         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
526         assert_eq!(Vec::<Balance>::new(),
527                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
528         expect_payment_failed!(nodes[0], timeout_payment_hash, false);
529
530         test_spendable_output(&nodes[0], &a_broadcast_txn[1]);
531
532         // Node B will no longer consider the HTLC "contentious" after the HTLC claim transaction
533         // confirms, and consider it simply "awaiting confirmations". Note that it has to wait for the
534         // standard revocable transaction CSV delay before receiving a `SpendableOutputs`.
535         let node_b_htlc_claimable = nodes[1].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
536         mine_transaction(&nodes[1], &b_broadcast_txn[0]);
537
538         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
539                         claimable_amount_satoshis: 1_000,
540                         confirmation_height: node_b_commitment_claimable,
541                 }, 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,
547                 }]),
548                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
549
550         // After reaching the commitment output CSV, we'll get a SpendableOutputs event for it and have
551         // only the HTLCs claimable on node B.
552         connect_blocks(&nodes[1], node_b_commitment_claimable - nodes[1].best_block_info().1);
553         test_spendable_output(&nodes[1], &remote_txn[0]);
554
555         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
556                         claimable_amount_satoshis: 3_000,
557                         confirmation_height: node_b_htlc_claimable,
558                 }, Balance::ContentiousClaimable {
559                         claimable_amount_satoshis: 4_000,
560                         timeout_height: htlc_cltv_timeout,
561                 }]),
562                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
563
564         // After reaching the claimed HTLC output CSV, we'll get a SpendableOutptus event for it and
565         // have only one HTLC output left spendable.
566         connect_blocks(&nodes[1], node_b_htlc_claimable - nodes[1].best_block_info().1);
567         test_spendable_output(&nodes[1], &b_broadcast_txn[0]);
568
569         assert_eq!(vec![Balance::ContentiousClaimable {
570                         claimable_amount_satoshis: 4_000,
571                         timeout_height: htlc_cltv_timeout,
572                 }],
573                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
574
575         // Finally, mine the HTLC timeout transaction that A broadcasted (even though B should be able
576         // to claim this HTLC with the preimage it knows!). It will remain listed as a claimable HTLC
577         // until ANTI_REORG_DELAY confirmations on the spend.
578         mine_transaction(&nodes[1], &a_broadcast_txn[1]);
579         assert_eq!(vec![Balance::ContentiousClaimable {
580                         claimable_amount_satoshis: 4_000,
581                         timeout_height: htlc_cltv_timeout,
582                 }],
583                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
584         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
585         assert_eq!(Vec::<Balance>::new(),
586                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
587
588         // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
589         // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
590         // monitor events or claimable balances.
591         for node in nodes.iter() {
592                 connect_blocks(node, 6);
593                 connect_blocks(node, 6);
594                 assert!(node.chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
595                 assert!(node.chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
596         }
597 }
598
599 #[test]
600 fn test_claim_value_force_close() {
601         do_test_claim_value_force_close(true);
602         do_test_claim_value_force_close(false);
603 }
604
605 #[test]
606 fn test_balances_on_local_commitment_htlcs() {
607         // Previously, when handling the broadcast of a local commitment transactions (with associated
608         // CSV delays prior to spendability), we incorrectly handled the CSV delays on HTLC
609         // transactions. This caused us to miss spendable outputs for HTLCs which were awaiting a CSV
610         // delay prior to spendability.
611         //
612         // Further, because of this, we could hit an assertion as `get_claimable_balances` asserted
613         // that HTLCs were resolved after the funding spend was resolved, which was not true if the
614         // HTLC did not have a CSV delay attached (due to the above bug or due to it being an HTLC
615         // claim by our counterparty).
616         let chanmon_cfgs = create_chanmon_cfgs(2);
617         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
618         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
619         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
620
621         // Create a single channel with two pending HTLCs from nodes[0] to nodes[1], one which nodes[1]
622         // knows the preimage for, one which it does not.
623         let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
624         let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
625
626         let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 10_000_000);
627         let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
628         nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap();
629         check_added_monitors!(nodes[0], 1);
630
631         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
632         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
633         commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
634
635         expect_pending_htlcs_forwardable!(nodes[1]);
636         expect_payment_claimable!(nodes[1], payment_hash, payment_secret, 10_000_000);
637
638         let (route_2, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 20_000_000);
639         nodes[0].node.send_payment(&route_2, payment_hash_2, &Some(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
640         check_added_monitors!(nodes[0], 1);
641
642         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
643         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
644         commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
645
646         expect_pending_htlcs_forwardable!(nodes[1]);
647         expect_payment_claimable!(nodes[1], payment_hash_2, payment_secret_2, 20_000_000);
648         nodes[1].node.claim_funds(payment_preimage_2);
649         get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
650         check_added_monitors!(nodes[1], 1);
651         expect_payment_claimed!(nodes[1], payment_hash_2, 20_000_000);
652
653         let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
654         let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
655
656         // Get nodes[0]'s commitment transaction and HTLC-Timeout transactions
657         let as_txn = get_local_commitment_txn!(nodes[0], chan_id);
658         assert_eq!(as_txn.len(), 3);
659         check_spends!(as_txn[1], as_txn[0]);
660         check_spends!(as_txn[2], as_txn[0]);
661         check_spends!(as_txn[0], funding_tx);
662
663         // First confirm the commitment transaction on nodes[0], which should leave us with three
664         // claimable balances.
665         let node_a_commitment_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
666         mine_transaction(&nodes[0], &as_txn[0]);
667         check_added_monitors!(nodes[0], 1);
668         check_closed_broadcast!(nodes[0], true);
669         check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
670
671         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
672                         claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
673                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
674                         confirmation_height: node_a_commitment_claimable,
675                 }, Balance::MaybeTimeoutClaimableHTLC {
676                         claimable_amount_satoshis: 10_000,
677                         claimable_height: htlc_cltv_timeout,
678                 }, Balance::MaybeTimeoutClaimableHTLC {
679                         claimable_amount_satoshis: 20_000,
680                         claimable_height: htlc_cltv_timeout,
681                 }]),
682                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
683
684         // Get nodes[1]'s HTLC claim tx for the second HTLC
685         mine_transaction(&nodes[1], &as_txn[0]);
686         check_added_monitors!(nodes[1], 1);
687         check_closed_broadcast!(nodes[1], true);
688         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
689         let bs_htlc_claim_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
690         assert_eq!(bs_htlc_claim_txn.len(), 1);
691         check_spends!(bs_htlc_claim_txn[0], as_txn[0]);
692
693         // Connect blocks until the HTLCs expire, allowing us to (validly) broadcast the HTLC-Timeout
694         // transaction.
695         connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1);
696         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
697                         claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
698                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
699                         confirmation_height: node_a_commitment_claimable,
700                 }, Balance::MaybeTimeoutClaimableHTLC {
701                         claimable_amount_satoshis: 10_000,
702                         claimable_height: htlc_cltv_timeout,
703                 }, Balance::MaybeTimeoutClaimableHTLC {
704                         claimable_amount_satoshis: 20_000,
705                         claimable_height: htlc_cltv_timeout,
706                 }]),
707                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
708         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
709
710         // Now confirm nodes[0]'s HTLC-Timeout transaction, which changes the claimable balance to an
711         // "awaiting confirmations" one.
712         let node_a_htlc_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
713         mine_transaction(&nodes[0], &as_txn[1]);
714         // Note that prior to the fix in the commit which introduced this test, this (and the next
715         // balance) check failed. With this check removed, the code panicked in the `connect_blocks`
716         // call, as described, two hunks down.
717         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
718                         claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
719                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
720                         confirmation_height: node_a_commitment_claimable,
721                 }, Balance::ClaimableAwaitingConfirmations {
722                         claimable_amount_satoshis: 10_000,
723                         confirmation_height: node_a_htlc_claimable,
724                 }, Balance::MaybeTimeoutClaimableHTLC {
725                         claimable_amount_satoshis: 20_000,
726                         claimable_height: htlc_cltv_timeout,
727                 }]),
728                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
729
730         // Now confirm nodes[1]'s HTLC claim, giving nodes[0] the preimage. Note that the "maybe
731         // claimable" balance remains until we see ANTI_REORG_DELAY blocks.
732         mine_transaction(&nodes[0], &bs_htlc_claim_txn[0]);
733         expect_payment_sent!(nodes[0], payment_preimage_2);
734         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
735                         claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
736                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
737                         confirmation_height: node_a_commitment_claimable,
738                 }, Balance::ClaimableAwaitingConfirmations {
739                         claimable_amount_satoshis: 10_000,
740                         confirmation_height: node_a_htlc_claimable,
741                 }, Balance::MaybeTimeoutClaimableHTLC {
742                         claimable_amount_satoshis: 20_000,
743                         claimable_height: htlc_cltv_timeout,
744                 }]),
745                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
746
747         // Finally make the HTLC transactions have ANTI_REORG_DELAY blocks. This call previously
748         // panicked as described in the test introduction. This will remove the "maybe claimable"
749         // spendable output as nodes[1] has fully claimed the second HTLC.
750         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
751         expect_payment_failed!(nodes[0], payment_hash, false);
752
753         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
754                         claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
755                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
756                         confirmation_height: node_a_commitment_claimable,
757                 }, Balance::ClaimableAwaitingConfirmations {
758                         claimable_amount_satoshis: 10_000,
759                         confirmation_height: node_a_htlc_claimable,
760                 }]),
761                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
762
763         // Connect blocks until the commitment transaction's CSV expires, providing us the relevant
764         // `SpendableOutputs` event and removing the claimable balance entry.
765         connect_blocks(&nodes[0], node_a_commitment_claimable - nodes[0].best_block_info().1);
766         assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
767                         claimable_amount_satoshis: 10_000,
768                         confirmation_height: node_a_htlc_claimable,
769                 }],
770                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
771         test_spendable_output(&nodes[0], &as_txn[0]);
772
773         // Connect blocks until the HTLC-Timeout's CSV expires, providing us the relevant
774         // `SpendableOutputs` event and removing the claimable balance entry.
775         connect_blocks(&nodes[0], node_a_htlc_claimable - nodes[0].best_block_info().1);
776         assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
777         test_spendable_output(&nodes[0], &as_txn[1]);
778
779         // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
780         // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
781         // monitor events or claimable balances.
782         connect_blocks(&nodes[0], 6);
783         connect_blocks(&nodes[0], 6);
784         assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
785         assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
786 }
787
788 #[test]
789 fn test_no_preimage_inbound_htlc_balances() {
790         // Tests that MaybePreimageClaimableHTLC are generated for inbound HTLCs for which we do not
791         // have a preimage.
792         let chanmon_cfgs = create_chanmon_cfgs(2);
793         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
794         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
795         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
796
797         let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000);
798         let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
799
800         // Send two HTLCs, one from A to B, and one from B to A.
801         let to_b_failed_payment_hash = route_payment(&nodes[0], &[&nodes[1]], 10_000_000).1;
802         let to_a_failed_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 20_000_000).1;
803         let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
804
805         let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
806         let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
807
808         // Both A and B will have an HTLC that's claimable on timeout and one that's claimable if they
809         // receive the preimage. These will remain the same through the channel closure and until the
810         // HTLC output is spent.
811
812         assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
813                         claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
814                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
815                 }, Balance::MaybePreimageClaimableHTLC {
816                         claimable_amount_satoshis: 20_000,
817                         expiry_height: htlc_cltv_timeout,
818                 }, Balance::MaybeTimeoutClaimableHTLC {
819                         claimable_amount_satoshis: 10_000,
820                         claimable_height: htlc_cltv_timeout,
821                 }]),
822                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
823
824         assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
825                         claimable_amount_satoshis: 500_000 - 20_000,
826                 }, Balance::MaybePreimageClaimableHTLC {
827                         claimable_amount_satoshis: 10_000,
828                         expiry_height: htlc_cltv_timeout,
829                 }, Balance::MaybeTimeoutClaimableHTLC {
830                         claimable_amount_satoshis: 20_000,
831                         claimable_height: htlc_cltv_timeout,
832                 }]),
833                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
834
835         // Get nodes[0]'s commitment transaction and HTLC-Timeout transaction
836         let as_txn = get_local_commitment_txn!(nodes[0], chan_id);
837         assert_eq!(as_txn.len(), 2);
838         check_spends!(as_txn[1], as_txn[0]);
839         check_spends!(as_txn[0], funding_tx);
840
841         // Now close the channel by confirming A's commitment transaction on both nodes, checking the
842         // claimable balances remain the same except for the non-HTLC balance changing variant.
843         let node_a_commitment_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
844         let as_pre_spend_claims = sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
845                         claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
846                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
847                         confirmation_height: node_a_commitment_claimable,
848                 }, Balance::MaybePreimageClaimableHTLC {
849                         claimable_amount_satoshis: 20_000,
850                         expiry_height: htlc_cltv_timeout,
851                 }, Balance::MaybeTimeoutClaimableHTLC {
852                         claimable_amount_satoshis: 10_000,
853                         claimable_height: htlc_cltv_timeout,
854                 }]);
855
856         mine_transaction(&nodes[0], &as_txn[0]);
857         nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
858         check_added_monitors!(nodes[0], 1);
859         check_closed_broadcast!(nodes[0], true);
860         check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
861
862         assert_eq!(as_pre_spend_claims,
863                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
864
865         mine_transaction(&nodes[1], &as_txn[0]);
866         check_added_monitors!(nodes[1], 1);
867         check_closed_broadcast!(nodes[1], true);
868         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
869
870         let node_b_commitment_claimable = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
871         let mut bs_pre_spend_claims = sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
872                         claimable_amount_satoshis: 500_000 - 20_000,
873                         confirmation_height: node_b_commitment_claimable,
874                 }, Balance::MaybePreimageClaimableHTLC {
875                         claimable_amount_satoshis: 10_000,
876                         expiry_height: htlc_cltv_timeout,
877                 }, Balance::MaybeTimeoutClaimableHTLC {
878                         claimable_amount_satoshis: 20_000,
879                         claimable_height: htlc_cltv_timeout,
880                 }]);
881         assert_eq!(bs_pre_spend_claims,
882                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
883
884         // We'll broadcast the HTLC-Timeout transaction one block prior to the htlc's expiration (as it
885         // is confirmable in the next block), but will still include the same claimable balances as no
886         // HTLC has been spent, even after the HTLC expires. We'll also fail the inbound HTLC, but it
887         // won't do anything as the channel is already closed.
888
889         connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1);
890         let as_htlc_timeout_claim = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
891         assert_eq!(as_htlc_timeout_claim.len(), 1);
892         check_spends!(as_htlc_timeout_claim[0], as_txn[0]);
893         expect_pending_htlcs_forwardable_conditions!(nodes[0],
894                 [HTLCDestination::FailedPayment { payment_hash: to_a_failed_payment_hash }]);
895
896         assert_eq!(as_pre_spend_claims,
897                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
898
899         connect_blocks(&nodes[0], 1);
900         assert_eq!(as_pre_spend_claims,
901                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
902
903         // For node B, we'll get the non-HTLC funds claimable after ANTI_REORG_DELAY confirmations
904         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
905         test_spendable_output(&nodes[1], &as_txn[0]);
906         bs_pre_spend_claims.retain(|e| if let Balance::ClaimableAwaitingConfirmations { .. } = e { false } else { true });
907
908         // The next few blocks for B look the same as for A, though for the opposite HTLC
909         nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
910         connect_blocks(&nodes[1], TEST_FINAL_CLTV - (ANTI_REORG_DELAY - 1) - 1);
911         expect_pending_htlcs_forwardable_conditions!(nodes[1],
912                 [HTLCDestination::FailedPayment { payment_hash: to_b_failed_payment_hash }]);
913         let bs_htlc_timeout_claim = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
914         assert_eq!(bs_htlc_timeout_claim.len(), 1);
915         check_spends!(bs_htlc_timeout_claim[0], as_txn[0]);
916
917         assert_eq!(bs_pre_spend_claims,
918                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
919
920         connect_blocks(&nodes[1], 1);
921         assert_eq!(bs_pre_spend_claims,
922                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
923
924         // Now confirm the two HTLC timeout transactions for A, checking that the inbound HTLC resolves
925         // after ANTI_REORG_DELAY confirmations and the other takes BREAKDOWN_TIMEOUT confirmations.
926         mine_transaction(&nodes[0], &as_htlc_timeout_claim[0]);
927         let as_timeout_claimable_height = nodes[0].best_block_info().1 + (BREAKDOWN_TIMEOUT as u32) - 1;
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,
938                 }]),
939                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
940
941         mine_transaction(&nodes[0], &bs_htlc_timeout_claim[0]);
942         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
943                         claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
944                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
945                         confirmation_height: node_a_commitment_claimable,
946                 }, Balance::MaybePreimageClaimableHTLC {
947                         claimable_amount_satoshis: 20_000,
948                         expiry_height: htlc_cltv_timeout,
949                 }, Balance::ClaimableAwaitingConfirmations {
950                         claimable_amount_satoshis: 10_000,
951                         confirmation_height: as_timeout_claimable_height,
952                 }]),
953                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
954
955         // Once as_htlc_timeout_claim[0] reaches ANTI_REORG_DELAY confirmations, we should get a
956         // payment failure event.
957         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 2);
958         expect_payment_failed!(nodes[0], to_b_failed_payment_hash, false);
959
960         connect_blocks(&nodes[0], 1);
961         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
962                         claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
963                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
964                         confirmation_height: node_a_commitment_claimable,
965                 }, Balance::ClaimableAwaitingConfirmations {
966                         claimable_amount_satoshis: 10_000,
967                         confirmation_height: core::cmp::max(as_timeout_claimable_height, htlc_cltv_timeout),
968                 }]),
969                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
970
971         connect_blocks(&nodes[0], node_a_commitment_claimable - nodes[0].best_block_info().1);
972         assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
973                         claimable_amount_satoshis: 10_000,
974                         confirmation_height: core::cmp::max(as_timeout_claimable_height, htlc_cltv_timeout),
975                 }],
976                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
977         test_spendable_output(&nodes[0], &as_txn[0]);
978
979         connect_blocks(&nodes[0], as_timeout_claimable_height - nodes[0].best_block_info().1);
980         assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
981         test_spendable_output(&nodes[0], &as_htlc_timeout_claim[0]);
982
983         // The process for B should be completely identical as well, noting that the non-HTLC-balance
984         // was already claimed.
985         mine_transaction(&nodes[1], &bs_htlc_timeout_claim[0]);
986         let bs_timeout_claimable_height = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
987         assert_eq!(sorted_vec(vec![Balance::MaybePreimageClaimableHTLC {
988                         claimable_amount_satoshis: 10_000,
989                         expiry_height: htlc_cltv_timeout,
990                 }, Balance::ClaimableAwaitingConfirmations {
991                         claimable_amount_satoshis: 20_000,
992                         confirmation_height: bs_timeout_claimable_height,
993                 }]),
994                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
995
996         mine_transaction(&nodes[1], &as_htlc_timeout_claim[0]);
997         assert_eq!(sorted_vec(vec![Balance::MaybePreimageClaimableHTLC {
998                         claimable_amount_satoshis: 10_000,
999                         expiry_height: htlc_cltv_timeout,
1000                 }, Balance::ClaimableAwaitingConfirmations {
1001                         claimable_amount_satoshis: 20_000,
1002                         confirmation_height: bs_timeout_claimable_height,
1003                 }]),
1004                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1005
1006         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 2);
1007         expect_payment_failed!(nodes[1], to_a_failed_payment_hash, false);
1008
1009         assert_eq!(vec![Balance::MaybePreimageClaimableHTLC {
1010                         claimable_amount_satoshis: 10_000,
1011                         expiry_height: htlc_cltv_timeout,
1012                 }],
1013                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
1014         test_spendable_output(&nodes[1], &bs_htlc_timeout_claim[0]);
1015
1016         connect_blocks(&nodes[1], 1);
1017         assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1018
1019         // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
1020         // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
1021         // monitor events or claimable balances.
1022         connect_blocks(&nodes[1], 6);
1023         connect_blocks(&nodes[1], 6);
1024         assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1025         assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1026 }
1027
1028 fn sorted_vec_with_additions<T: Ord + Clone>(v_orig: &Vec<T>, extra_ts: &[&T]) -> Vec<T> {
1029         let mut v = v_orig.clone();
1030         for t in extra_ts {
1031                 v.push((*t).clone());
1032         }
1033         v.sort_unstable();
1034         v
1035 }
1036
1037 fn do_test_revoked_counterparty_commitment_balances(confirm_htlc_spend_first: bool) {
1038         // Tests `get_claimable_balances` for revoked counterparty commitment transactions.
1039         let mut chanmon_cfgs = create_chanmon_cfgs(2);
1040         // We broadcast a second-to-latest commitment transaction, without providing the revocation
1041         // secret to the counterparty. However, because we always immediately take the revocation
1042         // secret from the keys_manager, we would panic at broadcast as we're trying to sign a
1043         // transaction which, from the point of view of our keys_manager, is revoked.
1044         chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
1045         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1046         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1047         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1048
1049         let (_, _, chan_id, funding_tx) =
1050                 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 100_000_000);
1051         let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
1052         assert_eq!(funding_outpoint.to_channel_id(), chan_id);
1053
1054         // We create five HTLCs for B to claim against A's revoked commitment transaction:
1055         //
1056         // (1) one for which A is the originator and B knows the preimage
1057         // (2) one for which B is the originator where the HTLC has since timed-out
1058         // (3) one for which B is the originator but where the HTLC has not yet timed-out
1059         // (4) one dust HTLC which is lost in the channel closure
1060         // (5) one that actually isn't in the revoked commitment transaction at all, but was added in
1061         //     later commitment transaction updates
1062         //
1063         // Though they could all be claimed in a single claim transaction, due to CLTV timeouts they
1064         // are all currently claimed in separate transactions, which helps us test as we can claim
1065         // HTLCs individually.
1066
1067         let (claimed_payment_preimage, claimed_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 3_000_000);
1068         let timeout_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 4_000_000).1;
1069         let dust_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 3_000).1;
1070
1071         let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
1072
1073         connect_blocks(&nodes[0], 10);
1074         connect_blocks(&nodes[1], 10);
1075
1076         let live_htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
1077         let live_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 5_000_000).1;
1078
1079         // Get the latest commitment transaction from A and then update the fee to revoke it
1080         let as_revoked_txn = get_local_commitment_txn!(nodes[0], chan_id);
1081         let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
1082
1083         let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
1084
1085         let missing_htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
1086         let missing_htlc_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 2_000_000).1;
1087
1088         nodes[1].node.claim_funds(claimed_payment_preimage);
1089         expect_payment_claimed!(nodes[1], claimed_payment_hash, 3_000_000);
1090         check_added_monitors!(nodes[1], 1);
1091         let _b_htlc_msgs = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
1092
1093         connect_blocks(&nodes[0], htlc_cltv_timeout + 1 - 10);
1094         check_closed_broadcast!(nodes[0], true);
1095         check_added_monitors!(nodes[0], 1);
1096
1097         let mut events = nodes[0].node.get_and_clear_pending_events();
1098         assert_eq!(events.len(), 6);
1099         let mut failed_payments: HashSet<_> =
1100                 [timeout_payment_hash, dust_payment_hash, live_payment_hash, missing_htlc_payment_hash]
1101                 .iter().map(|a| *a).collect();
1102         events.retain(|ev| {
1103                 match ev {
1104                         Event::HTLCHandlingFailed { failed_next_destination: HTLCDestination::NextHopChannel { node_id, channel_id }, .. } => {
1105                                 assert_eq!(*channel_id, chan_id);
1106                                 assert_eq!(*node_id, Some(nodes[1].node.get_our_node_id()));
1107                                 false
1108                         },
1109                         Event::HTLCHandlingFailed { failed_next_destination: HTLCDestination::FailedPayment { payment_hash }, .. } => {
1110                                 assert!(failed_payments.remove(payment_hash));
1111                                 false
1112                         },
1113                         _ => true,
1114                 }
1115         });
1116         assert!(failed_payments.is_empty());
1117         if let Event::PendingHTLCsForwardable { .. } = events[0] {} else { panic!(); }
1118         match &events[1] {
1119                 Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {},
1120                 _ => panic!(),
1121         }
1122
1123         connect_blocks(&nodes[1], htlc_cltv_timeout + 1 - 10);
1124         check_closed_broadcast!(nodes[1], true);
1125         check_added_monitors!(nodes[1], 1);
1126         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
1127
1128         // Prior to channel closure, B considers the preimage HTLC as its own, and otherwise only
1129         // lists the two on-chain timeout-able HTLCs as claimable balances.
1130         assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
1131                         claimable_amount_satoshis: 100_000 - 5_000 - 4_000 - 3 - 2_000 + 3_000,
1132                 }, Balance::MaybeTimeoutClaimableHTLC {
1133                         claimable_amount_satoshis: 2_000,
1134                         claimable_height: missing_htlc_cltv_timeout,
1135                 }, Balance::MaybeTimeoutClaimableHTLC {
1136                         claimable_amount_satoshis: 4_000,
1137                         claimable_height: htlc_cltv_timeout,
1138                 }, Balance::MaybeTimeoutClaimableHTLC {
1139                         claimable_amount_satoshis: 5_000,
1140                         claimable_height: live_htlc_cltv_timeout,
1141                 }]),
1142                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1143
1144         mine_transaction(&nodes[1], &as_revoked_txn[0]);
1145         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();
1146         // Currently the revoked commitment is claimed in four transactions as the HTLCs all expire
1147         // quite soon.
1148         assert_eq!(claim_txn.len(), 4);
1149         claim_txn.sort_unstable_by_key(|tx| tx.output.iter().map(|output| output.value).sum::<u64>());
1150
1151         // The following constants were determined experimentally
1152         const BS_TO_SELF_CLAIM_EXP_WEIGHT: usize = 483;
1153         const OUTBOUND_HTLC_CLAIM_EXP_WEIGHT: usize = 571;
1154         const INBOUND_HTLC_CLAIM_EXP_WEIGHT: usize = 578;
1155
1156         // Check that the weight is close to the expected weight. Note that signature sizes vary
1157         // somewhat so it may not always be exact.
1158         fuzzy_assert_eq(claim_txn[0].weight(), OUTBOUND_HTLC_CLAIM_EXP_WEIGHT);
1159         fuzzy_assert_eq(claim_txn[1].weight(), INBOUND_HTLC_CLAIM_EXP_WEIGHT);
1160         fuzzy_assert_eq(claim_txn[2].weight(), INBOUND_HTLC_CLAIM_EXP_WEIGHT);
1161         fuzzy_assert_eq(claim_txn[3].weight(), BS_TO_SELF_CLAIM_EXP_WEIGHT);
1162
1163         // The expected balance for the next three checks, with the largest-HTLC and to_self output
1164         // claim balances separated out.
1165         let expected_balance = vec![Balance::ClaimableAwaitingConfirmations {
1166                         // to_remote output in A's revoked commitment
1167                         claimable_amount_satoshis: 100_000 - 5_000 - 4_000 - 3,
1168                         confirmation_height: nodes[1].best_block_info().1 + 5,
1169                 }, Balance::CounterpartyRevokedOutputClaimable {
1170                         claimable_amount_satoshis: 3_000,
1171                 }, Balance::CounterpartyRevokedOutputClaimable {
1172                         claimable_amount_satoshis: 4_000,
1173                 }];
1174
1175         let to_self_unclaimed_balance = Balance::CounterpartyRevokedOutputClaimable {
1176                 claimable_amount_satoshis: 1_000_000 - 100_000 - 3_000 - chan_feerate *
1177                         (channel::commitment_tx_base_weight(opt_anchors) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1178         };
1179         let to_self_claimed_avail_height;
1180         let largest_htlc_unclaimed_balance = Balance::CounterpartyRevokedOutputClaimable {
1181                 claimable_amount_satoshis: 5_000,
1182         };
1183         let largest_htlc_claimed_avail_height;
1184
1185         // Once the channel has been closed by A, B now considers all of the commitment transactions'
1186         // outputs as `CounterpartyRevokedOutputClaimable`.
1187         assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_unclaimed_balance, &largest_htlc_unclaimed_balance]),
1188                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1189
1190         if confirm_htlc_spend_first {
1191                 mine_transaction(&nodes[1], &claim_txn[2]);
1192                 largest_htlc_claimed_avail_height = nodes[1].best_block_info().1 + 5;
1193                 to_self_claimed_avail_height = nodes[1].best_block_info().1 + 6; // will be claimed in the next block
1194         } else {
1195                 // Connect the to_self output claim, taking all of A's non-HTLC funds
1196                 mine_transaction(&nodes[1], &claim_txn[3]);
1197                 to_self_claimed_avail_height = nodes[1].best_block_info().1 + 5;
1198                 largest_htlc_claimed_avail_height = nodes[1].best_block_info().1 + 6; // will be claimed in the next block
1199         }
1200
1201         let largest_htlc_claimed_balance = Balance::ClaimableAwaitingConfirmations {
1202                 claimable_amount_satoshis: 5_000 - chan_feerate * INBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
1203                 confirmation_height: largest_htlc_claimed_avail_height,
1204         };
1205         let to_self_claimed_balance = Balance::ClaimableAwaitingConfirmations {
1206                 claimable_amount_satoshis: 1_000_000 - 100_000 - 3_000 - chan_feerate *
1207                         (channel::commitment_tx_base_weight(opt_anchors) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
1208                         - chan_feerate * claim_txn[3].weight() as u64 / 1000,
1209                 confirmation_height: to_self_claimed_avail_height,
1210         };
1211
1212         if confirm_htlc_spend_first {
1213                 assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_unclaimed_balance, &largest_htlc_claimed_balance]),
1214                         sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1215         } else {
1216                 assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_claimed_balance, &largest_htlc_unclaimed_balance]),
1217                         sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1218         }
1219
1220         if confirm_htlc_spend_first {
1221                 mine_transaction(&nodes[1], &claim_txn[3]);
1222         } else {
1223                 mine_transaction(&nodes[1], &claim_txn[2]);
1224         }
1225         assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_claimed_balance, &largest_htlc_claimed_balance]),
1226                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1227
1228         // Finally, connect the last two remaining HTLC spends and check that they move to
1229         // `ClaimableAwaitingConfirmations`
1230         mine_transaction(&nodes[1], &claim_txn[0]);
1231         mine_transaction(&nodes[1], &claim_txn[1]);
1232
1233         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1234                         // to_remote output in A's revoked commitment
1235                         claimable_amount_satoshis: 100_000 - 5_000 - 4_000 - 3,
1236                         confirmation_height: nodes[1].best_block_info().1 + 1,
1237                 }, Balance::ClaimableAwaitingConfirmations {
1238                         claimable_amount_satoshis: 1_000_000 - 100_000 - 3_000 - chan_feerate *
1239                                 (channel::commitment_tx_base_weight(opt_anchors) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
1240                                 - chan_feerate * claim_txn[3].weight() as u64 / 1000,
1241                         confirmation_height: to_self_claimed_avail_height,
1242                 }, Balance::ClaimableAwaitingConfirmations {
1243                         claimable_amount_satoshis: 3_000 - chan_feerate * OUTBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
1244                         confirmation_height: nodes[1].best_block_info().1 + 4,
1245                 }, Balance::ClaimableAwaitingConfirmations {
1246                         claimable_amount_satoshis: 4_000 - chan_feerate * INBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
1247                         confirmation_height: nodes[1].best_block_info().1 + 5,
1248                 }, Balance::ClaimableAwaitingConfirmations {
1249                         claimable_amount_satoshis: 5_000 - chan_feerate * INBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
1250                         confirmation_height: largest_htlc_claimed_avail_height,
1251                 }]),
1252                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1253
1254         connect_blocks(&nodes[1], 1);
1255         test_spendable_output(&nodes[1], &as_revoked_txn[0]);
1256
1257         let mut payment_failed_events = nodes[1].node.get_and_clear_pending_events();
1258         expect_payment_failed_conditions_event(payment_failed_events[..2].to_vec(),
1259                 missing_htlc_payment_hash, false, PaymentFailedConditions::new());
1260         expect_payment_failed_conditions_event(payment_failed_events[2..].to_vec(),
1261                 dust_payment_hash, false, PaymentFailedConditions::new());
1262
1263         connect_blocks(&nodes[1], 1);
1264         test_spendable_output(&nodes[1], &claim_txn[if confirm_htlc_spend_first { 2 } else { 3 }]);
1265         connect_blocks(&nodes[1], 1);
1266         test_spendable_output(&nodes[1], &claim_txn[if confirm_htlc_spend_first { 3 } else { 2 }]);
1267         expect_payment_failed!(nodes[1], live_payment_hash, false);
1268         connect_blocks(&nodes[1], 1);
1269         test_spendable_output(&nodes[1], &claim_txn[0]);
1270         connect_blocks(&nodes[1], 1);
1271         test_spendable_output(&nodes[1], &claim_txn[1]);
1272         expect_payment_failed!(nodes[1], timeout_payment_hash, false);
1273         assert_eq!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances(), Vec::new());
1274
1275         // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
1276         // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
1277         // monitor events or claimable balances.
1278         connect_blocks(&nodes[1], 6);
1279         connect_blocks(&nodes[1], 6);
1280         assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1281         assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1282 }
1283
1284 #[test]
1285 fn test_revoked_counterparty_commitment_balances() {
1286         do_test_revoked_counterparty_commitment_balances(true);
1287         do_test_revoked_counterparty_commitment_balances(false);
1288 }
1289
1290 #[test]
1291 fn test_revoked_counterparty_htlc_tx_balances() {
1292         // Tests `get_claimable_balances` for revocation spends of HTLC transactions.
1293         let mut chanmon_cfgs = create_chanmon_cfgs(2);
1294         chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
1295         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1296         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1297         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1298
1299         // Create some initial channels
1300         let (_, _, chan_id, funding_tx) =
1301                 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 11_000_000);
1302         let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
1303         assert_eq!(funding_outpoint.to_channel_id(), chan_id);
1304
1305         let payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 3_000_000).0;
1306         let failed_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 1_000_000).1;
1307         let revoked_local_txn = get_local_commitment_txn!(nodes[1], chan_id);
1308         assert_eq!(revoked_local_txn[0].input.len(), 1);
1309         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, funding_tx.txid());
1310
1311         // The to-be-revoked commitment tx should have two HTLCs and an output for both sides
1312         assert_eq!(revoked_local_txn[0].output.len(), 4);
1313
1314         claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
1315
1316         let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
1317         let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
1318
1319         // B will generate an HTLC-Success from its revoked commitment tx
1320         mine_transaction(&nodes[1], &revoked_local_txn[0]);
1321         check_closed_broadcast!(nodes[1], true);
1322         check_added_monitors!(nodes[1], 1);
1323         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
1324         let revoked_htlc_success_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1325
1326         assert_eq!(revoked_htlc_success_txn.len(), 1);
1327         assert_eq!(revoked_htlc_success_txn[0].input.len(), 1);
1328         assert_eq!(revoked_htlc_success_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
1329         check_spends!(revoked_htlc_success_txn[0], revoked_local_txn[0]);
1330
1331         connect_blocks(&nodes[1], TEST_FINAL_CLTV);
1332         let revoked_htlc_timeout_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1333         assert_eq!(revoked_htlc_timeout_txn.len(), 1);
1334         check_spends!(revoked_htlc_timeout_txn[0], revoked_local_txn[0]);
1335         assert_ne!(revoked_htlc_success_txn[0].input[0].previous_output, revoked_htlc_timeout_txn[0].input[0].previous_output);
1336         assert_eq!(revoked_htlc_success_txn[0].lock_time.0, 0);
1337         assert_ne!(revoked_htlc_timeout_txn[0].lock_time.0, 0);
1338
1339         // A will generate justice tx from B's revoked commitment/HTLC tx
1340         mine_transaction(&nodes[0], &revoked_local_txn[0]);
1341         check_closed_broadcast!(nodes[0], true);
1342         check_added_monitors!(nodes[0], 1);
1343         check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
1344         let to_remote_conf_height = nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1;
1345
1346         let as_commitment_claim_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1347         assert_eq!(as_commitment_claim_txn.len(), 1);
1348         check_spends!(as_commitment_claim_txn[0], revoked_local_txn[0]);
1349
1350         // The next two checks have the same balance set for A - even though we confirm a revoked HTLC
1351         // transaction our balance tracking doesn't use the on-chain value so the
1352         // `CounterpartyRevokedOutputClaimable` entry doesn't change.
1353         let as_balances = sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1354                         // to_remote output in B's revoked commitment
1355                         claimable_amount_satoshis: 1_000_000 - 11_000 - 3_000 - chan_feerate *
1356                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1357                         confirmation_height: to_remote_conf_height,
1358                 }, Balance::CounterpartyRevokedOutputClaimable {
1359                         // to_self output in B's revoked commitment
1360                         claimable_amount_satoshis: 10_000,
1361                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1362                         claimable_amount_satoshis: 3_000,
1363                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1364                         claimable_amount_satoshis: 1_000,
1365                 }]);
1366         assert_eq!(as_balances,
1367                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1368
1369         mine_transaction(&nodes[0], &revoked_htlc_success_txn[0]);
1370         let as_htlc_claim_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1371         assert_eq!(as_htlc_claim_tx.len(), 2);
1372         check_spends!(as_htlc_claim_tx[0], revoked_htlc_success_txn[0]);
1373         check_spends!(as_htlc_claim_tx[1], revoked_local_txn[0]); // A has to generate a new claim for the remaining revoked
1374                                                                   // outputs (which no longer includes the spent HTLC output)
1375
1376         assert_eq!(as_balances,
1377                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1378
1379         assert_eq!(as_htlc_claim_tx[0].output.len(), 1);
1380         fuzzy_assert_eq(as_htlc_claim_tx[0].output[0].value,
1381                 3_000 - chan_feerate * (revoked_htlc_success_txn[0].weight() + as_htlc_claim_tx[0].weight()) as u64 / 1000);
1382
1383         mine_transaction(&nodes[0], &as_htlc_claim_tx[0]);
1384         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1385                         // to_remote output in B's revoked commitment
1386                         claimable_amount_satoshis: 1_000_000 - 11_000 - 3_000 - chan_feerate *
1387                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1388                         confirmation_height: to_remote_conf_height,
1389                 }, Balance::CounterpartyRevokedOutputClaimable {
1390                         // to_self output in B's revoked commitment
1391                         claimable_amount_satoshis: 10_000,
1392                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1393                         claimable_amount_satoshis: 1_000,
1394                 }, Balance::ClaimableAwaitingConfirmations {
1395                         claimable_amount_satoshis: as_htlc_claim_tx[0].output[0].value,
1396                         confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
1397                 }]),
1398                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1399
1400         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 3);
1401         test_spendable_output(&nodes[0], &revoked_local_txn[0]);
1402         assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1403                         // to_self output to B
1404                         claimable_amount_satoshis: 10_000,
1405                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1406                         claimable_amount_satoshis: 1_000,
1407                 }, Balance::ClaimableAwaitingConfirmations {
1408                         claimable_amount_satoshis: as_htlc_claim_tx[0].output[0].value,
1409                         confirmation_height: nodes[0].best_block_info().1 + 2,
1410                 }]),
1411                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1412
1413         connect_blocks(&nodes[0], 2);
1414         test_spendable_output(&nodes[0], &as_htlc_claim_tx[0]);
1415         assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1416                         // to_self output in B's revoked commitment
1417                         claimable_amount_satoshis: 10_000,
1418                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1419                         claimable_amount_satoshis: 1_000,
1420                 }]),
1421                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1422
1423         connect_blocks(&nodes[0], revoked_htlc_timeout_txn[0].lock_time.0 - nodes[0].best_block_info().1);
1424         expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(&nodes[0],
1425                 [HTLCDestination::FailedPayment { payment_hash: failed_payment_hash }]);
1426         // As time goes on A may split its revocation claim transaction into multiple.
1427         let as_fewer_input_rbf = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1428         for tx in as_fewer_input_rbf.iter() {
1429                 check_spends!(tx, revoked_local_txn[0]);
1430         }
1431
1432         // Connect a number of additional blocks to ensure we don't forget the HTLC output needs
1433         // claiming.
1434         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
1435         let as_fewer_input_rbf = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1436         for tx in as_fewer_input_rbf.iter() {
1437                 check_spends!(tx, revoked_local_txn[0]);
1438         }
1439
1440         mine_transaction(&nodes[0], &revoked_htlc_timeout_txn[0]);
1441         let as_second_htlc_claim_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1442         assert_eq!(as_second_htlc_claim_tx.len(), 2);
1443
1444         check_spends!(as_second_htlc_claim_tx[0], revoked_htlc_timeout_txn[0]);
1445         check_spends!(as_second_htlc_claim_tx[1], revoked_local_txn[0]);
1446
1447         // Connect blocks to finalize the HTLC resolution with the HTLC-Timeout transaction. In a
1448         // previous iteration of the revoked balance handling this would result in us "forgetting" that
1449         // the revoked HTLC output still needed to be claimed.
1450         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
1451         assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1452                         // to_self output in B's revoked commitment
1453                         claimable_amount_satoshis: 10_000,
1454                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1455                         claimable_amount_satoshis: 1_000,
1456                 }]),
1457                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1458
1459         mine_transaction(&nodes[0], &as_second_htlc_claim_tx[0]);
1460         assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1461                         // to_self output in B's revoked commitment
1462                         claimable_amount_satoshis: 10_000,
1463                 }, Balance::ClaimableAwaitingConfirmations {
1464                         claimable_amount_satoshis: as_second_htlc_claim_tx[0].output[0].value,
1465                         confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
1466                 }]),
1467                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1468
1469         mine_transaction(&nodes[0], &as_second_htlc_claim_tx[1]);
1470         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1471                         // to_self output in B's revoked commitment
1472                         claimable_amount_satoshis: as_second_htlc_claim_tx[1].output[0].value,
1473                         confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
1474                 }, Balance::ClaimableAwaitingConfirmations {
1475                         claimable_amount_satoshis: as_second_htlc_claim_tx[0].output[0].value,
1476                         confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 2,
1477                 }]),
1478                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1479
1480         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 2);
1481         test_spendable_output(&nodes[0], &as_second_htlc_claim_tx[0]);
1482         connect_blocks(&nodes[0], 1);
1483         test_spendable_output(&nodes[0], &as_second_htlc_claim_tx[1]);
1484
1485         assert_eq!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances(), Vec::new());
1486
1487         // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
1488         // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
1489         // monitor events or claimable balances.
1490         connect_blocks(&nodes[0], 6);
1491         connect_blocks(&nodes[0], 6);
1492         assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1493         assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1494 }
1495
1496 #[test]
1497 fn test_revoked_counterparty_aggregated_claims() {
1498         // Tests `get_claimable_balances` for revoked counterparty commitment transactions when
1499         // claiming with an aggregated claim transaction.
1500         let mut chanmon_cfgs = create_chanmon_cfgs(2);
1501         // We broadcast a second-to-latest commitment transaction, without providing the revocation
1502         // secret to the counterparty. However, because we always immediately take the revocation
1503         // secret from the keys_manager, we would panic at broadcast as we're trying to sign a
1504         // transaction which, from the point of view of our keys_manager, is revoked.
1505         chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
1506         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1507         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1508         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1509
1510         let (_, _, chan_id, funding_tx) =
1511                 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 100_000_000);
1512         let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
1513         assert_eq!(funding_outpoint.to_channel_id(), chan_id);
1514
1515         // We create two HTLCs, one which we will give A the preimage to to generate an HTLC-Success
1516         // transaction, and one which we will not, allowing B to claim the HTLC output in an aggregated
1517         // revocation-claim transaction.
1518
1519         let (claimed_payment_preimage, claimed_payment_hash, ..) = route_payment(&nodes[1], &[&nodes[0]], 3_000_000);
1520         let revoked_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 4_000_000).1;
1521
1522         let htlc_cltv_timeout = nodes[1].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
1523
1524         // Cheat by giving A's ChannelMonitor the preimage to the to-be-claimed HTLC so that we have an
1525         // HTLC-claim transaction on the to-be-revoked state.
1526         get_monitor!(nodes[0], chan_id).provide_payment_preimage(&claimed_payment_hash, &claimed_payment_preimage,
1527                 &node_cfgs[0].tx_broadcaster, &LowerBoundedFeeEstimator::new(node_cfgs[0].fee_estimator), &nodes[0].logger);
1528
1529         // Now get the latest commitment transaction from A and then update the fee to revoke it
1530         let as_revoked_txn = get_local_commitment_txn!(nodes[0], chan_id);
1531
1532         assert_eq!(as_revoked_txn.len(), 2);
1533         check_spends!(as_revoked_txn[0], funding_tx);
1534         check_spends!(as_revoked_txn[1], as_revoked_txn[0]); // The HTLC-Claim transaction
1535
1536         let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
1537         let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
1538
1539         {
1540                 let mut feerate = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
1541                 *feerate += 1;
1542         }
1543         nodes[0].node.timer_tick_occurred();
1544         check_added_monitors!(nodes[0], 1);
1545
1546         let fee_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1547         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &fee_update.update_fee.unwrap());
1548         commitment_signed_dance!(nodes[1], nodes[0], fee_update.commitment_signed, false);
1549
1550         nodes[0].node.claim_funds(claimed_payment_preimage);
1551         expect_payment_claimed!(nodes[0], claimed_payment_hash, 3_000_000);
1552         check_added_monitors!(nodes[0], 1);
1553         let _a_htlc_msgs = get_htlc_update_msgs!(&nodes[0], nodes[1].node.get_our_node_id());
1554
1555         assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
1556                         claimable_amount_satoshis: 100_000 - 4_000 - 3_000,
1557                 }, Balance::MaybeTimeoutClaimableHTLC {
1558                         claimable_amount_satoshis: 4_000,
1559                         claimable_height: htlc_cltv_timeout,
1560                 }, Balance::MaybeTimeoutClaimableHTLC {
1561                         claimable_amount_satoshis: 3_000,
1562                         claimable_height: htlc_cltv_timeout,
1563                 }]),
1564                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1565
1566         mine_transaction(&nodes[1], &as_revoked_txn[0]);
1567         check_closed_broadcast!(nodes[1], true);
1568         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
1569         check_added_monitors!(nodes[1], 1);
1570
1571         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();
1572         // Currently the revoked commitment outputs are all claimed in one aggregated transaction
1573         assert_eq!(claim_txn.len(), 1);
1574         assert_eq!(claim_txn[0].input.len(), 3);
1575         check_spends!(claim_txn[0], as_revoked_txn[0]);
1576
1577         let to_remote_maturity = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
1578
1579         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1580                         // to_remote output in A's revoked commitment
1581                         claimable_amount_satoshis: 100_000 - 4_000 - 3_000,
1582                         confirmation_height: to_remote_maturity,
1583                 }, Balance::CounterpartyRevokedOutputClaimable {
1584                         // to_self output in A's revoked commitment
1585                         claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1586                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1587                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1588                         claimable_amount_satoshis: 4_000,
1589                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1590                         claimable_amount_satoshis: 3_000,
1591                 }]),
1592                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1593
1594         // Confirm A's HTLC-Success tranasction which presumably raced B's claim, causing B to create a
1595         // new claim.
1596         mine_transaction(&nodes[1], &as_revoked_txn[1]);
1597         expect_payment_sent!(nodes[1], claimed_payment_preimage);
1598         let mut claim_txn_2: Vec<_> = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
1599         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 });
1600         // Once B sees the HTLC-Success transaction it splits its claim transaction into two, though in
1601         // theory it could re-aggregate the claims as well.
1602         assert_eq!(claim_txn_2.len(), 2);
1603         assert_eq!(claim_txn_2[0].input.len(), 2);
1604         check_spends!(claim_txn_2[0], as_revoked_txn[0]);
1605         assert_eq!(claim_txn_2[1].input.len(), 1);
1606         check_spends!(claim_txn_2[1], as_revoked_txn[1]);
1607
1608         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1609                         // to_remote output in A's revoked commitment
1610                         claimable_amount_satoshis: 100_000 - 4_000 - 3_000,
1611                         confirmation_height: to_remote_maturity,
1612                 }, Balance::CounterpartyRevokedOutputClaimable {
1613                         // to_self output in A's revoked commitment
1614                         claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1615                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1616                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1617                         claimable_amount_satoshis: 4_000,
1618                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1619                         // The amount here is a bit of a misnomer, really its been reduced by the HTLC
1620                         // transaction fee, but the claimable amount is always a bit of an overshoot for HTLCs
1621                         // anyway, so its not a big change.
1622                         claimable_amount_satoshis: 3_000,
1623                 }]),
1624                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1625
1626         connect_blocks(&nodes[1], 5);
1627         test_spendable_output(&nodes[1], &as_revoked_txn[0]);
1628
1629         assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1630                         // to_self output in A's revoked commitment
1631                         claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1632                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1633                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1634                         claimable_amount_satoshis: 4_000,
1635                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1636                         // The amount here is a bit of a misnomer, really its been reduced by the HTLC
1637                         // transaction fee, but the claimable amount is always a bit of an overshoot for HTLCs
1638                         // anyway, so its not a big change.
1639                         claimable_amount_satoshis: 3_000,
1640                 }]),
1641                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1642
1643         mine_transaction(&nodes[1], &claim_txn_2[1]);
1644         let htlc_2_claim_maturity = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
1645
1646         assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1647                         // to_self output in A's revoked commitment
1648                         claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1649                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1650                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1651                         claimable_amount_satoshis: 4_000,
1652                 }, Balance::ClaimableAwaitingConfirmations { // HTLC 2
1653                         claimable_amount_satoshis: claim_txn_2[1].output[0].value,
1654                         confirmation_height: htlc_2_claim_maturity,
1655                 }]),
1656                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1657
1658         connect_blocks(&nodes[1], 5);
1659         test_spendable_output(&nodes[1], &claim_txn_2[1]);
1660
1661         assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1662                         // to_self output in A's revoked commitment
1663                         claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1664                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1665                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1666                         claimable_amount_satoshis: 4_000,
1667                 }]),
1668                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1669
1670         mine_transaction(&nodes[1], &claim_txn_2[0]);
1671         let rest_claim_maturity = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
1672
1673         assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
1674                         claimable_amount_satoshis: claim_txn_2[0].output[0].value,
1675                         confirmation_height: rest_claim_maturity,
1676                 }],
1677                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
1678
1679         assert!(nodes[1].node.get_and_clear_pending_events().is_empty()); // We shouldn't fail the payment until we spend the output
1680
1681         connect_blocks(&nodes[1], 5);
1682         expect_payment_failed!(nodes[1], revoked_payment_hash, false);
1683         test_spendable_output(&nodes[1], &claim_txn_2[0]);
1684         assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1685
1686         // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
1687         // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
1688         // monitor events or claimable balances.
1689         connect_blocks(&nodes[1], 6);
1690         connect_blocks(&nodes[1], 6);
1691         assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1692         assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1693 }
1694
1695 #[cfg(anchors)]
1696 #[test]
1697 fn test_yield_anchors_events() {
1698         // Tests that two parties supporting anchor outputs can open a channel, route payments over
1699         // it, and finalize its resolution uncooperatively. Once the HTLCs are locked in, one side will
1700         // force close once the HTLCs expire. The force close should stem from an event emitted by LDK,
1701         // allowing the consumer to provide additional fees to the commitment transaction to be
1702         // broadcast. Once the commitment transaction confirms, events for the HTLC resolution should be
1703         // emitted by LDK, such that the consumer can attach fees to the zero fee HTLC transactions.
1704         let secp = Secp256k1::new();
1705         let mut chanmon_cfgs = create_chanmon_cfgs(2);
1706         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1707         let mut anchors_config = UserConfig::default();
1708         anchors_config.channel_handshake_config.announced_channel = true;
1709         anchors_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
1710         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(anchors_config), Some(anchors_config)]);
1711         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1712
1713         let chan_id = create_announced_chan_between_nodes_with_value(
1714                 &nodes, 0, 1, 1_000_000, 500_000_000
1715         ).2;
1716         route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
1717         let (payment_preimage, payment_hash, _) = route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1718
1719         assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
1720
1721         connect_blocks(&nodes[0], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1);
1722         check_closed_broadcast!(&nodes[0], true);
1723         assert!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().is_empty());
1724
1725         get_monitor!(nodes[0], chan_id).provide_payment_preimage(
1726                 &payment_hash, &payment_preimage, &node_cfgs[0].tx_broadcaster,
1727                 &LowerBoundedFeeEstimator::new(node_cfgs[0].fee_estimator), &nodes[0].logger
1728         );
1729
1730         let mut holder_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
1731         assert_eq!(holder_events.len(), 1);
1732         let (commitment_tx, anchor_tx) = match holder_events.pop().unwrap() {
1733                 Event::BumpTransaction(BumpTransactionEvent::ChannelClose { commitment_tx, anchor_descriptor, .. })  => {
1734                         assert_eq!(commitment_tx.input.len(), 1);
1735                         assert_eq!(commitment_tx.output.len(), 6);
1736                         let mut anchor_tx = Transaction {
1737                                 version: 2,
1738                                 lock_time: PackedLockTime::ZERO,
1739                                 input: vec![
1740                                         TxIn { previous_output: anchor_descriptor.outpoint, ..Default::default() },
1741                                         TxIn { ..Default::default() },
1742                                 ],
1743                                 output: vec![TxOut {
1744                                         value: Amount::ONE_BTC.to_sat(),
1745                                         script_pubkey: Script::new_op_return(&[]),
1746                                 }],
1747                         };
1748                         let signer = nodes[0].keys_manager.derive_channel_keys(
1749                                 anchor_descriptor.channel_value_satoshis, &anchor_descriptor.channel_keys_id,
1750                         );
1751                         let funding_sig = signer.sign_holder_anchor_input(&mut anchor_tx, 0, &secp).unwrap();
1752                         anchor_tx.input[0].witness = chan_utils::build_anchor_input_witness(
1753                                 &signer.pubkeys().funding_pubkey, &funding_sig
1754                         );
1755                         (commitment_tx, anchor_tx)
1756                 },
1757                 _ => panic!("Unexpected event"),
1758         };
1759
1760         mine_transactions(&nodes[0], &[&commitment_tx, &anchor_tx]);
1761         check_added_monitors!(nodes[0], 1);
1762
1763         let mut holder_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
1764         // Certain block `ConnectStyle`s cause an extra `ChannelClose` event to be emitted since the
1765         // best block is updated before the confirmed transactions are notified.
1766         match *nodes[0].connect_style.borrow() {
1767                 ConnectStyle::BestBlockFirst|ConnectStyle::BestBlockFirstReorgsOnlyTip|ConnectStyle::BestBlockFirstSkippingBlocks => {
1768                         assert_eq!(holder_events.len(), 3);
1769                         if let Event::BumpTransaction(BumpTransactionEvent::ChannelClose { .. }) = holder_events.remove(0) {}
1770                         else { panic!("unexpected event"); }
1771
1772                 },
1773                 _ => assert_eq!(holder_events.len(), 2),
1774         };
1775         let mut htlc_txs = Vec::with_capacity(2);
1776         for event in holder_events {
1777                 match event {
1778                         Event::BumpTransaction(BumpTransactionEvent::HTLCResolution { htlc_descriptors, tx_lock_time, .. }) => {
1779                                 assert_eq!(htlc_descriptors.len(), 1);
1780                                 let htlc_descriptor = &htlc_descriptors[0];
1781                                 let signer = nodes[0].keys_manager.derive_channel_keys(
1782                                         htlc_descriptor.channel_value_satoshis, &htlc_descriptor.channel_keys_id
1783                                 );
1784                                 let per_commitment_point = signer.get_per_commitment_point(htlc_descriptor.per_commitment_number, &secp);
1785                                 let mut htlc_tx = Transaction {
1786                                         version: 2,
1787                                         lock_time: tx_lock_time,
1788                                         input: vec![
1789                                                 htlc_descriptor.unsigned_tx_input(), // HTLC input
1790                                                 TxIn { ..Default::default() } // Fee input
1791                                         ],
1792                                         output: vec![
1793                                                 htlc_descriptor.tx_output(&per_commitment_point, &secp), // HTLC output
1794                                                 TxOut { // Fee input change
1795                                                         value: Amount::ONE_BTC.to_sat(),
1796                                                         script_pubkey: Script::new_op_return(&[]),
1797                                                 }
1798                                         ]
1799                                 };
1800                                 let our_sig = signer.sign_holder_htlc_transaction(&mut htlc_tx, 0, htlc_descriptor, &secp).unwrap();
1801                                 let witness_script = htlc_descriptor.witness_script(&per_commitment_point, &secp);
1802                                 htlc_tx.input[0].witness = htlc_descriptor.tx_input_witness(&our_sig, &witness_script);
1803                                 htlc_txs.push(htlc_tx);
1804                         },
1805                         _ => panic!("Unexpected event"),
1806                 }
1807         }
1808
1809         mine_transactions(&nodes[0], &[&htlc_txs[0], &htlc_txs[1]]);
1810         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
1811
1812         assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1813
1814         connect_blocks(&nodes[0], BREAKDOWN_TIMEOUT as u32);
1815
1816         let holder_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
1817         assert_eq!(holder_events.len(), 3);
1818         for event in holder_events {
1819                 match event {
1820                         Event::SpendableOutputs { .. } => {},
1821                         _ => panic!("Unexpected event"),
1822                 }
1823         }
1824
1825         // Clear the remaining events as they're not relevant to what we're testing.
1826         nodes[0].node.get_and_clear_pending_events();
1827 }
1828
1829 #[cfg(anchors)]
1830 #[test]
1831 fn test_anchors_aggregated_revoked_htlc_tx() {
1832         // Test that `ChannelMonitor`s can properly detect and claim funds from a counterparty claiming
1833         // multiple HTLCs from multiple channels in a single transaction via the success path from a
1834         // revoked commitment.
1835         let secp = Secp256k1::new();
1836         let mut chanmon_cfgs = create_chanmon_cfgs(2);
1837         // Required to sign a revoked commitment transaction
1838         chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
1839         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1840         let mut anchors_config = UserConfig::default();
1841         anchors_config.channel_handshake_config.announced_channel = true;
1842         anchors_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
1843         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(anchors_config), Some(anchors_config)]);
1844
1845         let bob_persister: test_utils::TestPersister;
1846         let bob_chain_monitor: test_utils::TestChainMonitor;
1847         let bob_deserialized: ChannelManager<
1848                 &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface,
1849                 &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator,
1850                 &test_utils::TestRouter, &test_utils::TestLogger,
1851         >;
1852
1853         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1854
1855         let chan_a = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 20_000_000);
1856         let chan_b = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 20_000_000);
1857
1858         // Serialize Bob with the initial state of both channels, which we'll use later.
1859         let bob_serialized = nodes[1].node.encode();
1860
1861         // Route two payments for each channel from Alice to Bob to lock in the HTLCs.
1862         let payment_a = route_payment(&nodes[0], &[&nodes[1]], 50_000_000);
1863         let payment_b = route_payment(&nodes[0], &[&nodes[1]], 50_000_000);
1864         let payment_c = route_payment(&nodes[0], &[&nodes[1]], 50_000_000);
1865         let payment_d = route_payment(&nodes[0], &[&nodes[1]], 50_000_000);
1866
1867         // Serialize Bob's monitors with the HTLCs locked in. We'll restart Bob later on with the state
1868         // at this point such that he broadcasts a revoked commitment transaction with the HTLCs
1869         // present.
1870         let bob_serialized_monitor_a = get_monitor!(nodes[1], chan_a.2).encode();
1871         let bob_serialized_monitor_b = get_monitor!(nodes[1], chan_b.2).encode();
1872
1873         // Bob claims all the HTLCs...
1874         claim_payment(&nodes[0], &[&nodes[1]], payment_a.0);
1875         claim_payment(&nodes[0], &[&nodes[1]], payment_b.0);
1876         claim_payment(&nodes[0], &[&nodes[1]], payment_c.0);
1877         claim_payment(&nodes[0], &[&nodes[1]], payment_d.0);
1878
1879         // ...and sends one back through each channel such that he has a motive to broadcast his
1880         // revoked state.
1881         send_payment(&nodes[1], &[&nodes[0]], 30_000_000);
1882         send_payment(&nodes[1], &[&nodes[0]], 30_000_000);
1883
1884         // Restart Bob with the revoked state and provide the HTLC preimages he claimed.
1885         reload_node!(
1886                 nodes[1], anchors_config, bob_serialized, &[&bob_serialized_monitor_a, &bob_serialized_monitor_b],
1887                 bob_persister, bob_chain_monitor, bob_deserialized
1888         );
1889         for chan_id in [chan_a.2, chan_b.2].iter() {
1890                 let monitor = get_monitor!(nodes[1], chan_id);
1891                 for payment in [payment_a, payment_b, payment_c, payment_d].iter() {
1892                         monitor.provide_payment_preimage(
1893                                 &payment.1, &payment.0, &node_cfgs[1].tx_broadcaster,
1894                                 &LowerBoundedFeeEstimator::new(node_cfgs[1].fee_estimator), &nodes[1].logger
1895                         );
1896                 }
1897         }
1898
1899         // Bob force closes by restarting with the outdated state, prompting the ChannelMonitors to
1900         // broadcast the latest commitment transaction known to them, which in our case is the one with
1901         // the HTLCs still pending.
1902         nodes[1].node.timer_tick_occurred();
1903         check_added_monitors(&nodes[1], 2);
1904         check_closed_event!(&nodes[1], 2, ClosureReason::OutdatedChannelManager);
1905         let (revoked_commitment_a, revoked_commitment_b) = {
1906                 let txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1907                 assert_eq!(txn.len(), 2);
1908                 assert_eq!(txn[0].output.len(), 6); // 2 HTLC outputs + 1 to_self output + 1 to_remote output + 2 anchor outputs
1909                 assert_eq!(txn[1].output.len(), 6); // 2 HTLC outputs + 1 to_self output + 1 to_remote output + 2 anchor outputs
1910                 if txn[0].input[0].previous_output.txid == chan_a.3.txid() {
1911                         check_spends!(&txn[0], &chan_a.3);
1912                         check_spends!(&txn[1], &chan_b.3);
1913                         (txn[0].clone(), txn[1].clone())
1914                 } else {
1915                         check_spends!(&txn[1], &chan_a.3);
1916                         check_spends!(&txn[0], &chan_b.3);
1917                         (txn[1].clone(), txn[0].clone())
1918                 }
1919         };
1920
1921         // Bob should now receive two events to bump his revoked commitment transaction fees.
1922         assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1923         let events = nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events();
1924         assert_eq!(events.len(), 2);
1925         let anchor_tx = {
1926                 let secret_key = SecretKey::from_slice(&[1; 32]).unwrap();
1927                 let public_key = PublicKey::new(secret_key.public_key(&secp));
1928                 let fee_utxo_script = Script::new_v0_p2wpkh(&public_key.wpubkey_hash().unwrap());
1929                 let coinbase_tx = Transaction {
1930                         version: 2,
1931                         lock_time: PackedLockTime::ZERO,
1932                         input: vec![TxIn { ..Default::default() }],
1933                         output: vec![TxOut { // UTXO to attach fees to `anchor_tx`
1934                                 value: Amount::ONE_BTC.to_sat(),
1935                                 script_pubkey: fee_utxo_script.clone(),
1936                         }],
1937                 };
1938                 let mut anchor_tx = Transaction {
1939                         version: 2,
1940                         lock_time: PackedLockTime::ZERO,
1941                         input: vec![
1942                                 TxIn { // Fee input
1943                                         previous_output: bitcoin::OutPoint { txid: coinbase_tx.txid(), vout: 0 },
1944                                         ..Default::default()
1945                                 },
1946                         ],
1947                         output: vec![TxOut { // Fee input change
1948                                 value: coinbase_tx.output[0].value / 2 ,
1949                                 script_pubkey: Script::new_op_return(&[]),
1950                         }],
1951                 };
1952                 let mut signers = Vec::with_capacity(2);
1953                 for event in events {
1954                         match event {
1955                                 Event::BumpTransaction(BumpTransactionEvent::ChannelClose { anchor_descriptor, .. })  => {
1956                                         anchor_tx.input.push(TxIn {
1957                                                 previous_output: anchor_descriptor.outpoint,
1958                                                 ..Default::default()
1959                                         });
1960                                         let signer = nodes[1].keys_manager.derive_channel_keys(
1961                                                 anchor_descriptor.channel_value_satoshis, &anchor_descriptor.channel_keys_id,
1962                                         );
1963                                         signers.push(signer);
1964                                 },
1965                                 _ => panic!("Unexpected event"),
1966                         }
1967                 }
1968                 for (i, signer) in signers.into_iter().enumerate() {
1969                         let anchor_idx = i + 1;
1970                         let funding_sig = signer.sign_holder_anchor_input(&mut anchor_tx, anchor_idx, &secp).unwrap();
1971                         anchor_tx.input[anchor_idx].witness = chan_utils::build_anchor_input_witness(
1972                                 &signer.pubkeys().funding_pubkey, &funding_sig
1973                         );
1974                 }
1975                 let fee_utxo_sig = {
1976                         let witness_script = Script::new_p2pkh(&public_key.pubkey_hash());
1977                         let sighash = hash_to_message!(&SighashCache::new(&anchor_tx).segwit_signature_hash(
1978                                 0, &witness_script, coinbase_tx.output[0].value, EcdsaSighashType::All
1979                         ).unwrap()[..]);
1980                         let sig = sign(&secp, &sighash, &secret_key);
1981                         let mut sig = sig.serialize_der().to_vec();
1982                         sig.push(EcdsaSighashType::All as u8);
1983                         sig
1984                 };
1985                 anchor_tx.input[0].witness = Witness::from_vec(vec![fee_utxo_sig, public_key.to_bytes()]);
1986                 check_spends!(anchor_tx, coinbase_tx, revoked_commitment_a, revoked_commitment_b);
1987                 anchor_tx
1988         };
1989
1990         for node in &nodes {
1991                 mine_transactions(node, &[&revoked_commitment_a, &revoked_commitment_b, &anchor_tx]);
1992         }
1993         check_added_monitors!(&nodes[0], 2);
1994         check_closed_broadcast(&nodes[0], 2, true);
1995         check_closed_event!(&nodes[0], 2, ClosureReason::CommitmentTxConfirmed);
1996
1997         // Alice should detect the confirmed revoked commitments, and attempt to claim all of the
1998         // revoked outputs.
1999         {
2000                 let txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
2001                 assert_eq!(txn.len(), 2);
2002
2003                 let (revoked_claim_a, revoked_claim_b) = if txn[0].input[0].previous_output.txid == revoked_commitment_a.txid() {
2004                         (&txn[0], &txn[1])
2005                 } else {
2006                         (&txn[1], &txn[0])
2007                 };
2008
2009                 // TODO: to_self claim must be separate from HTLC claims
2010                 assert_eq!(revoked_claim_a.input.len(), 3); // Spends both HTLC outputs and to_self output
2011                 assert_eq!(revoked_claim_a.output.len(), 1);
2012                 check_spends!(revoked_claim_a, revoked_commitment_a);
2013                 assert_eq!(revoked_claim_b.input.len(), 3); // Spends both HTLC outputs and to_self output
2014                 assert_eq!(revoked_claim_b.output.len(), 1);
2015                 check_spends!(revoked_claim_b, revoked_commitment_b);
2016         }
2017
2018         // Since Bob was able to confirm his revoked commitment, he'll now try to claim the HTLCs
2019         // through the success path.
2020         assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
2021         let mut events = nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events();
2022         // Certain block `ConnectStyle`s cause an extra `ChannelClose` event to be emitted since the
2023         // best block is updated before the confirmed transactions are notified.
2024         match *nodes[1].connect_style.borrow() {
2025                 ConnectStyle::BestBlockFirst|ConnectStyle::BestBlockFirstReorgsOnlyTip|ConnectStyle::BestBlockFirstSkippingBlocks => {
2026                         assert_eq!(events.len(), 4);
2027                         if let Event::BumpTransaction(BumpTransactionEvent::ChannelClose { .. }) = events.remove(0) {}
2028                         else { panic!("unexpected event"); }
2029                         if let Event::BumpTransaction(BumpTransactionEvent::ChannelClose { .. }) = events.remove(1) {}
2030                         else { panic!("unexpected event"); }
2031
2032                 },
2033                 _ => assert_eq!(events.len(), 2),
2034         };
2035         let htlc_tx = {
2036                 let secret_key = SecretKey::from_slice(&[1; 32]).unwrap();
2037                 let public_key = PublicKey::new(secret_key.public_key(&secp));
2038                 let fee_utxo_script = Script::new_v0_p2wpkh(&public_key.wpubkey_hash().unwrap());
2039                 let coinbase_tx = Transaction {
2040                         version: 2,
2041                         lock_time: PackedLockTime::ZERO,
2042                         input: vec![TxIn { ..Default::default() }],
2043                         output: vec![TxOut { // UTXO to attach fees to `htlc_tx`
2044                                 value: Amount::ONE_BTC.to_sat(),
2045                                 script_pubkey: fee_utxo_script.clone(),
2046                         }],
2047                 };
2048                 let mut htlc_tx = Transaction {
2049                         version: 2,
2050                         lock_time: PackedLockTime::ZERO,
2051                         input: vec![TxIn { // Fee input
2052                                 previous_output: bitcoin::OutPoint { txid: coinbase_tx.txid(), vout: 0 },
2053                                 ..Default::default()
2054                         }],
2055                         output: vec![TxOut { // Fee input change
2056                                 value: coinbase_tx.output[0].value / 2 ,
2057                                 script_pubkey: Script::new_op_return(&[]),
2058                         }],
2059                 };
2060                 let mut descriptors = Vec::with_capacity(4);
2061                 for event in events {
2062                         if let Event::BumpTransaction(BumpTransactionEvent::HTLCResolution { mut htlc_descriptors, tx_lock_time, .. }) = event {
2063                                 assert_eq!(htlc_descriptors.len(), 2);
2064                                 for htlc_descriptor in &htlc_descriptors {
2065                                         assert!(!htlc_descriptor.htlc.offered);
2066                                         let signer = nodes[1].keys_manager.derive_channel_keys(
2067                                                 htlc_descriptor.channel_value_satoshis, &htlc_descriptor.channel_keys_id
2068                                         );
2069                                         let per_commitment_point = signer.get_per_commitment_point(htlc_descriptor.per_commitment_number, &secp);
2070                                         htlc_tx.input.push(htlc_descriptor.unsigned_tx_input());
2071                                         htlc_tx.output.push(htlc_descriptor.tx_output(&per_commitment_point, &secp));
2072                                 }
2073                                 descriptors.append(&mut htlc_descriptors);
2074                                 htlc_tx.lock_time = tx_lock_time;
2075                         } else {
2076                                 panic!("Unexpected event");
2077                         }
2078                 }
2079                 for (idx, htlc_descriptor) in descriptors.into_iter().enumerate() {
2080                         let htlc_input_idx = idx + 1;
2081                         let signer = nodes[1].keys_manager.derive_channel_keys(
2082                                 htlc_descriptor.channel_value_satoshis, &htlc_descriptor.channel_keys_id
2083                         );
2084                         let our_sig = signer.sign_holder_htlc_transaction(&htlc_tx, htlc_input_idx, &htlc_descriptor, &secp).unwrap();
2085                         let per_commitment_point = signer.get_per_commitment_point(htlc_descriptor.per_commitment_number, &secp);
2086                         let witness_script = htlc_descriptor.witness_script(&per_commitment_point, &secp);
2087                         htlc_tx.input[htlc_input_idx].witness = htlc_descriptor.tx_input_witness(&our_sig, &witness_script);
2088                 }
2089                 let fee_utxo_sig = {
2090                         let witness_script = Script::new_p2pkh(&public_key.pubkey_hash());
2091                         let sighash = hash_to_message!(&SighashCache::new(&htlc_tx).segwit_signature_hash(
2092                                 0, &witness_script, coinbase_tx.output[0].value, EcdsaSighashType::All
2093                         ).unwrap()[..]);
2094                         let sig = sign(&secp, &sighash, &secret_key);
2095                         let mut sig = sig.serialize_der().to_vec();
2096                         sig.push(EcdsaSighashType::All as u8);
2097                         sig
2098                 };
2099                 htlc_tx.input[0].witness = Witness::from_vec(vec![fee_utxo_sig, public_key.to_bytes()]);
2100                 check_spends!(htlc_tx, coinbase_tx, revoked_commitment_a, revoked_commitment_b);
2101                 htlc_tx
2102         };
2103
2104         for node in &nodes {
2105                 mine_transaction(node, &htlc_tx);
2106         }
2107
2108         // Alice should see that Bob is trying to claim to HTLCs, so she should now try to claim them at
2109         // the second level instead.
2110         let revoked_claims = {
2111                 let txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
2112                 assert_eq!(txn.len(), 4);
2113
2114                 let revoked_to_self_claim_a = txn.iter().find(|tx|
2115                         tx.input.len() == 1 &&
2116                         tx.output.len() == 1 &&
2117                         tx.input[0].previous_output.txid == revoked_commitment_a.txid()
2118                 ).unwrap();
2119                 check_spends!(revoked_to_self_claim_a, revoked_commitment_a);
2120
2121                 let revoked_to_self_claim_b = txn.iter().find(|tx|
2122                         tx.input.len() == 1 &&
2123                         tx.output.len() == 1 &&
2124                         tx.input[0].previous_output.txid == revoked_commitment_b.txid()
2125                 ).unwrap();
2126                 check_spends!(revoked_to_self_claim_b, revoked_commitment_b);
2127
2128                 let revoked_htlc_claims = txn.iter().filter(|tx|
2129                         tx.input.len() == 2 &&
2130                         tx.output.len() == 1 &&
2131                         tx.input[0].previous_output.txid == htlc_tx.txid()
2132                 ).collect::<Vec<_>>();
2133                 assert_eq!(revoked_htlc_claims.len(), 2);
2134                 for revoked_htlc_claim in revoked_htlc_claims {
2135                         check_spends!(revoked_htlc_claim, htlc_tx);
2136                 }
2137
2138                 txn
2139         };
2140         for node in &nodes {
2141                 mine_transactions(node, &revoked_claims.iter().collect::<Vec<_>>());
2142         }
2143
2144
2145         // Connect one block to make sure the HTLC events are not yielded while ANTI_REORG_DELAY has not
2146         // been reached.
2147         connect_blocks(&nodes[0], 1);
2148         connect_blocks(&nodes[1], 1);
2149
2150         assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
2151         assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
2152
2153         // Connect the remaining blocks to reach ANTI_REORG_DELAY.
2154         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 2);
2155         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 2);
2156
2157         assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
2158         let spendable_output_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
2159         assert_eq!(spendable_output_events.len(), 4);
2160         for (idx, event) in spendable_output_events.iter().enumerate() {
2161                 if let Event::SpendableOutputs { outputs } = event {
2162                         assert_eq!(outputs.len(), 1);
2163                         let spend_tx = nodes[0].keys_manager.backing.spend_spendable_outputs(
2164                                 &[&outputs[0]], Vec::new(), Script::new_op_return(&[]), 253, &Secp256k1::new(),
2165                         ).unwrap();
2166                         check_spends!(spend_tx, revoked_claims[idx]);
2167                 } else {
2168                         panic!("unexpected event");
2169                 }
2170         }
2171
2172         assert!(nodes[0].node.list_channels().is_empty());
2173         assert!(nodes[1].node.list_channels().is_empty());
2174         assert!(nodes[0].chain_monitor.chain_monitor.get_claimable_balances(&[]).is_empty());
2175         // TODO: From Bob's PoV, he still thinks he can claim the outputs from his revoked commitment.
2176         // This needs to be fixed before we enable pruning `ChannelMonitor`s once they don't have any
2177         // balances to claim.
2178         //
2179         // The 6 claimable balances correspond to his `to_self` outputs and the 2 HTLC outputs in each
2180         // revoked commitment which Bob has the preimage for.
2181         assert_eq!(nodes[1].chain_monitor.chain_monitor.get_claimable_balances(&[]).len(), 6);
2182 }