Merge pull request #1339 from TheBlueMatt/2022-02-0.0.105-sec
[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).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 = route_payment(&nodes[0], &[&nodes[1]], 3_000_000).0;
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         let b_htlc_msgs = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
240         // We claim the dust payment here as well, but it won't impact our claimable balances as its
241         // dust and thus doesn't appear on chain at all.
242         nodes[1].node.claim_funds(dust_payment_preimage);
243         check_added_monitors!(nodes[1], 1);
244         nodes[1].node.claim_funds(timeout_payment_preimage);
245         check_added_monitors!(nodes[1], 1);
246
247         if prev_commitment_tx {
248                 // To build a previous commitment transaction, deliver one round of commitment messages.
249                 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &b_htlc_msgs.update_fulfill_htlcs[0]);
250                 expect_payment_sent_without_paths!(nodes[0], payment_preimage);
251                 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &b_htlc_msgs.commitment_signed);
252                 check_added_monitors!(nodes[0], 1);
253                 let (as_raa, as_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
254                 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
255                 let _htlc_updates = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
256                 check_added_monitors!(nodes[1], 1);
257                 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs);
258                 let _bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
259                 check_added_monitors!(nodes[1], 1);
260         }
261
262         // Once B has received the payment preimage, it includes the value of the HTLC in its
263         // "claimable if you were to close the channel" balance.
264         let mut a_expected_balances = vec![Balance::ClaimableOnChannelClose {
265                         claimable_amount_satoshis: 1_000_000 - // Channel funding value in satoshis
266                                 4_000 - // The to-be-failed HTLC value in satoshis
267                                 3_000 - // The claimed HTLC value in satoshis
268                                 1_000 - // The push_msat value in satoshis
269                                 3 - // The dust HTLC value in satoshis
270                                 // The commitment transaction fee with two HTLC outputs:
271                                 chan_feerate * (channel::commitment_tx_base_weight(opt_anchors) +
272                                                                 if prev_commitment_tx { 1 } else { 2 } *
273                                                                 channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
274                 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
275                         claimable_amount_satoshis: 4_000,
276                         claimable_height: htlc_cltv_timeout,
277                 }];
278         if !prev_commitment_tx {
279                 a_expected_balances.push(Balance::MaybeClaimableHTLCAwaitingTimeout {
280                         claimable_amount_satoshis: 3_000,
281                         claimable_height: htlc_cltv_timeout,
282                 });
283         }
284         assert_eq!(sorted_vec(a_expected_balances),
285                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
286         assert_eq!(vec![Balance::ClaimableOnChannelClose {
287                         claimable_amount_satoshis: 1_000 + 3_000 + 4_000,
288                 }],
289                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
290
291         // Broadcast the closing transaction (which has both pending HTLCs in it) and get B's
292         // broadcasted HTLC claim transaction with preimage.
293         let node_b_commitment_claimable = nodes[1].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
294         mine_transaction(&nodes[0], &remote_txn[0]);
295         mine_transaction(&nodes[1], &remote_txn[0]);
296
297         let b_broadcast_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
298         assert_eq!(b_broadcast_txn.len(), if prev_commitment_tx { 4 } else { 5 });
299         if prev_commitment_tx {
300                 check_spends!(b_broadcast_txn[3], b_broadcast_txn[2]);
301         } else {
302                 assert_eq!(b_broadcast_txn[0], b_broadcast_txn[3]);
303                 assert_eq!(b_broadcast_txn[1], b_broadcast_txn[4]);
304         }
305         // b_broadcast_txn[0] should spend the HTLC output of the commitment tx for 3_000 sats
306         check_spends!(b_broadcast_txn[0], remote_txn[0]);
307         check_spends!(b_broadcast_txn[1], remote_txn[0]);
308         assert_eq!(b_broadcast_txn[0].input.len(), 1);
309         assert_eq!(b_broadcast_txn[1].input.len(), 1);
310         assert_eq!(remote_txn[0].output[b_broadcast_txn[0].input[0].previous_output.vout as usize].value, 3_000);
311         assert_eq!(remote_txn[0].output[b_broadcast_txn[1].input[0].previous_output.vout as usize].value, 4_000);
312         check_spends!(b_broadcast_txn[2], funding_tx);
313
314         assert!(nodes[0].node.list_channels().is_empty());
315         check_closed_broadcast!(nodes[0], true);
316         check_added_monitors!(nodes[0], 1);
317         check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
318         assert!(nodes[1].node.list_channels().is_empty());
319         check_closed_broadcast!(nodes[1], true);
320         check_added_monitors!(nodes[1], 1);
321         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
322         assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
323         assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
324
325         // Once the commitment transaction confirms, we will wait until ANTI_REORG_DELAY until we
326         // generate any `SpendableOutputs` events. Thus, the same balances will still be listed
327         // available in `get_claimable_balances`. However, both will swap from `ClaimableOnClose` to
328         // other Balance variants, as close has already happened.
329         assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
330         assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
331
332         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
333                         claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
334                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
335                         confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
336                 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
337                         claimable_amount_satoshis: 3_000,
338                         claimable_height: htlc_cltv_timeout,
339                 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
340                         claimable_amount_satoshis: 4_000,
341                         claimable_height: htlc_cltv_timeout,
342                 }]),
343                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
344         // The main non-HTLC balance is just awaiting confirmations, but the claimable height is the
345         // CSV delay, not ANTI_REORG_DELAY.
346         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
347                         claimable_amount_satoshis: 1_000,
348                         confirmation_height: node_b_commitment_claimable,
349                 },
350                 // Both HTLC balances are "contentious" as our counterparty could claim them if we wait too
351                 // long.
352                 Balance::ContentiousClaimable {
353                         claimable_amount_satoshis: 3_000,
354                         timeout_height: htlc_cltv_timeout,
355                 }, Balance::ContentiousClaimable {
356                         claimable_amount_satoshis: 4_000,
357                         timeout_height: htlc_cltv_timeout,
358                 }]),
359                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
360
361         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
362         expect_payment_failed!(nodes[0], dust_payment_hash, true);
363         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
364
365         // After ANTI_REORG_DELAY, A will consider its balance fully spendable and generate a
366         // `SpendableOutputs` event. However, B still has to wait for the CSV delay.
367         assert_eq!(sorted_vec(vec![Balance::MaybeClaimableHTLCAwaitingTimeout {
368                         claimable_amount_satoshis: 3_000,
369                         claimable_height: htlc_cltv_timeout,
370                 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
371                         claimable_amount_satoshis: 4_000,
372                         claimable_height: htlc_cltv_timeout,
373                 }]),
374                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
375         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
376                         claimable_amount_satoshis: 1_000,
377                         confirmation_height: node_b_commitment_claimable,
378                 }, Balance::ContentiousClaimable {
379                         claimable_amount_satoshis: 3_000,
380                         timeout_height: htlc_cltv_timeout,
381                 }, Balance::ContentiousClaimable {
382                         claimable_amount_satoshis: 4_000,
383                         timeout_height: htlc_cltv_timeout,
384                 }]),
385                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
386
387         let mut node_a_spendable = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
388         assert_eq!(node_a_spendable.len(), 1);
389         if let Event::SpendableOutputs { outputs } = node_a_spendable.pop().unwrap() {
390                 assert_eq!(outputs.len(), 1);
391                 let spend_tx = nodes[0].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
392                         Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
393                 check_spends!(spend_tx, remote_txn[0]);
394         }
395
396         assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
397
398         // After broadcasting the HTLC claim transaction, node A will still consider the HTLC
399         // possibly-claimable up to ANTI_REORG_DELAY, at which point it will drop it.
400         mine_transaction(&nodes[0], &b_broadcast_txn[0]);
401         if prev_commitment_tx {
402                 expect_payment_path_successful!(nodes[0]);
403         } else {
404                 expect_payment_sent!(nodes[0], payment_preimage);
405         }
406         assert_eq!(sorted_vec(vec![Balance::MaybeClaimableHTLCAwaitingTimeout {
407                         claimable_amount_satoshis: 3_000,
408                         claimable_height: htlc_cltv_timeout,
409                 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
410                         claimable_amount_satoshis: 4_000,
411                         claimable_height: htlc_cltv_timeout,
412                 }]),
413                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
414         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
415         assert_eq!(vec![Balance::MaybeClaimableHTLCAwaitingTimeout {
416                         claimable_amount_satoshis: 4_000,
417                         claimable_height: htlc_cltv_timeout,
418                 }],
419                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
420
421         // When the HTLC timeout output is spendable in the next block, A should broadcast it
422         connect_blocks(&nodes[0], htlc_cltv_timeout - nodes[0].best_block_info().1 - 1);
423         let a_broadcast_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
424         assert_eq!(a_broadcast_txn.len(), 3);
425         check_spends!(a_broadcast_txn[0], funding_tx);
426         assert_eq!(a_broadcast_txn[1].input.len(), 1);
427         check_spends!(a_broadcast_txn[1], remote_txn[0]);
428         assert_eq!(a_broadcast_txn[2].input.len(), 1);
429         check_spends!(a_broadcast_txn[2], remote_txn[0]);
430         assert_ne!(a_broadcast_txn[1].input[0].previous_output.vout,
431                    a_broadcast_txn[2].input[0].previous_output.vout);
432         // a_broadcast_txn [1] and [2] should spend the HTLC outputs of the commitment tx
433         assert_eq!(remote_txn[0].output[a_broadcast_txn[1].input[0].previous_output.vout as usize].value, 3_000);
434         assert_eq!(remote_txn[0].output[a_broadcast_txn[2].input[0].previous_output.vout as usize].value, 4_000);
435
436         // Once the HTLC-Timeout transaction confirms, A will no longer consider the HTLC
437         // "MaybeClaimable", but instead move it to "AwaitingConfirmations".
438         mine_transaction(&nodes[0], &a_broadcast_txn[2]);
439         assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
440         assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
441                         claimable_amount_satoshis: 4_000,
442                         confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
443                 }],
444                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
445         // After ANTI_REORG_DELAY, A will generate a SpendableOutputs event and drop the claimable
446         // balance entry.
447         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
448         assert_eq!(Vec::<Balance>::new(),
449                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
450         expect_payment_failed!(nodes[0], timeout_payment_hash, true);
451
452         let mut node_a_spendable = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
453         assert_eq!(node_a_spendable.len(), 1);
454         if let Event::SpendableOutputs { outputs } = node_a_spendable.pop().unwrap() {
455                 assert_eq!(outputs.len(), 1);
456                 let spend_tx = nodes[0].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
457                         Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
458                 check_spends!(spend_tx, a_broadcast_txn[2]);
459         } else { panic!(); }
460
461         // Node B will no longer consider the HTLC "contentious" after the HTLC claim transaction
462         // confirms, and consider it simply "awaiting confirmations". Note that it has to wait for the
463         // standard revocable transaction CSV delay before receiving a `SpendableOutputs`.
464         let node_b_htlc_claimable = nodes[1].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
465         mine_transaction(&nodes[1], &b_broadcast_txn[0]);
466
467         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
468                         claimable_amount_satoshis: 1_000,
469                         confirmation_height: node_b_commitment_claimable,
470                 }, Balance::ClaimableAwaitingConfirmations {
471                         claimable_amount_satoshis: 3_000,
472                         confirmation_height: node_b_htlc_claimable,
473                 }, Balance::ContentiousClaimable {
474                         claimable_amount_satoshis: 4_000,
475                         timeout_height: htlc_cltv_timeout,
476                 }]),
477                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
478
479         // After reaching the commitment output CSV, we'll get a SpendableOutputs event for it and have
480         // only the HTLCs claimable on node B.
481         connect_blocks(&nodes[1], node_b_commitment_claimable - nodes[1].best_block_info().1);
482
483         let mut node_b_spendable = nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events();
484         assert_eq!(node_b_spendable.len(), 1);
485         if let Event::SpendableOutputs { outputs } = node_b_spendable.pop().unwrap() {
486                 assert_eq!(outputs.len(), 1);
487                 let spend_tx = nodes[1].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
488                         Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
489                 check_spends!(spend_tx, remote_txn[0]);
490         }
491
492         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
493                         claimable_amount_satoshis: 3_000,
494                         confirmation_height: node_b_htlc_claimable,
495                 }, Balance::ContentiousClaimable {
496                         claimable_amount_satoshis: 4_000,
497                         timeout_height: htlc_cltv_timeout,
498                 }]),
499                 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
500
501         // After reaching the claimed HTLC output CSV, we'll get a SpendableOutptus event for it and
502         // have only one HTLC output left spendable.
503         connect_blocks(&nodes[1], node_b_htlc_claimable - nodes[1].best_block_info().1);
504
505         let mut node_b_spendable = nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events();
506         assert_eq!(node_b_spendable.len(), 1);
507         if let Event::SpendableOutputs { outputs } = node_b_spendable.pop().unwrap() {
508                 assert_eq!(outputs.len(), 1);
509                 let spend_tx = nodes[1].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
510                         Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
511                 check_spends!(spend_tx, b_broadcast_txn[0]);
512         } else { panic!(); }
513
514         assert_eq!(vec![Balance::ContentiousClaimable {
515                         claimable_amount_satoshis: 4_000,
516                         timeout_height: htlc_cltv_timeout,
517                 }],
518                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
519
520         // Finally, mine the HTLC timeout transaction that A broadcasted (even though B should be able
521         // to claim this HTLC with the preimage it knows!). It will remain listed as a claimable HTLC
522         // until ANTI_REORG_DELAY confirmations on the spend.
523         mine_transaction(&nodes[1], &a_broadcast_txn[2]);
524         assert_eq!(vec![Balance::ContentiousClaimable {
525                         claimable_amount_satoshis: 4_000,
526                         timeout_height: htlc_cltv_timeout,
527                 }],
528                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
529         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
530         assert_eq!(Vec::<Balance>::new(),
531                 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
532 }
533
534 #[test]
535 fn test_claim_value_force_close() {
536         do_test_claim_value_force_close(true);
537         do_test_claim_value_force_close(false);
538 }
539
540 #[test]
541 fn test_balances_on_local_commitment_htlcs() {
542         // Previously, when handling the broadcast of a local commitment transactions (with associated
543         // CSV delays prior to spendability), we incorrectly handled the CSV delays on HTLC
544         // transactions. This caused us to miss spendable outputs for HTLCs which were awaiting a CSV
545         // delay prior to spendability.
546         //
547         // Further, because of this, we could hit an assertion as `get_claimable_balances` asserted
548         // that HTLCs were resolved after the funding spend was resolved, which was not true if the
549         // HTLC did not have a CSV delay attached (due to the above bug or due to it being an HTLC
550         // claim by our counterparty).
551         let chanmon_cfgs = create_chanmon_cfgs(2);
552         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
553         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
554         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
555
556         // Create a single channel with two pending HTLCs from nodes[0] to nodes[1], one which nodes[1]
557         // knows the preimage for, one which it does not.
558         let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
559         let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
560
561         let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 10_000_000);
562         let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
563         nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
564         check_added_monitors!(nodes[0], 1);
565
566         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
567         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
568         commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
569
570         expect_pending_htlcs_forwardable!(nodes[1]);
571         expect_payment_received!(nodes[1], payment_hash, payment_secret, 10_000_000);
572
573         let (route_2, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 20_000_000);
574         nodes[0].node.send_payment(&route_2, payment_hash_2, &Some(payment_secret_2)).unwrap();
575         check_added_monitors!(nodes[0], 1);
576
577         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
578         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
579         commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
580
581         expect_pending_htlcs_forwardable!(nodes[1]);
582         expect_payment_received!(nodes[1], payment_hash_2, payment_secret_2, 20_000_000);
583         assert!(nodes[1].node.claim_funds(payment_preimage_2));
584         get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
585         check_added_monitors!(nodes[1], 1);
586
587         let chan_feerate = get_feerate!(nodes[0], chan_id) as u64;
588         let opt_anchors = get_opt_anchors!(nodes[0], chan_id);
589
590         // Get nodes[0]'s commitment transaction and HTLC-Timeout transactions
591         let as_txn = get_local_commitment_txn!(nodes[0], chan_id);
592         assert_eq!(as_txn.len(), 3);
593         check_spends!(as_txn[1], as_txn[0]);
594         check_spends!(as_txn[2], as_txn[0]);
595         check_spends!(as_txn[0], funding_tx);
596
597         // First confirm the commitment transaction on nodes[0], which should leave us with three
598         // claimable balances.
599         let node_a_commitment_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
600         mine_transaction(&nodes[0], &as_txn[0]);
601         check_added_monitors!(nodes[0], 1);
602         check_closed_broadcast!(nodes[0], true);
603         check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
604
605         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
606                         claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
607                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
608                         confirmation_height: node_a_commitment_claimable,
609                 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
610                         claimable_amount_satoshis: 10_000,
611                         claimable_height: htlc_cltv_timeout,
612                 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
613                         claimable_amount_satoshis: 20_000,
614                         claimable_height: htlc_cltv_timeout,
615                 }]),
616                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
617
618         // Get nodes[1]'s HTLC claim tx for the second HTLC
619         mine_transaction(&nodes[1], &as_txn[0]);
620         check_added_monitors!(nodes[1], 1);
621         check_closed_broadcast!(nodes[1], true);
622         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
623         let bs_htlc_claim_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
624         assert_eq!(bs_htlc_claim_txn.len(), 3);
625         check_spends!(bs_htlc_claim_txn[0], as_txn[0]);
626         check_spends!(bs_htlc_claim_txn[1], funding_tx);
627         check_spends!(bs_htlc_claim_txn[2], bs_htlc_claim_txn[1]);
628
629         // Connect blocks until the HTLCs expire, allowing us to (validly) broadcast the HTLC-Timeout
630         // transaction.
631         connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1);
632         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
633                         claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
634                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
635                         confirmation_height: node_a_commitment_claimable,
636                 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
637                         claimable_amount_satoshis: 10_000,
638                         claimable_height: htlc_cltv_timeout,
639                 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
640                         claimable_amount_satoshis: 20_000,
641                         claimable_height: htlc_cltv_timeout,
642                 }]),
643                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
644         assert_eq!(as_txn[1].lock_time, nodes[0].best_block_info().1 + 1); // as_txn[1] can be included in the next block
645
646         // Now confirm nodes[0]'s HTLC-Timeout transaction, which changes the claimable balance to an
647         // "awaiting confirmations" one.
648         let node_a_htlc_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
649         mine_transaction(&nodes[0], &as_txn[1]);
650         // Note that prior to the fix in the commit which introduced this test, this (and the next
651         // balance) check failed. With this check removed, the code panicked in the `connect_blocks`
652         // call, as described, two hunks down.
653         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
654                         claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
655                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
656                         confirmation_height: node_a_commitment_claimable,
657                 }, Balance::ClaimableAwaitingConfirmations {
658                         claimable_amount_satoshis: 10_000,
659                         confirmation_height: node_a_htlc_claimable,
660                 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
661                         claimable_amount_satoshis: 20_000,
662                         claimable_height: htlc_cltv_timeout,
663                 }]),
664                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
665
666         // Now confirm nodes[1]'s HTLC claim, giving nodes[0] the preimage. Note that the "maybe
667         // claimable" balance remains until we see ANTI_REORG_DELAY blocks.
668         mine_transaction(&nodes[0], &bs_htlc_claim_txn[0]);
669         expect_payment_sent!(nodes[0], payment_preimage_2);
670         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
671                         claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
672                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
673                         confirmation_height: node_a_commitment_claimable,
674                 }, Balance::ClaimableAwaitingConfirmations {
675                         claimable_amount_satoshis: 10_000,
676                         confirmation_height: node_a_htlc_claimable,
677                 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
678                         claimable_amount_satoshis: 20_000,
679                         claimable_height: htlc_cltv_timeout,
680                 }]),
681                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
682
683         // Finally make the HTLC transactions have ANTI_REORG_DELAY blocks. This call previously
684         // panicked as described in the test introduction. This will remove the "maybe claimable"
685         // spendable output as nodes[1] has fully claimed the second HTLC.
686         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
687         expect_payment_failed!(nodes[0], payment_hash, true);
688
689         assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
690                         claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
691                                 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
692                         confirmation_height: node_a_commitment_claimable,
693                 }, Balance::ClaimableAwaitingConfirmations {
694                         claimable_amount_satoshis: 10_000,
695                         confirmation_height: node_a_htlc_claimable,
696                 }]),
697                 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
698
699         // Connect blocks until the commitment transaction's CSV expires, providing us the relevant
700         // `SpendableOutputs` event and removing the claimable balance entry.
701         connect_blocks(&nodes[0], node_a_commitment_claimable - nodes[0].best_block_info().1);
702         assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
703                         claimable_amount_satoshis: 10_000,
704                         confirmation_height: node_a_htlc_claimable,
705                 }],
706                 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
707         let mut node_a_spendable = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
708         assert_eq!(node_a_spendable.len(), 1);
709         if let Event::SpendableOutputs { outputs } = node_a_spendable.pop().unwrap() {
710                 assert_eq!(outputs.len(), 1);
711                 let spend_tx = nodes[0].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
712                         Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
713                 check_spends!(spend_tx, as_txn[0]);
714         }
715
716         // Connect blocks until the HTLC-Timeout's CSV expires, providing us the relevant
717         // `SpendableOutputs` event and removing the claimable balance entry.
718         connect_blocks(&nodes[0], node_a_htlc_claimable - nodes[0].best_block_info().1);
719         assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
720         let mut node_a_spendable = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
721         assert_eq!(node_a_spendable.len(), 1);
722         if let Event::SpendableOutputs { outputs } = node_a_spendable.pop().unwrap() {
723                 assert_eq!(outputs.len(), 1);
724                 let spend_tx = nodes[0].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
725                         Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
726                 check_spends!(spend_tx, as_txn[1]);
727         }
728 }