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 chain::chaininterface::LowerBoundedFeeEstimator;
16 use ln::channelmanager::BREAKDOWN_TIMEOUT;
17 use ln::features::InitFeatures;
18 use ln::msgs::ChannelMessageHandler;
19 use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination};
21 use bitcoin::blockdata::script::Builder;
22 use bitcoin::blockdata::opcodes;
23 use bitcoin::secp256k1::Secp256k1;
24 use bitcoin::Transaction;
28 use ln::functional_test_utils::*;
31 fn chanmon_fail_from_stale_commitment() {
32 // If we forward an HTLC to our counterparty, but we force-closed the channel before our
33 // counterparty provides us an updated commitment transaction, we'll end up with a commitment
34 // transaction that does not contain the HTLC which we attempted to forward. In this case, we
35 // need to wait `ANTI_REORG_DELAY` blocks and then fail back the HTLC as there is no way for us
36 // to learn the preimage and the confirmed commitment transaction paid us the value of the
39 // However, previously, we did not do this, ignoring the HTLC entirely.
41 // This could lead to channel closure if the sender we received the HTLC from decides to go on
42 // chain to get their HTLC back before it times out.
44 // Here, we check exactly this case, forwarding a payment from A, through B, to C, before B
45 // broadcasts its latest commitment transaction, which should result in it eventually failing
46 // the HTLC back off-chain to A.
47 let chanmon_cfgs = create_chanmon_cfgs(3);
48 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
49 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
50 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
52 create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
53 let (update_a, _, chan_id_2, _) = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
55 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 1_000_000);
56 nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
57 check_added_monitors!(nodes[0], 1);
59 let bs_txn = get_local_commitment_txn!(nodes[1], chan_id_2);
61 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
62 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
63 commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
65 expect_pending_htlcs_forwardable!(nodes[1]);
66 get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
67 check_added_monitors!(nodes[1], 1);
69 // Don't bother delivering the new HTLC add/commits, instead confirming the pre-HTLC commitment
70 // transaction for nodes[1].
71 mine_transaction(&nodes[1], &bs_txn[0]);
72 check_added_monitors!(nodes[1], 1);
73 check_closed_broadcast!(nodes[1], true);
74 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
75 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
77 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
78 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_id_2 }]);
79 check_added_monitors!(nodes[1], 1);
80 let fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
82 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates.update_fail_htlcs[0]);
83 commitment_signed_dance!(nodes[0], nodes[1], fail_updates.commitment_signed, true, true);
84 expect_payment_failed_with_update!(nodes[0], payment_hash, false, update_a.contents.short_channel_id, true);
87 fn test_spendable_output<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, spendable_tx: &Transaction) {
88 let mut spendable = node.chain_monitor.chain_monitor.get_and_clear_pending_events();
89 assert_eq!(spendable.len(), 1);
90 if let Event::SpendableOutputs { outputs } = spendable.pop().unwrap() {
91 assert_eq!(outputs.len(), 1);
92 let spend_tx = node.keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
93 Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
94 check_spends!(spend_tx, spendable_tx);
99 fn revoked_output_htlc_resolution_timing() {
100 // Tests that HTLCs which were present in a broadcasted remote revoked commitment transaction
101 // are resolved only after a spend of the HTLC output reaches six confirmations. Preivously
102 // they would resolve after the revoked commitment transaction itself reaches six
104 let chanmon_cfgs = create_chanmon_cfgs(2);
105 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
106 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
107 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
109 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000, InitFeatures::known(), InitFeatures::known());
111 let payment_hash_1 = route_payment(&nodes[1], &[&nodes[0]], 1_000_000).1;
113 // Get a commitment transaction which contains the HTLC we care about, but which we'll revoke
114 // before forwarding.
115 let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan.2);
116 assert_eq!(revoked_local_txn.len(), 1);
118 // Route a dust payment to revoke the above commitment transaction
119 route_payment(&nodes[0], &[&nodes[1]], 1_000);
121 // Confirm the revoked commitment transaction, closing the channel.
122 mine_transaction(&nodes[1], &revoked_local_txn[0]);
123 check_added_monitors!(nodes[1], 1);
124 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
125 check_closed_broadcast!(nodes[1], true);
127 let bs_spend_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
128 assert_eq!(bs_spend_txn.len(), 2);
129 check_spends!(bs_spend_txn[0], revoked_local_txn[0]);
130 check_spends!(bs_spend_txn[1], chan.3);
132 // After the commitment transaction confirms, we should still wait on the HTLC spend
133 // transaction to confirm before resolving the HTLC.
134 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
135 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
136 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
138 // Spend the HTLC output, generating a HTLC failure event after ANTI_REORG_DELAY confirmations.
139 mine_transaction(&nodes[1], &bs_spend_txn[0]);
140 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
141 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
143 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
144 expect_payment_failed!(nodes[1], payment_hash_1, true);
148 fn chanmon_claim_value_coop_close() {
149 // Tests `get_claimable_balances` returns the correct values across a simple cooperative claim.
150 // Specifically, this tests that the channel non-HTLC balances show up in
151 // `get_claimable_balances` until the cooperative claims have confirmed and generated a
152 // `SpendableOutputs` event, and no longer.
153 let chanmon_cfgs = create_chanmon_cfgs(2);
154 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
155 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
156 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
158 let (_, _, chan_id, funding_tx) =
159 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 1_000_000, InitFeatures::known(), InitFeatures::known());
160 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
161 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
163 let chan_feerate = get_feerate!(nodes[0], chan_id) as u64;
164 let opt_anchors = get_opt_anchors!(nodes[0], chan_id);
166 assert_eq!(vec![Balance::ClaimableOnChannelClose {
167 claimable_amount_satoshis: 1_000_000 - 1_000 - chan_feerate * channel::commitment_tx_base_weight(opt_anchors) / 1000
169 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
170 assert_eq!(vec![Balance::ClaimableOnChannelClose { claimable_amount_satoshis: 1_000, }],
171 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
173 nodes[0].node.close_channel(&chan_id, &nodes[1].node.get_our_node_id()).unwrap();
174 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
175 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &InitFeatures::known(), &node_0_shutdown);
176 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
177 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_1_shutdown);
179 let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
180 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
181 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
182 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
183 let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
184 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
185 let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
186 assert!(node_1_none.is_none());
188 let shutdown_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
189 assert_eq!(shutdown_tx, nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0));
190 assert_eq!(shutdown_tx.len(), 1);
192 mine_transaction(&nodes[0], &shutdown_tx[0]);
193 mine_transaction(&nodes[1], &shutdown_tx[0]);
195 assert!(nodes[0].node.list_channels().is_empty());
196 assert!(nodes[1].node.list_channels().is_empty());
198 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
199 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
201 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
202 claimable_amount_satoshis: 1_000_000 - 1_000 - chan_feerate * channel::commitment_tx_base_weight(opt_anchors) / 1000,
203 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
205 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
206 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
207 claimable_amount_satoshis: 1000,
208 confirmation_height: nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1,
210 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
212 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
213 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
215 assert_eq!(Vec::<Balance>::new(),
216 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
217 assert_eq!(Vec::<Balance>::new(),
218 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
220 test_spendable_output(&nodes[0], &shutdown_tx[0]);
221 test_spendable_output(&nodes[1], &shutdown_tx[0]);
223 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure);
224 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure);
227 fn sorted_vec<T: Ord>(mut v: Vec<T>) -> Vec<T> {
232 /// Asserts that `a` and `b` are close, but maybe off by up to 5.
233 /// This is useful when checking fees and weights on transactions as things may vary by a few based
234 /// on signature size and signature size estimation being non-exact.
235 fn fuzzy_assert_eq<V: core::convert::TryInto<u64>>(a: V, b: V) {
236 let a_u64 = a.try_into().map_err(|_| ()).unwrap();
237 let b_u64 = b.try_into().map_err(|_| ()).unwrap();
238 eprintln!("Checking {} and {} for fuzzy equality", a_u64, b_u64);
239 assert!(a_u64 >= b_u64 - 5);
240 assert!(b_u64 >= a_u64 - 5);
243 fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
244 // Tests `get_claimable_balances` with an HTLC across a force-close.
245 // We build a channel with an HTLC pending, then force close the channel and check that the
246 // `get_claimable_balances` return value is correct as transactions confirm on-chain.
247 let mut chanmon_cfgs = create_chanmon_cfgs(2);
248 if prev_commitment_tx {
249 // We broadcast a second-to-latest commitment transaction, without providing the revocation
250 // secret to the counterparty. However, because we always immediately take the revocation
251 // secret from the keys_manager, we would panic at broadcast as we're trying to sign a
252 // transaction which, from the point of view of our keys_manager, is revoked.
253 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
255 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
256 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
257 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
259 let (_, _, chan_id, funding_tx) =
260 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 1_000_000, InitFeatures::known(), InitFeatures::known());
261 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
262 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
264 // This HTLC is immediately claimed, giving node B the preimage
265 let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 3_000_000);
266 // This HTLC is allowed to time out, letting A claim it. However, in order to test claimable
267 // balances more fully we also give B the preimage for this HTLC.
268 let (timeout_payment_preimage, timeout_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 4_000_000);
269 // This HTLC will be dust, and not be claimable at all:
270 let (dust_payment_preimage, dust_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 3_000);
272 let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
274 let chan_feerate = get_feerate!(nodes[0], chan_id) as u64;
275 let opt_anchors = get_opt_anchors!(nodes[0], chan_id);
277 let remote_txn = get_local_commitment_txn!(nodes[1], chan_id);
278 // Before B receives the payment preimage, it only suggests the push_msat value of 1_000 sats
279 // as claimable. A lists both its to-self balance and the (possibly-claimable) HTLCs.
280 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
281 claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
282 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
283 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
284 claimable_amount_satoshis: 3_000,
285 claimable_height: htlc_cltv_timeout,
286 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
287 claimable_amount_satoshis: 4_000,
288 claimable_height: htlc_cltv_timeout,
290 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
291 assert_eq!(vec![Balance::ClaimableOnChannelClose {
292 claimable_amount_satoshis: 1_000,
294 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
296 nodes[1].node.claim_funds(payment_preimage);
297 check_added_monitors!(nodes[1], 1);
298 expect_payment_claimed!(nodes[1], payment_hash, 3_000_000);
300 let b_htlc_msgs = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
301 // We claim the dust payment here as well, but it won't impact our claimable balances as its
302 // dust and thus doesn't appear on chain at all.
303 nodes[1].node.claim_funds(dust_payment_preimage);
304 check_added_monitors!(nodes[1], 1);
305 expect_payment_claimed!(nodes[1], dust_payment_hash, 3_000);
307 nodes[1].node.claim_funds(timeout_payment_preimage);
308 check_added_monitors!(nodes[1], 1);
309 expect_payment_claimed!(nodes[1], timeout_payment_hash, 4_000_000);
311 if prev_commitment_tx {
312 // To build a previous commitment transaction, deliver one round of commitment messages.
313 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &b_htlc_msgs.update_fulfill_htlcs[0]);
314 expect_payment_sent_without_paths!(nodes[0], payment_preimage);
315 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &b_htlc_msgs.commitment_signed);
316 check_added_monitors!(nodes[0], 1);
317 let (as_raa, as_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
318 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
319 let _htlc_updates = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
320 check_added_monitors!(nodes[1], 1);
321 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs);
322 let _bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
323 check_added_monitors!(nodes[1], 1);
326 // Once B has received the payment preimage, it includes the value of the HTLC in its
327 // "claimable if you were to close the channel" balance.
328 let mut a_expected_balances = vec![Balance::ClaimableOnChannelClose {
329 claimable_amount_satoshis: 1_000_000 - // Channel funding value in satoshis
330 4_000 - // The to-be-failed HTLC value in satoshis
331 3_000 - // The claimed HTLC value in satoshis
332 1_000 - // The push_msat value in satoshis
333 3 - // The dust HTLC value in satoshis
334 // The commitment transaction fee with two HTLC outputs:
335 chan_feerate * (channel::commitment_tx_base_weight(opt_anchors) +
336 if prev_commitment_tx { 1 } else { 2 } *
337 channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
338 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
339 claimable_amount_satoshis: 4_000,
340 claimable_height: htlc_cltv_timeout,
342 if !prev_commitment_tx {
343 a_expected_balances.push(Balance::MaybeClaimableHTLCAwaitingTimeout {
344 claimable_amount_satoshis: 3_000,
345 claimable_height: htlc_cltv_timeout,
348 assert_eq!(sorted_vec(a_expected_balances),
349 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
350 assert_eq!(vec![Balance::ClaimableOnChannelClose {
351 claimable_amount_satoshis: 1_000 + 3_000 + 4_000,
353 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
355 // Broadcast the closing transaction (which has both pending HTLCs in it) and get B's
356 // broadcasted HTLC claim transaction with preimage.
357 let node_b_commitment_claimable = nodes[1].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
358 mine_transaction(&nodes[0], &remote_txn[0]);
359 mine_transaction(&nodes[1], &remote_txn[0]);
361 let b_broadcast_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
362 assert_eq!(b_broadcast_txn.len(), if prev_commitment_tx { 4 } else { 5 });
363 if prev_commitment_tx {
364 check_spends!(b_broadcast_txn[3], b_broadcast_txn[2]);
366 assert_eq!(b_broadcast_txn[0], b_broadcast_txn[3]);
367 assert_eq!(b_broadcast_txn[1], b_broadcast_txn[4]);
369 // b_broadcast_txn[0] should spend the HTLC output of the commitment tx for 3_000 sats
370 check_spends!(b_broadcast_txn[0], remote_txn[0]);
371 check_spends!(b_broadcast_txn[1], remote_txn[0]);
372 assert_eq!(b_broadcast_txn[0].input.len(), 1);
373 assert_eq!(b_broadcast_txn[1].input.len(), 1);
374 assert_eq!(remote_txn[0].output[b_broadcast_txn[0].input[0].previous_output.vout as usize].value, 3_000);
375 assert_eq!(remote_txn[0].output[b_broadcast_txn[1].input[0].previous_output.vout as usize].value, 4_000);
376 check_spends!(b_broadcast_txn[2], funding_tx);
378 assert!(nodes[0].node.list_channels().is_empty());
379 check_closed_broadcast!(nodes[0], true);
380 check_added_monitors!(nodes[0], 1);
381 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
382 assert!(nodes[1].node.list_channels().is_empty());
383 check_closed_broadcast!(nodes[1], true);
384 check_added_monitors!(nodes[1], 1);
385 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
386 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
387 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
389 // Once the commitment transaction confirms, we will wait until ANTI_REORG_DELAY until we
390 // generate any `SpendableOutputs` events. Thus, the same balances will still be listed
391 // available in `get_claimable_balances`. However, both will swap from `ClaimableOnClose` to
392 // other Balance variants, as close has already happened.
393 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
394 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
396 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
397 claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
398 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
399 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
400 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
401 claimable_amount_satoshis: 3_000,
402 claimable_height: htlc_cltv_timeout,
403 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
404 claimable_amount_satoshis: 4_000,
405 claimable_height: htlc_cltv_timeout,
407 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
408 // The main non-HTLC balance is just awaiting confirmations, but the claimable height is the
409 // CSV delay, not ANTI_REORG_DELAY.
410 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
411 claimable_amount_satoshis: 1_000,
412 confirmation_height: node_b_commitment_claimable,
414 // Both HTLC balances are "contentious" as our counterparty could claim them if we wait too
416 Balance::ContentiousClaimable {
417 claimable_amount_satoshis: 3_000,
418 timeout_height: htlc_cltv_timeout,
419 }, Balance::ContentiousClaimable {
420 claimable_amount_satoshis: 4_000,
421 timeout_height: htlc_cltv_timeout,
423 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
425 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
426 expect_payment_failed!(nodes[0], dust_payment_hash, true);
427 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
429 // After ANTI_REORG_DELAY, A will consider its balance fully spendable and generate a
430 // `SpendableOutputs` event. However, B still has to wait for the CSV delay.
431 assert_eq!(sorted_vec(vec![Balance::MaybeClaimableHTLCAwaitingTimeout {
432 claimable_amount_satoshis: 3_000,
433 claimable_height: htlc_cltv_timeout,
434 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
435 claimable_amount_satoshis: 4_000,
436 claimable_height: htlc_cltv_timeout,
438 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
439 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
440 claimable_amount_satoshis: 1_000,
441 confirmation_height: node_b_commitment_claimable,
442 }, Balance::ContentiousClaimable {
443 claimable_amount_satoshis: 3_000,
444 timeout_height: htlc_cltv_timeout,
445 }, Balance::ContentiousClaimable {
446 claimable_amount_satoshis: 4_000,
447 timeout_height: htlc_cltv_timeout,
449 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
451 test_spendable_output(&nodes[0], &remote_txn[0]);
452 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
454 // After broadcasting the HTLC claim transaction, node A will still consider the HTLC
455 // possibly-claimable up to ANTI_REORG_DELAY, at which point it will drop it.
456 mine_transaction(&nodes[0], &b_broadcast_txn[0]);
457 if prev_commitment_tx {
458 expect_payment_path_successful!(nodes[0]);
460 expect_payment_sent!(nodes[0], payment_preimage);
462 assert_eq!(sorted_vec(vec![Balance::MaybeClaimableHTLCAwaitingTimeout {
463 claimable_amount_satoshis: 3_000,
464 claimable_height: htlc_cltv_timeout,
465 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
466 claimable_amount_satoshis: 4_000,
467 claimable_height: htlc_cltv_timeout,
469 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
470 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
471 assert_eq!(vec![Balance::MaybeClaimableHTLCAwaitingTimeout {
472 claimable_amount_satoshis: 4_000,
473 claimable_height: htlc_cltv_timeout,
475 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
477 // When the HTLC timeout output is spendable in the next block, A should broadcast it
478 connect_blocks(&nodes[0], htlc_cltv_timeout - nodes[0].best_block_info().1 - 1);
479 let a_broadcast_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
480 assert_eq!(a_broadcast_txn.len(), 3);
481 check_spends!(a_broadcast_txn[0], funding_tx);
482 assert_eq!(a_broadcast_txn[1].input.len(), 1);
483 check_spends!(a_broadcast_txn[1], remote_txn[0]);
484 assert_eq!(a_broadcast_txn[2].input.len(), 1);
485 check_spends!(a_broadcast_txn[2], remote_txn[0]);
486 assert_ne!(a_broadcast_txn[1].input[0].previous_output.vout,
487 a_broadcast_txn[2].input[0].previous_output.vout);
488 // a_broadcast_txn [1] and [2] should spend the HTLC outputs of the commitment tx
489 assert_eq!(remote_txn[0].output[a_broadcast_txn[1].input[0].previous_output.vout as usize].value, 3_000);
490 assert_eq!(remote_txn[0].output[a_broadcast_txn[2].input[0].previous_output.vout as usize].value, 4_000);
492 // Once the HTLC-Timeout transaction confirms, A will no longer consider the HTLC
493 // "MaybeClaimable", but instead move it to "AwaitingConfirmations".
494 mine_transaction(&nodes[0], &a_broadcast_txn[2]);
495 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
496 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
497 claimable_amount_satoshis: 4_000,
498 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
500 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
501 // After ANTI_REORG_DELAY, A will generate a SpendableOutputs event and drop the claimable
503 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
504 assert_eq!(Vec::<Balance>::new(),
505 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
506 expect_payment_failed!(nodes[0], timeout_payment_hash, true);
508 test_spendable_output(&nodes[0], &a_broadcast_txn[2]);
510 // Node B will no longer consider the HTLC "contentious" after the HTLC claim transaction
511 // confirms, and consider it simply "awaiting confirmations". Note that it has to wait for the
512 // standard revocable transaction CSV delay before receiving a `SpendableOutputs`.
513 let node_b_htlc_claimable = nodes[1].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
514 mine_transaction(&nodes[1], &b_broadcast_txn[0]);
516 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
517 claimable_amount_satoshis: 1_000,
518 confirmation_height: node_b_commitment_claimable,
519 }, Balance::ClaimableAwaitingConfirmations {
520 claimable_amount_satoshis: 3_000,
521 confirmation_height: node_b_htlc_claimable,
522 }, Balance::ContentiousClaimable {
523 claimable_amount_satoshis: 4_000,
524 timeout_height: htlc_cltv_timeout,
526 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
528 // After reaching the commitment output CSV, we'll get a SpendableOutputs event for it and have
529 // only the HTLCs claimable on node B.
530 connect_blocks(&nodes[1], node_b_commitment_claimable - nodes[1].best_block_info().1);
531 test_spendable_output(&nodes[1], &remote_txn[0]);
533 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
534 claimable_amount_satoshis: 3_000,
535 confirmation_height: node_b_htlc_claimable,
536 }, Balance::ContentiousClaimable {
537 claimable_amount_satoshis: 4_000,
538 timeout_height: htlc_cltv_timeout,
540 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
542 // After reaching the claimed HTLC output CSV, we'll get a SpendableOutptus event for it and
543 // have only one HTLC output left spendable.
544 connect_blocks(&nodes[1], node_b_htlc_claimable - nodes[1].best_block_info().1);
545 test_spendable_output(&nodes[1], &b_broadcast_txn[0]);
547 assert_eq!(vec![Balance::ContentiousClaimable {
548 claimable_amount_satoshis: 4_000,
549 timeout_height: htlc_cltv_timeout,
551 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
553 // Finally, mine the HTLC timeout transaction that A broadcasted (even though B should be able
554 // to claim this HTLC with the preimage it knows!). It will remain listed as a claimable HTLC
555 // until ANTI_REORG_DELAY confirmations on the spend.
556 mine_transaction(&nodes[1], &a_broadcast_txn[2]);
557 assert_eq!(vec![Balance::ContentiousClaimable {
558 claimable_amount_satoshis: 4_000,
559 timeout_height: htlc_cltv_timeout,
561 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
562 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
563 assert_eq!(Vec::<Balance>::new(),
564 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
568 fn test_claim_value_force_close() {
569 do_test_claim_value_force_close(true);
570 do_test_claim_value_force_close(false);
574 fn test_balances_on_local_commitment_htlcs() {
575 // Previously, when handling the broadcast of a local commitment transactions (with associated
576 // CSV delays prior to spendability), we incorrectly handled the CSV delays on HTLC
577 // transactions. This caused us to miss spendable outputs for HTLCs which were awaiting a CSV
578 // delay prior to spendability.
580 // Further, because of this, we could hit an assertion as `get_claimable_balances` asserted
581 // that HTLCs were resolved after the funding spend was resolved, which was not true if the
582 // HTLC did not have a CSV delay attached (due to the above bug or due to it being an HTLC
583 // claim by our counterparty).
584 let chanmon_cfgs = create_chanmon_cfgs(2);
585 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
586 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
587 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
589 // Create a single channel with two pending HTLCs from nodes[0] to nodes[1], one which nodes[1]
590 // knows the preimage for, one which it does not.
591 let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known());
592 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
594 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 10_000_000);
595 let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
596 nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
597 check_added_monitors!(nodes[0], 1);
599 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
600 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
601 commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
603 expect_pending_htlcs_forwardable!(nodes[1]);
604 expect_payment_received!(nodes[1], payment_hash, payment_secret, 10_000_000);
606 let (route_2, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 20_000_000);
607 nodes[0].node.send_payment(&route_2, payment_hash_2, &Some(payment_secret_2)).unwrap();
608 check_added_monitors!(nodes[0], 1);
610 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
611 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
612 commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
614 expect_pending_htlcs_forwardable!(nodes[1]);
615 expect_payment_received!(nodes[1], payment_hash_2, payment_secret_2, 20_000_000);
616 nodes[1].node.claim_funds(payment_preimage_2);
617 get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
618 check_added_monitors!(nodes[1], 1);
619 expect_payment_claimed!(nodes[1], payment_hash_2, 20_000_000);
621 let chan_feerate = get_feerate!(nodes[0], chan_id) as u64;
622 let opt_anchors = get_opt_anchors!(nodes[0], chan_id);
624 // Get nodes[0]'s commitment transaction and HTLC-Timeout transactions
625 let as_txn = get_local_commitment_txn!(nodes[0], chan_id);
626 assert_eq!(as_txn.len(), 3);
627 check_spends!(as_txn[1], as_txn[0]);
628 check_spends!(as_txn[2], as_txn[0]);
629 check_spends!(as_txn[0], funding_tx);
631 // First confirm the commitment transaction on nodes[0], which should leave us with three
632 // claimable balances.
633 let node_a_commitment_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
634 mine_transaction(&nodes[0], &as_txn[0]);
635 check_added_monitors!(nodes[0], 1);
636 check_closed_broadcast!(nodes[0], true);
637 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
639 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
640 claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
641 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
642 confirmation_height: node_a_commitment_claimable,
643 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
644 claimable_amount_satoshis: 10_000,
645 claimable_height: htlc_cltv_timeout,
646 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
647 claimable_amount_satoshis: 20_000,
648 claimable_height: htlc_cltv_timeout,
650 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
652 // Get nodes[1]'s HTLC claim tx for the second HTLC
653 mine_transaction(&nodes[1], &as_txn[0]);
654 check_added_monitors!(nodes[1], 1);
655 check_closed_broadcast!(nodes[1], true);
656 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
657 let bs_htlc_claim_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
658 assert_eq!(bs_htlc_claim_txn.len(), 3);
659 check_spends!(bs_htlc_claim_txn[0], as_txn[0]);
660 check_spends!(bs_htlc_claim_txn[1], funding_tx);
661 check_spends!(bs_htlc_claim_txn[2], bs_htlc_claim_txn[1]);
663 // Connect blocks until the HTLCs expire, allowing us to (validly) broadcast the HTLC-Timeout
665 connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1);
666 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
667 claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
668 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
669 confirmation_height: node_a_commitment_claimable,
670 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
671 claimable_amount_satoshis: 10_000,
672 claimable_height: htlc_cltv_timeout,
673 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
674 claimable_amount_satoshis: 20_000,
675 claimable_height: htlc_cltv_timeout,
677 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
678 assert_eq!(as_txn[1].lock_time.0, nodes[0].best_block_info().1 + 1); // as_txn[1] can be included in the next block
680 // Now confirm nodes[0]'s HTLC-Timeout transaction, which changes the claimable balance to an
681 // "awaiting confirmations" one.
682 let node_a_htlc_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
683 mine_transaction(&nodes[0], &as_txn[1]);
684 // Note that prior to the fix in the commit which introduced this test, this (and the next
685 // balance) check failed. With this check removed, the code panicked in the `connect_blocks`
686 // call, as described, two hunks down.
687 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
688 claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
689 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
690 confirmation_height: node_a_commitment_claimable,
691 }, Balance::ClaimableAwaitingConfirmations {
692 claimable_amount_satoshis: 10_000,
693 confirmation_height: node_a_htlc_claimable,
694 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
695 claimable_amount_satoshis: 20_000,
696 claimable_height: htlc_cltv_timeout,
698 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
700 // Now confirm nodes[1]'s HTLC claim, giving nodes[0] the preimage. Note that the "maybe
701 // claimable" balance remains until we see ANTI_REORG_DELAY blocks.
702 mine_transaction(&nodes[0], &bs_htlc_claim_txn[0]);
703 expect_payment_sent!(nodes[0], payment_preimage_2);
704 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
705 claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
706 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
707 confirmation_height: node_a_commitment_claimable,
708 }, Balance::ClaimableAwaitingConfirmations {
709 claimable_amount_satoshis: 10_000,
710 confirmation_height: node_a_htlc_claimable,
711 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
712 claimable_amount_satoshis: 20_000,
713 claimable_height: htlc_cltv_timeout,
715 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
717 // Finally make the HTLC transactions have ANTI_REORG_DELAY blocks. This call previously
718 // panicked as described in the test introduction. This will remove the "maybe claimable"
719 // spendable output as nodes[1] has fully claimed the second HTLC.
720 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
721 expect_payment_failed!(nodes[0], payment_hash, true);
723 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
724 claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
725 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
726 confirmation_height: node_a_commitment_claimable,
727 }, Balance::ClaimableAwaitingConfirmations {
728 claimable_amount_satoshis: 10_000,
729 confirmation_height: node_a_htlc_claimable,
731 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
733 // Connect blocks until the commitment transaction's CSV expires, providing us the relevant
734 // `SpendableOutputs` event and removing the claimable balance entry.
735 connect_blocks(&nodes[0], node_a_commitment_claimable - nodes[0].best_block_info().1);
736 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
737 claimable_amount_satoshis: 10_000,
738 confirmation_height: node_a_htlc_claimable,
740 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
741 test_spendable_output(&nodes[0], &as_txn[0]);
743 // Connect blocks until the HTLC-Timeout's CSV expires, providing us the relevant
744 // `SpendableOutputs` event and removing the claimable balance entry.
745 connect_blocks(&nodes[0], node_a_htlc_claimable - nodes[0].best_block_info().1);
746 assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
747 test_spendable_output(&nodes[0], &as_txn[1]);
750 fn sorted_vec_with_additions<T: Ord + Clone>(v_orig: &Vec<T>, extra_ts: &[&T]) -> Vec<T> {
751 let mut v = v_orig.clone();
753 v.push((*t).clone());
759 fn do_test_revoked_counterparty_commitment_balances(confirm_htlc_spend_first: bool) {
760 // Tests `get_claimable_balances` for revoked counterparty commitment transactions.
761 let mut chanmon_cfgs = create_chanmon_cfgs(2);
762 // We broadcast a second-to-latest commitment transaction, without providing the revocation
763 // secret to the counterparty. However, because we always immediately take the revocation
764 // secret from the keys_manager, we would panic at broadcast as we're trying to sign a
765 // transaction which, from the point of view of our keys_manager, is revoked.
766 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
767 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
768 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
769 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
771 let (_, _, chan_id, funding_tx) =
772 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 100_000_000, InitFeatures::known(), InitFeatures::known());
773 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
774 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
776 // We create five HTLCs for B to claim against A's revoked commitment transaction:
778 // (1) one for which A is the originator and B knows the preimage
779 // (2) one for which B is the originator where the HTLC has since timed-out
780 // (3) one for which B is the originator but where the HTLC has not yet timed-out
781 // (4) one dust HTLC which is lost in the channel closure
782 // (5) one that actually isn't in the revoked commitment transaction at all, but was added in
783 // later commitment transaction updates
785 // Though they could all be claimed in a single claim transaction, due to CLTV timeouts they
786 // are all currently claimed in separate transactions, which helps us test as we can claim
787 // HTLCs individually.
789 let (claimed_payment_preimage, claimed_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 3_000_000);
790 let timeout_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 4_000_000).1;
791 let dust_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 3_000).1;
793 let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
795 connect_blocks(&nodes[0], 10);
796 connect_blocks(&nodes[1], 10);
798 let live_htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
799 let live_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 5_000_000).1;
801 // Get the latest commitment transaction from A and then update the fee to revoke it
802 let as_revoked_txn = get_local_commitment_txn!(nodes[0], chan_id);
803 let opt_anchors = get_opt_anchors!(nodes[0], chan_id);
805 let chan_feerate = get_feerate!(nodes[0], chan_id) as u64;
807 let missing_htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
808 let missing_htlc_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 2_000_000).1;
810 nodes[1].node.claim_funds(claimed_payment_preimage);
811 expect_payment_claimed!(nodes[1], claimed_payment_hash, 3_000_000);
812 check_added_monitors!(nodes[1], 1);
813 let _b_htlc_msgs = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
815 connect_blocks(&nodes[0], htlc_cltv_timeout + 1 - 10);
816 check_closed_broadcast!(nodes[0], true);
817 check_added_monitors!(nodes[0], 1);
819 let mut events = nodes[0].node.get_and_clear_pending_events();
820 assert_eq!(events.len(), 6);
821 let mut failed_payments: HashSet<_> =
822 [timeout_payment_hash, dust_payment_hash, live_payment_hash, missing_htlc_payment_hash]
823 .iter().map(|a| *a).collect();
826 Event::HTLCHandlingFailed { failed_next_destination: HTLCDestination::NextHopChannel { node_id, channel_id }, .. } => {
827 assert_eq!(*channel_id, chan_id);
828 assert_eq!(*node_id, Some(nodes[1].node.get_our_node_id()));
831 Event::HTLCHandlingFailed { failed_next_destination: HTLCDestination::FailedPayment { payment_hash }, .. } => {
832 assert!(failed_payments.remove(payment_hash));
838 assert!(failed_payments.is_empty());
839 if let Event::PendingHTLCsForwardable { .. } = events[0] {} else { panic!(); }
841 Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {},
845 connect_blocks(&nodes[1], htlc_cltv_timeout + 1 - 10);
846 check_closed_broadcast!(nodes[1], true);
847 check_added_monitors!(nodes[1], 1);
848 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
850 // Prior to channel closure, B considers the preimage HTLC as its own, and otherwise only
851 // lists the two on-chain timeout-able HTLCs as claimable balances.
852 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
853 claimable_amount_satoshis: 100_000 - 5_000 - 4_000 - 3 - 2_000 + 3_000,
854 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
855 claimable_amount_satoshis: 2_000,
856 claimable_height: missing_htlc_cltv_timeout,
857 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
858 claimable_amount_satoshis: 4_000,
859 claimable_height: htlc_cltv_timeout,
860 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
861 claimable_amount_satoshis: 5_000,
862 claimable_height: live_htlc_cltv_timeout,
864 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
866 mine_transaction(&nodes[1], &as_revoked_txn[0]);
867 let mut claim_txn: Vec<_> = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().drain(..).filter(|tx| tx.input.iter().any(|inp| inp.previous_output.txid == as_revoked_txn[0].txid())).collect();
868 // Currently the revoked commitment is claimed in four transactions as the HTLCs all expire
870 assert_eq!(claim_txn.len(), 4);
871 claim_txn.sort_unstable_by_key(|tx| tx.output.iter().map(|output| output.value).sum::<u64>());
873 // The following constants were determined experimentally
874 const BS_TO_SELF_CLAIM_EXP_WEIGHT: usize = 483;
875 const OUTBOUND_HTLC_CLAIM_EXP_WEIGHT: usize = 571;
876 const INBOUND_HTLC_CLAIM_EXP_WEIGHT: usize = 578;
878 // Check that the weight is close to the expected weight. Note that signature sizes vary
879 // somewhat so it may not always be exact.
880 fuzzy_assert_eq(claim_txn[0].weight(), OUTBOUND_HTLC_CLAIM_EXP_WEIGHT);
881 fuzzy_assert_eq(claim_txn[1].weight(), INBOUND_HTLC_CLAIM_EXP_WEIGHT);
882 fuzzy_assert_eq(claim_txn[2].weight(), INBOUND_HTLC_CLAIM_EXP_WEIGHT);
883 fuzzy_assert_eq(claim_txn[3].weight(), BS_TO_SELF_CLAIM_EXP_WEIGHT);
885 // The expected balance for the next three checks, with the largest-HTLC and to_self output
886 // claim balances separated out.
887 let expected_balance = vec![Balance::ClaimableAwaitingConfirmations {
888 // to_remote output in A's revoked commitment
889 claimable_amount_satoshis: 100_000 - 5_000 - 4_000 - 3,
890 confirmation_height: nodes[1].best_block_info().1 + 5,
891 }, Balance::CounterpartyRevokedOutputClaimable {
892 claimable_amount_satoshis: 3_000,
893 }, Balance::CounterpartyRevokedOutputClaimable {
894 claimable_amount_satoshis: 4_000,
897 let to_self_unclaimed_balance = Balance::CounterpartyRevokedOutputClaimable {
898 claimable_amount_satoshis: 1_000_000 - 100_000 - 3_000 - chan_feerate *
899 (channel::commitment_tx_base_weight(opt_anchors) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
901 let to_self_claimed_avail_height;
902 let largest_htlc_unclaimed_balance = Balance::CounterpartyRevokedOutputClaimable {
903 claimable_amount_satoshis: 5_000,
905 let largest_htlc_claimed_avail_height;
907 // Once the channel has been closed by A, B now considers all of the commitment transactions'
908 // outputs as `CounterpartyRevokedOutputClaimable`.
909 assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_unclaimed_balance, &largest_htlc_unclaimed_balance]),
910 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
912 if confirm_htlc_spend_first {
913 mine_transaction(&nodes[1], &claim_txn[2]);
914 largest_htlc_claimed_avail_height = nodes[1].best_block_info().1 + 5;
915 to_self_claimed_avail_height = nodes[1].best_block_info().1 + 6; // will be claimed in the next block
917 // Connect the to_self output claim, taking all of A's non-HTLC funds
918 mine_transaction(&nodes[1], &claim_txn[3]);
919 to_self_claimed_avail_height = nodes[1].best_block_info().1 + 5;
920 largest_htlc_claimed_avail_height = nodes[1].best_block_info().1 + 6; // will be claimed in the next block
923 let largest_htlc_claimed_balance = Balance::ClaimableAwaitingConfirmations {
924 claimable_amount_satoshis: 5_000 - chan_feerate * INBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
925 confirmation_height: largest_htlc_claimed_avail_height,
927 let to_self_claimed_balance = Balance::ClaimableAwaitingConfirmations {
928 claimable_amount_satoshis: 1_000_000 - 100_000 - 3_000 - chan_feerate *
929 (channel::commitment_tx_base_weight(opt_anchors) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
930 - chan_feerate * claim_txn[3].weight() as u64 / 1000,
931 confirmation_height: to_self_claimed_avail_height,
934 if confirm_htlc_spend_first {
935 assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_unclaimed_balance, &largest_htlc_claimed_balance]),
936 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
938 assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_claimed_balance, &largest_htlc_unclaimed_balance]),
939 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
942 if confirm_htlc_spend_first {
943 mine_transaction(&nodes[1], &claim_txn[3]);
945 mine_transaction(&nodes[1], &claim_txn[2]);
947 assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_claimed_balance, &largest_htlc_claimed_balance]),
948 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
950 // Finally, connect the last two remaining HTLC spends and check that they move to
951 // `ClaimableAwaitingConfirmations`
952 mine_transaction(&nodes[1], &claim_txn[0]);
953 mine_transaction(&nodes[1], &claim_txn[1]);
955 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
956 // to_remote output in A's revoked commitment
957 claimable_amount_satoshis: 100_000 - 5_000 - 4_000 - 3,
958 confirmation_height: nodes[1].best_block_info().1 + 1,
959 }, Balance::ClaimableAwaitingConfirmations {
960 claimable_amount_satoshis: 1_000_000 - 100_000 - 3_000 - chan_feerate *
961 (channel::commitment_tx_base_weight(opt_anchors) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
962 - chan_feerate * claim_txn[3].weight() as u64 / 1000,
963 confirmation_height: to_self_claimed_avail_height,
964 }, Balance::ClaimableAwaitingConfirmations {
965 claimable_amount_satoshis: 3_000 - chan_feerate * OUTBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
966 confirmation_height: nodes[1].best_block_info().1 + 4,
967 }, Balance::ClaimableAwaitingConfirmations {
968 claimable_amount_satoshis: 4_000 - chan_feerate * INBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
969 confirmation_height: nodes[1].best_block_info().1 + 5,
970 }, Balance::ClaimableAwaitingConfirmations {
971 claimable_amount_satoshis: 5_000 - chan_feerate * INBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
972 confirmation_height: largest_htlc_claimed_avail_height,
974 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
976 connect_blocks(&nodes[1], 1);
977 test_spendable_output(&nodes[1], &as_revoked_txn[0]);
979 let mut payment_failed_events = nodes[1].node.get_and_clear_pending_events();
980 expect_payment_failed_conditions_event(&nodes[1], payment_failed_events.pop().unwrap(),
981 dust_payment_hash, true, PaymentFailedConditions::new());
982 expect_payment_failed_conditions_event(&nodes[1], payment_failed_events.pop().unwrap(),
983 missing_htlc_payment_hash, true, PaymentFailedConditions::new());
984 assert!(payment_failed_events.is_empty());
986 connect_blocks(&nodes[1], 1);
987 test_spendable_output(&nodes[1], &claim_txn[if confirm_htlc_spend_first { 2 } else { 3 }]);
988 connect_blocks(&nodes[1], 1);
989 test_spendable_output(&nodes[1], &claim_txn[if confirm_htlc_spend_first { 3 } else { 2 }]);
990 expect_payment_failed!(nodes[1], live_payment_hash, true);
991 connect_blocks(&nodes[1], 1);
992 test_spendable_output(&nodes[1], &claim_txn[0]);
993 connect_blocks(&nodes[1], 1);
994 test_spendable_output(&nodes[1], &claim_txn[1]);
995 expect_payment_failed!(nodes[1], timeout_payment_hash, true);
996 assert_eq!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances(), Vec::new());
1000 fn test_revoked_counterparty_commitment_balances() {
1001 do_test_revoked_counterparty_commitment_balances(true);
1002 do_test_revoked_counterparty_commitment_balances(false);
1006 fn test_revoked_counterparty_htlc_tx_balances() {
1007 // Tests `get_claimable_balances` for revocation spends of HTLC transactions.
1008 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1009 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
1010 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1011 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1012 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1014 // Create some initial channels
1015 let (_, _, chan_id, funding_tx) =
1016 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 11_000_000, InitFeatures::known(), InitFeatures::known());
1017 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
1018 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
1020 let payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 3_000_000).0;
1021 let failed_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 1_000_000).1;
1022 let revoked_local_txn = get_local_commitment_txn!(nodes[1], chan_id);
1023 assert_eq!(revoked_local_txn[0].input.len(), 1);
1024 assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, funding_tx.txid());
1026 // The to-be-revoked commitment tx should have two HTLCs and an output for both sides
1027 assert_eq!(revoked_local_txn[0].output.len(), 4);
1029 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
1031 let chan_feerate = get_feerate!(nodes[0], chan_id) as u64;
1032 let opt_anchors = get_opt_anchors!(nodes[0], chan_id);
1034 // B will generate an HTLC-Success from its revoked commitment tx
1035 mine_transaction(&nodes[1], &revoked_local_txn[0]);
1036 check_closed_broadcast!(nodes[1], true);
1037 check_added_monitors!(nodes[1], 1);
1038 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
1039 let revoked_htlc_success_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1041 assert_eq!(revoked_htlc_success_txn.len(), 2);
1042 assert_eq!(revoked_htlc_success_txn[0].input.len(), 1);
1043 assert_eq!(revoked_htlc_success_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
1044 check_spends!(revoked_htlc_success_txn[0], revoked_local_txn[0]);
1045 check_spends!(revoked_htlc_success_txn[1], funding_tx);
1047 connect_blocks(&nodes[1], TEST_FINAL_CLTV);
1048 let revoked_htlc_timeout_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1049 assert_eq!(revoked_htlc_timeout_txn.len(), 1);
1050 check_spends!(revoked_htlc_timeout_txn[0], revoked_local_txn[0]);
1051 assert_ne!(revoked_htlc_success_txn[0].input[0].previous_output, revoked_htlc_timeout_txn[0].input[0].previous_output);
1052 assert_eq!(revoked_htlc_success_txn[0].lock_time.0, 0);
1053 assert_ne!(revoked_htlc_timeout_txn[0].lock_time.0, 0);
1055 // A will generate justice tx from B's revoked commitment/HTLC tx
1056 mine_transaction(&nodes[0], &revoked_local_txn[0]);
1057 check_closed_broadcast!(nodes[0], true);
1058 check_added_monitors!(nodes[0], 1);
1059 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
1060 let to_remote_conf_height = nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1;
1062 let as_commitment_claim_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1063 assert_eq!(as_commitment_claim_txn.len(), 2);
1064 check_spends!(as_commitment_claim_txn[0], revoked_local_txn[0]);
1065 check_spends!(as_commitment_claim_txn[1], funding_tx);
1067 // The next two checks have the same balance set for A - even though we confirm a revoked HTLC
1068 // transaction our balance tracking doesn't use the on-chain value so the
1069 // `CounterpartyRevokedOutputClaimable` entry doesn't change.
1070 let as_balances = sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1071 // to_remote output in B's revoked commitment
1072 claimable_amount_satoshis: 1_000_000 - 11_000 - 3_000 - chan_feerate *
1073 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1074 confirmation_height: to_remote_conf_height,
1075 }, Balance::CounterpartyRevokedOutputClaimable {
1076 // to_self output in B's revoked commitment
1077 claimable_amount_satoshis: 10_000,
1078 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1079 claimable_amount_satoshis: 3_000,
1080 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1081 claimable_amount_satoshis: 1_000,
1083 assert_eq!(as_balances,
1084 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1086 mine_transaction(&nodes[0], &revoked_htlc_success_txn[0]);
1087 let as_htlc_claim_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1088 assert_eq!(as_htlc_claim_tx.len(), 2);
1089 check_spends!(as_htlc_claim_tx[0], revoked_htlc_success_txn[0]);
1090 check_spends!(as_htlc_claim_tx[1], revoked_local_txn[0]); // A has to generate a new claim for the remaining revoked
1091 // outputs (which no longer includes the spent HTLC output)
1093 assert_eq!(as_balances,
1094 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1096 assert_eq!(as_htlc_claim_tx[0].output.len(), 1);
1097 fuzzy_assert_eq(as_htlc_claim_tx[0].output[0].value,
1098 3_000 - chan_feerate * (revoked_htlc_success_txn[0].weight() + as_htlc_claim_tx[0].weight()) as u64 / 1000);
1100 mine_transaction(&nodes[0], &as_htlc_claim_tx[0]);
1101 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1102 // to_remote output in B's revoked commitment
1103 claimable_amount_satoshis: 1_000_000 - 11_000 - 3_000 - chan_feerate *
1104 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1105 confirmation_height: to_remote_conf_height,
1106 }, Balance::CounterpartyRevokedOutputClaimable {
1107 // to_self output in B's revoked commitment
1108 claimable_amount_satoshis: 10_000,
1109 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1110 claimable_amount_satoshis: 1_000,
1111 }, Balance::ClaimableAwaitingConfirmations {
1112 claimable_amount_satoshis: as_htlc_claim_tx[0].output[0].value,
1113 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
1115 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1117 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 3);
1118 test_spendable_output(&nodes[0], &revoked_local_txn[0]);
1119 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1120 // to_self output to B
1121 claimable_amount_satoshis: 10_000,
1122 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1123 claimable_amount_satoshis: 1_000,
1124 }, Balance::ClaimableAwaitingConfirmations {
1125 claimable_amount_satoshis: as_htlc_claim_tx[0].output[0].value,
1126 confirmation_height: nodes[0].best_block_info().1 + 2,
1128 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1130 connect_blocks(&nodes[0], 2);
1131 test_spendable_output(&nodes[0], &as_htlc_claim_tx[0]);
1132 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1133 // to_self output in B's revoked commitment
1134 claimable_amount_satoshis: 10_000,
1135 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1136 claimable_amount_satoshis: 1_000,
1138 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1140 connect_blocks(&nodes[0], revoked_htlc_timeout_txn[0].lock_time.0 - nodes[0].best_block_info().1);
1141 expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(&nodes[0],
1142 [HTLCDestination::FailedPayment { payment_hash: failed_payment_hash }]);
1143 // As time goes on A may split its revocation claim transaction into multiple.
1144 let as_fewer_input_rbf = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1145 for tx in as_fewer_input_rbf.iter() {
1146 check_spends!(tx, revoked_local_txn[0]);
1149 // Connect a number of additional blocks to ensure we don't forget the HTLC output needs
1151 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
1152 let as_fewer_input_rbf = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1153 for tx in as_fewer_input_rbf.iter() {
1154 check_spends!(tx, revoked_local_txn[0]);
1157 mine_transaction(&nodes[0], &revoked_htlc_timeout_txn[0]);
1158 let as_second_htlc_claim_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1159 assert_eq!(as_second_htlc_claim_tx.len(), 2);
1161 check_spends!(as_second_htlc_claim_tx[0], revoked_htlc_timeout_txn[0]);
1162 check_spends!(as_second_htlc_claim_tx[1], revoked_local_txn[0]);
1164 // Connect blocks to finalize the HTLC resolution with the HTLC-Timeout transaction. In a
1165 // previous iteration of the revoked balance handling this would result in us "forgetting" that
1166 // the revoked HTLC output still needed to be claimed.
1167 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
1168 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1169 // to_self output in B's revoked commitment
1170 claimable_amount_satoshis: 10_000,
1171 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1172 claimable_amount_satoshis: 1_000,
1174 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1176 mine_transaction(&nodes[0], &as_second_htlc_claim_tx[0]);
1177 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1178 // to_self output in B's revoked commitment
1179 claimable_amount_satoshis: 10_000,
1180 }, Balance::ClaimableAwaitingConfirmations {
1181 claimable_amount_satoshis: as_second_htlc_claim_tx[0].output[0].value,
1182 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
1184 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1186 mine_transaction(&nodes[0], &as_second_htlc_claim_tx[1]);
1187 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1188 // to_self output in B's revoked commitment
1189 claimable_amount_satoshis: as_second_htlc_claim_tx[1].output[0].value,
1190 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
1191 }, Balance::ClaimableAwaitingConfirmations {
1192 claimable_amount_satoshis: as_second_htlc_claim_tx[0].output[0].value,
1193 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 2,
1195 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1197 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 2);
1198 test_spendable_output(&nodes[0], &as_second_htlc_claim_tx[0]);
1199 connect_blocks(&nodes[0], 1);
1200 test_spendable_output(&nodes[0], &as_second_htlc_claim_tx[1]);
1202 assert_eq!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances(), Vec::new());
1206 fn test_revoked_counterparty_aggregated_claims() {
1207 // Tests `get_claimable_balances` for revoked counterparty commitment transactions when
1208 // claiming with an aggregated claim transaction.
1209 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1210 // We broadcast a second-to-latest commitment transaction, without providing the revocation
1211 // secret to the counterparty. However, because we always immediately take the revocation
1212 // secret from the keys_manager, we would panic at broadcast as we're trying to sign a
1213 // transaction which, from the point of view of our keys_manager, is revoked.
1214 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
1215 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1216 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1217 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1219 let (_, _, chan_id, funding_tx) =
1220 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 100_000_000, InitFeatures::known(), InitFeatures::known());
1221 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
1222 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
1224 // We create two HTLCs, one which we will give A the preimage to to generate an HTLC-Success
1225 // transaction, and one which we will not, allowing B to claim the HTLC output in an aggregated
1226 // revocation-claim transaction.
1228 let (claimed_payment_preimage, claimed_payment_hash, ..) = route_payment(&nodes[1], &[&nodes[0]], 3_000_000);
1229 let revoked_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 4_000_000).1;
1231 let htlc_cltv_timeout = nodes[1].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
1233 // Cheat by giving A's ChannelMonitor the preimage to the to-be-claimed HTLC so that we have an
1234 // HTLC-claim transaction on the to-be-revoked state.
1235 get_monitor!(nodes[0], chan_id).provide_payment_preimage(&claimed_payment_hash, &claimed_payment_preimage,
1236 &node_cfgs[0].tx_broadcaster, &LowerBoundedFeeEstimator::new(node_cfgs[0].fee_estimator), &nodes[0].logger);
1238 // Now get the latest commitment transaction from A and then update the fee to revoke it
1239 let as_revoked_txn = get_local_commitment_txn!(nodes[0], chan_id);
1241 assert_eq!(as_revoked_txn.len(), 2);
1242 check_spends!(as_revoked_txn[0], funding_tx);
1243 check_spends!(as_revoked_txn[1], as_revoked_txn[0]); // The HTLC-Claim transaction
1245 let opt_anchors = get_opt_anchors!(nodes[0], chan_id);
1246 let chan_feerate = get_feerate!(nodes[0], chan_id) as u64;
1249 let mut feerate = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
1252 nodes[0].node.timer_tick_occurred();
1253 check_added_monitors!(nodes[0], 1);
1255 let fee_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1256 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &fee_update.update_fee.unwrap());
1257 commitment_signed_dance!(nodes[1], nodes[0], fee_update.commitment_signed, false);
1259 nodes[0].node.claim_funds(claimed_payment_preimage);
1260 expect_payment_claimed!(nodes[0], claimed_payment_hash, 3_000_000);
1261 check_added_monitors!(nodes[0], 1);
1262 let _a_htlc_msgs = get_htlc_update_msgs!(&nodes[0], nodes[1].node.get_our_node_id());
1264 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
1265 claimable_amount_satoshis: 100_000 - 4_000 - 3_000,
1266 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
1267 claimable_amount_satoshis: 4_000,
1268 claimable_height: htlc_cltv_timeout,
1269 }, Balance::MaybeClaimableHTLCAwaitingTimeout {
1270 claimable_amount_satoshis: 3_000,
1271 claimable_height: htlc_cltv_timeout,
1273 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1275 mine_transaction(&nodes[1], &as_revoked_txn[0]);
1276 check_closed_broadcast!(nodes[1], true);
1277 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
1278 check_added_monitors!(nodes[1], 1);
1280 let mut claim_txn: Vec<_> = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().drain(..).filter(|tx| tx.input.iter().any(|inp| inp.previous_output.txid == as_revoked_txn[0].txid())).collect();
1281 // Currently the revoked commitment outputs are all claimed in one aggregated transaction
1282 assert_eq!(claim_txn.len(), 1);
1283 assert_eq!(claim_txn[0].input.len(), 3);
1284 check_spends!(claim_txn[0], as_revoked_txn[0]);
1286 let to_remote_maturity = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
1288 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1289 // to_remote output in A's revoked commitment
1290 claimable_amount_satoshis: 100_000 - 4_000 - 3_000,
1291 confirmation_height: to_remote_maturity,
1292 }, Balance::CounterpartyRevokedOutputClaimable {
1293 // to_self output in A's revoked commitment
1294 claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1295 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1296 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1297 claimable_amount_satoshis: 4_000,
1298 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1299 claimable_amount_satoshis: 3_000,
1301 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1303 // Confirm A's HTLC-Success tranasction which presumably raced B's claim, causing B to create a
1305 mine_transaction(&nodes[1], &as_revoked_txn[1]);
1306 expect_payment_sent!(nodes[1], claimed_payment_preimage);
1307 let mut claim_txn_2: Vec<_> = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
1308 claim_txn_2.sort_unstable_by_key(|tx| if tx.input.iter().any(|inp| inp.previous_output.txid == as_revoked_txn[0].txid()) { 0 } else { 1 });
1309 // Once B sees the HTLC-Success transaction it splits its claim transaction into two, though in
1310 // theory it could re-aggregate the claims as well.
1311 assert_eq!(claim_txn_2.len(), 2);
1312 assert_eq!(claim_txn_2[0].input.len(), 2);
1313 check_spends!(claim_txn_2[0], as_revoked_txn[0]);
1314 assert_eq!(claim_txn_2[1].input.len(), 1);
1315 check_spends!(claim_txn_2[1], as_revoked_txn[1]);
1317 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1318 // to_remote output in A's revoked commitment
1319 claimable_amount_satoshis: 100_000 - 4_000 - 3_000,
1320 confirmation_height: to_remote_maturity,
1321 }, Balance::CounterpartyRevokedOutputClaimable {
1322 // to_self output in A's revoked commitment
1323 claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1324 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1325 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1326 claimable_amount_satoshis: 4_000,
1327 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1328 // The amount here is a bit of a misnomer, really its been reduced by the HTLC
1329 // transaction fee, but the claimable amount is always a bit of an overshoot for HTLCs
1330 // anyway, so its not a big change.
1331 claimable_amount_satoshis: 3_000,
1333 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1335 connect_blocks(&nodes[1], 5);
1336 test_spendable_output(&nodes[1], &as_revoked_txn[0]);
1338 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1339 // to_self output in A's revoked commitment
1340 claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1341 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1342 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1343 claimable_amount_satoshis: 4_000,
1344 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1345 // The amount here is a bit of a misnomer, really its been reduced by the HTLC
1346 // transaction fee, but the claimable amount is always a bit of an overshoot for HTLCs
1347 // anyway, so its not a big change.
1348 claimable_amount_satoshis: 3_000,
1350 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1352 mine_transaction(&nodes[1], &claim_txn_2[1]);
1353 let htlc_2_claim_maturity = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
1355 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1356 // to_self output in A's revoked commitment
1357 claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1358 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1359 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1360 claimable_amount_satoshis: 4_000,
1361 }, Balance::ClaimableAwaitingConfirmations { // HTLC 2
1362 claimable_amount_satoshis: claim_txn_2[1].output[0].value,
1363 confirmation_height: htlc_2_claim_maturity,
1365 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1367 connect_blocks(&nodes[1], 5);
1368 test_spendable_output(&nodes[1], &claim_txn_2[1]);
1370 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1371 // to_self output in A's revoked commitment
1372 claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1373 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1374 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1375 claimable_amount_satoshis: 4_000,
1377 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1379 mine_transaction(&nodes[1], &claim_txn_2[0]);
1380 let rest_claim_maturity = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
1382 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
1383 claimable_amount_satoshis: claim_txn_2[0].output[0].value,
1384 confirmation_height: rest_claim_maturity,
1386 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
1388 assert!(nodes[1].node.get_and_clear_pending_events().is_empty()); // We shouldn't fail the payment until we spend the output
1390 connect_blocks(&nodes[1], 5);
1391 expect_payment_failed!(nodes[1], revoked_payment_hash, true);
1392 test_spendable_output(&nodes[1], &claim_txn_2[0]);
1393 assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());