Merge pull request #1494 from TheBlueMatt/2022-05-mon-cleanups-renames
[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 chain::channelmonitor::{ANTI_REORG_DELAY, Balance};
13 use chain::transaction::OutPoint;
14 use ln::channel;
15 use ln::channelmanager::BREAKDOWN_TIMEOUT;
16 use ln::features::InitFeatures;
17 use ln::msgs::ChannelMessageHandler;
18 use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason};
19
20 use bitcoin::blockdata::script::Builder;
21 use bitcoin::blockdata::opcodes;
22 use bitcoin::secp256k1::Secp256k1;
23 use bitcoin::Transaction;
24
25 use prelude::*;
26
27 use 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, InitFeatures::known(), InitFeatures::known());
52         let (update_a, _, chan_id_2, _) = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
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)).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!(nodes[1]);
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 chanmon_claim_value_coop_close() {
99         // Tests `get_claimable_balances` returns the correct values across a simple cooperative claim.
100         // Specifically, this tests that the channel non-HTLC balances show up in
101         // `get_claimable_balances` until the cooperative claims have confirmed and generated a
102         // `SpendableOutputs` event, and no longer.
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_id, funding_tx) =
109                 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 1_000_000, InitFeatures::known(), InitFeatures::known());
110         let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
111         assert_eq!(funding_outpoint.to_channel_id(), chan_id);
112
113         let chan_feerate = get_feerate!(nodes[0], chan_id) as u64;
114         let opt_anchors = get_opt_anchors!(nodes[0], chan_id);
115
116         assert_eq!(vec![Balance::ClaimableOnChannelClose {
117                         claimable_amount_satoshis: 1_000_000 - 1_000 - chan_feerate * channel::commitment_tx_base_weight(opt_anchors) / 1000
118                 }],
119                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
120         assert_eq!(vec![Balance::ClaimableOnChannelClose { claimable_amount_satoshis: 1_000, }],
121                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
122
123         nodes[0].node.close_channel(&chan_id, &nodes[1].node.get_our_node_id()).unwrap();
124         let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
125         nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &InitFeatures::known(), &node_0_shutdown);
126         let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
127         nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_1_shutdown);
128
129         let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
130         nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
131         let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
132         nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
133         let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
134         nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
135         let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
136         assert!(node_1_none.is_none());
137
138         let shutdown_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
139         assert_eq!(shutdown_tx, nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0));
140         assert_eq!(shutdown_tx.len(), 1);
141
142         mine_transaction(&nodes[0], &shutdown_tx[0]);
143         mine_transaction(&nodes[1], &shutdown_tx[0]);
144
145         assert!(nodes[0].node.list_channels().is_empty());
146         assert!(nodes[1].node.list_channels().is_empty());
147
148         assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
149         assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
150
151         assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
152                         claimable_amount_satoshis: 1_000_000 - 1_000 - chan_feerate * channel::commitment_tx_base_weight(opt_anchors) / 1000,
153                         confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
154                 }],
155                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
156         assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
157                         claimable_amount_satoshis: 1000,
158                         confirmation_height: nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1,
159                 }],
160                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
161
162         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
163         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
164
165         assert_eq!(Vec::<Balance>::new(),
166                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
167         assert_eq!(Vec::<Balance>::new(),
168                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
169
170         test_spendable_output(&nodes[0], &shutdown_tx[0]);
171         test_spendable_output(&nodes[1], &shutdown_tx[0]);
172
173         check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure);
174         check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure);
175 }
176
177 fn sorted_vec<T: Ord>(mut v: Vec<T>) -> Vec<T> {
178         v.sort_unstable();
179         v
180 }
181
182 fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
183         // Tests `get_claimable_balances` with an HTLC across a force-close.
184         // We build a channel with an HTLC pending, then force close the channel and check that the
185         // `get_claimable_balances` return value is correct as transactions confirm on-chain.
186         let mut chanmon_cfgs = create_chanmon_cfgs(2);
187         if prev_commitment_tx {
188                 // We broadcast a second-to-latest commitment transaction, without providing the revocation
189                 // secret to the counterparty. However, because we always immediately take the revocation
190                 // secret from the keys_manager, we would panic at broadcast as we're trying to sign a
191                 // transaction which, from the point of view of our keys_manager, is revoked.
192                 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
193         }
194         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
195         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
196         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
197
198         let (_, _, chan_id, funding_tx) =
199                 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 1_000_000, InitFeatures::known(), InitFeatures::known());
200         let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
201         assert_eq!(funding_outpoint.to_channel_id(), chan_id);
202
203         // This HTLC is immediately claimed, giving node B the preimage
204         let payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 3_000_000).0;
205         // This HTLC is allowed to time out, letting A claim it. However, in order to test claimable
206         // balances more fully we also give B the preimage for this HTLC.
207         let (timeout_payment_preimage, timeout_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 4_000_000);
208         // This HTLC will be dust, and not be claimable at all:
209         let (dust_payment_preimage, dust_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 3_000);
210
211         let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
212
213         let chan_feerate = get_feerate!(nodes[0], chan_id) as u64;
214         let opt_anchors = get_opt_anchors!(nodes[0], chan_id);
215
216         let remote_txn = get_local_commitment_txn!(nodes[1], chan_id);
217         // Before B receives the payment preimage, it only suggests the push_msat value of 1_000 sats
218         // as claimable. A lists both its to-self balance and the (possibly-claimable) HTLCs.
219         assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
220                         claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
221                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
222                 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
223                         claimable_amount_satoshis: 3_000,
224                         claimable_height: htlc_cltv_timeout,
225                 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
226                         claimable_amount_satoshis: 4_000,
227                         claimable_height: htlc_cltv_timeout,
228                 }]),
229                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
230         assert_eq!(vec![Balance::ClaimableOnChannelClose {
231                         claimable_amount_satoshis: 1_000,
232                 }],
233                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
234
235         nodes[1].node.claim_funds(payment_preimage);
236         check_added_monitors!(nodes[1], 1);
237         let b_htlc_msgs = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
238         // We claim the dust payment here as well, but it won't impact our claimable balances as its
239         // dust and thus doesn't appear on chain at all.
240         nodes[1].node.claim_funds(dust_payment_preimage);
241         check_added_monitors!(nodes[1], 1);
242         nodes[1].node.claim_funds(timeout_payment_preimage);
243         check_added_monitors!(nodes[1], 1);
244
245         if prev_commitment_tx {
246                 // To build a previous commitment transaction, deliver one round of commitment messages.
247                 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &b_htlc_msgs.update_fulfill_htlcs[0]);
248                 expect_payment_sent_without_paths!(nodes[0], payment_preimage);
249                 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &b_htlc_msgs.commitment_signed);
250                 check_added_monitors!(nodes[0], 1);
251                 let (as_raa, as_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
252                 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
253                 let _htlc_updates = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
254                 check_added_monitors!(nodes[1], 1);
255                 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs);
256                 let _bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
257                 check_added_monitors!(nodes[1], 1);
258         }
259
260         // Once B has received the payment preimage, it includes the value of the HTLC in its
261         // "claimable if you were to close the channel" balance.
262         let mut a_expected_balances = vec![Balance::ClaimableOnChannelClose {
263                         claimable_amount_satoshis: 1_000_000 - // Channel funding value in satoshis
264                                 4_000 - // The to-be-failed HTLC value in satoshis
265                                 3_000 - // The claimed HTLC value in satoshis
266                                 1_000 - // The push_msat value in satoshis
267                                 3 - // The dust HTLC value in satoshis
268                                 // The commitment transaction fee with two HTLC outputs:
269                                 chan_feerate * (channel::commitment_tx_base_weight(opt_anchors) +
270                                                                 if prev_commitment_tx { 1 } else { 2 } *
271                                                                 channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
272                 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
273                         claimable_amount_satoshis: 4_000,
274                         claimable_height: htlc_cltv_timeout,
275                 }];
276         if !prev_commitment_tx {
277                 a_expected_balances.push(Balance::MaybeClaimableHTLCAwaitingTimeout {
278                         claimable_amount_satoshis: 3_000,
279                         claimable_height: htlc_cltv_timeout,
280                 });
281         }
282         assert_eq!(sorted_vec(a_expected_balances),
283                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
284         assert_eq!(vec![Balance::ClaimableOnChannelClose {
285                         claimable_amount_satoshis: 1_000 + 3_000 + 4_000,
286                 }],
287                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
288
289         // Broadcast the closing transaction (which has both pending HTLCs in it) and get B's
290         // broadcasted HTLC claim transaction with preimage.
291         let node_b_commitment_claimable = nodes[1].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
292         mine_transaction(&nodes[0], &remote_txn[0]);
293         mine_transaction(&nodes[1], &remote_txn[0]);
294
295         let b_broadcast_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
296         assert_eq!(b_broadcast_txn.len(), if prev_commitment_tx { 4 } else { 5 });
297         if prev_commitment_tx {
298                 check_spends!(b_broadcast_txn[3], b_broadcast_txn[2]);
299         } else {
300                 assert_eq!(b_broadcast_txn[0], b_broadcast_txn[3]);
301                 assert_eq!(b_broadcast_txn[1], b_broadcast_txn[4]);
302         }
303         // b_broadcast_txn[0] should spend the HTLC output of the commitment tx for 3_000 sats
304         check_spends!(b_broadcast_txn[0], remote_txn[0]);
305         check_spends!(b_broadcast_txn[1], remote_txn[0]);
306         assert_eq!(b_broadcast_txn[0].input.len(), 1);
307         assert_eq!(b_broadcast_txn[1].input.len(), 1);
308         assert_eq!(remote_txn[0].output[b_broadcast_txn[0].input[0].previous_output.vout as usize].value, 3_000);
309         assert_eq!(remote_txn[0].output[b_broadcast_txn[1].input[0].previous_output.vout as usize].value, 4_000);
310         check_spends!(b_broadcast_txn[2], funding_tx);
311
312         assert!(nodes[0].node.list_channels().is_empty());
313         check_closed_broadcast!(nodes[0], true);
314         check_added_monitors!(nodes[0], 1);
315         check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
316         assert!(nodes[1].node.list_channels().is_empty());
317         check_closed_broadcast!(nodes[1], true);
318         check_added_monitors!(nodes[1], 1);
319         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
320         assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
321         assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
322
323         // Once the commitment transaction confirms, we will wait until ANTI_REORG_DELAY until we
324         // generate any `SpendableOutputs` events. Thus, the same balances will still be listed
325         // available in `get_claimable_balances`. However, both will swap from `ClaimableOnClose` to
326         // other Balance variants, as close has already happened.
327         assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
328         assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
329
330         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
331                         claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
332                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
333                         confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
334                 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
335                         claimable_amount_satoshis: 3_000,
336                         claimable_height: htlc_cltv_timeout,
337                 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
338                         claimable_amount_satoshis: 4_000,
339                         claimable_height: htlc_cltv_timeout,
340                 }]),
341                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
342         // The main non-HTLC balance is just awaiting confirmations, but the claimable height is the
343         // CSV delay, not ANTI_REORG_DELAY.
344         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
345                         claimable_amount_satoshis: 1_000,
346                         confirmation_height: node_b_commitment_claimable,
347                 },
348                 // Both HTLC balances are "contentious" as our counterparty could claim them if we wait too
349                 // long.
350                 Balance::ContentiousClaimable {
351                         claimable_amount_satoshis: 3_000,
352                         timeout_height: htlc_cltv_timeout,
353                 }, Balance::ContentiousClaimable {
354                         claimable_amount_satoshis: 4_000,
355                         timeout_height: htlc_cltv_timeout,
356                 }]),
357                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
358
359         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
360         expect_payment_failed!(nodes[0], dust_payment_hash, true);
361         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
362
363         // After ANTI_REORG_DELAY, A will consider its balance fully spendable and generate a
364         // `SpendableOutputs` event. However, B still has to wait for the CSV delay.
365         assert_eq!(sorted_vec(vec![Balance::MaybeClaimableHTLCAwaitingTimeout {
366                         claimable_amount_satoshis: 3_000,
367                         claimable_height: htlc_cltv_timeout,
368                 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
369                         claimable_amount_satoshis: 4_000,
370                         claimable_height: htlc_cltv_timeout,
371                 }]),
372                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
373         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
374                         claimable_amount_satoshis: 1_000,
375                         confirmation_height: node_b_commitment_claimable,
376                 }, Balance::ContentiousClaimable {
377                         claimable_amount_satoshis: 3_000,
378                         timeout_height: htlc_cltv_timeout,
379                 }, Balance::ContentiousClaimable {
380                         claimable_amount_satoshis: 4_000,
381                         timeout_height: htlc_cltv_timeout,
382                 }]),
383                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
384
385         test_spendable_output(&nodes[0], &remote_txn[0]);
386         assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
387
388         // After broadcasting the HTLC claim transaction, node A will still consider the HTLC
389         // possibly-claimable up to ANTI_REORG_DELAY, at which point it will drop it.
390         mine_transaction(&nodes[0], &b_broadcast_txn[0]);
391         if prev_commitment_tx {
392                 expect_payment_path_successful!(nodes[0]);
393         } else {
394                 expect_payment_sent!(nodes[0], payment_preimage);
395         }
396         assert_eq!(sorted_vec(vec![Balance::MaybeClaimableHTLCAwaitingTimeout {
397                         claimable_amount_satoshis: 3_000,
398                         claimable_height: htlc_cltv_timeout,
399                 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
400                         claimable_amount_satoshis: 4_000,
401                         claimable_height: htlc_cltv_timeout,
402                 }]),
403                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
404         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
405         assert_eq!(vec![Balance::MaybeClaimableHTLCAwaitingTimeout {
406                         claimable_amount_satoshis: 4_000,
407                         claimable_height: htlc_cltv_timeout,
408                 }],
409                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
410
411         // When the HTLC timeout output is spendable in the next block, A should broadcast it
412         connect_blocks(&nodes[0], htlc_cltv_timeout - nodes[0].best_block_info().1 - 1);
413         let a_broadcast_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
414         assert_eq!(a_broadcast_txn.len(), 3);
415         check_spends!(a_broadcast_txn[0], funding_tx);
416         assert_eq!(a_broadcast_txn[1].input.len(), 1);
417         check_spends!(a_broadcast_txn[1], remote_txn[0]);
418         assert_eq!(a_broadcast_txn[2].input.len(), 1);
419         check_spends!(a_broadcast_txn[2], remote_txn[0]);
420         assert_ne!(a_broadcast_txn[1].input[0].previous_output.vout,
421                    a_broadcast_txn[2].input[0].previous_output.vout);
422         // a_broadcast_txn [1] and [2] should spend the HTLC outputs of the commitment tx
423         assert_eq!(remote_txn[0].output[a_broadcast_txn[1].input[0].previous_output.vout as usize].value, 3_000);
424         assert_eq!(remote_txn[0].output[a_broadcast_txn[2].input[0].previous_output.vout as usize].value, 4_000);
425
426         // Once the HTLC-Timeout transaction confirms, A will no longer consider the HTLC
427         // "MaybeClaimable", but instead move it to "AwaitingConfirmations".
428         mine_transaction(&nodes[0], &a_broadcast_txn[2]);
429         assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
430         assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
431                         claimable_amount_satoshis: 4_000,
432                         confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
433                 }],
434                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
435         // After ANTI_REORG_DELAY, A will generate a SpendableOutputs event and drop the claimable
436         // balance entry.
437         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
438         assert_eq!(Vec::<Balance>::new(),
439                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
440         expect_payment_failed!(nodes[0], timeout_payment_hash, true);
441
442         test_spendable_output(&nodes[0], &a_broadcast_txn[2]);
443
444         // Node B will no longer consider the HTLC "contentious" after the HTLC claim transaction
445         // confirms, and consider it simply "awaiting confirmations". Note that it has to wait for the
446         // standard revocable transaction CSV delay before receiving a `SpendableOutputs`.
447         let node_b_htlc_claimable = nodes[1].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
448         mine_transaction(&nodes[1], &b_broadcast_txn[0]);
449
450         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
451                         claimable_amount_satoshis: 1_000,
452                         confirmation_height: node_b_commitment_claimable,
453                 }, Balance::ClaimableAwaitingConfirmations {
454                         claimable_amount_satoshis: 3_000,
455                         confirmation_height: node_b_htlc_claimable,
456                 }, Balance::ContentiousClaimable {
457                         claimable_amount_satoshis: 4_000,
458                         timeout_height: htlc_cltv_timeout,
459                 }]),
460                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
461
462         // After reaching the commitment output CSV, we'll get a SpendableOutputs event for it and have
463         // only the HTLCs claimable on node B.
464         connect_blocks(&nodes[1], node_b_commitment_claimable - nodes[1].best_block_info().1);
465         test_spendable_output(&nodes[1], &remote_txn[0]);
466
467         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
468                         claimable_amount_satoshis: 3_000,
469                         confirmation_height: node_b_htlc_claimable,
470                 }, Balance::ContentiousClaimable {
471                         claimable_amount_satoshis: 4_000,
472                         timeout_height: htlc_cltv_timeout,
473                 }]),
474                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
475
476         // After reaching the claimed HTLC output CSV, we'll get a SpendableOutptus event for it and
477         // have only one HTLC output left spendable.
478         connect_blocks(&nodes[1], node_b_htlc_claimable - nodes[1].best_block_info().1);
479         test_spendable_output(&nodes[1], &b_broadcast_txn[0]);
480
481         assert_eq!(vec![Balance::ContentiousClaimable {
482                         claimable_amount_satoshis: 4_000,
483                         timeout_height: htlc_cltv_timeout,
484                 }],
485                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
486
487         // Finally, mine the HTLC timeout transaction that A broadcasted (even though B should be able
488         // to claim this HTLC with the preimage it knows!). It will remain listed as a claimable HTLC
489         // until ANTI_REORG_DELAY confirmations on the spend.
490         mine_transaction(&nodes[1], &a_broadcast_txn[2]);
491         assert_eq!(vec![Balance::ContentiousClaimable {
492                         claimable_amount_satoshis: 4_000,
493                         timeout_height: htlc_cltv_timeout,
494                 }],
495                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
496         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
497         assert_eq!(Vec::<Balance>::new(),
498                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
499 }
500
501 #[test]
502 fn test_claim_value_force_close() {
503         do_test_claim_value_force_close(true);
504         do_test_claim_value_force_close(false);
505 }
506
507 #[test]
508 fn test_balances_on_local_commitment_htlcs() {
509         // Previously, when handling the broadcast of a local commitment transactions (with associated
510         // CSV delays prior to spendability), we incorrectly handled the CSV delays on HTLC
511         // transactions. This caused us to miss spendable outputs for HTLCs which were awaiting a CSV
512         // delay prior to spendability.
513         //
514         // Further, because of this, we could hit an assertion as `get_claimable_balances` asserted
515         // that HTLCs were resolved after the funding spend was resolved, which was not true if the
516         // HTLC did not have a CSV delay attached (due to the above bug or due to it being an HTLC
517         // claim by our counterparty).
518         let chanmon_cfgs = create_chanmon_cfgs(2);
519         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
520         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
521         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
522
523         // Create a single channel with two pending HTLCs from nodes[0] to nodes[1], one which nodes[1]
524         // knows the preimage for, one which it does not.
525         let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
526         let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
527
528         let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 10_000_000);
529         let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
530         nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
531         check_added_monitors!(nodes[0], 1);
532
533         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
534         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
535         commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
536
537         expect_pending_htlcs_forwardable!(nodes[1]);
538         expect_payment_received!(nodes[1], payment_hash, payment_secret, 10_000_000);
539
540         let (route_2, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 20_000_000);
541         nodes[0].node.send_payment(&route_2, payment_hash_2, &Some(payment_secret_2)).unwrap();
542         check_added_monitors!(nodes[0], 1);
543
544         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
545         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
546         commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
547
548         expect_pending_htlcs_forwardable!(nodes[1]);
549         expect_payment_received!(nodes[1], payment_hash_2, payment_secret_2, 20_000_000);
550         assert!(nodes[1].node.claim_funds(payment_preimage_2));
551         get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
552         check_added_monitors!(nodes[1], 1);
553
554         let chan_feerate = get_feerate!(nodes[0], chan_id) as u64;
555         let opt_anchors = get_opt_anchors!(nodes[0], chan_id);
556
557         // Get nodes[0]'s commitment transaction and HTLC-Timeout transactions
558         let as_txn = get_local_commitment_txn!(nodes[0], chan_id);
559         assert_eq!(as_txn.len(), 3);
560         check_spends!(as_txn[1], as_txn[0]);
561         check_spends!(as_txn[2], as_txn[0]);
562         check_spends!(as_txn[0], funding_tx);
563
564         // First confirm the commitment transaction on nodes[0], which should leave us with three
565         // claimable balances.
566         let node_a_commitment_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
567         mine_transaction(&nodes[0], &as_txn[0]);
568         check_added_monitors!(nodes[0], 1);
569         check_closed_broadcast!(nodes[0], true);
570         check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
571
572         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
573                         claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
574                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
575                         confirmation_height: node_a_commitment_claimable,
576                 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
577                         claimable_amount_satoshis: 10_000,
578                         claimable_height: htlc_cltv_timeout,
579                 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
580                         claimable_amount_satoshis: 20_000,
581                         claimable_height: htlc_cltv_timeout,
582                 }]),
583                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
584
585         // Get nodes[1]'s HTLC claim tx for the second HTLC
586         mine_transaction(&nodes[1], &as_txn[0]);
587         check_added_monitors!(nodes[1], 1);
588         check_closed_broadcast!(nodes[1], true);
589         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
590         let bs_htlc_claim_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
591         assert_eq!(bs_htlc_claim_txn.len(), 3);
592         check_spends!(bs_htlc_claim_txn[0], as_txn[0]);
593         check_spends!(bs_htlc_claim_txn[1], funding_tx);
594         check_spends!(bs_htlc_claim_txn[2], bs_htlc_claim_txn[1]);
595
596         // Connect blocks until the HTLCs expire, allowing us to (validly) broadcast the HTLC-Timeout
597         // transaction.
598         connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1);
599         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
600                         claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
601                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
602                         confirmation_height: node_a_commitment_claimable,
603                 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
604                         claimable_amount_satoshis: 10_000,
605                         claimable_height: htlc_cltv_timeout,
606                 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
607                         claimable_amount_satoshis: 20_000,
608                         claimable_height: htlc_cltv_timeout,
609                 }]),
610                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
611         assert_eq!(as_txn[1].lock_time, nodes[0].best_block_info().1 + 1); // as_txn[1] can be included in the next block
612
613         // Now confirm nodes[0]'s HTLC-Timeout transaction, which changes the claimable balance to an
614         // "awaiting confirmations" one.
615         let node_a_htlc_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
616         mine_transaction(&nodes[0], &as_txn[1]);
617         // Note that prior to the fix in the commit which introduced this test, this (and the next
618         // balance) check failed. With this check removed, the code panicked in the `connect_blocks`
619         // call, as described, two hunks down.
620         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
621                         claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
622                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
623                         confirmation_height: node_a_commitment_claimable,
624                 }, Balance::ClaimableAwaitingConfirmations {
625                         claimable_amount_satoshis: 10_000,
626                         confirmation_height: node_a_htlc_claimable,
627                 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
628                         claimable_amount_satoshis: 20_000,
629                         claimable_height: htlc_cltv_timeout,
630                 }]),
631                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
632
633         // Now confirm nodes[1]'s HTLC claim, giving nodes[0] the preimage. Note that the "maybe
634         // claimable" balance remains until we see ANTI_REORG_DELAY blocks.
635         mine_transaction(&nodes[0], &bs_htlc_claim_txn[0]);
636         expect_payment_sent!(nodes[0], payment_preimage_2);
637         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
638                         claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
639                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
640                         confirmation_height: node_a_commitment_claimable,
641                 }, Balance::ClaimableAwaitingConfirmations {
642                         claimable_amount_satoshis: 10_000,
643                         confirmation_height: node_a_htlc_claimable,
644                 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
645                         claimable_amount_satoshis: 20_000,
646                         claimable_height: htlc_cltv_timeout,
647                 }]),
648                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
649
650         // Finally make the HTLC transactions have ANTI_REORG_DELAY blocks. This call previously
651         // panicked as described in the test introduction. This will remove the "maybe claimable"
652         // spendable output as nodes[1] has fully claimed the second HTLC.
653         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
654         expect_payment_failed!(nodes[0], payment_hash, true);
655
656         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
657                         claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
658                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
659                         confirmation_height: node_a_commitment_claimable,
660                 }, Balance::ClaimableAwaitingConfirmations {
661                         claimable_amount_satoshis: 10_000,
662                         confirmation_height: node_a_htlc_claimable,
663                 }]),
664                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
665
666         // Connect blocks until the commitment transaction's CSV expires, providing us the relevant
667         // `SpendableOutputs` event and removing the claimable balance entry.
668         connect_blocks(&nodes[0], node_a_commitment_claimable - nodes[0].best_block_info().1);
669         assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
670                         claimable_amount_satoshis: 10_000,
671                         confirmation_height: node_a_htlc_claimable,
672                 }],
673                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
674         test_spendable_output(&nodes[0], &as_txn[0]);
675
676         // Connect blocks until the HTLC-Timeout's CSV expires, providing us the relevant
677         // `SpendableOutputs` event and removing the claimable balance entry.
678         connect_blocks(&nodes[0], node_a_htlc_claimable - nodes[0].best_block_info().1);
679         assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
680         test_spendable_output(&nodes[0], &as_txn[1]);
681 }