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;
14 use ln::{channel, PaymentPreimage, PaymentHash};
15 use ln::channelmanager::BREAKDOWN_TIMEOUT;
16 use ln::features::InitFeatures;
17 use ln::msgs::{ChannelMessageHandler, ErrorAction};
18 use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason};
19 use routing::network_graph::NetworkUpdate;
21 use bitcoin::hashes::sha256::Hash as Sha256;
22 use bitcoin::hashes::Hash;
24 use bitcoin::blockdata::script::Builder;
25 use bitcoin::blockdata::opcodes;
26 use bitcoin::secp256k1::Secp256k1;
30 use ln::functional_test_utils::*;
33 fn chanmon_fail_from_stale_commitment() {
34 // If we forward an HTLC to our counterparty, but we force-closed the channel before our
35 // counterparty provides us an updated commitment transaction, we'll end up with a commitment
36 // transaction that does not contain the HTLC which we attempted to forward. In this case, we
37 // need to wait `ANTI_REORG_DELAY` blocks and then fail back the HTLC as there is no way for us
38 // to learn the preimage and the confirmed commitment transaction paid us the value of the
41 // However, previously, we did not do this, ignoring the HTLC entirely.
43 // This could lead to channel closure if the sender we received the HTLC from decides to go on
44 // chain to get their HTLC back before it times out.
46 // Here, we check exactly this case, forwarding a payment from A, through B, to C, before B
47 // broadcasts its latest commitment transaction, which should result in it eventually failing
48 // the HTLC back off-chain to A.
49 let chanmon_cfgs = create_chanmon_cfgs(3);
50 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
51 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
52 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
54 create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
55 let (update_a, _, chan_id_2, _) = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
57 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 1_000_000);
58 nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
59 check_added_monitors!(nodes[0], 1);
61 let bs_txn = get_local_commitment_txn!(nodes[1], chan_id_2);
63 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
64 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
65 commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
67 expect_pending_htlcs_forwardable!(nodes[1]);
68 get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
69 check_added_monitors!(nodes[1], 1);
71 // Don't bother delivering the new HTLC add/commits, instead confirming the pre-HTLC commitment
72 // transaction for nodes[1].
73 mine_transaction(&nodes[1], &bs_txn[0]);
74 check_added_monitors!(nodes[1], 1);
75 check_closed_broadcast!(nodes[1], true);
76 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
77 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
79 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
80 expect_pending_htlcs_forwardable!(nodes[1]);
81 check_added_monitors!(nodes[1], 1);
82 let fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
84 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates.update_fail_htlcs[0]);
85 commitment_signed_dance!(nodes[0], nodes[1], fail_updates.commitment_signed, true, true);
86 expect_payment_failed_with_update!(nodes[0], payment_hash, false, update_a.contents.short_channel_id, true);
90 fn chanmon_claim_value_coop_close() {
91 // Tests `get_claimable_balances` returns the correct values across a simple cooperative claim.
92 // Specifically, this tests that the channel non-HTLC balances show up in
93 // `get_claimable_balances` until the cooperative claims have confirmed and generated a
94 // `SpendableOutputs` event, and no longer.
95 let chanmon_cfgs = create_chanmon_cfgs(2);
96 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
97 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
98 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
100 let (_, _, chan_id, funding_tx) =
101 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 1_000_000, InitFeatures::known(), InitFeatures::known());
102 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
103 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
105 let chan_feerate = get_feerate!(nodes[0], chan_id) as u64;
107 assert_eq!(vec![Balance::ClaimableOnChannelClose {
108 claimable_amount_satoshis: 1_000_000 - 1_000 - chan_feerate * channel::COMMITMENT_TX_BASE_WEIGHT / 1000
110 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
111 assert_eq!(vec![Balance::ClaimableOnChannelClose { claimable_amount_satoshis: 1_000, }],
112 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
114 nodes[0].node.close_channel(&chan_id).unwrap();
115 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
116 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &InitFeatures::known(), &node_0_shutdown);
117 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
118 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_1_shutdown);
120 let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
121 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
122 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
123 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
124 let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
125 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
126 let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
127 assert!(node_1_none.is_none());
129 let shutdown_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
130 assert_eq!(shutdown_tx, nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0));
131 assert_eq!(shutdown_tx.len(), 1);
133 mine_transaction(&nodes[0], &shutdown_tx[0]);
134 mine_transaction(&nodes[1], &shutdown_tx[0]);
136 assert!(nodes[0].node.list_channels().is_empty());
137 assert!(nodes[1].node.list_channels().is_empty());
139 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
140 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
142 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
143 claimable_amount_satoshis: 1_000_000 - 1_000 - chan_feerate * channel::COMMITMENT_TX_BASE_WEIGHT / 1000,
144 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
146 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
147 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
148 claimable_amount_satoshis: 1000,
149 confirmation_height: nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1,
151 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
153 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
154 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
156 assert_eq!(Vec::<Balance>::new(),
157 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
158 assert_eq!(Vec::<Balance>::new(),
159 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
161 let mut node_a_spendable = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
162 assert_eq!(node_a_spendable.len(), 1);
163 if let Event::SpendableOutputs { outputs } = node_a_spendable.pop().unwrap() {
164 assert_eq!(outputs.len(), 1);
165 let spend_tx = nodes[0].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
166 Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
167 check_spends!(spend_tx, shutdown_tx[0]);
170 let mut node_b_spendable = nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events();
171 assert_eq!(node_b_spendable.len(), 1);
172 if let Event::SpendableOutputs { outputs } = node_b_spendable.pop().unwrap() {
173 assert_eq!(outputs.len(), 1);
174 let spend_tx = nodes[1].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
175 Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
176 check_spends!(spend_tx, shutdown_tx[0]);
178 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure);
179 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure);
182 fn sorted_vec<T: Ord>(mut v: Vec<T>) -> Vec<T> {
187 fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
188 // Tests `get_claimable_balances` with an HTLC across a force-close.
189 // We build a channel with an HTLC pending, then force close the channel and check that the
190 // `get_claimable_balances` return value is correct as transactions confirm on-chain.
191 let mut chanmon_cfgs = create_chanmon_cfgs(2);
192 if prev_commitment_tx {
193 // We broadcast a second-to-latest commitment transaction, without providing the revocation
194 // secret to the counterparty. However, because we always immediately take the revocation
195 // secret from the keys_manager, we would panic at broadcast as we're trying to sign a
196 // transaction which, from the point of view of our keys_manager, is revoked.
197 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
199 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
200 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
201 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
203 let (_, _, chan_id, funding_tx) =
204 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 1_000_000, InitFeatures::known(), InitFeatures::known());
205 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
206 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
208 // This HTLC is immediately claimed, giving node B the preimage
209 let payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 3_000_000).0;
210 // This HTLC is allowed to time out, letting A claim it. However, in order to test claimable
211 // balances more fully we also give B the preimage for this HTLC.
212 let (timeout_payment_preimage, timeout_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 4_000_000);
213 // This HTLC will be dust, and not be claimable at all:
214 let (dust_payment_preimage, dust_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 3_000);
216 let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
218 let chan_feerate = get_feerate!(nodes[0], chan_id) as u64;
220 let remote_txn = get_local_commitment_txn!(nodes[1], chan_id);
221 // Before B receives the payment preimage, it only suggests the push_msat value of 1_000 sats
222 // as claimable. A lists both its to-self balance and the (possibly-claimable) HTLCs.
223 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
224 claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
225 (channel::COMMITMENT_TX_BASE_WEIGHT + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
226 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
227 claimable_amount_satoshis: 3_000,
228 claimable_height: htlc_cltv_timeout,
229 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
230 claimable_amount_satoshis: 4_000,
231 claimable_height: htlc_cltv_timeout,
233 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
234 assert_eq!(vec![Balance::ClaimableOnChannelClose {
235 claimable_amount_satoshis: 1_000,
237 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
239 nodes[1].node.claim_funds(payment_preimage);
240 check_added_monitors!(nodes[1], 1);
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 nodes[1].node.claim_funds(timeout_payment_preimage);
247 check_added_monitors!(nodes[1], 1);
249 if prev_commitment_tx {
250 // To build a previous commitment transaction, deliver one round of commitment messages.
251 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &b_htlc_msgs.update_fulfill_htlcs[0]);
252 expect_payment_sent!(nodes[0], payment_preimage);
253 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &b_htlc_msgs.commitment_signed);
254 check_added_monitors!(nodes[0], 1);
255 let (as_raa, as_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
256 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
257 let _htlc_updates = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
258 check_added_monitors!(nodes[1], 1);
259 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs);
260 let _bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
261 check_added_monitors!(nodes[1], 1);
264 // Once B has received the payment preimage, it includes the value of the HTLC in its
265 // "claimable if you were to close the channel" balance.
266 let mut a_expected_balances = vec![Balance::ClaimableOnChannelClose {
267 claimable_amount_satoshis: 1_000_000 - // Channel funding value in satoshis
268 4_000 - // The to-be-failed HTLC value in satoshis
269 3_000 - // The claimed HTLC value in satoshis
270 1_000 - // The push_msat value in satoshis
271 3 - // The dust HTLC value in satoshis
272 // The commitment transaction fee with two HTLC outputs:
273 chan_feerate * (channel::COMMITMENT_TX_BASE_WEIGHT +
274 if prev_commitment_tx { 1 } else { 2 } *
275 channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
276 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
277 claimable_amount_satoshis: 4_000,
278 claimable_height: htlc_cltv_timeout,
280 if !prev_commitment_tx {
281 a_expected_balances.push(Balance::MaybeClaimableHTLCAwaitingTimeout {
282 claimable_amount_satoshis: 3_000,
283 claimable_height: htlc_cltv_timeout,
286 assert_eq!(sorted_vec(a_expected_balances),
287 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
288 assert_eq!(vec![Balance::ClaimableOnChannelClose {
289 claimable_amount_satoshis: 1_000 + 3_000 + 4_000,
291 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
293 // Broadcast the closing transaction (which has both pending HTLCs in it) and get B's
294 // broadcasted HTLC claim transaction with preimage.
295 let node_b_commitment_claimable = nodes[1].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
296 mine_transaction(&nodes[0], &remote_txn[0]);
297 mine_transaction(&nodes[1], &remote_txn[0]);
299 let b_broadcast_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
300 assert_eq!(b_broadcast_txn.len(), if prev_commitment_tx { 4 } else { 5 });
301 if prev_commitment_tx {
302 check_spends!(b_broadcast_txn[3], b_broadcast_txn[2]);
304 assert_eq!(b_broadcast_txn[0], b_broadcast_txn[3]);
305 assert_eq!(b_broadcast_txn[1], b_broadcast_txn[4]);
307 // b_broadcast_txn[0] should spend the HTLC output of the commitment tx for 3_000 sats
308 check_spends!(b_broadcast_txn[0], remote_txn[0]);
309 check_spends!(b_broadcast_txn[1], remote_txn[0]);
310 assert_eq!(b_broadcast_txn[0].input.len(), 1);
311 assert_eq!(b_broadcast_txn[1].input.len(), 1);
312 assert_eq!(remote_txn[0].output[b_broadcast_txn[0].input[0].previous_output.vout as usize].value, 3_000);
313 assert_eq!(remote_txn[0].output[b_broadcast_txn[1].input[0].previous_output.vout as usize].value, 4_000);
314 check_spends!(b_broadcast_txn[2], funding_tx);
316 assert!(nodes[0].node.list_channels().is_empty());
317 check_closed_broadcast!(nodes[0], true);
318 check_added_monitors!(nodes[0], 1);
319 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
320 assert!(nodes[1].node.list_channels().is_empty());
321 check_closed_broadcast!(nodes[1], true);
322 check_added_monitors!(nodes[1], 1);
323 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
324 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
325 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
327 // Once the commitment transaction confirms, we will wait until ANTI_REORG_DELAY until we
328 // generate any `SpendableOutputs` events. Thus, the same balances will still be listed
329 // available in `get_claimable_balances`. However, both will swap from `ClaimableOnClose` to
330 // other Balance variants, as close has already happened.
331 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
332 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
334 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
335 claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
336 (channel::COMMITMENT_TX_BASE_WEIGHT + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
337 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
338 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
339 claimable_amount_satoshis: 3_000,
340 claimable_height: htlc_cltv_timeout,
341 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
342 claimable_amount_satoshis: 4_000,
343 claimable_height: htlc_cltv_timeout,
345 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
346 // The main non-HTLC balance is just awaiting confirmations, but the claimable height is the
347 // CSV delay, not ANTI_REORG_DELAY.
348 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
349 claimable_amount_satoshis: 1_000,
350 confirmation_height: node_b_commitment_claimable,
352 // Both HTLC balances are "contentious" as our counterparty could claim them if we wait too
354 Balance::ContentiousClaimable {
355 claimable_amount_satoshis: 3_000,
356 timeout_height: htlc_cltv_timeout,
357 }, Balance::ContentiousClaimable {
358 claimable_amount_satoshis: 4_000,
359 timeout_height: htlc_cltv_timeout,
361 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
363 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
364 expect_payment_failed!(nodes[0], dust_payment_hash, true);
365 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
367 // After ANTI_REORG_DELAY, A will consider its balance fully spendable and generate a
368 // `SpendableOutputs` event. However, B still has to wait for the CSV delay.
369 assert_eq!(sorted_vec(vec![Balance::MaybeClaimableHTLCAwaitingTimeout {
370 claimable_amount_satoshis: 3_000,
371 claimable_height: htlc_cltv_timeout,
372 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
373 claimable_amount_satoshis: 4_000,
374 claimable_height: htlc_cltv_timeout,
376 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
377 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
378 claimable_amount_satoshis: 1_000,
379 confirmation_height: node_b_commitment_claimable,
380 }, Balance::ContentiousClaimable {
381 claimable_amount_satoshis: 3_000,
382 timeout_height: htlc_cltv_timeout,
383 }, Balance::ContentiousClaimable {
384 claimable_amount_satoshis: 4_000,
385 timeout_height: htlc_cltv_timeout,
387 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
389 let mut node_a_spendable = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
390 assert_eq!(node_a_spendable.len(), 1);
391 if let Event::SpendableOutputs { outputs } = node_a_spendable.pop().unwrap() {
392 assert_eq!(outputs.len(), 1);
393 let spend_tx = nodes[0].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
394 Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
395 check_spends!(spend_tx, remote_txn[0]);
398 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
400 // After broadcasting the HTLC claim transaction, node A will still consider the HTLC
401 // possibly-claimable up to ANTI_REORG_DELAY, at which point it will drop it.
402 mine_transaction(&nodes[0], &b_broadcast_txn[0]);
403 if !prev_commitment_tx {
404 expect_payment_sent!(nodes[0], payment_preimage);
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,
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,
419 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
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);
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,
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
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);
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]);
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]);
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,
477 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
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);
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]);
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,
499 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
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);
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]);
514 assert_eq!(vec![Balance::ContentiousClaimable {
515 claimable_amount_satoshis: 4_000,
516 timeout_height: htlc_cltv_timeout,
518 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
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,
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());
535 fn test_claim_value_force_close() {
536 do_test_claim_value_force_close(true);
537 do_test_claim_value_force_close(false);