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