Expect `pending_msg_events` to be in random peer order in tests
[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 use crate::chain::channelmonitor::{ANTI_REORG_DELAY, Balance};
13 use crate::chain::transaction::OutPoint;
14 use crate::chain::chaininterface::LowerBoundedFeeEstimator;
15 use crate::ln::channel;
16 use crate::ln::channelmanager::{self, BREAKDOWN_TIMEOUT, PaymentId};
17 use crate::ln::msgs::ChannelMessageHandler;
18 use crate::util::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination};
19
20 use bitcoin::blockdata::script::Builder;
21 use bitcoin::blockdata::opcodes;
22 use bitcoin::secp256k1::Secp256k1;
23 use bitcoin::Transaction;
24
25 use crate::prelude::*;
26
27 use crate::ln::functional_test_utils::*;
28
29 #[test]
30 fn chanmon_fail_from_stale_commitment() {
31         // If we forward an HTLC to our counterparty, but we force-closed the channel before our
32         // counterparty provides us an updated commitment transaction, we'll end up with a commitment
33         // transaction that does not contain the HTLC which we attempted to forward. In this case, we
34         // need to wait `ANTI_REORG_DELAY` blocks and then fail back the HTLC as there is no way for us
35         // to learn the preimage and the confirmed commitment transaction paid us the value of the
36         // HTLC.
37         //
38         // However, previously, we did not do this, ignoring the HTLC entirely.
39         //
40         // This could lead to channel closure if the sender we received the HTLC from decides to go on
41         // chain to get their HTLC back before it times out.
42         //
43         // Here, we check exactly this case, forwarding a payment from A, through B, to C, before B
44         // broadcasts its latest commitment transaction, which should result in it eventually failing
45         // the HTLC back off-chain to A.
46         let chanmon_cfgs = create_chanmon_cfgs(3);
47         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
48         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
49         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
50
51         create_announced_chan_between_nodes(&nodes, 0, 1, channelmanager::provided_init_features(), channelmanager::provided_init_features());
52         let (update_a, _, chan_id_2, _) = create_announced_chan_between_nodes(&nodes, 1, 2, channelmanager::provided_init_features(), channelmanager::provided_init_features());
53
54         let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 1_000_000);
55         nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap();
56         check_added_monitors!(nodes[0], 1);
57
58         let bs_txn = get_local_commitment_txn!(nodes[1], chan_id_2);
59
60         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
61         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
62         commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
63
64         expect_pending_htlcs_forwardable!(nodes[1]);
65         get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
66         check_added_monitors!(nodes[1], 1);
67
68         // Don't bother delivering the new HTLC add/commits, instead confirming the pre-HTLC commitment
69         // transaction for nodes[1].
70         mine_transaction(&nodes[1], &bs_txn[0]);
71         check_added_monitors!(nodes[1], 1);
72         check_closed_broadcast!(nodes[1], true);
73         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
74         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
75
76         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
77         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 }]);
78         check_added_monitors!(nodes[1], 1);
79         let fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
80
81         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates.update_fail_htlcs[0]);
82         commitment_signed_dance!(nodes[0], nodes[1], fail_updates.commitment_signed, true, true);
83         expect_payment_failed_with_update!(nodes[0], payment_hash, false, update_a.contents.short_channel_id, true);
84 }
85
86 fn test_spendable_output<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, spendable_tx: &Transaction) {
87         let mut spendable = node.chain_monitor.chain_monitor.get_and_clear_pending_events();
88         assert_eq!(spendable.len(), 1);
89         if let Event::SpendableOutputs { outputs } = spendable.pop().unwrap() {
90                 assert_eq!(outputs.len(), 1);
91                 let spend_tx = node.keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
92                         Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
93                 check_spends!(spend_tx, spendable_tx);
94         } else { panic!(); }
95 }
96
97 #[test]
98 fn revoked_output_htlc_resolution_timing() {
99         // Tests that HTLCs which were present in a broadcasted remote revoked commitment transaction
100         // are resolved only after a spend of the HTLC output reaches six confirmations. Preivously
101         // they would resolve after the revoked commitment transaction itself reaches six
102         // confirmations.
103         let chanmon_cfgs = create_chanmon_cfgs(2);
104         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
105         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
106         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
107
108         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000, channelmanager::provided_init_features(), channelmanager::provided_init_features());
109
110         let payment_hash_1 = route_payment(&nodes[1], &[&nodes[0]], 1_000_000).1;
111
112         // Get a commitment transaction which contains the HTLC we care about, but which we'll revoke
113         // before forwarding.
114         let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan.2);
115         assert_eq!(revoked_local_txn.len(), 1);
116
117         // Route a dust payment to revoke the above commitment transaction
118         route_payment(&nodes[0], &[&nodes[1]], 1_000);
119
120         // Confirm the revoked commitment transaction, closing the channel.
121         mine_transaction(&nodes[1], &revoked_local_txn[0]);
122         check_added_monitors!(nodes[1], 1);
123         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
124         check_closed_broadcast!(nodes[1], true);
125
126         let bs_spend_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
127         assert_eq!(bs_spend_txn.len(), 1);
128         check_spends!(bs_spend_txn[0], revoked_local_txn[0]);
129
130         // After the commitment transaction confirms, we should still wait on the HTLC spend
131         // transaction to confirm before resolving the HTLC.
132         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
133         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
134         assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
135
136         // Spend the HTLC output, generating a HTLC failure event after ANTI_REORG_DELAY confirmations.
137         mine_transaction(&nodes[1], &bs_spend_txn[0]);
138         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
139         assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
140
141         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
142         expect_payment_failed!(nodes[1], payment_hash_1, false);
143 }
144
145 #[test]
146 fn chanmon_claim_value_coop_close() {
147         // Tests `get_claimable_balances` returns the correct values across a simple cooperative claim.
148         // Specifically, this tests that the channel non-HTLC balances show up in
149         // `get_claimable_balances` until the cooperative claims have confirmed and generated a
150         // `SpendableOutputs` event, and no longer.
151         let chanmon_cfgs = create_chanmon_cfgs(2);
152         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
153         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
154         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
155
156         let (_, _, chan_id, funding_tx) =
157                 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 1_000_000, channelmanager::provided_init_features(), channelmanager::provided_init_features());
158         let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
159         assert_eq!(funding_outpoint.to_channel_id(), chan_id);
160
161         let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
162         let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
163
164         assert_eq!(vec![Balance::ClaimableOnChannelClose {
165                         claimable_amount_satoshis: 1_000_000 - 1_000 - chan_feerate * channel::commitment_tx_base_weight(opt_anchors) / 1000
166                 }],
167                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
168         assert_eq!(vec![Balance::ClaimableOnChannelClose { claimable_amount_satoshis: 1_000, }],
169                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
170
171         nodes[0].node.close_channel(&chan_id, &nodes[1].node.get_our_node_id()).unwrap();
172         let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
173         nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &channelmanager::provided_init_features(), &node_0_shutdown);
174         let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
175         nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &channelmanager::provided_init_features(), &node_1_shutdown);
176
177         let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
178         nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
179         let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
180         nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
181         let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
182         nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
183         let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
184         assert!(node_1_none.is_none());
185
186         let shutdown_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
187         assert_eq!(shutdown_tx, nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0));
188         assert_eq!(shutdown_tx.len(), 1);
189
190         mine_transaction(&nodes[0], &shutdown_tx[0]);
191         mine_transaction(&nodes[1], &shutdown_tx[0]);
192
193         assert!(nodes[0].node.list_channels().is_empty());
194         assert!(nodes[1].node.list_channels().is_empty());
195
196         assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
197         assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
198
199         assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
200                         claimable_amount_satoshis: 1_000_000 - 1_000 - chan_feerate * channel::commitment_tx_base_weight(opt_anchors) / 1000,
201                         confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
202                 }],
203                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
204         assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
205                         claimable_amount_satoshis: 1000,
206                         confirmation_height: nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1,
207                 }],
208                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
209
210         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
211         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
212
213         assert_eq!(Vec::<Balance>::new(),
214                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
215         assert_eq!(Vec::<Balance>::new(),
216                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
217
218         test_spendable_output(&nodes[0], &shutdown_tx[0]);
219         test_spendable_output(&nodes[1], &shutdown_tx[0]);
220
221         check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure);
222         check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure);
223 }
224
225 fn sorted_vec<T: Ord>(mut v: Vec<T>) -> Vec<T> {
226         v.sort_unstable();
227         v
228 }
229
230 /// Asserts that `a` and `b` are close, but maybe off by up to 5.
231 /// This is useful when checking fees and weights on transactions as things may vary by a few based
232 /// on signature size and signature size estimation being non-exact.
233 fn fuzzy_assert_eq<V: core::convert::TryInto<u64>>(a: V, b: V) {
234         let a_u64 = a.try_into().map_err(|_| ()).unwrap();
235         let b_u64 = b.try_into().map_err(|_| ()).unwrap();
236         eprintln!("Checking {} and {} for fuzzy equality", a_u64, b_u64);
237         assert!(a_u64 >= b_u64 - 5);
238         assert!(b_u64 >= a_u64 - 5);
239 }
240
241 fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
242         // Tests `get_claimable_balances` with an HTLC across a force-close.
243         // We build a channel with an HTLC pending, then force close the channel and check that the
244         // `get_claimable_balances` return value is correct as transactions confirm on-chain.
245         let mut chanmon_cfgs = create_chanmon_cfgs(2);
246         if prev_commitment_tx {
247                 // We broadcast a second-to-latest commitment transaction, without providing the revocation
248                 // secret to the counterparty. However, because we always immediately take the revocation
249                 // secret from the keys_manager, we would panic at broadcast as we're trying to sign a
250                 // transaction which, from the point of view of our keys_manager, is revoked.
251                 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
252         }
253         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
254         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
255         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
256
257         let (_, _, chan_id, funding_tx) =
258                 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 1_000_000, channelmanager::provided_init_features(), channelmanager::provided_init_features());
259         let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
260         assert_eq!(funding_outpoint.to_channel_id(), chan_id);
261
262         // This HTLC is immediately claimed, giving node B the preimage
263         let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 3_000_000);
264         // This HTLC is allowed to time out, letting A claim it. However, in order to test claimable
265         // balances more fully we also give B the preimage for this HTLC.
266         let (timeout_payment_preimage, timeout_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 4_000_000);
267         // This HTLC will be dust, and not be claimable at all:
268         let (dust_payment_preimage, dust_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 3_000);
269
270         let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
271
272         let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
273         let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
274
275         let remote_txn = get_local_commitment_txn!(nodes[1], chan_id);
276         // Before B receives the payment preimage, it only suggests the push_msat value of 1_000 sats
277         // as claimable. A lists both its to-self balance and the (possibly-claimable) HTLCs.
278         assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
279                         claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
280                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
281                 }, Balance::MaybeTimeoutClaimableHTLC {
282                         claimable_amount_satoshis: 3_000,
283                         claimable_height: htlc_cltv_timeout,
284                 }, Balance::MaybeTimeoutClaimableHTLC {
285                         claimable_amount_satoshis: 4_000,
286                         claimable_height: htlc_cltv_timeout,
287                 }]),
288                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
289         assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
290                         claimable_amount_satoshis: 1_000,
291                 }, Balance::MaybePreimageClaimableHTLC {
292                         claimable_amount_satoshis: 3_000,
293                         expiry_height: htlc_cltv_timeout,
294                 }, Balance::MaybePreimageClaimableHTLC {
295                         claimable_amount_satoshis: 4_000,
296                         expiry_height: htlc_cltv_timeout,
297                 }]),
298                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
299
300         nodes[1].node.claim_funds(payment_preimage);
301         check_added_monitors!(nodes[1], 1);
302         expect_payment_claimed!(nodes[1], payment_hash, 3_000_000);
303
304         let b_htlc_msgs = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
305         // We claim the dust payment here as well, but it won't impact our claimable balances as its
306         // dust and thus doesn't appear on chain at all.
307         nodes[1].node.claim_funds(dust_payment_preimage);
308         check_added_monitors!(nodes[1], 1);
309         expect_payment_claimed!(nodes[1], dust_payment_hash, 3_000);
310
311         nodes[1].node.claim_funds(timeout_payment_preimage);
312         check_added_monitors!(nodes[1], 1);
313         expect_payment_claimed!(nodes[1], timeout_payment_hash, 4_000_000);
314
315         if prev_commitment_tx {
316                 // To build a previous commitment transaction, deliver one round of commitment messages.
317                 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &b_htlc_msgs.update_fulfill_htlcs[0]);
318                 expect_payment_sent_without_paths!(nodes[0], payment_preimage);
319                 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &b_htlc_msgs.commitment_signed);
320                 check_added_monitors!(nodes[0], 1);
321                 let (as_raa, as_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
322                 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
323                 let _htlc_updates = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
324                 check_added_monitors!(nodes[1], 1);
325                 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs);
326                 let _bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
327                 check_added_monitors!(nodes[1], 1);
328         }
329
330         // Once B has received the payment preimage, it includes the value of the HTLC in its
331         // "claimable if you were to close the channel" balance.
332         let mut a_expected_balances = vec![Balance::ClaimableOnChannelClose {
333                         claimable_amount_satoshis: 1_000_000 - // Channel funding value in satoshis
334                                 4_000 - // The to-be-failed HTLC value in satoshis
335                                 3_000 - // The claimed HTLC value in satoshis
336                                 1_000 - // The push_msat value in satoshis
337                                 3 - // The dust HTLC value in satoshis
338                                 // The commitment transaction fee with two HTLC outputs:
339                                 chan_feerate * (channel::commitment_tx_base_weight(opt_anchors) +
340                                                                 if prev_commitment_tx { 1 } else { 2 } *
341                                                                 channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
342                 }, Balance::MaybeTimeoutClaimableHTLC {
343                         claimable_amount_satoshis: 4_000,
344                         claimable_height: htlc_cltv_timeout,
345                 }];
346         if !prev_commitment_tx {
347                 a_expected_balances.push(Balance::MaybeTimeoutClaimableHTLC {
348                         claimable_amount_satoshis: 3_000,
349                         claimable_height: htlc_cltv_timeout,
350                 });
351         }
352         assert_eq!(sorted_vec(a_expected_balances),
353                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
354         assert_eq!(vec![Balance::ClaimableOnChannelClose {
355                         claimable_amount_satoshis: 1_000 + 3_000 + 4_000,
356                 }],
357                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
358
359         // Broadcast the closing transaction (which has both pending HTLCs in it) and get B's
360         // broadcasted HTLC claim transaction with preimage.
361         let node_b_commitment_claimable = nodes[1].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
362         mine_transaction(&nodes[0], &remote_txn[0]);
363         mine_transaction(&nodes[1], &remote_txn[0]);
364
365         let b_broadcast_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
366         assert_eq!(b_broadcast_txn.len(), 2);
367         // b_broadcast_txn should spend the HTLCs output of the commitment tx for 3_000 and 4_000 sats
368         check_spends!(b_broadcast_txn[0], remote_txn[0]);
369         check_spends!(b_broadcast_txn[1], remote_txn[0]);
370         assert_eq!(b_broadcast_txn[0].input.len(), 1);
371         assert_eq!(b_broadcast_txn[1].input.len(), 1);
372         assert_eq!(remote_txn[0].output[b_broadcast_txn[0].input[0].previous_output.vout as usize].value, 3_000);
373         assert_eq!(remote_txn[0].output[b_broadcast_txn[1].input[0].previous_output.vout as usize].value, 4_000);
374
375         assert!(nodes[0].node.list_channels().is_empty());
376         check_closed_broadcast!(nodes[0], true);
377         check_added_monitors!(nodes[0], 1);
378         check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
379         assert!(nodes[1].node.list_channels().is_empty());
380         check_closed_broadcast!(nodes[1], true);
381         check_added_monitors!(nodes[1], 1);
382         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
383         assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
384         assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
385
386         // Once the commitment transaction confirms, we will wait until ANTI_REORG_DELAY until we
387         // generate any `SpendableOutputs` events. Thus, the same balances will still be listed
388         // available in `get_claimable_balances`. However, both will swap from `ClaimableOnClose` to
389         // other Balance variants, as close has already happened.
390         assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
391         assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
392
393         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
394                         claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
395                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
396                         confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
397                 }, Balance::MaybeTimeoutClaimableHTLC {
398                         claimable_amount_satoshis: 3_000,
399                         claimable_height: htlc_cltv_timeout,
400                 }, Balance::MaybeTimeoutClaimableHTLC {
401                         claimable_amount_satoshis: 4_000,
402                         claimable_height: htlc_cltv_timeout,
403                 }]),
404                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
405         // The main non-HTLC balance is just awaiting confirmations, but the claimable height is the
406         // CSV delay, not ANTI_REORG_DELAY.
407         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
408                         claimable_amount_satoshis: 1_000,
409                         confirmation_height: node_b_commitment_claimable,
410                 },
411                 // Both HTLC balances are "contentious" as our counterparty could claim them if we wait too
412                 // long.
413                 Balance::ContentiousClaimable {
414                         claimable_amount_satoshis: 3_000,
415                         timeout_height: htlc_cltv_timeout,
416                 }, Balance::ContentiousClaimable {
417                         claimable_amount_satoshis: 4_000,
418                         timeout_height: htlc_cltv_timeout,
419                 }]),
420                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
421
422         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
423         expect_payment_failed!(nodes[0], dust_payment_hash, false);
424         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
425
426         // After ANTI_REORG_DELAY, A will consider its balance fully spendable and generate a
427         // `SpendableOutputs` event. However, B still has to wait for the CSV delay.
428         assert_eq!(sorted_vec(vec![Balance::MaybeTimeoutClaimableHTLC {
429                         claimable_amount_satoshis: 3_000,
430                         claimable_height: htlc_cltv_timeout,
431                 }, Balance::MaybeTimeoutClaimableHTLC {
432                         claimable_amount_satoshis: 4_000,
433                         claimable_height: htlc_cltv_timeout,
434                 }]),
435                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
436         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
437                         claimable_amount_satoshis: 1_000,
438                         confirmation_height: node_b_commitment_claimable,
439                 }, Balance::ContentiousClaimable {
440                         claimable_amount_satoshis: 3_000,
441                         timeout_height: htlc_cltv_timeout,
442                 }, Balance::ContentiousClaimable {
443                         claimable_amount_satoshis: 4_000,
444                         timeout_height: htlc_cltv_timeout,
445                 }]),
446                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
447
448         test_spendable_output(&nodes[0], &remote_txn[0]);
449         assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
450
451         // After broadcasting the HTLC claim transaction, node A will still consider the HTLC
452         // possibly-claimable up to ANTI_REORG_DELAY, at which point it will drop it.
453         mine_transaction(&nodes[0], &b_broadcast_txn[0]);
454         if prev_commitment_tx {
455                 expect_payment_path_successful!(nodes[0]);
456         } else {
457                 expect_payment_sent!(nodes[0], payment_preimage);
458         }
459         assert_eq!(sorted_vec(vec![Balance::MaybeTimeoutClaimableHTLC {
460                         claimable_amount_satoshis: 3_000,
461                         claimable_height: htlc_cltv_timeout,
462                 }, Balance::MaybeTimeoutClaimableHTLC {
463                         claimable_amount_satoshis: 4_000,
464                         claimable_height: htlc_cltv_timeout,
465                 }]),
466                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
467         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
468         assert_eq!(vec![Balance::MaybeTimeoutClaimableHTLC {
469                         claimable_amount_satoshis: 4_000,
470                         claimable_height: htlc_cltv_timeout,
471                 }],
472                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
473
474         // When the HTLC timeout output is spendable in the next block, A should broadcast it
475         connect_blocks(&nodes[0], htlc_cltv_timeout - nodes[0].best_block_info().1 - 1);
476         let a_broadcast_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
477         assert_eq!(a_broadcast_txn.len(), 2);
478         assert_eq!(a_broadcast_txn[0].input.len(), 1);
479         check_spends!(a_broadcast_txn[0], remote_txn[0]);
480         assert_eq!(a_broadcast_txn[1].input.len(), 1);
481         check_spends!(a_broadcast_txn[1], remote_txn[0]);
482         assert_ne!(a_broadcast_txn[0].input[0].previous_output.vout,
483                    a_broadcast_txn[1].input[0].previous_output.vout);
484         // a_broadcast_txn [0] and [1] should spend the HTLC outputs of the commitment tx
485         assert_eq!(remote_txn[0].output[a_broadcast_txn[0].input[0].previous_output.vout as usize].value, 3_000);
486         assert_eq!(remote_txn[0].output[a_broadcast_txn[1].input[0].previous_output.vout as usize].value, 4_000);
487
488         // Once the HTLC-Timeout transaction confirms, A will no longer consider the HTLC
489         // "MaybeClaimable", but instead move it to "AwaitingConfirmations".
490         mine_transaction(&nodes[0], &a_broadcast_txn[1]);
491         assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
492         assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
493                         claimable_amount_satoshis: 4_000,
494                         confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
495                 }],
496                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
497         // After ANTI_REORG_DELAY, A will generate a SpendableOutputs event and drop the claimable
498         // balance entry.
499         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
500         assert_eq!(Vec::<Balance>::new(),
501                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
502         expect_payment_failed!(nodes[0], timeout_payment_hash, false);
503
504         test_spendable_output(&nodes[0], &a_broadcast_txn[1]);
505
506         // Node B will no longer consider the HTLC "contentious" after the HTLC claim transaction
507         // confirms, and consider it simply "awaiting confirmations". Note that it has to wait for the
508         // standard revocable transaction CSV delay before receiving a `SpendableOutputs`.
509         let node_b_htlc_claimable = nodes[1].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
510         mine_transaction(&nodes[1], &b_broadcast_txn[0]);
511
512         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
513                         claimable_amount_satoshis: 1_000,
514                         confirmation_height: node_b_commitment_claimable,
515                 }, Balance::ClaimableAwaitingConfirmations {
516                         claimable_amount_satoshis: 3_000,
517                         confirmation_height: node_b_htlc_claimable,
518                 }, Balance::ContentiousClaimable {
519                         claimable_amount_satoshis: 4_000,
520                         timeout_height: htlc_cltv_timeout,
521                 }]),
522                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
523
524         // After reaching the commitment output CSV, we'll get a SpendableOutputs event for it and have
525         // only the HTLCs claimable on node B.
526         connect_blocks(&nodes[1], node_b_commitment_claimable - nodes[1].best_block_info().1);
527         test_spendable_output(&nodes[1], &remote_txn[0]);
528
529         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
530                         claimable_amount_satoshis: 3_000,
531                         confirmation_height: node_b_htlc_claimable,
532                 }, Balance::ContentiousClaimable {
533                         claimable_amount_satoshis: 4_000,
534                         timeout_height: htlc_cltv_timeout,
535                 }]),
536                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
537
538         // After reaching the claimed HTLC output CSV, we'll get a SpendableOutptus event for it and
539         // have only one HTLC output left spendable.
540         connect_blocks(&nodes[1], node_b_htlc_claimable - nodes[1].best_block_info().1);
541         test_spendable_output(&nodes[1], &b_broadcast_txn[0]);
542
543         assert_eq!(vec![Balance::ContentiousClaimable {
544                         claimable_amount_satoshis: 4_000,
545                         timeout_height: htlc_cltv_timeout,
546                 }],
547                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
548
549         // Finally, mine the HTLC timeout transaction that A broadcasted (even though B should be able
550         // to claim this HTLC with the preimage it knows!). It will remain listed as a claimable HTLC
551         // until ANTI_REORG_DELAY confirmations on the spend.
552         mine_transaction(&nodes[1], &a_broadcast_txn[1]);
553         assert_eq!(vec![Balance::ContentiousClaimable {
554                         claimable_amount_satoshis: 4_000,
555                         timeout_height: htlc_cltv_timeout,
556                 }],
557                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
558         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
559         assert_eq!(Vec::<Balance>::new(),
560                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
561
562         // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
563         // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
564         // monitor events or claimable balances.
565         for node in nodes.iter() {
566                 connect_blocks(node, 6);
567                 connect_blocks(node, 6);
568                 assert!(node.chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
569                 assert!(node.chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
570         }
571 }
572
573 #[test]
574 fn test_claim_value_force_close() {
575         do_test_claim_value_force_close(true);
576         do_test_claim_value_force_close(false);
577 }
578
579 #[test]
580 fn test_balances_on_local_commitment_htlcs() {
581         // Previously, when handling the broadcast of a local commitment transactions (with associated
582         // CSV delays prior to spendability), we incorrectly handled the CSV delays on HTLC
583         // transactions. This caused us to miss spendable outputs for HTLCs which were awaiting a CSV
584         // delay prior to spendability.
585         //
586         // Further, because of this, we could hit an assertion as `get_claimable_balances` asserted
587         // that HTLCs were resolved after the funding spend was resolved, which was not true if the
588         // HTLC did not have a CSV delay attached (due to the above bug or due to it being an HTLC
589         // claim by our counterparty).
590         let chanmon_cfgs = create_chanmon_cfgs(2);
591         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
592         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
593         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
594
595         // Create a single channel with two pending HTLCs from nodes[0] to nodes[1], one which nodes[1]
596         // knows the preimage for, one which it does not.
597         let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features());
598         let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
599
600         let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 10_000_000);
601         let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
602         nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap();
603         check_added_monitors!(nodes[0], 1);
604
605         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
606         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
607         commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
608
609         expect_pending_htlcs_forwardable!(nodes[1]);
610         expect_payment_claimable!(nodes[1], payment_hash, payment_secret, 10_000_000);
611
612         let (route_2, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 20_000_000);
613         nodes[0].node.send_payment(&route_2, payment_hash_2, &Some(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
614         check_added_monitors!(nodes[0], 1);
615
616         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
617         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
618         commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
619
620         expect_pending_htlcs_forwardable!(nodes[1]);
621         expect_payment_claimable!(nodes[1], payment_hash_2, payment_secret_2, 20_000_000);
622         nodes[1].node.claim_funds(payment_preimage_2);
623         get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
624         check_added_monitors!(nodes[1], 1);
625         expect_payment_claimed!(nodes[1], payment_hash_2, 20_000_000);
626
627         let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
628         let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
629
630         // Get nodes[0]'s commitment transaction and HTLC-Timeout transactions
631         let as_txn = get_local_commitment_txn!(nodes[0], chan_id);
632         assert_eq!(as_txn.len(), 3);
633         check_spends!(as_txn[1], as_txn[0]);
634         check_spends!(as_txn[2], as_txn[0]);
635         check_spends!(as_txn[0], funding_tx);
636
637         // First confirm the commitment transaction on nodes[0], which should leave us with three
638         // claimable balances.
639         let node_a_commitment_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
640         mine_transaction(&nodes[0], &as_txn[0]);
641         check_added_monitors!(nodes[0], 1);
642         check_closed_broadcast!(nodes[0], true);
643         check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
644
645         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
646                         claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
647                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
648                         confirmation_height: node_a_commitment_claimable,
649                 }, Balance::MaybeTimeoutClaimableHTLC {
650                         claimable_amount_satoshis: 10_000,
651                         claimable_height: htlc_cltv_timeout,
652                 }, Balance::MaybeTimeoutClaimableHTLC {
653                         claimable_amount_satoshis: 20_000,
654                         claimable_height: htlc_cltv_timeout,
655                 }]),
656                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
657
658         // Get nodes[1]'s HTLC claim tx for the second HTLC
659         mine_transaction(&nodes[1], &as_txn[0]);
660         check_added_monitors!(nodes[1], 1);
661         check_closed_broadcast!(nodes[1], true);
662         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
663         let bs_htlc_claim_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
664         assert_eq!(bs_htlc_claim_txn.len(), 1);
665         check_spends!(bs_htlc_claim_txn[0], as_txn[0]);
666
667         // Connect blocks until the HTLCs expire, allowing us to (validly) broadcast the HTLC-Timeout
668         // transaction.
669         connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1);
670         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
671                         claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
672                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
673                         confirmation_height: node_a_commitment_claimable,
674                 }, Balance::MaybeTimeoutClaimableHTLC {
675                         claimable_amount_satoshis: 10_000,
676                         claimable_height: htlc_cltv_timeout,
677                 }, Balance::MaybeTimeoutClaimableHTLC {
678                         claimable_amount_satoshis: 20_000,
679                         claimable_height: htlc_cltv_timeout,
680                 }]),
681                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
682         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
683
684         // Now confirm nodes[0]'s HTLC-Timeout transaction, which changes the claimable balance to an
685         // "awaiting confirmations" one.
686         let node_a_htlc_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
687         mine_transaction(&nodes[0], &as_txn[1]);
688         // Note that prior to the fix in the commit which introduced this test, this (and the next
689         // balance) check failed. With this check removed, the code panicked in the `connect_blocks`
690         // call, as described, two hunks down.
691         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
692                         claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
693                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
694                         confirmation_height: node_a_commitment_claimable,
695                 }, Balance::ClaimableAwaitingConfirmations {
696                         claimable_amount_satoshis: 10_000,
697                         confirmation_height: node_a_htlc_claimable,
698                 }, Balance::MaybeTimeoutClaimableHTLC {
699                         claimable_amount_satoshis: 20_000,
700                         claimable_height: htlc_cltv_timeout,
701                 }]),
702                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
703
704         // Now confirm nodes[1]'s HTLC claim, giving nodes[0] the preimage. Note that the "maybe
705         // claimable" balance remains until we see ANTI_REORG_DELAY blocks.
706         mine_transaction(&nodes[0], &bs_htlc_claim_txn[0]);
707         expect_payment_sent!(nodes[0], payment_preimage_2);
708         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
709                         claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
710                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
711                         confirmation_height: node_a_commitment_claimable,
712                 }, Balance::ClaimableAwaitingConfirmations {
713                         claimable_amount_satoshis: 10_000,
714                         confirmation_height: node_a_htlc_claimable,
715                 }, Balance::MaybeTimeoutClaimableHTLC {
716                         claimable_amount_satoshis: 20_000,
717                         claimable_height: htlc_cltv_timeout,
718                 }]),
719                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
720
721         // Finally make the HTLC transactions have ANTI_REORG_DELAY blocks. This call previously
722         // panicked as described in the test introduction. This will remove the "maybe claimable"
723         // spendable output as nodes[1] has fully claimed the second HTLC.
724         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
725         expect_payment_failed!(nodes[0], payment_hash, false);
726
727         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
728                         claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
729                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
730                         confirmation_height: node_a_commitment_claimable,
731                 }, Balance::ClaimableAwaitingConfirmations {
732                         claimable_amount_satoshis: 10_000,
733                         confirmation_height: node_a_htlc_claimable,
734                 }]),
735                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
736
737         // Connect blocks until the commitment transaction's CSV expires, providing us the relevant
738         // `SpendableOutputs` event and removing the claimable balance entry.
739         connect_blocks(&nodes[0], node_a_commitment_claimable - nodes[0].best_block_info().1);
740         assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
741                         claimable_amount_satoshis: 10_000,
742                         confirmation_height: node_a_htlc_claimable,
743                 }],
744                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
745         test_spendable_output(&nodes[0], &as_txn[0]);
746
747         // Connect blocks until the HTLC-Timeout's CSV expires, providing us the relevant
748         // `SpendableOutputs` event and removing the claimable balance entry.
749         connect_blocks(&nodes[0], node_a_htlc_claimable - nodes[0].best_block_info().1);
750         assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
751         test_spendable_output(&nodes[0], &as_txn[1]);
752
753         // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
754         // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
755         // monitor events or claimable balances.
756         connect_blocks(&nodes[0], 6);
757         connect_blocks(&nodes[0], 6);
758         assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
759         assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
760 }
761
762 #[test]
763 fn test_no_preimage_inbound_htlc_balances() {
764         // Tests that MaybePreimageClaimableHTLC are generated for inbound HTLCs for which we do not
765         // have a preimage.
766         let chanmon_cfgs = create_chanmon_cfgs(2);
767         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
768         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
769         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
770
771         let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000, channelmanager::provided_init_features(), channelmanager::provided_init_features());
772         let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
773
774         // Send two HTLCs, one from A to B, and one from B to A.
775         let to_b_failed_payment_hash = route_payment(&nodes[0], &[&nodes[1]], 10_000_000).1;
776         let to_a_failed_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 20_000_000).1;
777         let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
778
779         let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
780         let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
781
782         // Both A and B will have an HTLC that's claimable on timeout and one that's claimable if they
783         // receive the preimage. These will remain the same through the channel closure and until the
784         // HTLC output is spent.
785
786         assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
787                         claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
788                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
789                 }, Balance::MaybePreimageClaimableHTLC {
790                         claimable_amount_satoshis: 20_000,
791                         expiry_height: htlc_cltv_timeout,
792                 }, Balance::MaybeTimeoutClaimableHTLC {
793                         claimable_amount_satoshis: 10_000,
794                         claimable_height: htlc_cltv_timeout,
795                 }]),
796                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
797
798         assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
799                         claimable_amount_satoshis: 500_000 - 20_000,
800                 }, Balance::MaybePreimageClaimableHTLC {
801                         claimable_amount_satoshis: 10_000,
802                         expiry_height: htlc_cltv_timeout,
803                 }, Balance::MaybeTimeoutClaimableHTLC {
804                         claimable_amount_satoshis: 20_000,
805                         claimable_height: htlc_cltv_timeout,
806                 }]),
807                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
808
809         // Get nodes[0]'s commitment transaction and HTLC-Timeout transaction
810         let as_txn = get_local_commitment_txn!(nodes[0], chan_id);
811         assert_eq!(as_txn.len(), 2);
812         check_spends!(as_txn[1], as_txn[0]);
813         check_spends!(as_txn[0], funding_tx);
814
815         // Now close the channel by confirming A's commitment transaction on both nodes, checking the
816         // claimable balances remain the same except for the non-HTLC balance changing variant.
817         let node_a_commitment_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
818         let as_pre_spend_claims = sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
819                         claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
820                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
821                         confirmation_height: node_a_commitment_claimable,
822                 }, Balance::MaybePreimageClaimableHTLC {
823                         claimable_amount_satoshis: 20_000,
824                         expiry_height: htlc_cltv_timeout,
825                 }, Balance::MaybeTimeoutClaimableHTLC {
826                         claimable_amount_satoshis: 10_000,
827                         claimable_height: htlc_cltv_timeout,
828                 }]);
829
830         mine_transaction(&nodes[0], &as_txn[0]);
831         nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
832         check_added_monitors!(nodes[0], 1);
833         check_closed_broadcast!(nodes[0], true);
834         check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
835
836         assert_eq!(as_pre_spend_claims,
837                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
838
839         mine_transaction(&nodes[1], &as_txn[0]);
840         check_added_monitors!(nodes[1], 1);
841         check_closed_broadcast!(nodes[1], true);
842         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
843
844         let node_b_commitment_claimable = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
845         let mut bs_pre_spend_claims = sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
846                         claimable_amount_satoshis: 500_000 - 20_000,
847                         confirmation_height: node_b_commitment_claimable,
848                 }, Balance::MaybePreimageClaimableHTLC {
849                         claimable_amount_satoshis: 10_000,
850                         expiry_height: htlc_cltv_timeout,
851                 }, Balance::MaybeTimeoutClaimableHTLC {
852                         claimable_amount_satoshis: 20_000,
853                         claimable_height: htlc_cltv_timeout,
854                 }]);
855         assert_eq!(bs_pre_spend_claims,
856                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
857
858         // We'll broadcast the HTLC-Timeout transaction one block prior to the htlc's expiration (as it
859         // is confirmable in the next block), but will still include the same claimable balances as no
860         // HTLC has been spent, even after the HTLC expires. We'll also fail the inbound HTLC, but it
861         // won't do anything as the channel is already closed.
862
863         connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1);
864         let as_htlc_timeout_claim = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
865         assert_eq!(as_htlc_timeout_claim.len(), 1);
866         check_spends!(as_htlc_timeout_claim[0], as_txn[0]);
867         expect_pending_htlcs_forwardable_conditions!(nodes[0],
868                 [HTLCDestination::FailedPayment { payment_hash: to_a_failed_payment_hash }]);
869
870         assert_eq!(as_pre_spend_claims,
871                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
872
873         connect_blocks(&nodes[0], 1);
874         assert_eq!(as_pre_spend_claims,
875                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
876
877         // For node B, we'll get the non-HTLC funds claimable after ANTI_REORG_DELAY confirmations
878         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
879         test_spendable_output(&nodes[1], &as_txn[0]);
880         bs_pre_spend_claims.retain(|e| if let Balance::ClaimableAwaitingConfirmations { .. } = e { false } else { true });
881
882         // The next few blocks for B look the same as for A, though for the opposite HTLC
883         nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
884         connect_blocks(&nodes[1], TEST_FINAL_CLTV - (ANTI_REORG_DELAY - 1) - 1);
885         expect_pending_htlcs_forwardable_conditions!(nodes[1],
886                 [HTLCDestination::FailedPayment { payment_hash: to_b_failed_payment_hash }]);
887         let bs_htlc_timeout_claim = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
888         assert_eq!(bs_htlc_timeout_claim.len(), 1);
889         check_spends!(bs_htlc_timeout_claim[0], as_txn[0]);
890
891         assert_eq!(bs_pre_spend_claims,
892                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
893
894         connect_blocks(&nodes[1], 1);
895         assert_eq!(bs_pre_spend_claims,
896                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
897
898         // Now confirm the two HTLC timeout transactions for A, checking that the inbound HTLC resolves
899         // after ANTI_REORG_DELAY confirmations and the other takes BREAKDOWN_TIMEOUT confirmations.
900         mine_transaction(&nodes[0], &as_htlc_timeout_claim[0]);
901         let as_timeout_claimable_height = nodes[0].best_block_info().1 + (BREAKDOWN_TIMEOUT as u32) - 1;
902         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
903                         claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
904                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
905                         confirmation_height: node_a_commitment_claimable,
906                 }, Balance::MaybePreimageClaimableHTLC {
907                         claimable_amount_satoshis: 20_000,
908                         expiry_height: htlc_cltv_timeout,
909                 }, Balance::ClaimableAwaitingConfirmations {
910                         claimable_amount_satoshis: 10_000,
911                         confirmation_height: as_timeout_claimable_height,
912                 }]),
913                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
914
915         mine_transaction(&nodes[0], &bs_htlc_timeout_claim[0]);
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::MaybePreimageClaimableHTLC {
921                         claimable_amount_satoshis: 20_000,
922                         expiry_height: htlc_cltv_timeout,
923                 }, Balance::ClaimableAwaitingConfirmations {
924                         claimable_amount_satoshis: 10_000,
925                         confirmation_height: as_timeout_claimable_height,
926                 }]),
927                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
928
929         // Once as_htlc_timeout_claim[0] reaches ANTI_REORG_DELAY confirmations, we should get a
930         // payment failure event.
931         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 2);
932         expect_payment_failed!(nodes[0], to_b_failed_payment_hash, false);
933
934         connect_blocks(&nodes[0], 1);
935         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
936                         claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
937                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
938                         confirmation_height: node_a_commitment_claimable,
939                 }, Balance::ClaimableAwaitingConfirmations {
940                         claimable_amount_satoshis: 10_000,
941                         confirmation_height: core::cmp::max(as_timeout_claimable_height, htlc_cltv_timeout),
942                 }]),
943                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
944
945         connect_blocks(&nodes[0], node_a_commitment_claimable - nodes[0].best_block_info().1);
946         assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
947                         claimable_amount_satoshis: 10_000,
948                         confirmation_height: core::cmp::max(as_timeout_claimable_height, htlc_cltv_timeout),
949                 }],
950                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
951         test_spendable_output(&nodes[0], &as_txn[0]);
952
953         connect_blocks(&nodes[0], as_timeout_claimable_height - nodes[0].best_block_info().1);
954         assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
955         test_spendable_output(&nodes[0], &as_htlc_timeout_claim[0]);
956
957         // The process for B should be completely identical as well, noting that the non-HTLC-balance
958         // was already claimed.
959         mine_transaction(&nodes[1], &bs_htlc_timeout_claim[0]);
960         let bs_timeout_claimable_height = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
961         assert_eq!(sorted_vec(vec![Balance::MaybePreimageClaimableHTLC {
962                         claimable_amount_satoshis: 10_000,
963                         expiry_height: htlc_cltv_timeout,
964                 }, Balance::ClaimableAwaitingConfirmations {
965                         claimable_amount_satoshis: 20_000,
966                         confirmation_height: bs_timeout_claimable_height,
967                 }]),
968                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
969
970         mine_transaction(&nodes[1], &as_htlc_timeout_claim[0]);
971         assert_eq!(sorted_vec(vec![Balance::MaybePreimageClaimableHTLC {
972                         claimable_amount_satoshis: 10_000,
973                         expiry_height: htlc_cltv_timeout,
974                 }, Balance::ClaimableAwaitingConfirmations {
975                         claimable_amount_satoshis: 20_000,
976                         confirmation_height: bs_timeout_claimable_height,
977                 }]),
978                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
979
980         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 2);
981         expect_payment_failed!(nodes[1], to_a_failed_payment_hash, false);
982
983         assert_eq!(vec![Balance::MaybePreimageClaimableHTLC {
984                         claimable_amount_satoshis: 10_000,
985                         expiry_height: htlc_cltv_timeout,
986                 }],
987                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
988         test_spendable_output(&nodes[1], &bs_htlc_timeout_claim[0]);
989
990         connect_blocks(&nodes[1], 1);
991         assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
992
993         // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
994         // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
995         // monitor events or claimable balances.
996         connect_blocks(&nodes[1], 6);
997         connect_blocks(&nodes[1], 6);
998         assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
999         assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1000 }
1001
1002 fn sorted_vec_with_additions<T: Ord + Clone>(v_orig: &Vec<T>, extra_ts: &[&T]) -> Vec<T> {
1003         let mut v = v_orig.clone();
1004         for t in extra_ts {
1005                 v.push((*t).clone());
1006         }
1007         v.sort_unstable();
1008         v
1009 }
1010
1011 fn do_test_revoked_counterparty_commitment_balances(confirm_htlc_spend_first: bool) {
1012         // Tests `get_claimable_balances` for revoked counterparty commitment transactions.
1013         let mut chanmon_cfgs = create_chanmon_cfgs(2);
1014         // We broadcast a second-to-latest commitment transaction, without providing the revocation
1015         // secret to the counterparty. However, because we always immediately take the revocation
1016         // secret from the keys_manager, we would panic at broadcast as we're trying to sign a
1017         // transaction which, from the point of view of our keys_manager, is revoked.
1018         chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
1019         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1020         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1021         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1022
1023         let (_, _, chan_id, funding_tx) =
1024                 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 100_000_000, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1025         let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
1026         assert_eq!(funding_outpoint.to_channel_id(), chan_id);
1027
1028         // We create five HTLCs for B to claim against A's revoked commitment transaction:
1029         //
1030         // (1) one for which A is the originator and B knows the preimage
1031         // (2) one for which B is the originator where the HTLC has since timed-out
1032         // (3) one for which B is the originator but where the HTLC has not yet timed-out
1033         // (4) one dust HTLC which is lost in the channel closure
1034         // (5) one that actually isn't in the revoked commitment transaction at all, but was added in
1035         //     later commitment transaction updates
1036         //
1037         // Though they could all be claimed in a single claim transaction, due to CLTV timeouts they
1038         // are all currently claimed in separate transactions, which helps us test as we can claim
1039         // HTLCs individually.
1040
1041         let (claimed_payment_preimage, claimed_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 3_000_000);
1042         let timeout_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 4_000_000).1;
1043         let dust_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 3_000).1;
1044
1045         let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
1046
1047         connect_blocks(&nodes[0], 10);
1048         connect_blocks(&nodes[1], 10);
1049
1050         let live_htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
1051         let live_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 5_000_000).1;
1052
1053         // Get the latest commitment transaction from A and then update the fee to revoke it
1054         let as_revoked_txn = get_local_commitment_txn!(nodes[0], chan_id);
1055         let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
1056
1057         let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
1058
1059         let missing_htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
1060         let missing_htlc_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 2_000_000).1;
1061
1062         nodes[1].node.claim_funds(claimed_payment_preimage);
1063         expect_payment_claimed!(nodes[1], claimed_payment_hash, 3_000_000);
1064         check_added_monitors!(nodes[1], 1);
1065         let _b_htlc_msgs = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
1066
1067         connect_blocks(&nodes[0], htlc_cltv_timeout + 1 - 10);
1068         check_closed_broadcast!(nodes[0], true);
1069         check_added_monitors!(nodes[0], 1);
1070
1071         let mut events = nodes[0].node.get_and_clear_pending_events();
1072         assert_eq!(events.len(), 6);
1073         let mut failed_payments: HashSet<_> =
1074                 [timeout_payment_hash, dust_payment_hash, live_payment_hash, missing_htlc_payment_hash]
1075                 .iter().map(|a| *a).collect();
1076         events.retain(|ev| {
1077                 match ev {
1078                         Event::HTLCHandlingFailed { failed_next_destination: HTLCDestination::NextHopChannel { node_id, channel_id }, .. } => {
1079                                 assert_eq!(*channel_id, chan_id);
1080                                 assert_eq!(*node_id, Some(nodes[1].node.get_our_node_id()));
1081                                 false
1082                         },
1083                         Event::HTLCHandlingFailed { failed_next_destination: HTLCDestination::FailedPayment { payment_hash }, .. } => {
1084                                 assert!(failed_payments.remove(payment_hash));
1085                                 false
1086                         },
1087                         _ => true,
1088                 }
1089         });
1090         assert!(failed_payments.is_empty());
1091         if let Event::PendingHTLCsForwardable { .. } = events[0] {} else { panic!(); }
1092         match &events[1] {
1093                 Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {},
1094                 _ => panic!(),
1095         }
1096
1097         connect_blocks(&nodes[1], htlc_cltv_timeout + 1 - 10);
1098         check_closed_broadcast!(nodes[1], true);
1099         check_added_monitors!(nodes[1], 1);
1100         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
1101
1102         // Prior to channel closure, B considers the preimage HTLC as its own, and otherwise only
1103         // lists the two on-chain timeout-able HTLCs as claimable balances.
1104         assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
1105                         claimable_amount_satoshis: 100_000 - 5_000 - 4_000 - 3 - 2_000 + 3_000,
1106                 }, Balance::MaybeTimeoutClaimableHTLC {
1107                         claimable_amount_satoshis: 2_000,
1108                         claimable_height: missing_htlc_cltv_timeout,
1109                 }, Balance::MaybeTimeoutClaimableHTLC {
1110                         claimable_amount_satoshis: 4_000,
1111                         claimable_height: htlc_cltv_timeout,
1112                 }, Balance::MaybeTimeoutClaimableHTLC {
1113                         claimable_amount_satoshis: 5_000,
1114                         claimable_height: live_htlc_cltv_timeout,
1115                 }]),
1116                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1117
1118         mine_transaction(&nodes[1], &as_revoked_txn[0]);
1119         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();
1120         // Currently the revoked commitment is claimed in four transactions as the HTLCs all expire
1121         // quite soon.
1122         assert_eq!(claim_txn.len(), 4);
1123         claim_txn.sort_unstable_by_key(|tx| tx.output.iter().map(|output| output.value).sum::<u64>());
1124
1125         // The following constants were determined experimentally
1126         const BS_TO_SELF_CLAIM_EXP_WEIGHT: usize = 483;
1127         const OUTBOUND_HTLC_CLAIM_EXP_WEIGHT: usize = 571;
1128         const INBOUND_HTLC_CLAIM_EXP_WEIGHT: usize = 578;
1129
1130         // Check that the weight is close to the expected weight. Note that signature sizes vary
1131         // somewhat so it may not always be exact.
1132         fuzzy_assert_eq(claim_txn[0].weight(), OUTBOUND_HTLC_CLAIM_EXP_WEIGHT);
1133         fuzzy_assert_eq(claim_txn[1].weight(), INBOUND_HTLC_CLAIM_EXP_WEIGHT);
1134         fuzzy_assert_eq(claim_txn[2].weight(), INBOUND_HTLC_CLAIM_EXP_WEIGHT);
1135         fuzzy_assert_eq(claim_txn[3].weight(), BS_TO_SELF_CLAIM_EXP_WEIGHT);
1136
1137         // The expected balance for the next three checks, with the largest-HTLC and to_self output
1138         // claim balances separated out.
1139         let expected_balance = vec![Balance::ClaimableAwaitingConfirmations {
1140                         // to_remote output in A's revoked commitment
1141                         claimable_amount_satoshis: 100_000 - 5_000 - 4_000 - 3,
1142                         confirmation_height: nodes[1].best_block_info().1 + 5,
1143                 }, Balance::CounterpartyRevokedOutputClaimable {
1144                         claimable_amount_satoshis: 3_000,
1145                 }, Balance::CounterpartyRevokedOutputClaimable {
1146                         claimable_amount_satoshis: 4_000,
1147                 }];
1148
1149         let to_self_unclaimed_balance = Balance::CounterpartyRevokedOutputClaimable {
1150                 claimable_amount_satoshis: 1_000_000 - 100_000 - 3_000 - chan_feerate *
1151                         (channel::commitment_tx_base_weight(opt_anchors) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1152         };
1153         let to_self_claimed_avail_height;
1154         let largest_htlc_unclaimed_balance = Balance::CounterpartyRevokedOutputClaimable {
1155                 claimable_amount_satoshis: 5_000,
1156         };
1157         let largest_htlc_claimed_avail_height;
1158
1159         // Once the channel has been closed by A, B now considers all of the commitment transactions'
1160         // outputs as `CounterpartyRevokedOutputClaimable`.
1161         assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_unclaimed_balance, &largest_htlc_unclaimed_balance]),
1162                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1163
1164         if confirm_htlc_spend_first {
1165                 mine_transaction(&nodes[1], &claim_txn[2]);
1166                 largest_htlc_claimed_avail_height = nodes[1].best_block_info().1 + 5;
1167                 to_self_claimed_avail_height = nodes[1].best_block_info().1 + 6; // will be claimed in the next block
1168         } else {
1169                 // Connect the to_self output claim, taking all of A's non-HTLC funds
1170                 mine_transaction(&nodes[1], &claim_txn[3]);
1171                 to_self_claimed_avail_height = nodes[1].best_block_info().1 + 5;
1172                 largest_htlc_claimed_avail_height = nodes[1].best_block_info().1 + 6; // will be claimed in the next block
1173         }
1174
1175         let largest_htlc_claimed_balance = Balance::ClaimableAwaitingConfirmations {
1176                 claimable_amount_satoshis: 5_000 - chan_feerate * INBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
1177                 confirmation_height: largest_htlc_claimed_avail_height,
1178         };
1179         let to_self_claimed_balance = Balance::ClaimableAwaitingConfirmations {
1180                 claimable_amount_satoshis: 1_000_000 - 100_000 - 3_000 - chan_feerate *
1181                         (channel::commitment_tx_base_weight(opt_anchors) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
1182                         - chan_feerate * claim_txn[3].weight() as u64 / 1000,
1183                 confirmation_height: to_self_claimed_avail_height,
1184         };
1185
1186         if confirm_htlc_spend_first {
1187                 assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_unclaimed_balance, &largest_htlc_claimed_balance]),
1188                         sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1189         } else {
1190                 assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_claimed_balance, &largest_htlc_unclaimed_balance]),
1191                         sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1192         }
1193
1194         if confirm_htlc_spend_first {
1195                 mine_transaction(&nodes[1], &claim_txn[3]);
1196         } else {
1197                 mine_transaction(&nodes[1], &claim_txn[2]);
1198         }
1199         assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_claimed_balance, &largest_htlc_claimed_balance]),
1200                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1201
1202         // Finally, connect the last two remaining HTLC spends and check that they move to
1203         // `ClaimableAwaitingConfirmations`
1204         mine_transaction(&nodes[1], &claim_txn[0]);
1205         mine_transaction(&nodes[1], &claim_txn[1]);
1206
1207         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1208                         // to_remote output in A's revoked commitment
1209                         claimable_amount_satoshis: 100_000 - 5_000 - 4_000 - 3,
1210                         confirmation_height: nodes[1].best_block_info().1 + 1,
1211                 }, Balance::ClaimableAwaitingConfirmations {
1212                         claimable_amount_satoshis: 1_000_000 - 100_000 - 3_000 - chan_feerate *
1213                                 (channel::commitment_tx_base_weight(opt_anchors) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
1214                                 - chan_feerate * claim_txn[3].weight() as u64 / 1000,
1215                         confirmation_height: to_self_claimed_avail_height,
1216                 }, Balance::ClaimableAwaitingConfirmations {
1217                         claimable_amount_satoshis: 3_000 - chan_feerate * OUTBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
1218                         confirmation_height: nodes[1].best_block_info().1 + 4,
1219                 }, Balance::ClaimableAwaitingConfirmations {
1220                         claimable_amount_satoshis: 4_000 - chan_feerate * INBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
1221                         confirmation_height: nodes[1].best_block_info().1 + 5,
1222                 }, Balance::ClaimableAwaitingConfirmations {
1223                         claimable_amount_satoshis: 5_000 - chan_feerate * INBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
1224                         confirmation_height: largest_htlc_claimed_avail_height,
1225                 }]),
1226                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1227
1228         connect_blocks(&nodes[1], 1);
1229         test_spendable_output(&nodes[1], &as_revoked_txn[0]);
1230
1231         let mut payment_failed_events = nodes[1].node.get_and_clear_pending_events();
1232         expect_payment_failed_conditions_event(&nodes[1], payment_failed_events.pop().unwrap(),
1233                 dust_payment_hash, false, PaymentFailedConditions::new());
1234         expect_payment_failed_conditions_event(&nodes[1], payment_failed_events.pop().unwrap(),
1235                 missing_htlc_payment_hash, false, PaymentFailedConditions::new());
1236         assert!(payment_failed_events.is_empty());
1237
1238         connect_blocks(&nodes[1], 1);
1239         test_spendable_output(&nodes[1], &claim_txn[if confirm_htlc_spend_first { 2 } else { 3 }]);
1240         connect_blocks(&nodes[1], 1);
1241         test_spendable_output(&nodes[1], &claim_txn[if confirm_htlc_spend_first { 3 } else { 2 }]);
1242         expect_payment_failed!(nodes[1], live_payment_hash, false);
1243         connect_blocks(&nodes[1], 1);
1244         test_spendable_output(&nodes[1], &claim_txn[0]);
1245         connect_blocks(&nodes[1], 1);
1246         test_spendable_output(&nodes[1], &claim_txn[1]);
1247         expect_payment_failed!(nodes[1], timeout_payment_hash, false);
1248         assert_eq!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances(), Vec::new());
1249
1250         // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
1251         // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
1252         // monitor events or claimable balances.
1253         connect_blocks(&nodes[1], 6);
1254         connect_blocks(&nodes[1], 6);
1255         assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1256         assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1257 }
1258
1259 #[test]
1260 fn test_revoked_counterparty_commitment_balances() {
1261         do_test_revoked_counterparty_commitment_balances(true);
1262         do_test_revoked_counterparty_commitment_balances(false);
1263 }
1264
1265 #[test]
1266 fn test_revoked_counterparty_htlc_tx_balances() {
1267         // Tests `get_claimable_balances` for revocation spends of HTLC transactions.
1268         let mut chanmon_cfgs = create_chanmon_cfgs(2);
1269         chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
1270         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1271         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1272         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1273
1274         // Create some initial channels
1275         let (_, _, chan_id, funding_tx) =
1276                 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 11_000_000, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1277         let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
1278         assert_eq!(funding_outpoint.to_channel_id(), chan_id);
1279
1280         let payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 3_000_000).0;
1281         let failed_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 1_000_000).1;
1282         let revoked_local_txn = get_local_commitment_txn!(nodes[1], chan_id);
1283         assert_eq!(revoked_local_txn[0].input.len(), 1);
1284         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, funding_tx.txid());
1285
1286         // The to-be-revoked commitment tx should have two HTLCs and an output for both sides
1287         assert_eq!(revoked_local_txn[0].output.len(), 4);
1288
1289         claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
1290
1291         let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
1292         let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
1293
1294         // B will generate an HTLC-Success from its revoked commitment tx
1295         mine_transaction(&nodes[1], &revoked_local_txn[0]);
1296         check_closed_broadcast!(nodes[1], true);
1297         check_added_monitors!(nodes[1], 1);
1298         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
1299         let revoked_htlc_success_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1300
1301         assert_eq!(revoked_htlc_success_txn.len(), 1);
1302         assert_eq!(revoked_htlc_success_txn[0].input.len(), 1);
1303         assert_eq!(revoked_htlc_success_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
1304         check_spends!(revoked_htlc_success_txn[0], revoked_local_txn[0]);
1305
1306         connect_blocks(&nodes[1], TEST_FINAL_CLTV);
1307         let revoked_htlc_timeout_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1308         assert_eq!(revoked_htlc_timeout_txn.len(), 1);
1309         check_spends!(revoked_htlc_timeout_txn[0], revoked_local_txn[0]);
1310         assert_ne!(revoked_htlc_success_txn[0].input[0].previous_output, revoked_htlc_timeout_txn[0].input[0].previous_output);
1311         assert_eq!(revoked_htlc_success_txn[0].lock_time.0, 0);
1312         assert_ne!(revoked_htlc_timeout_txn[0].lock_time.0, 0);
1313
1314         // A will generate justice tx from B's revoked commitment/HTLC tx
1315         mine_transaction(&nodes[0], &revoked_local_txn[0]);
1316         check_closed_broadcast!(nodes[0], true);
1317         check_added_monitors!(nodes[0], 1);
1318         check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
1319         let to_remote_conf_height = nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1;
1320
1321         let as_commitment_claim_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1322         assert_eq!(as_commitment_claim_txn.len(), 1);
1323         check_spends!(as_commitment_claim_txn[0], revoked_local_txn[0]);
1324
1325         // The next two checks have the same balance set for A - even though we confirm a revoked HTLC
1326         // transaction our balance tracking doesn't use the on-chain value so the
1327         // `CounterpartyRevokedOutputClaimable` entry doesn't change.
1328         let as_balances = sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1329                         // to_remote output in B's revoked commitment
1330                         claimable_amount_satoshis: 1_000_000 - 11_000 - 3_000 - chan_feerate *
1331                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1332                         confirmation_height: to_remote_conf_height,
1333                 }, Balance::CounterpartyRevokedOutputClaimable {
1334                         // to_self output in B's revoked commitment
1335                         claimable_amount_satoshis: 10_000,
1336                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1337                         claimable_amount_satoshis: 3_000,
1338                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1339                         claimable_amount_satoshis: 1_000,
1340                 }]);
1341         assert_eq!(as_balances,
1342                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1343
1344         mine_transaction(&nodes[0], &revoked_htlc_success_txn[0]);
1345         let as_htlc_claim_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1346         assert_eq!(as_htlc_claim_tx.len(), 2);
1347         check_spends!(as_htlc_claim_tx[0], revoked_htlc_success_txn[0]);
1348         check_spends!(as_htlc_claim_tx[1], revoked_local_txn[0]); // A has to generate a new claim for the remaining revoked
1349                                                                   // outputs (which no longer includes the spent HTLC output)
1350
1351         assert_eq!(as_balances,
1352                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1353
1354         assert_eq!(as_htlc_claim_tx[0].output.len(), 1);
1355         fuzzy_assert_eq(as_htlc_claim_tx[0].output[0].value,
1356                 3_000 - chan_feerate * (revoked_htlc_success_txn[0].weight() + as_htlc_claim_tx[0].weight()) as u64 / 1000);
1357
1358         mine_transaction(&nodes[0], &as_htlc_claim_tx[0]);
1359         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1360                         // to_remote output in B's revoked commitment
1361                         claimable_amount_satoshis: 1_000_000 - 11_000 - 3_000 - chan_feerate *
1362                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1363                         confirmation_height: to_remote_conf_height,
1364                 }, Balance::CounterpartyRevokedOutputClaimable {
1365                         // to_self output in B's revoked commitment
1366                         claimable_amount_satoshis: 10_000,
1367                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1368                         claimable_amount_satoshis: 1_000,
1369                 }, Balance::ClaimableAwaitingConfirmations {
1370                         claimable_amount_satoshis: as_htlc_claim_tx[0].output[0].value,
1371                         confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
1372                 }]),
1373                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1374
1375         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 3);
1376         test_spendable_output(&nodes[0], &revoked_local_txn[0]);
1377         assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1378                         // to_self output to B
1379                         claimable_amount_satoshis: 10_000,
1380                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1381                         claimable_amount_satoshis: 1_000,
1382                 }, Balance::ClaimableAwaitingConfirmations {
1383                         claimable_amount_satoshis: as_htlc_claim_tx[0].output[0].value,
1384                         confirmation_height: nodes[0].best_block_info().1 + 2,
1385                 }]),
1386                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1387
1388         connect_blocks(&nodes[0], 2);
1389         test_spendable_output(&nodes[0], &as_htlc_claim_tx[0]);
1390         assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1391                         // to_self output in B's revoked commitment
1392                         claimable_amount_satoshis: 10_000,
1393                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1394                         claimable_amount_satoshis: 1_000,
1395                 }]),
1396                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1397
1398         connect_blocks(&nodes[0], revoked_htlc_timeout_txn[0].lock_time.0 - nodes[0].best_block_info().1);
1399         expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(&nodes[0],
1400                 [HTLCDestination::FailedPayment { payment_hash: failed_payment_hash }]);
1401         // As time goes on A may split its revocation claim transaction into multiple.
1402         let as_fewer_input_rbf = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1403         for tx in as_fewer_input_rbf.iter() {
1404                 check_spends!(tx, revoked_local_txn[0]);
1405         }
1406
1407         // Connect a number of additional blocks to ensure we don't forget the HTLC output needs
1408         // claiming.
1409         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
1410         let as_fewer_input_rbf = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1411         for tx in as_fewer_input_rbf.iter() {
1412                 check_spends!(tx, revoked_local_txn[0]);
1413         }
1414
1415         mine_transaction(&nodes[0], &revoked_htlc_timeout_txn[0]);
1416         let as_second_htlc_claim_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1417         assert_eq!(as_second_htlc_claim_tx.len(), 2);
1418
1419         check_spends!(as_second_htlc_claim_tx[0], revoked_htlc_timeout_txn[0]);
1420         check_spends!(as_second_htlc_claim_tx[1], revoked_local_txn[0]);
1421
1422         // Connect blocks to finalize the HTLC resolution with the HTLC-Timeout transaction. In a
1423         // previous iteration of the revoked balance handling this would result in us "forgetting" that
1424         // the revoked HTLC output still needed to be claimed.
1425         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
1426         assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1427                         // to_self output in B's revoked commitment
1428                         claimable_amount_satoshis: 10_000,
1429                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1430                         claimable_amount_satoshis: 1_000,
1431                 }]),
1432                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1433
1434         mine_transaction(&nodes[0], &as_second_htlc_claim_tx[0]);
1435         assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1436                         // to_self output in B's revoked commitment
1437                         claimable_amount_satoshis: 10_000,
1438                 }, Balance::ClaimableAwaitingConfirmations {
1439                         claimable_amount_satoshis: as_second_htlc_claim_tx[0].output[0].value,
1440                         confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
1441                 }]),
1442                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1443
1444         mine_transaction(&nodes[0], &as_second_htlc_claim_tx[1]);
1445         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1446                         // to_self output in B's revoked commitment
1447                         claimable_amount_satoshis: as_second_htlc_claim_tx[1].output[0].value,
1448                         confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
1449                 }, Balance::ClaimableAwaitingConfirmations {
1450                         claimable_amount_satoshis: as_second_htlc_claim_tx[0].output[0].value,
1451                         confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 2,
1452                 }]),
1453                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1454
1455         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 2);
1456         test_spendable_output(&nodes[0], &as_second_htlc_claim_tx[0]);
1457         connect_blocks(&nodes[0], 1);
1458         test_spendable_output(&nodes[0], &as_second_htlc_claim_tx[1]);
1459
1460         assert_eq!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances(), Vec::new());
1461
1462         // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
1463         // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
1464         // monitor events or claimable balances.
1465         connect_blocks(&nodes[0], 6);
1466         connect_blocks(&nodes[0], 6);
1467         assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1468         assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1469 }
1470
1471 #[test]
1472 fn test_revoked_counterparty_aggregated_claims() {
1473         // Tests `get_claimable_balances` for revoked counterparty commitment transactions when
1474         // claiming with an aggregated claim transaction.
1475         let mut chanmon_cfgs = create_chanmon_cfgs(2);
1476         // We broadcast a second-to-latest commitment transaction, without providing the revocation
1477         // secret to the counterparty. However, because we always immediately take the revocation
1478         // secret from the keys_manager, we would panic at broadcast as we're trying to sign a
1479         // transaction which, from the point of view of our keys_manager, is revoked.
1480         chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
1481         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1482         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1483         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1484
1485         let (_, _, chan_id, funding_tx) =
1486                 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 100_000_000, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1487         let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
1488         assert_eq!(funding_outpoint.to_channel_id(), chan_id);
1489
1490         // We create two HTLCs, one which we will give A the preimage to to generate an HTLC-Success
1491         // transaction, and one which we will not, allowing B to claim the HTLC output in an aggregated
1492         // revocation-claim transaction.
1493
1494         let (claimed_payment_preimage, claimed_payment_hash, ..) = route_payment(&nodes[1], &[&nodes[0]], 3_000_000);
1495         let revoked_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 4_000_000).1;
1496
1497         let htlc_cltv_timeout = nodes[1].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
1498
1499         // Cheat by giving A's ChannelMonitor the preimage to the to-be-claimed HTLC so that we have an
1500         // HTLC-claim transaction on the to-be-revoked state.
1501         get_monitor!(nodes[0], chan_id).provide_payment_preimage(&claimed_payment_hash, &claimed_payment_preimage,
1502                 &node_cfgs[0].tx_broadcaster, &LowerBoundedFeeEstimator::new(node_cfgs[0].fee_estimator), &nodes[0].logger);
1503
1504         // Now get the latest commitment transaction from A and then update the fee to revoke it
1505         let as_revoked_txn = get_local_commitment_txn!(nodes[0], chan_id);
1506
1507         assert_eq!(as_revoked_txn.len(), 2);
1508         check_spends!(as_revoked_txn[0], funding_tx);
1509         check_spends!(as_revoked_txn[1], as_revoked_txn[0]); // The HTLC-Claim transaction
1510
1511         let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
1512         let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
1513
1514         {
1515                 let mut feerate = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
1516                 *feerate += 1;
1517         }
1518         nodes[0].node.timer_tick_occurred();
1519         check_added_monitors!(nodes[0], 1);
1520
1521         let fee_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1522         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &fee_update.update_fee.unwrap());
1523         commitment_signed_dance!(nodes[1], nodes[0], fee_update.commitment_signed, false);
1524
1525         nodes[0].node.claim_funds(claimed_payment_preimage);
1526         expect_payment_claimed!(nodes[0], claimed_payment_hash, 3_000_000);
1527         check_added_monitors!(nodes[0], 1);
1528         let _a_htlc_msgs = get_htlc_update_msgs!(&nodes[0], nodes[1].node.get_our_node_id());
1529
1530         assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
1531                         claimable_amount_satoshis: 100_000 - 4_000 - 3_000,
1532                 }, Balance::MaybeTimeoutClaimableHTLC {
1533                         claimable_amount_satoshis: 4_000,
1534                         claimable_height: htlc_cltv_timeout,
1535                 }, Balance::MaybeTimeoutClaimableHTLC {
1536                         claimable_amount_satoshis: 3_000,
1537                         claimable_height: htlc_cltv_timeout,
1538                 }]),
1539                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1540
1541         mine_transaction(&nodes[1], &as_revoked_txn[0]);
1542         check_closed_broadcast!(nodes[1], true);
1543         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
1544         check_added_monitors!(nodes[1], 1);
1545
1546         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();
1547         // Currently the revoked commitment outputs are all claimed in one aggregated transaction
1548         assert_eq!(claim_txn.len(), 1);
1549         assert_eq!(claim_txn[0].input.len(), 3);
1550         check_spends!(claim_txn[0], as_revoked_txn[0]);
1551
1552         let to_remote_maturity = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
1553
1554         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1555                         // to_remote output in A's revoked commitment
1556                         claimable_amount_satoshis: 100_000 - 4_000 - 3_000,
1557                         confirmation_height: to_remote_maturity,
1558                 }, Balance::CounterpartyRevokedOutputClaimable {
1559                         // to_self output in A's revoked commitment
1560                         claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1561                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1562                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1563                         claimable_amount_satoshis: 4_000,
1564                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1565                         claimable_amount_satoshis: 3_000,
1566                 }]),
1567                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1568
1569         // Confirm A's HTLC-Success tranasction which presumably raced B's claim, causing B to create a
1570         // new claim.
1571         mine_transaction(&nodes[1], &as_revoked_txn[1]);
1572         expect_payment_sent!(nodes[1], claimed_payment_preimage);
1573         let mut claim_txn_2: Vec<_> = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
1574         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 });
1575         // Once B sees the HTLC-Success transaction it splits its claim transaction into two, though in
1576         // theory it could re-aggregate the claims as well.
1577         assert_eq!(claim_txn_2.len(), 2);
1578         assert_eq!(claim_txn_2[0].input.len(), 2);
1579         check_spends!(claim_txn_2[0], as_revoked_txn[0]);
1580         assert_eq!(claim_txn_2[1].input.len(), 1);
1581         check_spends!(claim_txn_2[1], as_revoked_txn[1]);
1582
1583         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1584                         // to_remote output in A's revoked commitment
1585                         claimable_amount_satoshis: 100_000 - 4_000 - 3_000,
1586                         confirmation_height: to_remote_maturity,
1587                 }, Balance::CounterpartyRevokedOutputClaimable {
1588                         // to_self output in A's revoked commitment
1589                         claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1590                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1591                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1592                         claimable_amount_satoshis: 4_000,
1593                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1594                         // The amount here is a bit of a misnomer, really its been reduced by the HTLC
1595                         // transaction fee, but the claimable amount is always a bit of an overshoot for HTLCs
1596                         // anyway, so its not a big change.
1597                         claimable_amount_satoshis: 3_000,
1598                 }]),
1599                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1600
1601         connect_blocks(&nodes[1], 5);
1602         test_spendable_output(&nodes[1], &as_revoked_txn[0]);
1603
1604         assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1605                         // to_self output in A's revoked commitment
1606                         claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1607                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1608                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1609                         claimable_amount_satoshis: 4_000,
1610                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1611                         // The amount here is a bit of a misnomer, really its been reduced by the HTLC
1612                         // transaction fee, but the claimable amount is always a bit of an overshoot for HTLCs
1613                         // anyway, so its not a big change.
1614                         claimable_amount_satoshis: 3_000,
1615                 }]),
1616                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1617
1618         mine_transaction(&nodes[1], &claim_txn_2[1]);
1619         let htlc_2_claim_maturity = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 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                 }, Balance::ClaimableAwaitingConfirmations { // HTLC 2
1628                         claimable_amount_satoshis: claim_txn_2[1].output[0].value,
1629                         confirmation_height: htlc_2_claim_maturity,
1630                 }]),
1631                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1632
1633         connect_blocks(&nodes[1], 5);
1634         test_spendable_output(&nodes[1], &claim_txn_2[1]);
1635
1636         assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1637                         // to_self output in A's revoked commitment
1638                         claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1639                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1640                 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1641                         claimable_amount_satoshis: 4_000,
1642                 }]),
1643                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1644
1645         mine_transaction(&nodes[1], &claim_txn_2[0]);
1646         let rest_claim_maturity = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
1647
1648         assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
1649                         claimable_amount_satoshis: claim_txn_2[0].output[0].value,
1650                         confirmation_height: rest_claim_maturity,
1651                 }],
1652                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
1653
1654         assert!(nodes[1].node.get_and_clear_pending_events().is_empty()); // We shouldn't fail the payment until we spend the output
1655
1656         connect_blocks(&nodes[1], 5);
1657         expect_payment_failed!(nodes[1], revoked_payment_hash, false);
1658         test_spendable_output(&nodes[1], &claim_txn_2[0]);
1659         assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1660
1661         // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
1662         // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
1663         // monitor events or claimable balances.
1664         connect_blocks(&nodes[1], 6);
1665         connect_blocks(&nodes[1], 6);
1666         assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1667         assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1668 }