1 // This file is Copyright its original authors, visible in version control
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
10 //! Further functional tests which test blockchain reorganizations.
12 use chain::channelmonitor::{ANTI_REORG_DELAY, Balance};
13 use chain::transaction::OutPoint;
15 use ln::channelmanager::BREAKDOWN_TIMEOUT;
16 use ln::features::InitFeatures;
17 use ln::msgs::ChannelMessageHandler;
18 use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason};
20 use bitcoin::blockdata::script::Builder;
21 use bitcoin::blockdata::opcodes;
22 use bitcoin::secp256k1::Secp256k1;
26 use ln::functional_test_utils::*;
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
37 // However, previously, we did not do this, ignoring the HTLC entirely.
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.
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);
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());
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);
57 let bs_txn = get_local_commitment_txn!(nodes[1], chan_id_2);
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);
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);
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());
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());
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);
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);
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);
101 let chan_feerate = get_feerate!(nodes[0], chan_id) as u64;
103 assert_eq!(vec![Balance::ClaimableOnChannelClose {
104 claimable_amount_satoshis: 1_000_000 - 1_000 - chan_feerate * channel::COMMITMENT_TX_BASE_WEIGHT / 1000
106 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
107 assert_eq!(vec![Balance::ClaimableOnChannelClose { claimable_amount_satoshis: 1_000, }],
108 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
110 nodes[0].node.close_channel(&chan_id).unwrap();
111 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
112 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &InitFeatures::known(), &node_0_shutdown);
113 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
114 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_1_shutdown);
116 let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
117 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
118 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
119 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
120 let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
121 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
122 let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
123 assert!(node_1_none.is_none());
125 let shutdown_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
126 assert_eq!(shutdown_tx, nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0));
127 assert_eq!(shutdown_tx.len(), 1);
129 mine_transaction(&nodes[0], &shutdown_tx[0]);
130 mine_transaction(&nodes[1], &shutdown_tx[0]);
132 assert!(nodes[0].node.list_channels().is_empty());
133 assert!(nodes[1].node.list_channels().is_empty());
135 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
136 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
138 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
139 claimable_amount_satoshis: 1_000_000 - 1_000 - chan_feerate * channel::COMMITMENT_TX_BASE_WEIGHT / 1000,
140 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
142 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
143 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
144 claimable_amount_satoshis: 1000,
145 confirmation_height: nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1,
147 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
149 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
150 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
152 assert_eq!(Vec::<Balance>::new(),
153 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
154 assert_eq!(Vec::<Balance>::new(),
155 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
157 let mut node_a_spendable = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
158 assert_eq!(node_a_spendable.len(), 1);
159 if let Event::SpendableOutputs { outputs } = node_a_spendable.pop().unwrap() {
160 assert_eq!(outputs.len(), 1);
161 let spend_tx = nodes[0].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
162 Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
163 check_spends!(spend_tx, shutdown_tx[0]);
166 let mut node_b_spendable = nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events();
167 assert_eq!(node_b_spendable.len(), 1);
168 if let Event::SpendableOutputs { outputs } = node_b_spendable.pop().unwrap() {
169 assert_eq!(outputs.len(), 1);
170 let spend_tx = nodes[1].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
171 Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
172 check_spends!(spend_tx, shutdown_tx[0]);
174 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure);
175 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure);
178 fn sorted_vec<T: Ord>(mut v: Vec<T>) -> Vec<T> {
183 fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
184 // Tests `get_claimable_balances` with an HTLC across a force-close.
185 // We build a channel with an HTLC pending, then force close the channel and check that the
186 // `get_claimable_balances` return value is correct as transactions confirm on-chain.
187 let mut chanmon_cfgs = create_chanmon_cfgs(2);
188 if prev_commitment_tx {
189 // We broadcast a second-to-latest commitment transaction, without providing the revocation
190 // secret to the counterparty. However, because we always immediately take the revocation
191 // secret from the keys_manager, we would panic at broadcast as we're trying to sign a
192 // transaction which, from the point of view of our keys_manager, is revoked.
193 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
195 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
196 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
197 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
199 let (_, _, chan_id, funding_tx) =
200 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 1_000_000, InitFeatures::known(), InitFeatures::known());
201 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
202 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
204 // This HTLC is immediately claimed, giving node B the preimage
205 let payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 3_000_000).0;
206 // This HTLC is allowed to time out, letting A claim it. However, in order to test claimable
207 // balances more fully we also give B the preimage for this HTLC.
208 let (timeout_payment_preimage, timeout_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 4_000_000);
209 // This HTLC will be dust, and not be claimable at all:
210 let (dust_payment_preimage, dust_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 3_000);
212 let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
214 let chan_feerate = get_feerate!(nodes[0], chan_id) as u64;
216 let remote_txn = get_local_commitment_txn!(nodes[1], chan_id);
217 // Before B receives the payment preimage, it only suggests the push_msat value of 1_000 sats
218 // as claimable. A lists both its to-self balance and the (possibly-claimable) HTLCs.
219 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
220 claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
221 (channel::COMMITMENT_TX_BASE_WEIGHT + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
222 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
223 claimable_amount_satoshis: 3_000,
224 claimable_height: htlc_cltv_timeout,
225 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
226 claimable_amount_satoshis: 4_000,
227 claimable_height: htlc_cltv_timeout,
229 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
230 assert_eq!(vec![Balance::ClaimableOnChannelClose {
231 claimable_amount_satoshis: 1_000,
233 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
235 nodes[1].node.claim_funds(payment_preimage);
236 check_added_monitors!(nodes[1], 1);
237 let b_htlc_msgs = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
238 // We claim the dust payment here as well, but it won't impact our claimable balances as its
239 // dust and thus doesn't appear on chain at all.
240 nodes[1].node.claim_funds(dust_payment_preimage);
241 check_added_monitors!(nodes[1], 1);
242 nodes[1].node.claim_funds(timeout_payment_preimage);
243 check_added_monitors!(nodes[1], 1);
245 if prev_commitment_tx {
246 // To build a previous commitment transaction, deliver one round of commitment messages.
247 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &b_htlc_msgs.update_fulfill_htlcs[0]);
248 expect_payment_sent_without_paths!(nodes[0], payment_preimage);
249 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &b_htlc_msgs.commitment_signed);
250 check_added_monitors!(nodes[0], 1);
251 let (as_raa, as_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
252 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
253 let _htlc_updates = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
254 check_added_monitors!(nodes[1], 1);
255 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs);
256 let _bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
257 check_added_monitors!(nodes[1], 1);
260 // Once B has received the payment preimage, it includes the value of the HTLC in its
261 // "claimable if you were to close the channel" balance.
262 let mut a_expected_balances = vec![Balance::ClaimableOnChannelClose {
263 claimable_amount_satoshis: 1_000_000 - // Channel funding value in satoshis
264 4_000 - // The to-be-failed HTLC value in satoshis
265 3_000 - // The claimed HTLC value in satoshis
266 1_000 - // The push_msat value in satoshis
267 3 - // The dust HTLC value in satoshis
268 // The commitment transaction fee with two HTLC outputs:
269 chan_feerate * (channel::COMMITMENT_TX_BASE_WEIGHT +
270 if prev_commitment_tx { 1 } else { 2 } *
271 channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
272 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
273 claimable_amount_satoshis: 4_000,
274 claimable_height: htlc_cltv_timeout,
276 if !prev_commitment_tx {
277 a_expected_balances.push(Balance::MaybeClaimableHTLCAwaitingTimeout {
278 claimable_amount_satoshis: 3_000,
279 claimable_height: htlc_cltv_timeout,
282 assert_eq!(sorted_vec(a_expected_balances),
283 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
284 assert_eq!(vec![Balance::ClaimableOnChannelClose {
285 claimable_amount_satoshis: 1_000 + 3_000 + 4_000,
287 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
289 // Broadcast the closing transaction (which has both pending HTLCs in it) and get B's
290 // broadcasted HTLC claim transaction with preimage.
291 let node_b_commitment_claimable = nodes[1].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
292 mine_transaction(&nodes[0], &remote_txn[0]);
293 mine_transaction(&nodes[1], &remote_txn[0]);
295 let b_broadcast_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
296 assert_eq!(b_broadcast_txn.len(), if prev_commitment_tx { 4 } else { 5 });
297 if prev_commitment_tx {
298 check_spends!(b_broadcast_txn[3], b_broadcast_txn[2]);
300 assert_eq!(b_broadcast_txn[0], b_broadcast_txn[3]);
301 assert_eq!(b_broadcast_txn[1], b_broadcast_txn[4]);
303 // b_broadcast_txn[0] should spend the HTLC output of the commitment tx for 3_000 sats
304 check_spends!(b_broadcast_txn[0], remote_txn[0]);
305 check_spends!(b_broadcast_txn[1], remote_txn[0]);
306 assert_eq!(b_broadcast_txn[0].input.len(), 1);
307 assert_eq!(b_broadcast_txn[1].input.len(), 1);
308 assert_eq!(remote_txn[0].output[b_broadcast_txn[0].input[0].previous_output.vout as usize].value, 3_000);
309 assert_eq!(remote_txn[0].output[b_broadcast_txn[1].input[0].previous_output.vout as usize].value, 4_000);
310 check_spends!(b_broadcast_txn[2], funding_tx);
312 assert!(nodes[0].node.list_channels().is_empty());
313 check_closed_broadcast!(nodes[0], true);
314 check_added_monitors!(nodes[0], 1);
315 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
316 assert!(nodes[1].node.list_channels().is_empty());
317 check_closed_broadcast!(nodes[1], true);
318 check_added_monitors!(nodes[1], 1);
319 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
320 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
321 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
323 // Once the commitment transaction confirms, we will wait until ANTI_REORG_DELAY until we
324 // generate any `SpendableOutputs` events. Thus, the same balances will still be listed
325 // available in `get_claimable_balances`. However, both will swap from `ClaimableOnClose` to
326 // other Balance variants, as close has already happened.
327 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
328 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
330 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
331 claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
332 (channel::COMMITMENT_TX_BASE_WEIGHT + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
333 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
334 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
335 claimable_amount_satoshis: 3_000,
336 claimable_height: htlc_cltv_timeout,
337 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
338 claimable_amount_satoshis: 4_000,
339 claimable_height: htlc_cltv_timeout,
341 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
342 // The main non-HTLC balance is just awaiting confirmations, but the claimable height is the
343 // CSV delay, not ANTI_REORG_DELAY.
344 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
345 claimable_amount_satoshis: 1_000,
346 confirmation_height: node_b_commitment_claimable,
348 // Both HTLC balances are "contentious" as our counterparty could claim them if we wait too
350 Balance::ContentiousClaimable {
351 claimable_amount_satoshis: 3_000,
352 timeout_height: htlc_cltv_timeout,
353 }, Balance::ContentiousClaimable {
354 claimable_amount_satoshis: 4_000,
355 timeout_height: htlc_cltv_timeout,
357 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
359 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
360 expect_payment_failed!(nodes[0], dust_payment_hash, true);
361 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
363 // After ANTI_REORG_DELAY, A will consider its balance fully spendable and generate a
364 // `SpendableOutputs` event. However, B still has to wait for the CSV delay.
365 assert_eq!(sorted_vec(vec![Balance::MaybeClaimableHTLCAwaitingTimeout {
366 claimable_amount_satoshis: 3_000,
367 claimable_height: htlc_cltv_timeout,
368 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
369 claimable_amount_satoshis: 4_000,
370 claimable_height: htlc_cltv_timeout,
372 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
373 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
374 claimable_amount_satoshis: 1_000,
375 confirmation_height: node_b_commitment_claimable,
376 }, Balance::ContentiousClaimable {
377 claimable_amount_satoshis: 3_000,
378 timeout_height: htlc_cltv_timeout,
379 }, Balance::ContentiousClaimable {
380 claimable_amount_satoshis: 4_000,
381 timeout_height: htlc_cltv_timeout,
383 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
385 let mut node_a_spendable = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
386 assert_eq!(node_a_spendable.len(), 1);
387 if let Event::SpendableOutputs { outputs } = node_a_spendable.pop().unwrap() {
388 assert_eq!(outputs.len(), 1);
389 let spend_tx = nodes[0].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
390 Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
391 check_spends!(spend_tx, remote_txn[0]);
394 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
396 // After broadcasting the HTLC claim transaction, node A will still consider the HTLC
397 // possibly-claimable up to ANTI_REORG_DELAY, at which point it will drop it.
398 mine_transaction(&nodes[0], &b_broadcast_txn[0]);
399 if prev_commitment_tx {
400 expect_payment_path_successful!(nodes[0]);
402 expect_payment_sent!(nodes[0], payment_preimage);
404 assert_eq!(sorted_vec(vec![Balance::MaybeClaimableHTLCAwaitingTimeout {
405 claimable_amount_satoshis: 3_000,
406 claimable_height: htlc_cltv_timeout,
407 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
408 claimable_amount_satoshis: 4_000,
409 claimable_height: htlc_cltv_timeout,
411 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
412 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
413 assert_eq!(vec![Balance::MaybeClaimableHTLCAwaitingTimeout {
414 claimable_amount_satoshis: 4_000,
415 claimable_height: htlc_cltv_timeout,
417 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
419 // When the HTLC timeout output is spendable in the next block, A should broadcast it
420 connect_blocks(&nodes[0], htlc_cltv_timeout - nodes[0].best_block_info().1 - 1);
421 let a_broadcast_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
422 assert_eq!(a_broadcast_txn.len(), 3);
423 check_spends!(a_broadcast_txn[0], funding_tx);
424 assert_eq!(a_broadcast_txn[1].input.len(), 1);
425 check_spends!(a_broadcast_txn[1], remote_txn[0]);
426 assert_eq!(a_broadcast_txn[2].input.len(), 1);
427 check_spends!(a_broadcast_txn[2], remote_txn[0]);
428 assert_ne!(a_broadcast_txn[1].input[0].previous_output.vout,
429 a_broadcast_txn[2].input[0].previous_output.vout);
430 // a_broadcast_txn [1] and [2] should spend the HTLC outputs of the commitment tx
431 assert_eq!(remote_txn[0].output[a_broadcast_txn[1].input[0].previous_output.vout as usize].value, 3_000);
432 assert_eq!(remote_txn[0].output[a_broadcast_txn[2].input[0].previous_output.vout as usize].value, 4_000);
434 // Once the HTLC-Timeout transaction confirms, A will no longer consider the HTLC
435 // "MaybeClaimable", but instead move it to "AwaitingConfirmations".
436 mine_transaction(&nodes[0], &a_broadcast_txn[2]);
437 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
438 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
439 claimable_amount_satoshis: 4_000,
440 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
442 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
443 // After ANTI_REORG_DELAY, A will generate a SpendableOutputs event and drop the claimable
445 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
446 assert_eq!(Vec::<Balance>::new(),
447 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
448 expect_payment_failed!(nodes[0], timeout_payment_hash, true);
450 let mut node_a_spendable = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
451 assert_eq!(node_a_spendable.len(), 1);
452 if let Event::SpendableOutputs { outputs } = node_a_spendable.pop().unwrap() {
453 assert_eq!(outputs.len(), 1);
454 let spend_tx = nodes[0].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
455 Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
456 check_spends!(spend_tx, a_broadcast_txn[2]);
459 // Node B will no longer consider the HTLC "contentious" after the HTLC claim transaction
460 // confirms, and consider it simply "awaiting confirmations". Note that it has to wait for the
461 // standard revocable transaction CSV delay before receiving a `SpendableOutputs`.
462 let node_b_htlc_claimable = nodes[1].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
463 mine_transaction(&nodes[1], &b_broadcast_txn[0]);
465 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
466 claimable_amount_satoshis: 1_000,
467 confirmation_height: node_b_commitment_claimable,
468 }, Balance::ClaimableAwaitingConfirmations {
469 claimable_amount_satoshis: 3_000,
470 confirmation_height: node_b_htlc_claimable,
471 }, Balance::ContentiousClaimable {
472 claimable_amount_satoshis: 4_000,
473 timeout_height: htlc_cltv_timeout,
475 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
477 // After reaching the commitment output CSV, we'll get a SpendableOutputs event for it and have
478 // only the HTLCs claimable on node B.
479 connect_blocks(&nodes[1], node_b_commitment_claimable - nodes[1].best_block_info().1);
481 let mut node_b_spendable = nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events();
482 assert_eq!(node_b_spendable.len(), 1);
483 if let Event::SpendableOutputs { outputs } = node_b_spendable.pop().unwrap() {
484 assert_eq!(outputs.len(), 1);
485 let spend_tx = nodes[1].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
486 Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
487 check_spends!(spend_tx, remote_txn[0]);
490 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
491 claimable_amount_satoshis: 3_000,
492 confirmation_height: node_b_htlc_claimable,
493 }, Balance::ContentiousClaimable {
494 claimable_amount_satoshis: 4_000,
495 timeout_height: htlc_cltv_timeout,
497 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
499 // After reaching the claimed HTLC output CSV, we'll get a SpendableOutptus event for it and
500 // have only one HTLC output left spendable.
501 connect_blocks(&nodes[1], node_b_htlc_claimable - nodes[1].best_block_info().1);
503 let mut node_b_spendable = nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events();
504 assert_eq!(node_b_spendable.len(), 1);
505 if let Event::SpendableOutputs { outputs } = node_b_spendable.pop().unwrap() {
506 assert_eq!(outputs.len(), 1);
507 let spend_tx = nodes[1].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
508 Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
509 check_spends!(spend_tx, b_broadcast_txn[0]);
512 assert_eq!(vec![Balance::ContentiousClaimable {
513 claimable_amount_satoshis: 4_000,
514 timeout_height: htlc_cltv_timeout,
516 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
518 // Finally, mine the HTLC timeout transaction that A broadcasted (even though B should be able
519 // to claim this HTLC with the preimage it knows!). It will remain listed as a claimable HTLC
520 // until ANTI_REORG_DELAY confirmations on the spend.
521 mine_transaction(&nodes[1], &a_broadcast_txn[2]);
522 assert_eq!(vec![Balance::ContentiousClaimable {
523 claimable_amount_satoshis: 4_000,
524 timeout_height: htlc_cltv_timeout,
526 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
527 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
528 assert_eq!(Vec::<Balance>::new(),
529 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
533 fn test_claim_value_force_close() {
534 do_test_claim_value_force_close(true);
535 do_test_claim_value_force_close(false);