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;
20 use routing::router::get_route;
22 use bitcoin::hashes::sha256::Hash as Sha256;
23 use bitcoin::hashes::Hash;
25 use bitcoin::blockdata::script::Builder;
26 use bitcoin::blockdata::opcodes;
27 use bitcoin::secp256k1::Secp256k1;
31 use ln::functional_test_utils::*;
34 fn chanmon_fail_from_stale_commitment() {
35 // If we forward an HTLC to our counterparty, but we force-closed the channel before our
36 // counterparty provides us an updated commitment transaction, we'll end up with a commitment
37 // transaction that does not contain the HTLC which we attempted to forward. In this case, we
38 // need to wait `ANTI_REORG_DELAY` blocks and then fail back the HTLC as there is no way for us
39 // to learn the preimage and the confirmed commitment transaction paid us the value of the
42 // However, previously, we did not do this, ignoring the HTLC entirely.
44 // This could lead to channel closure if the sender we received the HTLC from decides to go on
45 // chain to get their HTLC back before it times out.
47 // Here, we check exactly this case, forwarding a payment from A, through B, to C, before B
48 // broadcasts its latest commitment transaction, which should result in it eventually failing
49 // the HTLC back off-chain to A.
50 let chanmon_cfgs = create_chanmon_cfgs(3);
51 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
52 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
53 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
55 create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
56 let (update_a, _, chan_id_2, _) = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
58 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 1_000_000);
59 nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
60 check_added_monitors!(nodes[0], 1);
62 let bs_txn = get_local_commitment_txn!(nodes[1], chan_id_2);
64 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
65 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
66 commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
68 expect_pending_htlcs_forwardable!(nodes[1]);
69 get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
70 check_added_monitors!(nodes[1], 1);
72 // Don't bother delivering the new HTLC add/commits, instead confirming the pre-HTLC commitment
73 // transaction for nodes[1].
74 mine_transaction(&nodes[1], &bs_txn[0]);
75 check_added_monitors!(nodes[1], 1);
76 check_closed_broadcast!(nodes[1], true);
77 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxBroadcasted);
78 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
80 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
81 expect_pending_htlcs_forwardable!(nodes[1]);
82 check_added_monitors!(nodes[1], 1);
83 let fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
85 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates.update_fail_htlcs[0]);
86 commitment_signed_dance!(nodes[0], nodes[1], fail_updates.commitment_signed, true, true);
87 expect_payment_failed_with_update!(nodes[0], payment_hash, false, update_a.contents.short_channel_id, true);
91 fn chanmon_claim_value_coop_close() {
92 // Tests `get_claimable_balances` returns the correct values across a simple cooperative claim.
93 // Specifically, this tests that the channel non-HTLC balances show up in
94 // `get_claimable_balances` until the cooperative claims have confirmed and generated a
95 // `SpendableOutputs` event, and no longer.
96 let chanmon_cfgs = create_chanmon_cfgs(2);
97 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
98 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
99 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
101 let (_, _, chan_id, funding_tx) =
102 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 1_000_000, InitFeatures::known(), InitFeatures::known());
103 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
104 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
106 let chan_feerate = get_feerate!(nodes[0], chan_id) as u64;
108 assert_eq!(vec![Balance::ClaimableOnChannelClose {
109 claimable_amount_satoshis: 1_000_000 - 1_000 - chan_feerate * channel::COMMITMENT_TX_BASE_WEIGHT / 1000
111 nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().get(&funding_outpoint).unwrap().get_claimable_balances());
112 assert_eq!(vec![Balance::ClaimableOnChannelClose { claimable_amount_satoshis: 1_000, }],
113 nodes[1].chain_monitor.chain_monitor.monitors.read().unwrap().get(&funding_outpoint).unwrap().get_claimable_balances());
115 nodes[0].node.close_channel(&chan_id).unwrap();
116 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
117 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &InitFeatures::known(), &node_0_shutdown);
118 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
119 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_1_shutdown);
121 let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
122 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
123 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
124 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
125 let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
126 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
127 let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
128 assert!(node_1_none.is_none());
130 let shutdown_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
131 assert_eq!(shutdown_tx, nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0));
132 assert_eq!(shutdown_tx.len(), 1);
134 mine_transaction(&nodes[0], &shutdown_tx[0]);
135 mine_transaction(&nodes[1], &shutdown_tx[0]);
137 assert!(nodes[0].node.list_channels().is_empty());
138 assert!(nodes[1].node.list_channels().is_empty());
140 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
141 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
143 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
144 claimable_amount_satoshis: 1_000_000 - 1_000 - chan_feerate * channel::COMMITMENT_TX_BASE_WEIGHT / 1000,
145 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
147 nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().get(&funding_outpoint).unwrap().get_claimable_balances());
148 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
149 claimable_amount_satoshis: 1000,
150 confirmation_height: nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1,
152 nodes[1].chain_monitor.chain_monitor.monitors.read().unwrap().get(&funding_outpoint).unwrap().get_claimable_balances());
154 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
155 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
157 assert_eq!(Vec::<Balance>::new(),
158 nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().get(&funding_outpoint).unwrap().get_claimable_balances());
159 assert_eq!(Vec::<Balance>::new(),
160 nodes[1].chain_monitor.chain_monitor.monitors.read().unwrap().get(&funding_outpoint).unwrap().get_claimable_balances());
162 let mut node_a_spendable = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
163 assert_eq!(node_a_spendable.len(), 1);
164 if let Event::SpendableOutputs { outputs } = node_a_spendable.pop().unwrap() {
165 assert_eq!(outputs.len(), 1);
166 let spend_tx = nodes[0].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
167 Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
168 check_spends!(spend_tx, shutdown_tx[0]);
171 let mut node_b_spendable = nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events();
172 assert_eq!(node_b_spendable.len(), 1);
173 if let Event::SpendableOutputs { outputs } = node_b_spendable.pop().unwrap() {
174 assert_eq!(outputs.len(), 1);
175 let spend_tx = nodes[1].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
176 Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
177 check_spends!(spend_tx, shutdown_tx[0]);
179 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure);
180 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure);
183 fn sorted_vec<T: Ord>(mut v: Vec<T>) -> Vec<T> {
188 fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
189 // Tests `get_claimable_balances` with an HTLC across a force-close.
190 // We build a channel with an HTLC pending, then force close the channel and check that the
191 // `get_claimable_balances` return value is correct as transactions confirm on-chain.
192 let mut chanmon_cfgs = create_chanmon_cfgs(2);
193 if prev_commitment_tx {
194 // We broadcast a second-to-latest commitment transaction, without providing the revocation
195 // secret to the counterparty. However, because we always immediately take the revocation
196 // secret from the keys_manager, we would panic at broadcast as we're trying to sign a
197 // transaction which, from the point of view of our keys_manager, is revoked.
198 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
200 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
201 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
202 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
204 let (_, _, chan_id, funding_tx) =
205 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 1_000_000, InitFeatures::known(), InitFeatures::known());
206 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
207 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
209 // This HTLC is immediately claimed, giving node B the preimage
210 let payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 3_000_000).0;
211 // This HTLC is allowed to time out, letting A claim it. However, in order to test claimable
212 // balances more fully we also give B the preimage for this HTLC.
213 let (timeout_payment_preimage, timeout_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 4_000_000);
214 // This HTLC will be dust, and not be claimable at all:
215 let (dust_payment_preimage, dust_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 3_000);
217 let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
219 let chan_feerate = get_feerate!(nodes[0], chan_id) as u64;
221 let remote_txn = get_local_commitment_txn!(nodes[1], chan_id);
222 // Before B receives the payment preimage, it only suggests the push_msat value of 1_000 sats
223 // as claimable. A lists both its to-self balance and the (possibly-claimable) HTLCs.
224 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
225 claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
226 (channel::COMMITMENT_TX_BASE_WEIGHT + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
227 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
228 claimable_amount_satoshis: 3_000,
229 claimable_height: htlc_cltv_timeout,
230 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
231 claimable_amount_satoshis: 4_000,
232 claimable_height: htlc_cltv_timeout,
234 sorted_vec(nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().get(&funding_outpoint).unwrap().get_claimable_balances()));
235 assert_eq!(vec![Balance::ClaimableOnChannelClose {
236 claimable_amount_satoshis: 1_000,
238 nodes[1].chain_monitor.chain_monitor.monitors.read().unwrap().get(&funding_outpoint).unwrap().get_claimable_balances());
240 nodes[1].node.claim_funds(payment_preimage);
241 check_added_monitors!(nodes[1], 1);
242 let b_htlc_msgs = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
243 // We claim the dust payment here as well, but it won't impact our claimable balances as its
244 // dust and thus doesn't appear on chain at all.
245 nodes[1].node.claim_funds(dust_payment_preimage);
246 check_added_monitors!(nodes[1], 1);
247 nodes[1].node.claim_funds(timeout_payment_preimage);
248 check_added_monitors!(nodes[1], 1);
250 if prev_commitment_tx {
251 // To build a previous commitment transaction, deliver one round of commitment messages.
252 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &b_htlc_msgs.update_fulfill_htlcs[0]);
253 let events = nodes[0].node.get_and_clear_pending_events();
254 expect_payment_sent!(nodes[0], payment_preimage, events);
255 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &b_htlc_msgs.commitment_signed);
256 check_added_monitors!(nodes[0], 1);
257 let (as_raa, as_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
258 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
259 let _htlc_updates = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
260 check_added_monitors!(nodes[1], 1);
261 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs);
262 let _bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
263 check_added_monitors!(nodes[1], 1);
266 // Once B has received the payment preimage, it includes the value of the HTLC in its
267 // "claimable if you were to close the channel" balance.
268 let mut a_expected_balances = vec![Balance::ClaimableOnChannelClose {
269 claimable_amount_satoshis: 1_000_000 - // Channel funding value in satoshis
270 4_000 - // The to-be-failed HTLC value in satoshis
271 3_000 - // The claimed HTLC value in satoshis
272 1_000 - // The push_msat value in satoshis
273 3 - // The dust HTLC value in satoshis
274 // The commitment transaction fee with two HTLC outputs:
275 chan_feerate * (channel::COMMITMENT_TX_BASE_WEIGHT +
276 if prev_commitment_tx { 1 } else { 2 } *
277 channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
278 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
279 claimable_amount_satoshis: 4_000,
280 claimable_height: htlc_cltv_timeout,
282 if !prev_commitment_tx {
283 a_expected_balances.push(Balance::MaybeClaimableHTLCAwaitingTimeout {
284 claimable_amount_satoshis: 3_000,
285 claimable_height: htlc_cltv_timeout,
288 assert_eq!(sorted_vec(a_expected_balances),
289 sorted_vec(nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().get(&funding_outpoint).unwrap().get_claimable_balances()));
290 assert_eq!(vec![Balance::ClaimableOnChannelClose {
291 claimable_amount_satoshis: 1_000 + 3_000 + 4_000,
293 nodes[1].chain_monitor.chain_monitor.monitors.read().unwrap().get(&funding_outpoint).unwrap().get_claimable_balances());
295 // Broadcast the closing transaction (which has both pending HTLCs in it) and get B's
296 // broadcasted HTLC claim transaction with preimage.
297 let node_b_commitment_claimable = nodes[1].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
298 mine_transaction(&nodes[0], &remote_txn[0]);
299 mine_transaction(&nodes[1], &remote_txn[0]);
301 let b_broadcast_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
302 assert_eq!(b_broadcast_txn.len(), if prev_commitment_tx { 4 } else { 5 });
303 if prev_commitment_tx {
304 check_spends!(b_broadcast_txn[3], b_broadcast_txn[2]);
306 assert_eq!(b_broadcast_txn[0], b_broadcast_txn[3]);
307 assert_eq!(b_broadcast_txn[1], b_broadcast_txn[4]);
309 // b_broadcast_txn[0] should spend the HTLC output of the commitment tx for 3_000 sats
310 check_spends!(b_broadcast_txn[0], remote_txn[0]);
311 check_spends!(b_broadcast_txn[1], remote_txn[0]);
312 assert_eq!(b_broadcast_txn[0].input.len(), 1);
313 assert_eq!(b_broadcast_txn[1].input.len(), 1);
314 assert_eq!(remote_txn[0].output[b_broadcast_txn[0].input[0].previous_output.vout as usize].value, 3_000);
315 assert_eq!(remote_txn[0].output[b_broadcast_txn[1].input[0].previous_output.vout as usize].value, 4_000);
316 check_spends!(b_broadcast_txn[2], funding_tx);
318 assert!(nodes[0].node.list_channels().is_empty());
319 check_closed_broadcast!(nodes[0], true);
320 check_added_monitors!(nodes[0], 1);
321 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxBroadcasted);
322 assert!(nodes[1].node.list_channels().is_empty());
323 check_closed_broadcast!(nodes[1], true);
324 check_added_monitors!(nodes[1], 1);
325 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxBroadcasted);
326 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
327 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
329 // Once the commitment transaction confirms, we will wait until ANTI_REORG_DELAY until we
330 // generate any `SpendableOutputs` events. Thus, the same balances will still be listed
331 // available in `get_claimable_balances`. However, both will swap from `ClaimableOnClose` to
332 // other Balance variants, as close has already happened.
333 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
334 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
336 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
337 claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
338 (channel::COMMITMENT_TX_BASE_WEIGHT + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
339 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
340 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
341 claimable_amount_satoshis: 3_000,
342 claimable_height: htlc_cltv_timeout,
343 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
344 claimable_amount_satoshis: 4_000,
345 claimable_height: htlc_cltv_timeout,
347 sorted_vec(nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().get(&funding_outpoint).unwrap().get_claimable_balances()));
348 // The main non-HTLC balance is just awaiting confirmations, but the claimable height is the
349 // CSV delay, not ANTI_REORG_DELAY.
350 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
351 claimable_amount_satoshis: 1_000,
352 confirmation_height: node_b_commitment_claimable,
354 // Both HTLC balances are "contentious" as our counterparty could claim them if we wait too
356 Balance::ContentiousClaimable {
357 claimable_amount_satoshis: 3_000,
358 timeout_height: htlc_cltv_timeout,
359 }, Balance::ContentiousClaimable {
360 claimable_amount_satoshis: 4_000,
361 timeout_height: htlc_cltv_timeout,
363 sorted_vec(nodes[1].chain_monitor.chain_monitor.monitors.read().unwrap().get(&funding_outpoint).unwrap().get_claimable_balances()));
365 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
366 let events = nodes[0].node.get_and_clear_pending_events();
367 expect_payment_failed!(nodes[0], events, dust_payment_hash, true);
368 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
370 // After ANTI_REORG_DELAY, A will consider its balance fully spendable and generate a
371 // `SpendableOutputs` event. However, B still has to wait for the CSV delay.
372 assert_eq!(sorted_vec(vec![Balance::MaybeClaimableHTLCAwaitingTimeout {
373 claimable_amount_satoshis: 3_000,
374 claimable_height: htlc_cltv_timeout,
375 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
376 claimable_amount_satoshis: 4_000,
377 claimable_height: htlc_cltv_timeout,
379 sorted_vec(nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().get(&funding_outpoint).unwrap().get_claimable_balances()));
380 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
381 claimable_amount_satoshis: 1_000,
382 confirmation_height: node_b_commitment_claimable,
383 }, Balance::ContentiousClaimable {
384 claimable_amount_satoshis: 3_000,
385 timeout_height: htlc_cltv_timeout,
386 }, Balance::ContentiousClaimable {
387 claimable_amount_satoshis: 4_000,
388 timeout_height: htlc_cltv_timeout,
390 sorted_vec(nodes[1].chain_monitor.chain_monitor.monitors.read().unwrap().get(&funding_outpoint).unwrap().get_claimable_balances()));
392 let mut node_a_spendable = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
393 assert_eq!(node_a_spendable.len(), 1);
394 if let Event::SpendableOutputs { outputs } = node_a_spendable.pop().unwrap() {
395 assert_eq!(outputs.len(), 1);
396 let spend_tx = nodes[0].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
397 Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
398 check_spends!(spend_tx, remote_txn[0]);
401 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
403 // After broadcasting the HTLC claim transaction, node A will still consider the HTLC
404 // possibly-claimable up to ANTI_REORG_DELAY, at which point it will drop it.
405 mine_transaction(&nodes[0], &b_broadcast_txn[0]);
406 if !prev_commitment_tx {
407 let events = nodes[0].node.get_and_clear_pending_events();
408 expect_payment_sent!(nodes[0], payment_preimage, events);
410 assert_eq!(sorted_vec(vec![Balance::MaybeClaimableHTLCAwaitingTimeout {
411 claimable_amount_satoshis: 3_000,
412 claimable_height: htlc_cltv_timeout,
413 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
414 claimable_amount_satoshis: 4_000,
415 claimable_height: htlc_cltv_timeout,
417 sorted_vec(nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().get(&funding_outpoint).unwrap().get_claimable_balances()));
418 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
419 assert_eq!(vec![Balance::MaybeClaimableHTLCAwaitingTimeout {
420 claimable_amount_satoshis: 4_000,
421 claimable_height: htlc_cltv_timeout,
423 nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().get(&funding_outpoint).unwrap().get_claimable_balances());
425 // When the HTLC timeout output is spendable in the next block, A should broadcast it
426 connect_blocks(&nodes[0], htlc_cltv_timeout - nodes[0].best_block_info().1 - 1);
427 let a_broadcast_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
428 assert_eq!(a_broadcast_txn.len(), 3);
429 check_spends!(a_broadcast_txn[0], funding_tx);
430 assert_eq!(a_broadcast_txn[1].input.len(), 1);
431 check_spends!(a_broadcast_txn[1], remote_txn[0]);
432 assert_eq!(a_broadcast_txn[2].input.len(), 1);
433 check_spends!(a_broadcast_txn[2], remote_txn[0]);
434 assert_ne!(a_broadcast_txn[1].input[0].previous_output.vout,
435 a_broadcast_txn[2].input[0].previous_output.vout);
436 // a_broadcast_txn [1] and [2] should spend the HTLC outputs of the commitment tx
437 assert_eq!(remote_txn[0].output[a_broadcast_txn[1].input[0].previous_output.vout as usize].value, 3_000);
438 assert_eq!(remote_txn[0].output[a_broadcast_txn[2].input[0].previous_output.vout as usize].value, 4_000);
440 // Once the HTLC-Timeout transaction confirms, A will no longer consider the HTLC
441 // "MaybeClaimable", but instead move it to "AwaitingConfirmations".
442 mine_transaction(&nodes[0], &a_broadcast_txn[2]);
443 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
444 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
445 claimable_amount_satoshis: 4_000,
446 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
448 nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().get(&funding_outpoint).unwrap().get_claimable_balances());
449 // After ANTI_REORG_DELAY, A will generate a SpendableOutputs event and drop the claimable
451 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
452 assert_eq!(Vec::<Balance>::new(),
453 nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().get(&funding_outpoint).unwrap().get_claimable_balances());
454 let events = nodes[0].node.get_and_clear_pending_events();
455 expect_payment_failed!(nodes[0], events, timeout_payment_hash, true);
457 let mut node_a_spendable = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
458 assert_eq!(node_a_spendable.len(), 1);
459 if let Event::SpendableOutputs { outputs } = node_a_spendable.pop().unwrap() {
460 assert_eq!(outputs.len(), 1);
461 let spend_tx = nodes[0].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
462 Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
463 check_spends!(spend_tx, a_broadcast_txn[2]);
466 // Node B will no longer consider the HTLC "contentious" after the HTLC claim transaction
467 // confirms, and consider it simply "awaiting confirmations". Note that it has to wait for the
468 // standard revocable transaction CSV delay before receiving a `SpendableOutputs`.
469 let node_b_htlc_claimable = nodes[1].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
470 mine_transaction(&nodes[1], &b_broadcast_txn[0]);
472 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
473 claimable_amount_satoshis: 1_000,
474 confirmation_height: node_b_commitment_claimable,
475 }, Balance::ClaimableAwaitingConfirmations {
476 claimable_amount_satoshis: 3_000,
477 confirmation_height: node_b_htlc_claimable,
478 }, Balance::ContentiousClaimable {
479 claimable_amount_satoshis: 4_000,
480 timeout_height: htlc_cltv_timeout,
482 sorted_vec(nodes[1].chain_monitor.chain_monitor.monitors.read().unwrap().get(&funding_outpoint).unwrap().get_claimable_balances()));
484 // After reaching the commitment output CSV, we'll get a SpendableOutputs event for it and have
485 // only the HTLCs claimable on node B.
486 connect_blocks(&nodes[1], node_b_commitment_claimable - nodes[1].best_block_info().1);
488 let mut node_b_spendable = nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events();
489 assert_eq!(node_b_spendable.len(), 1);
490 if let Event::SpendableOutputs { outputs } = node_b_spendable.pop().unwrap() {
491 assert_eq!(outputs.len(), 1);
492 let spend_tx = nodes[1].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
493 Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
494 check_spends!(spend_tx, remote_txn[0]);
497 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
498 claimable_amount_satoshis: 3_000,
499 confirmation_height: node_b_htlc_claimable,
500 }, Balance::ContentiousClaimable {
501 claimable_amount_satoshis: 4_000,
502 timeout_height: htlc_cltv_timeout,
504 sorted_vec(nodes[1].chain_monitor.chain_monitor.monitors.read().unwrap().get(&funding_outpoint).unwrap().get_claimable_balances()));
506 // After reaching the claimed HTLC output CSV, we'll get a SpendableOutptus event for it and
507 // have only one HTLC output left spendable.
508 connect_blocks(&nodes[1], node_b_htlc_claimable - nodes[1].best_block_info().1);
510 let mut node_b_spendable = nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events();
511 assert_eq!(node_b_spendable.len(), 1);
512 if let Event::SpendableOutputs { outputs } = node_b_spendable.pop().unwrap() {
513 assert_eq!(outputs.len(), 1);
514 let spend_tx = nodes[1].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
515 Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
516 check_spends!(spend_tx, b_broadcast_txn[0]);
519 assert_eq!(vec![Balance::ContentiousClaimable {
520 claimable_amount_satoshis: 4_000,
521 timeout_height: htlc_cltv_timeout,
523 nodes[1].chain_monitor.chain_monitor.monitors.read().unwrap().get(&funding_outpoint).unwrap().get_claimable_balances());
525 // Finally, mine the HTLC timeout transaction that A broadcasted (even though B should be able
526 // to claim this HTLC with the preimage it knows!). It will remain listed as a claimable HTLC
527 // until ANTI_REORG_DELAY confirmations on the spend.
528 mine_transaction(&nodes[1], &a_broadcast_txn[2]);
529 assert_eq!(vec![Balance::ContentiousClaimable {
530 claimable_amount_satoshis: 4_000,
531 timeout_height: htlc_cltv_timeout,
533 nodes[1].chain_monitor.chain_monitor.monitors.read().unwrap().get(&funding_outpoint).unwrap().get_claimable_balances());
534 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
535 assert_eq!(Vec::<Balance>::new(),
536 nodes[1].chain_monitor.chain_monitor.monitors.read().unwrap().get(&funding_outpoint).unwrap().get_claimable_balances());
540 fn test_claim_value_force_close() {
541 do_test_claim_value_force_close(true);
542 do_test_claim_value_force_close(false);