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