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.
13 use crate::chain::keysinterface::{ChannelSigner, EcdsaChannelSigner};
15 use crate::chain::channelmonitor::LATENCY_GRACE_PERIOD_BLOCKS;
16 use crate::chain::channelmonitor::{ANTI_REORG_DELAY, Balance};
17 use crate::chain::transaction::OutPoint;
18 use crate::chain::chaininterface::LowerBoundedFeeEstimator;
20 use crate::events::bump_transaction::BumpTransactionEvent;
21 use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination};
22 use crate::ln::channel;
24 use crate::ln::chan_utils;
26 use crate::ln::channelmanager::ChannelManager;
27 use crate::ln::channelmanager::{BREAKDOWN_TIMEOUT, PaymentId, RecipientOnionFields};
28 use crate::ln::msgs::ChannelMessageHandler;
30 use crate::util::config::UserConfig;
32 use crate::util::crypto::sign;
33 use crate::util::ser::Writeable;
34 use crate::util::test_utils;
37 use bitcoin::blockdata::transaction::EcdsaSighashType;
38 use bitcoin::blockdata::script::Builder;
39 use bitcoin::blockdata::opcodes;
40 use bitcoin::secp256k1::Secp256k1;
42 use bitcoin::secp256k1::SecretKey;
44 use bitcoin::{Amount, PublicKey, Script, TxIn, TxOut, PackedLockTime, Witness};
45 use bitcoin::Transaction;
47 use bitcoin::util::sighash::SighashCache;
49 use crate::prelude::*;
51 use crate::ln::functional_test_utils::*;
54 fn chanmon_fail_from_stale_commitment() {
55 // If we forward an HTLC to our counterparty, but we force-closed the channel before our
56 // counterparty provides us an updated commitment transaction, we'll end up with a commitment
57 // transaction that does not contain the HTLC which we attempted to forward. In this case, we
58 // need to wait `ANTI_REORG_DELAY` blocks and then fail back the HTLC as there is no way for us
59 // to learn the preimage and the confirmed commitment transaction paid us the value of the
62 // However, previously, we did not do this, ignoring the HTLC entirely.
64 // This could lead to channel closure if the sender we received the HTLC from decides to go on
65 // chain to get their HTLC back before it times out.
67 // Here, we check exactly this case, forwarding a payment from A, through B, to C, before B
68 // broadcasts its latest commitment transaction, which should result in it eventually failing
69 // the HTLC back off-chain to A.
70 let chanmon_cfgs = create_chanmon_cfgs(3);
71 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
72 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
73 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
75 create_announced_chan_between_nodes(&nodes, 0, 1);
76 let (update_a, _, chan_id_2, _) = create_announced_chan_between_nodes(&nodes, 1, 2);
78 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 1_000_000);
79 nodes[0].node.send_payment_with_route(&route, payment_hash,
80 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
81 check_added_monitors!(nodes[0], 1);
83 let bs_txn = get_local_commitment_txn!(nodes[1], chan_id_2);
85 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
86 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
87 commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
89 expect_pending_htlcs_forwardable!(nodes[1]);
90 get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
91 check_added_monitors!(nodes[1], 1);
93 // Don't bother delivering the new HTLC add/commits, instead confirming the pre-HTLC commitment
94 // transaction for nodes[1].
95 mine_transaction(&nodes[1], &bs_txn[0]);
96 check_added_monitors!(nodes[1], 1);
97 check_closed_broadcast!(nodes[1], true);
98 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
99 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
101 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
102 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 }]);
103 check_added_monitors!(nodes[1], 1);
104 let fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
106 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates.update_fail_htlcs[0]);
107 commitment_signed_dance!(nodes[0], nodes[1], fail_updates.commitment_signed, true, true);
108 expect_payment_failed_with_update!(nodes[0], payment_hash, false, update_a.contents.short_channel_id, true);
111 fn test_spendable_output<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, spendable_tx: &Transaction) {
112 let mut spendable = node.chain_monitor.chain_monitor.get_and_clear_pending_events();
113 assert_eq!(spendable.len(), 1);
114 if let Event::SpendableOutputs { outputs } = spendable.pop().unwrap() {
115 assert_eq!(outputs.len(), 1);
116 let spend_tx = node.keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
117 Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
118 check_spends!(spend_tx, spendable_tx);
123 fn revoked_output_htlc_resolution_timing() {
124 // Tests that HTLCs which were present in a broadcasted remote revoked commitment transaction
125 // are resolved only after a spend of the HTLC output reaches six confirmations. Preivously
126 // they would resolve after the revoked commitment transaction itself reaches six
128 let chanmon_cfgs = create_chanmon_cfgs(2);
129 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
130 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
131 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
133 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000);
135 let payment_hash_1 = route_payment(&nodes[1], &[&nodes[0]], 1_000_000).1;
137 // Get a commitment transaction which contains the HTLC we care about, but which we'll revoke
138 // before forwarding.
139 let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan.2);
140 assert_eq!(revoked_local_txn.len(), 1);
142 // Route a dust payment to revoke the above commitment transaction
143 route_payment(&nodes[0], &[&nodes[1]], 1_000);
145 // Confirm the revoked commitment transaction, closing the channel.
146 mine_transaction(&nodes[1], &revoked_local_txn[0]);
147 check_added_monitors!(nodes[1], 1);
148 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
149 check_closed_broadcast!(nodes[1], true);
151 let bs_spend_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
152 assert_eq!(bs_spend_txn.len(), 1);
153 check_spends!(bs_spend_txn[0], revoked_local_txn[0]);
155 // After the commitment transaction confirms, we should still wait on the HTLC spend
156 // transaction to confirm before resolving the HTLC.
157 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
158 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
159 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
161 // Spend the HTLC output, generating a HTLC failure event after ANTI_REORG_DELAY confirmations.
162 mine_transaction(&nodes[1], &bs_spend_txn[0]);
163 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
164 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
166 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
167 expect_payment_failed!(nodes[1], payment_hash_1, false);
171 fn chanmon_claim_value_coop_close() {
172 // Tests `get_claimable_balances` returns the correct values across a simple cooperative claim.
173 // Specifically, this tests that the channel non-HTLC balances show up in
174 // `get_claimable_balances` until the cooperative claims have confirmed and generated a
175 // `SpendableOutputs` event, and no longer.
176 let chanmon_cfgs = create_chanmon_cfgs(2);
177 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
178 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
179 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
181 let (_, _, chan_id, funding_tx) =
182 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 1_000_000);
183 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
184 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
186 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
187 let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
189 assert_eq!(vec![Balance::ClaimableOnChannelClose {
190 claimable_amount_satoshis: 1_000_000 - 1_000 - chan_feerate * channel::commitment_tx_base_weight(opt_anchors) / 1000
192 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
193 assert_eq!(vec![Balance::ClaimableOnChannelClose { claimable_amount_satoshis: 1_000, }],
194 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
196 nodes[0].node.close_channel(&chan_id, &nodes[1].node.get_our_node_id()).unwrap();
197 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
198 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
199 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
200 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
202 let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
203 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
204 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
205 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
206 let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
207 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
208 let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
209 assert!(node_1_none.is_none());
211 let shutdown_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
212 assert_eq!(shutdown_tx, nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0));
213 assert_eq!(shutdown_tx.len(), 1);
215 mine_transaction(&nodes[0], &shutdown_tx[0]);
216 mine_transaction(&nodes[1], &shutdown_tx[0]);
218 assert!(nodes[0].node.list_channels().is_empty());
219 assert!(nodes[1].node.list_channels().is_empty());
221 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
222 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
224 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
225 claimable_amount_satoshis: 1_000_000 - 1_000 - chan_feerate * channel::commitment_tx_base_weight(opt_anchors) / 1000,
226 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
228 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
229 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
230 claimable_amount_satoshis: 1000,
231 confirmation_height: nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1,
233 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
235 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
236 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
238 assert_eq!(Vec::<Balance>::new(),
239 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
240 assert_eq!(Vec::<Balance>::new(),
241 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
243 test_spendable_output(&nodes[0], &shutdown_tx[0]);
244 test_spendable_output(&nodes[1], &shutdown_tx[0]);
246 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure);
247 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure);
250 fn sorted_vec<T: Ord>(mut v: Vec<T>) -> Vec<T> {
255 /// Asserts that `a` and `b` are close, but maybe off by up to 5.
256 /// This is useful when checking fees and weights on transactions as things may vary by a few based
257 /// on signature size and signature size estimation being non-exact.
258 fn fuzzy_assert_eq<V: core::convert::TryInto<u64>>(a: V, b: V) {
259 let a_u64 = a.try_into().map_err(|_| ()).unwrap();
260 let b_u64 = b.try_into().map_err(|_| ()).unwrap();
261 eprintln!("Checking {} and {} for fuzzy equality", a_u64, b_u64);
262 assert!(a_u64 >= b_u64 - 5);
263 assert!(b_u64 >= a_u64 - 5);
266 fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
267 // Tests `get_claimable_balances` with an HTLC across a force-close.
268 // We build a channel with an HTLC pending, then force close the channel and check that the
269 // `get_claimable_balances` return value is correct as transactions confirm on-chain.
270 let mut chanmon_cfgs = create_chanmon_cfgs(2);
271 if prev_commitment_tx {
272 // We broadcast a second-to-latest commitment transaction, without providing the revocation
273 // secret to the counterparty. However, because we always immediately take the revocation
274 // secret from the keys_manager, we would panic at broadcast as we're trying to sign a
275 // transaction which, from the point of view of our keys_manager, is revoked.
276 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
278 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
279 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
280 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
282 let (_, _, chan_id, funding_tx) =
283 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 1_000_000);
284 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
285 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
287 // This HTLC is immediately claimed, giving node B the preimage
288 let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 3_000_000);
289 // This HTLC is allowed to time out, letting A claim it. However, in order to test claimable
290 // balances more fully we also give B the preimage for this HTLC.
291 let (timeout_payment_preimage, timeout_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 4_000_000);
292 // This HTLC will be dust, and not be claimable at all:
293 let (dust_payment_preimage, dust_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 3_000);
295 let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
297 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
298 let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
300 let remote_txn = get_local_commitment_txn!(nodes[1], chan_id);
301 // Before B receives the payment preimage, it only suggests the push_msat value of 1_000 sats
302 // as claimable. A lists both its to-self balance and the (possibly-claimable) HTLCs.
303 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
304 claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
305 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
306 }, Balance::MaybeTimeoutClaimableHTLC {
307 claimable_amount_satoshis: 3_000,
308 claimable_height: htlc_cltv_timeout,
309 }, Balance::MaybeTimeoutClaimableHTLC {
310 claimable_amount_satoshis: 4_000,
311 claimable_height: htlc_cltv_timeout,
313 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
314 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
315 claimable_amount_satoshis: 1_000,
316 }, Balance::MaybePreimageClaimableHTLC {
317 claimable_amount_satoshis: 3_000,
318 expiry_height: htlc_cltv_timeout,
319 }, Balance::MaybePreimageClaimableHTLC {
320 claimable_amount_satoshis: 4_000,
321 expiry_height: htlc_cltv_timeout,
323 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
325 nodes[1].node.claim_funds(payment_preimage);
326 check_added_monitors!(nodes[1], 1);
327 expect_payment_claimed!(nodes[1], payment_hash, 3_000_000);
329 let b_htlc_msgs = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
330 // We claim the dust payment here as well, but it won't impact our claimable balances as its
331 // dust and thus doesn't appear on chain at all.
332 nodes[1].node.claim_funds(dust_payment_preimage);
333 check_added_monitors!(nodes[1], 1);
334 expect_payment_claimed!(nodes[1], dust_payment_hash, 3_000);
336 nodes[1].node.claim_funds(timeout_payment_preimage);
337 check_added_monitors!(nodes[1], 1);
338 expect_payment_claimed!(nodes[1], timeout_payment_hash, 4_000_000);
340 if prev_commitment_tx {
341 // To build a previous commitment transaction, deliver one round of commitment messages.
342 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &b_htlc_msgs.update_fulfill_htlcs[0]);
343 expect_payment_sent_without_paths!(nodes[0], payment_preimage);
344 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &b_htlc_msgs.commitment_signed);
345 check_added_monitors!(nodes[0], 1);
346 let (as_raa, as_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
347 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
348 let _htlc_updates = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
349 check_added_monitors!(nodes[1], 1);
350 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs);
351 let _bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
352 check_added_monitors!(nodes[1], 1);
355 // Once B has received the payment preimage, it includes the value of the HTLC in its
356 // "claimable if you were to close the channel" balance.
357 let mut a_expected_balances = vec![Balance::ClaimableOnChannelClose {
358 claimable_amount_satoshis: 1_000_000 - // Channel funding value in satoshis
359 4_000 - // The to-be-failed HTLC value in satoshis
360 3_000 - // The claimed HTLC value in satoshis
361 1_000 - // The push_msat value in satoshis
362 3 - // The dust HTLC value in satoshis
363 // The commitment transaction fee with two HTLC outputs:
364 chan_feerate * (channel::commitment_tx_base_weight(opt_anchors) +
365 if prev_commitment_tx { 1 } else { 2 } *
366 channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
367 }, Balance::MaybeTimeoutClaimableHTLC {
368 claimable_amount_satoshis: 4_000,
369 claimable_height: htlc_cltv_timeout,
371 if !prev_commitment_tx {
372 a_expected_balances.push(Balance::MaybeTimeoutClaimableHTLC {
373 claimable_amount_satoshis: 3_000,
374 claimable_height: htlc_cltv_timeout,
377 assert_eq!(sorted_vec(a_expected_balances),
378 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
379 assert_eq!(vec![Balance::ClaimableOnChannelClose {
380 claimable_amount_satoshis: 1_000 + 3_000 + 4_000,
382 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
384 // Broadcast the closing transaction (which has both pending HTLCs in it) and get B's
385 // broadcasted HTLC claim transaction with preimage.
386 let node_b_commitment_claimable = nodes[1].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
387 mine_transaction(&nodes[0], &remote_txn[0]);
388 mine_transaction(&nodes[1], &remote_txn[0]);
390 let b_broadcast_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
391 assert_eq!(b_broadcast_txn.len(), 2);
392 // b_broadcast_txn should spend the HTLCs output of the commitment tx for 3_000 and 4_000 sats
393 check_spends!(b_broadcast_txn[0], remote_txn[0]);
394 check_spends!(b_broadcast_txn[1], remote_txn[0]);
395 assert_eq!(b_broadcast_txn[0].input.len(), 1);
396 assert_eq!(b_broadcast_txn[1].input.len(), 1);
397 assert_eq!(remote_txn[0].output[b_broadcast_txn[0].input[0].previous_output.vout as usize].value, 3_000);
398 assert_eq!(remote_txn[0].output[b_broadcast_txn[1].input[0].previous_output.vout as usize].value, 4_000);
400 assert!(nodes[0].node.list_channels().is_empty());
401 check_closed_broadcast!(nodes[0], true);
402 check_added_monitors!(nodes[0], 1);
403 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
404 assert!(nodes[1].node.list_channels().is_empty());
405 check_closed_broadcast!(nodes[1], true);
406 check_added_monitors!(nodes[1], 1);
407 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
408 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
409 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
411 // Once the commitment transaction confirms, we will wait until ANTI_REORG_DELAY until we
412 // generate any `SpendableOutputs` events. Thus, the same balances will still be listed
413 // available in `get_claimable_balances`. However, both will swap from `ClaimableOnClose` to
414 // other Balance variants, as close has already happened.
415 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
416 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
418 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
419 claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
420 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
421 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
422 }, Balance::MaybeTimeoutClaimableHTLC {
423 claimable_amount_satoshis: 3_000,
424 claimable_height: htlc_cltv_timeout,
425 }, Balance::MaybeTimeoutClaimableHTLC {
426 claimable_amount_satoshis: 4_000,
427 claimable_height: htlc_cltv_timeout,
429 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
430 // The main non-HTLC balance is just awaiting confirmations, but the claimable height is the
431 // CSV delay, not ANTI_REORG_DELAY.
432 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
433 claimable_amount_satoshis: 1_000,
434 confirmation_height: node_b_commitment_claimable,
436 // Both HTLC balances are "contentious" as our counterparty could claim them if we wait too
438 Balance::ContentiousClaimable {
439 claimable_amount_satoshis: 3_000,
440 timeout_height: htlc_cltv_timeout,
441 }, Balance::ContentiousClaimable {
442 claimable_amount_satoshis: 4_000,
443 timeout_height: htlc_cltv_timeout,
445 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
447 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
448 expect_payment_failed!(nodes[0], dust_payment_hash, false);
449 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
451 // After ANTI_REORG_DELAY, A will consider its balance fully spendable and generate a
452 // `SpendableOutputs` event. However, B still has to wait for the CSV delay.
453 assert_eq!(sorted_vec(vec![Balance::MaybeTimeoutClaimableHTLC {
454 claimable_amount_satoshis: 3_000,
455 claimable_height: htlc_cltv_timeout,
456 }, Balance::MaybeTimeoutClaimableHTLC {
457 claimable_amount_satoshis: 4_000,
458 claimable_height: htlc_cltv_timeout,
460 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
461 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
462 claimable_amount_satoshis: 1_000,
463 confirmation_height: node_b_commitment_claimable,
464 }, Balance::ContentiousClaimable {
465 claimable_amount_satoshis: 3_000,
466 timeout_height: htlc_cltv_timeout,
467 }, Balance::ContentiousClaimable {
468 claimable_amount_satoshis: 4_000,
469 timeout_height: htlc_cltv_timeout,
471 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
473 test_spendable_output(&nodes[0], &remote_txn[0]);
474 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
476 // After broadcasting the HTLC claim transaction, node A will still consider the HTLC
477 // possibly-claimable up to ANTI_REORG_DELAY, at which point it will drop it.
478 mine_transaction(&nodes[0], &b_broadcast_txn[0]);
479 if prev_commitment_tx {
480 expect_payment_path_successful!(nodes[0]);
482 expect_payment_sent!(nodes[0], payment_preimage);
484 assert_eq!(sorted_vec(vec![Balance::MaybeTimeoutClaimableHTLC {
485 claimable_amount_satoshis: 3_000,
486 claimable_height: htlc_cltv_timeout,
487 }, Balance::MaybeTimeoutClaimableHTLC {
488 claimable_amount_satoshis: 4_000,
489 claimable_height: htlc_cltv_timeout,
491 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
492 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
493 assert_eq!(vec![Balance::MaybeTimeoutClaimableHTLC {
494 claimable_amount_satoshis: 4_000,
495 claimable_height: htlc_cltv_timeout,
497 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
499 // When the HTLC timeout output is spendable in the next block, A should broadcast it
500 connect_blocks(&nodes[0], htlc_cltv_timeout - nodes[0].best_block_info().1 - 1);
501 let a_broadcast_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
502 assert_eq!(a_broadcast_txn.len(), 2);
503 assert_eq!(a_broadcast_txn[0].input.len(), 1);
504 check_spends!(a_broadcast_txn[0], remote_txn[0]);
505 assert_eq!(a_broadcast_txn[1].input.len(), 1);
506 check_spends!(a_broadcast_txn[1], remote_txn[0]);
507 assert_ne!(a_broadcast_txn[0].input[0].previous_output.vout,
508 a_broadcast_txn[1].input[0].previous_output.vout);
509 // a_broadcast_txn [0] and [1] should spend the HTLC outputs of the commitment tx
510 assert_eq!(remote_txn[0].output[a_broadcast_txn[0].input[0].previous_output.vout as usize].value, 3_000);
511 assert_eq!(remote_txn[0].output[a_broadcast_txn[1].input[0].previous_output.vout as usize].value, 4_000);
513 // Once the HTLC-Timeout transaction confirms, A will no longer consider the HTLC
514 // "MaybeClaimable", but instead move it to "AwaitingConfirmations".
515 mine_transaction(&nodes[0], &a_broadcast_txn[1]);
516 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
517 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
518 claimable_amount_satoshis: 4_000,
519 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
521 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
522 // After ANTI_REORG_DELAY, A will generate a SpendableOutputs event and drop the claimable
524 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
525 assert_eq!(Vec::<Balance>::new(),
526 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
527 expect_payment_failed!(nodes[0], timeout_payment_hash, false);
529 test_spendable_output(&nodes[0], &a_broadcast_txn[1]);
531 // Node B will no longer consider the HTLC "contentious" after the HTLC claim transaction
532 // confirms, and consider it simply "awaiting confirmations". Note that it has to wait for the
533 // standard revocable transaction CSV delay before receiving a `SpendableOutputs`.
534 let node_b_htlc_claimable = nodes[1].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
535 mine_transaction(&nodes[1], &b_broadcast_txn[0]);
537 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
538 claimable_amount_satoshis: 1_000,
539 confirmation_height: node_b_commitment_claimable,
540 }, Balance::ClaimableAwaitingConfirmations {
541 claimable_amount_satoshis: 3_000,
542 confirmation_height: node_b_htlc_claimable,
543 }, Balance::ContentiousClaimable {
544 claimable_amount_satoshis: 4_000,
545 timeout_height: htlc_cltv_timeout,
547 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
549 // After reaching the commitment output CSV, we'll get a SpendableOutputs event for it and have
550 // only the HTLCs claimable on node B.
551 connect_blocks(&nodes[1], node_b_commitment_claimable - nodes[1].best_block_info().1);
552 test_spendable_output(&nodes[1], &remote_txn[0]);
554 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
555 claimable_amount_satoshis: 3_000,
556 confirmation_height: node_b_htlc_claimable,
557 }, Balance::ContentiousClaimable {
558 claimable_amount_satoshis: 4_000,
559 timeout_height: htlc_cltv_timeout,
561 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
563 // After reaching the claimed HTLC output CSV, we'll get a SpendableOutptus event for it and
564 // have only one HTLC output left spendable.
565 connect_blocks(&nodes[1], node_b_htlc_claimable - nodes[1].best_block_info().1);
566 test_spendable_output(&nodes[1], &b_broadcast_txn[0]);
568 assert_eq!(vec![Balance::ContentiousClaimable {
569 claimable_amount_satoshis: 4_000,
570 timeout_height: htlc_cltv_timeout,
572 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
574 // Finally, mine the HTLC timeout transaction that A broadcasted (even though B should be able
575 // to claim this HTLC with the preimage it knows!). It will remain listed as a claimable HTLC
576 // until ANTI_REORG_DELAY confirmations on the spend.
577 mine_transaction(&nodes[1], &a_broadcast_txn[1]);
578 assert_eq!(vec![Balance::ContentiousClaimable {
579 claimable_amount_satoshis: 4_000,
580 timeout_height: htlc_cltv_timeout,
582 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
583 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
584 assert_eq!(Vec::<Balance>::new(),
585 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
587 // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
588 // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
589 // monitor events or claimable balances.
590 for node in nodes.iter() {
591 connect_blocks(node, 6);
592 connect_blocks(node, 6);
593 assert!(node.chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
594 assert!(node.chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
599 fn test_claim_value_force_close() {
600 do_test_claim_value_force_close(true);
601 do_test_claim_value_force_close(false);
605 fn test_balances_on_local_commitment_htlcs() {
606 // Previously, when handling the broadcast of a local commitment transactions (with associated
607 // CSV delays prior to spendability), we incorrectly handled the CSV delays on HTLC
608 // transactions. This caused us to miss spendable outputs for HTLCs which were awaiting a CSV
609 // delay prior to spendability.
611 // Further, because of this, we could hit an assertion as `get_claimable_balances` asserted
612 // that HTLCs were resolved after the funding spend was resolved, which was not true if the
613 // HTLC did not have a CSV delay attached (due to the above bug or due to it being an HTLC
614 // claim by our counterparty).
615 let chanmon_cfgs = create_chanmon_cfgs(2);
616 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
617 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
618 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
620 // Create a single channel with two pending HTLCs from nodes[0] to nodes[1], one which nodes[1]
621 // knows the preimage for, one which it does not.
622 let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
623 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
625 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 10_000_000);
626 let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
627 nodes[0].node.send_payment_with_route(&route, payment_hash,
628 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
629 check_added_monitors!(nodes[0], 1);
631 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
632 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
633 commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
635 expect_pending_htlcs_forwardable!(nodes[1]);
636 expect_payment_claimable!(nodes[1], payment_hash, payment_secret, 10_000_000);
638 let (route_2, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 20_000_000);
639 nodes[0].node.send_payment_with_route(&route_2, payment_hash_2,
640 RecipientOnionFields::secret_only(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
641 check_added_monitors!(nodes[0], 1);
643 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
644 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
645 commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
647 expect_pending_htlcs_forwardable!(nodes[1]);
648 expect_payment_claimable!(nodes[1], payment_hash_2, payment_secret_2, 20_000_000);
649 nodes[1].node.claim_funds(payment_preimage_2);
650 get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
651 check_added_monitors!(nodes[1], 1);
652 expect_payment_claimed!(nodes[1], payment_hash_2, 20_000_000);
654 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
655 let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
657 // Get nodes[0]'s commitment transaction and HTLC-Timeout transactions
658 let as_txn = get_local_commitment_txn!(nodes[0], chan_id);
659 assert_eq!(as_txn.len(), 3);
660 check_spends!(as_txn[1], as_txn[0]);
661 check_spends!(as_txn[2], as_txn[0]);
662 check_spends!(as_txn[0], funding_tx);
664 // First confirm the commitment transaction on nodes[0], which should leave us with three
665 // claimable balances.
666 let node_a_commitment_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
667 mine_transaction(&nodes[0], &as_txn[0]);
668 check_added_monitors!(nodes[0], 1);
669 check_closed_broadcast!(nodes[0], true);
670 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
672 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
673 claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
674 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
675 confirmation_height: node_a_commitment_claimable,
676 }, Balance::MaybeTimeoutClaimableHTLC {
677 claimable_amount_satoshis: 10_000,
678 claimable_height: htlc_cltv_timeout,
679 }, Balance::MaybeTimeoutClaimableHTLC {
680 claimable_amount_satoshis: 20_000,
681 claimable_height: htlc_cltv_timeout,
683 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
685 // Get nodes[1]'s HTLC claim tx for the second HTLC
686 mine_transaction(&nodes[1], &as_txn[0]);
687 check_added_monitors!(nodes[1], 1);
688 check_closed_broadcast!(nodes[1], true);
689 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
690 let bs_htlc_claim_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
691 assert_eq!(bs_htlc_claim_txn.len(), 1);
692 check_spends!(bs_htlc_claim_txn[0], as_txn[0]);
694 // Connect blocks until the HTLCs expire, allowing us to (validly) broadcast the HTLC-Timeout
696 connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1);
697 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
698 claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
699 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
700 confirmation_height: node_a_commitment_claimable,
701 }, Balance::MaybeTimeoutClaimableHTLC {
702 claimable_amount_satoshis: 10_000,
703 claimable_height: htlc_cltv_timeout,
704 }, Balance::MaybeTimeoutClaimableHTLC {
705 claimable_amount_satoshis: 20_000,
706 claimable_height: htlc_cltv_timeout,
708 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
709 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
711 // Now confirm nodes[0]'s HTLC-Timeout transaction, which changes the claimable balance to an
712 // "awaiting confirmations" one.
713 let node_a_htlc_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
714 mine_transaction(&nodes[0], &as_txn[1]);
715 // Note that prior to the fix in the commit which introduced this test, this (and the next
716 // balance) check failed. With this check removed, the code panicked in the `connect_blocks`
717 // call, as described, two hunks down.
718 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
719 claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
720 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
721 confirmation_height: node_a_commitment_claimable,
722 }, Balance::ClaimableAwaitingConfirmations {
723 claimable_amount_satoshis: 10_000,
724 confirmation_height: node_a_htlc_claimable,
725 }, Balance::MaybeTimeoutClaimableHTLC {
726 claimable_amount_satoshis: 20_000,
727 claimable_height: htlc_cltv_timeout,
729 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
731 // Now confirm nodes[1]'s HTLC claim, giving nodes[0] the preimage. Note that the "maybe
732 // claimable" balance remains until we see ANTI_REORG_DELAY blocks.
733 mine_transaction(&nodes[0], &bs_htlc_claim_txn[0]);
734 expect_payment_sent!(nodes[0], payment_preimage_2);
735 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
736 claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
737 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
738 confirmation_height: node_a_commitment_claimable,
739 }, Balance::ClaimableAwaitingConfirmations {
740 claimable_amount_satoshis: 10_000,
741 confirmation_height: node_a_htlc_claimable,
742 }, Balance::MaybeTimeoutClaimableHTLC {
743 claimable_amount_satoshis: 20_000,
744 claimable_height: htlc_cltv_timeout,
746 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
748 // Finally make the HTLC transactions have ANTI_REORG_DELAY blocks. This call previously
749 // panicked as described in the test introduction. This will remove the "maybe claimable"
750 // spendable output as nodes[1] has fully claimed the second HTLC.
751 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
752 expect_payment_failed!(nodes[0], payment_hash, false);
754 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
755 claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
756 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
757 confirmation_height: node_a_commitment_claimable,
758 }, Balance::ClaimableAwaitingConfirmations {
759 claimable_amount_satoshis: 10_000,
760 confirmation_height: node_a_htlc_claimable,
762 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
764 // Connect blocks until the commitment transaction's CSV expires, providing us the relevant
765 // `SpendableOutputs` event and removing the claimable balance entry.
766 connect_blocks(&nodes[0], node_a_commitment_claimable - nodes[0].best_block_info().1);
767 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
768 claimable_amount_satoshis: 10_000,
769 confirmation_height: node_a_htlc_claimable,
771 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
772 test_spendable_output(&nodes[0], &as_txn[0]);
774 // Connect blocks until the HTLC-Timeout's CSV expires, providing us the relevant
775 // `SpendableOutputs` event and removing the claimable balance entry.
776 connect_blocks(&nodes[0], node_a_htlc_claimable - nodes[0].best_block_info().1);
777 assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
778 test_spendable_output(&nodes[0], &as_txn[1]);
780 // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
781 // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
782 // monitor events or claimable balances.
783 connect_blocks(&nodes[0], 6);
784 connect_blocks(&nodes[0], 6);
785 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
786 assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
790 fn test_no_preimage_inbound_htlc_balances() {
791 // Tests that MaybePreimageClaimableHTLC are generated for inbound HTLCs for which we do not
793 let chanmon_cfgs = create_chanmon_cfgs(2);
794 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
795 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
796 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
798 let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000);
799 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
801 // Send two HTLCs, one from A to B, and one from B to A.
802 let to_b_failed_payment_hash = route_payment(&nodes[0], &[&nodes[1]], 10_000_000).1;
803 let to_a_failed_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 20_000_000).1;
804 let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
806 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
807 let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
809 // Both A and B will have an HTLC that's claimable on timeout and one that's claimable if they
810 // receive the preimage. These will remain the same through the channel closure and until the
811 // HTLC output is spent.
813 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
814 claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
815 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
816 }, Balance::MaybePreimageClaimableHTLC {
817 claimable_amount_satoshis: 20_000,
818 expiry_height: htlc_cltv_timeout,
819 }, Balance::MaybeTimeoutClaimableHTLC {
820 claimable_amount_satoshis: 10_000,
821 claimable_height: htlc_cltv_timeout,
823 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
825 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
826 claimable_amount_satoshis: 500_000 - 20_000,
827 }, Balance::MaybePreimageClaimableHTLC {
828 claimable_amount_satoshis: 10_000,
829 expiry_height: htlc_cltv_timeout,
830 }, Balance::MaybeTimeoutClaimableHTLC {
831 claimable_amount_satoshis: 20_000,
832 claimable_height: htlc_cltv_timeout,
834 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
836 // Get nodes[0]'s commitment transaction and HTLC-Timeout transaction
837 let as_txn = get_local_commitment_txn!(nodes[0], chan_id);
838 assert_eq!(as_txn.len(), 2);
839 check_spends!(as_txn[1], as_txn[0]);
840 check_spends!(as_txn[0], funding_tx);
842 // Now close the channel by confirming A's commitment transaction on both nodes, checking the
843 // claimable balances remain the same except for the non-HTLC balance changing variant.
844 let node_a_commitment_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
845 let as_pre_spend_claims = sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
846 claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
847 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
848 confirmation_height: node_a_commitment_claimable,
849 }, Balance::MaybePreimageClaimableHTLC {
850 claimable_amount_satoshis: 20_000,
851 expiry_height: htlc_cltv_timeout,
852 }, Balance::MaybeTimeoutClaimableHTLC {
853 claimable_amount_satoshis: 10_000,
854 claimable_height: htlc_cltv_timeout,
857 mine_transaction(&nodes[0], &as_txn[0]);
858 nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
859 check_added_monitors!(nodes[0], 1);
860 check_closed_broadcast!(nodes[0], true);
861 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
863 assert_eq!(as_pre_spend_claims,
864 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
866 mine_transaction(&nodes[1], &as_txn[0]);
867 check_added_monitors!(nodes[1], 1);
868 check_closed_broadcast!(nodes[1], true);
869 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
871 let node_b_commitment_claimable = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
872 let mut bs_pre_spend_claims = sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
873 claimable_amount_satoshis: 500_000 - 20_000,
874 confirmation_height: node_b_commitment_claimable,
875 }, Balance::MaybePreimageClaimableHTLC {
876 claimable_amount_satoshis: 10_000,
877 expiry_height: htlc_cltv_timeout,
878 }, Balance::MaybeTimeoutClaimableHTLC {
879 claimable_amount_satoshis: 20_000,
880 claimable_height: htlc_cltv_timeout,
882 assert_eq!(bs_pre_spend_claims,
883 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
885 // We'll broadcast the HTLC-Timeout transaction one block prior to the htlc's expiration (as it
886 // is confirmable in the next block), but will still include the same claimable balances as no
887 // HTLC has been spent, even after the HTLC expires. We'll also fail the inbound HTLC, but it
888 // won't do anything as the channel is already closed.
890 connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1);
891 let as_htlc_timeout_claim = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
892 assert_eq!(as_htlc_timeout_claim.len(), 1);
893 check_spends!(as_htlc_timeout_claim[0], as_txn[0]);
894 expect_pending_htlcs_forwardable_conditions!(nodes[0],
895 [HTLCDestination::FailedPayment { payment_hash: to_a_failed_payment_hash }]);
897 assert_eq!(as_pre_spend_claims,
898 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
900 connect_blocks(&nodes[0], 1);
901 assert_eq!(as_pre_spend_claims,
902 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
904 // For node B, we'll get the non-HTLC funds claimable after ANTI_REORG_DELAY confirmations
905 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
906 test_spendable_output(&nodes[1], &as_txn[0]);
907 bs_pre_spend_claims.retain(|e| if let Balance::ClaimableAwaitingConfirmations { .. } = e { false } else { true });
909 // The next few blocks for B look the same as for A, though for the opposite HTLC
910 nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
911 connect_blocks(&nodes[1], TEST_FINAL_CLTV - (ANTI_REORG_DELAY - 1) - 1);
912 expect_pending_htlcs_forwardable_conditions!(nodes[1],
913 [HTLCDestination::FailedPayment { payment_hash: to_b_failed_payment_hash }]);
914 let bs_htlc_timeout_claim = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
915 assert_eq!(bs_htlc_timeout_claim.len(), 1);
916 check_spends!(bs_htlc_timeout_claim[0], as_txn[0]);
918 assert_eq!(bs_pre_spend_claims,
919 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
921 connect_blocks(&nodes[1], 1);
922 assert_eq!(bs_pre_spend_claims,
923 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
925 // Now confirm the two HTLC timeout transactions for A, checking that the inbound HTLC resolves
926 // after ANTI_REORG_DELAY confirmations and the other takes BREAKDOWN_TIMEOUT confirmations.
927 mine_transaction(&nodes[0], &as_htlc_timeout_claim[0]);
928 let as_timeout_claimable_height = nodes[0].best_block_info().1 + (BREAKDOWN_TIMEOUT as u32) - 1;
929 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
930 claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
931 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
932 confirmation_height: node_a_commitment_claimable,
933 }, Balance::MaybePreimageClaimableHTLC {
934 claimable_amount_satoshis: 20_000,
935 expiry_height: htlc_cltv_timeout,
936 }, Balance::ClaimableAwaitingConfirmations {
937 claimable_amount_satoshis: 10_000,
938 confirmation_height: as_timeout_claimable_height,
940 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
942 mine_transaction(&nodes[0], &bs_htlc_timeout_claim[0]);
943 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
944 claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
945 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
946 confirmation_height: node_a_commitment_claimable,
947 }, Balance::MaybePreimageClaimableHTLC {
948 claimable_amount_satoshis: 20_000,
949 expiry_height: htlc_cltv_timeout,
950 }, Balance::ClaimableAwaitingConfirmations {
951 claimable_amount_satoshis: 10_000,
952 confirmation_height: as_timeout_claimable_height,
954 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
956 // Once as_htlc_timeout_claim[0] reaches ANTI_REORG_DELAY confirmations, we should get a
957 // payment failure event.
958 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 2);
959 expect_payment_failed!(nodes[0], to_b_failed_payment_hash, false);
961 connect_blocks(&nodes[0], 1);
962 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
963 claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
964 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
965 confirmation_height: node_a_commitment_claimable,
966 }, Balance::ClaimableAwaitingConfirmations {
967 claimable_amount_satoshis: 10_000,
968 confirmation_height: core::cmp::max(as_timeout_claimable_height, htlc_cltv_timeout),
970 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
972 connect_blocks(&nodes[0], node_a_commitment_claimable - nodes[0].best_block_info().1);
973 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
974 claimable_amount_satoshis: 10_000,
975 confirmation_height: core::cmp::max(as_timeout_claimable_height, htlc_cltv_timeout),
977 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
978 test_spendable_output(&nodes[0], &as_txn[0]);
980 connect_blocks(&nodes[0], as_timeout_claimable_height - nodes[0].best_block_info().1);
981 assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
982 test_spendable_output(&nodes[0], &as_htlc_timeout_claim[0]);
984 // The process for B should be completely identical as well, noting that the non-HTLC-balance
985 // was already claimed.
986 mine_transaction(&nodes[1], &bs_htlc_timeout_claim[0]);
987 let bs_timeout_claimable_height = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
988 assert_eq!(sorted_vec(vec![Balance::MaybePreimageClaimableHTLC {
989 claimable_amount_satoshis: 10_000,
990 expiry_height: htlc_cltv_timeout,
991 }, Balance::ClaimableAwaitingConfirmations {
992 claimable_amount_satoshis: 20_000,
993 confirmation_height: bs_timeout_claimable_height,
995 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
997 mine_transaction(&nodes[1], &as_htlc_timeout_claim[0]);
998 assert_eq!(sorted_vec(vec![Balance::MaybePreimageClaimableHTLC {
999 claimable_amount_satoshis: 10_000,
1000 expiry_height: htlc_cltv_timeout,
1001 }, Balance::ClaimableAwaitingConfirmations {
1002 claimable_amount_satoshis: 20_000,
1003 confirmation_height: bs_timeout_claimable_height,
1005 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1007 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 2);
1008 expect_payment_failed!(nodes[1], to_a_failed_payment_hash, false);
1010 assert_eq!(vec![Balance::MaybePreimageClaimableHTLC {
1011 claimable_amount_satoshis: 10_000,
1012 expiry_height: htlc_cltv_timeout,
1014 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
1015 test_spendable_output(&nodes[1], &bs_htlc_timeout_claim[0]);
1017 connect_blocks(&nodes[1], 1);
1018 assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1020 // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
1021 // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
1022 // monitor events or claimable balances.
1023 connect_blocks(&nodes[1], 6);
1024 connect_blocks(&nodes[1], 6);
1025 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1026 assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1029 fn sorted_vec_with_additions<T: Ord + Clone>(v_orig: &Vec<T>, extra_ts: &[&T]) -> Vec<T> {
1030 let mut v = v_orig.clone();
1032 v.push((*t).clone());
1038 fn do_test_revoked_counterparty_commitment_balances(confirm_htlc_spend_first: bool) {
1039 // Tests `get_claimable_balances` for revoked counterparty commitment transactions.
1040 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1041 // We broadcast a second-to-latest commitment transaction, without providing the revocation
1042 // secret to the counterparty. However, because we always immediately take the revocation
1043 // secret from the keys_manager, we would panic at broadcast as we're trying to sign a
1044 // transaction which, from the point of view of our keys_manager, is revoked.
1045 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
1046 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1047 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1048 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1050 let (_, _, chan_id, funding_tx) =
1051 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 100_000_000);
1052 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
1053 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
1055 // We create five HTLCs for B to claim against A's revoked commitment transaction:
1057 // (1) one for which A is the originator and B knows the preimage
1058 // (2) one for which B is the originator where the HTLC has since timed-out
1059 // (3) one for which B is the originator but where the HTLC has not yet timed-out
1060 // (4) one dust HTLC which is lost in the channel closure
1061 // (5) one that actually isn't in the revoked commitment transaction at all, but was added in
1062 // later commitment transaction updates
1064 // Though they could all be claimed in a single claim transaction, due to CLTV timeouts they
1065 // are all currently claimed in separate transactions, which helps us test as we can claim
1066 // HTLCs individually.
1068 let (claimed_payment_preimage, claimed_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 3_000_000);
1069 let timeout_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 4_000_000).1;
1070 let dust_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 3_000).1;
1072 let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
1074 connect_blocks(&nodes[0], 10);
1075 connect_blocks(&nodes[1], 10);
1077 let live_htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
1078 let live_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 5_000_000).1;
1080 // Get the latest commitment transaction from A and then update the fee to revoke it
1081 let as_revoked_txn = get_local_commitment_txn!(nodes[0], chan_id);
1082 let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
1084 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
1086 let missing_htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
1087 let missing_htlc_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 2_000_000).1;
1089 nodes[1].node.claim_funds(claimed_payment_preimage);
1090 expect_payment_claimed!(nodes[1], claimed_payment_hash, 3_000_000);
1091 check_added_monitors!(nodes[1], 1);
1092 let _b_htlc_msgs = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
1094 connect_blocks(&nodes[0], htlc_cltv_timeout + 1 - 10);
1095 check_closed_broadcast!(nodes[0], true);
1096 check_added_monitors!(nodes[0], 1);
1098 let mut events = nodes[0].node.get_and_clear_pending_events();
1099 assert_eq!(events.len(), 6);
1100 let mut failed_payments: HashSet<_> =
1101 [timeout_payment_hash, dust_payment_hash, live_payment_hash, missing_htlc_payment_hash]
1102 .iter().map(|a| *a).collect();
1103 events.retain(|ev| {
1105 Event::HTLCHandlingFailed { failed_next_destination: HTLCDestination::NextHopChannel { node_id, channel_id }, .. } => {
1106 assert_eq!(*channel_id, chan_id);
1107 assert_eq!(*node_id, Some(nodes[1].node.get_our_node_id()));
1110 Event::HTLCHandlingFailed { failed_next_destination: HTLCDestination::FailedPayment { payment_hash }, .. } => {
1111 assert!(failed_payments.remove(payment_hash));
1117 assert!(failed_payments.is_empty());
1118 if let Event::PendingHTLCsForwardable { .. } = events[0] {} else { panic!(); }
1120 Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {},
1124 connect_blocks(&nodes[1], htlc_cltv_timeout + 1 - 10);
1125 check_closed_broadcast!(nodes[1], true);
1126 check_added_monitors!(nodes[1], 1);
1127 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
1129 // Prior to channel closure, B considers the preimage HTLC as its own, and otherwise only
1130 // lists the two on-chain timeout-able HTLCs as claimable balances.
1131 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
1132 claimable_amount_satoshis: 100_000 - 5_000 - 4_000 - 3 - 2_000 + 3_000,
1133 }, Balance::MaybeTimeoutClaimableHTLC {
1134 claimable_amount_satoshis: 2_000,
1135 claimable_height: missing_htlc_cltv_timeout,
1136 }, Balance::MaybeTimeoutClaimableHTLC {
1137 claimable_amount_satoshis: 4_000,
1138 claimable_height: htlc_cltv_timeout,
1139 }, Balance::MaybeTimeoutClaimableHTLC {
1140 claimable_amount_satoshis: 5_000,
1141 claimable_height: live_htlc_cltv_timeout,
1143 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1145 mine_transaction(&nodes[1], &as_revoked_txn[0]);
1146 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();
1147 // Currently the revoked commitment is claimed in four transactions as the HTLCs all expire
1149 assert_eq!(claim_txn.len(), 4);
1150 claim_txn.sort_unstable_by_key(|tx| tx.output.iter().map(|output| output.value).sum::<u64>());
1152 // The following constants were determined experimentally
1153 const BS_TO_SELF_CLAIM_EXP_WEIGHT: usize = 483;
1154 const OUTBOUND_HTLC_CLAIM_EXP_WEIGHT: usize = 571;
1155 const INBOUND_HTLC_CLAIM_EXP_WEIGHT: usize = 578;
1157 // Check that the weight is close to the expected weight. Note that signature sizes vary
1158 // somewhat so it may not always be exact.
1159 fuzzy_assert_eq(claim_txn[0].weight(), OUTBOUND_HTLC_CLAIM_EXP_WEIGHT);
1160 fuzzy_assert_eq(claim_txn[1].weight(), INBOUND_HTLC_CLAIM_EXP_WEIGHT);
1161 fuzzy_assert_eq(claim_txn[2].weight(), INBOUND_HTLC_CLAIM_EXP_WEIGHT);
1162 fuzzy_assert_eq(claim_txn[3].weight(), BS_TO_SELF_CLAIM_EXP_WEIGHT);
1164 // The expected balance for the next three checks, with the largest-HTLC and to_self output
1165 // claim balances separated out.
1166 let expected_balance = vec![Balance::ClaimableAwaitingConfirmations {
1167 // to_remote output in A's revoked commitment
1168 claimable_amount_satoshis: 100_000 - 5_000 - 4_000 - 3,
1169 confirmation_height: nodes[1].best_block_info().1 + 5,
1170 }, Balance::CounterpartyRevokedOutputClaimable {
1171 claimable_amount_satoshis: 3_000,
1172 }, Balance::CounterpartyRevokedOutputClaimable {
1173 claimable_amount_satoshis: 4_000,
1176 let to_self_unclaimed_balance = Balance::CounterpartyRevokedOutputClaimable {
1177 claimable_amount_satoshis: 1_000_000 - 100_000 - 3_000 - chan_feerate *
1178 (channel::commitment_tx_base_weight(opt_anchors) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1180 let to_self_claimed_avail_height;
1181 let largest_htlc_unclaimed_balance = Balance::CounterpartyRevokedOutputClaimable {
1182 claimable_amount_satoshis: 5_000,
1184 let largest_htlc_claimed_avail_height;
1186 // Once the channel has been closed by A, B now considers all of the commitment transactions'
1187 // outputs as `CounterpartyRevokedOutputClaimable`.
1188 assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_unclaimed_balance, &largest_htlc_unclaimed_balance]),
1189 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1191 if confirm_htlc_spend_first {
1192 mine_transaction(&nodes[1], &claim_txn[2]);
1193 largest_htlc_claimed_avail_height = nodes[1].best_block_info().1 + 5;
1194 to_self_claimed_avail_height = nodes[1].best_block_info().1 + 6; // will be claimed in the next block
1196 // Connect the to_self output claim, taking all of A's non-HTLC funds
1197 mine_transaction(&nodes[1], &claim_txn[3]);
1198 to_self_claimed_avail_height = nodes[1].best_block_info().1 + 5;
1199 largest_htlc_claimed_avail_height = nodes[1].best_block_info().1 + 6; // will be claimed in the next block
1202 let largest_htlc_claimed_balance = Balance::ClaimableAwaitingConfirmations {
1203 claimable_amount_satoshis: 5_000 - chan_feerate * INBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
1204 confirmation_height: largest_htlc_claimed_avail_height,
1206 let to_self_claimed_balance = Balance::ClaimableAwaitingConfirmations {
1207 claimable_amount_satoshis: 1_000_000 - 100_000 - 3_000 - chan_feerate *
1208 (channel::commitment_tx_base_weight(opt_anchors) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
1209 - chan_feerate * claim_txn[3].weight() as u64 / 1000,
1210 confirmation_height: to_self_claimed_avail_height,
1213 if confirm_htlc_spend_first {
1214 assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_unclaimed_balance, &largest_htlc_claimed_balance]),
1215 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1217 assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_claimed_balance, &largest_htlc_unclaimed_balance]),
1218 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1221 if confirm_htlc_spend_first {
1222 mine_transaction(&nodes[1], &claim_txn[3]);
1224 mine_transaction(&nodes[1], &claim_txn[2]);
1226 assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_claimed_balance, &largest_htlc_claimed_balance]),
1227 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1229 // Finally, connect the last two remaining HTLC spends and check that they move to
1230 // `ClaimableAwaitingConfirmations`
1231 mine_transaction(&nodes[1], &claim_txn[0]);
1232 mine_transaction(&nodes[1], &claim_txn[1]);
1234 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1235 // to_remote output in A's revoked commitment
1236 claimable_amount_satoshis: 100_000 - 5_000 - 4_000 - 3,
1237 confirmation_height: nodes[1].best_block_info().1 + 1,
1238 }, Balance::ClaimableAwaitingConfirmations {
1239 claimable_amount_satoshis: 1_000_000 - 100_000 - 3_000 - chan_feerate *
1240 (channel::commitment_tx_base_weight(opt_anchors) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
1241 - chan_feerate * claim_txn[3].weight() as u64 / 1000,
1242 confirmation_height: to_self_claimed_avail_height,
1243 }, Balance::ClaimableAwaitingConfirmations {
1244 claimable_amount_satoshis: 3_000 - chan_feerate * OUTBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
1245 confirmation_height: nodes[1].best_block_info().1 + 4,
1246 }, Balance::ClaimableAwaitingConfirmations {
1247 claimable_amount_satoshis: 4_000 - chan_feerate * INBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
1248 confirmation_height: nodes[1].best_block_info().1 + 5,
1249 }, Balance::ClaimableAwaitingConfirmations {
1250 claimable_amount_satoshis: 5_000 - chan_feerate * INBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
1251 confirmation_height: largest_htlc_claimed_avail_height,
1253 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1255 connect_blocks(&nodes[1], 1);
1256 test_spendable_output(&nodes[1], &as_revoked_txn[0]);
1258 let mut payment_failed_events = nodes[1].node.get_and_clear_pending_events();
1259 expect_payment_failed_conditions_event(payment_failed_events[..2].to_vec(),
1260 missing_htlc_payment_hash, false, PaymentFailedConditions::new());
1261 expect_payment_failed_conditions_event(payment_failed_events[2..].to_vec(),
1262 dust_payment_hash, false, PaymentFailedConditions::new());
1264 connect_blocks(&nodes[1], 1);
1265 test_spendable_output(&nodes[1], &claim_txn[if confirm_htlc_spend_first { 2 } else { 3 }]);
1266 connect_blocks(&nodes[1], 1);
1267 test_spendable_output(&nodes[1], &claim_txn[if confirm_htlc_spend_first { 3 } else { 2 }]);
1268 expect_payment_failed!(nodes[1], live_payment_hash, false);
1269 connect_blocks(&nodes[1], 1);
1270 test_spendable_output(&nodes[1], &claim_txn[0]);
1271 connect_blocks(&nodes[1], 1);
1272 test_spendable_output(&nodes[1], &claim_txn[1]);
1273 expect_payment_failed!(nodes[1], timeout_payment_hash, false);
1274 assert_eq!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances(), Vec::new());
1276 // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
1277 // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
1278 // monitor events or claimable balances.
1279 connect_blocks(&nodes[1], 6);
1280 connect_blocks(&nodes[1], 6);
1281 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1282 assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1286 fn test_revoked_counterparty_commitment_balances() {
1287 do_test_revoked_counterparty_commitment_balances(true);
1288 do_test_revoked_counterparty_commitment_balances(false);
1292 fn test_revoked_counterparty_htlc_tx_balances() {
1293 // Tests `get_claimable_balances` for revocation spends of HTLC transactions.
1294 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1295 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
1296 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1297 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1298 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1300 // Create some initial channels
1301 let (_, _, chan_id, funding_tx) =
1302 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 11_000_000);
1303 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
1304 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
1306 let payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 3_000_000).0;
1307 let failed_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 1_000_000).1;
1308 let revoked_local_txn = get_local_commitment_txn!(nodes[1], chan_id);
1309 assert_eq!(revoked_local_txn[0].input.len(), 1);
1310 assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, funding_tx.txid());
1312 // The to-be-revoked commitment tx should have two HTLCs and an output for both sides
1313 assert_eq!(revoked_local_txn[0].output.len(), 4);
1315 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
1317 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
1318 let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
1320 // B will generate an HTLC-Success from its revoked commitment tx
1321 mine_transaction(&nodes[1], &revoked_local_txn[0]);
1322 check_closed_broadcast!(nodes[1], true);
1323 check_added_monitors!(nodes[1], 1);
1324 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
1325 let revoked_htlc_success = {
1326 let mut txn = nodes[1].tx_broadcaster.txn_broadcast();
1327 assert_eq!(txn.len(), 1);
1328 assert_eq!(txn[0].input.len(), 1);
1329 assert_eq!(txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
1330 check_spends!(txn[0], revoked_local_txn[0]);
1334 connect_blocks(&nodes[1], TEST_FINAL_CLTV);
1335 let revoked_htlc_timeout = {
1336 let mut txn = nodes[1].tx_broadcaster.unique_txn_broadcast();
1337 assert_eq!(txn.len(), 2);
1338 if txn[0].input[0].previous_output == revoked_htlc_success.input[0].previous_output {
1344 check_spends!(revoked_htlc_timeout, revoked_local_txn[0]);
1345 assert_ne!(revoked_htlc_success.input[0].previous_output, revoked_htlc_timeout.input[0].previous_output);
1346 assert_eq!(revoked_htlc_success.lock_time.0, 0);
1347 assert_ne!(revoked_htlc_timeout.lock_time.0, 0);
1349 // A will generate justice tx from B's revoked commitment/HTLC tx
1350 mine_transaction(&nodes[0], &revoked_local_txn[0]);
1351 check_closed_broadcast!(nodes[0], true);
1352 check_added_monitors!(nodes[0], 1);
1353 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
1354 let to_remote_conf_height = nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1;
1356 let as_commitment_claim_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1357 assert_eq!(as_commitment_claim_txn.len(), 1);
1358 check_spends!(as_commitment_claim_txn[0], revoked_local_txn[0]);
1360 // The next two checks have the same balance set for A - even though we confirm a revoked HTLC
1361 // transaction our balance tracking doesn't use the on-chain value so the
1362 // `CounterpartyRevokedOutputClaimable` entry doesn't change.
1363 let as_balances = sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1364 // to_remote output in B's revoked commitment
1365 claimable_amount_satoshis: 1_000_000 - 11_000 - 3_000 - chan_feerate *
1366 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1367 confirmation_height: to_remote_conf_height,
1368 }, Balance::CounterpartyRevokedOutputClaimable {
1369 // to_self output in B's revoked commitment
1370 claimable_amount_satoshis: 10_000,
1371 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1372 claimable_amount_satoshis: 3_000,
1373 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1374 claimable_amount_satoshis: 1_000,
1376 assert_eq!(as_balances,
1377 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1379 mine_transaction(&nodes[0], &revoked_htlc_success);
1380 let as_htlc_claim_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1381 assert_eq!(as_htlc_claim_tx.len(), 2);
1382 check_spends!(as_htlc_claim_tx[0], revoked_htlc_success);
1383 check_spends!(as_htlc_claim_tx[1], revoked_local_txn[0]); // A has to generate a new claim for the remaining revoked
1384 // outputs (which no longer includes the spent HTLC output)
1386 assert_eq!(as_balances,
1387 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1389 assert_eq!(as_htlc_claim_tx[0].output.len(), 1);
1390 fuzzy_assert_eq(as_htlc_claim_tx[0].output[0].value,
1391 3_000 - chan_feerate * (revoked_htlc_success.weight() + as_htlc_claim_tx[0].weight()) as u64 / 1000);
1393 mine_transaction(&nodes[0], &as_htlc_claim_tx[0]);
1394 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1395 // to_remote output in B's revoked commitment
1396 claimable_amount_satoshis: 1_000_000 - 11_000 - 3_000 - chan_feerate *
1397 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1398 confirmation_height: to_remote_conf_height,
1399 }, Balance::CounterpartyRevokedOutputClaimable {
1400 // to_self output in B's revoked commitment
1401 claimable_amount_satoshis: 10_000,
1402 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1403 claimable_amount_satoshis: 1_000,
1404 }, Balance::ClaimableAwaitingConfirmations {
1405 claimable_amount_satoshis: as_htlc_claim_tx[0].output[0].value,
1406 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
1408 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1410 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 3);
1411 test_spendable_output(&nodes[0], &revoked_local_txn[0]);
1412 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1413 // to_self output to B
1414 claimable_amount_satoshis: 10_000,
1415 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1416 claimable_amount_satoshis: 1_000,
1417 }, Balance::ClaimableAwaitingConfirmations {
1418 claimable_amount_satoshis: as_htlc_claim_tx[0].output[0].value,
1419 confirmation_height: nodes[0].best_block_info().1 + 2,
1421 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1423 connect_blocks(&nodes[0], 2);
1424 test_spendable_output(&nodes[0], &as_htlc_claim_tx[0]);
1425 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1426 // to_self output in B's revoked commitment
1427 claimable_amount_satoshis: 10_000,
1428 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1429 claimable_amount_satoshis: 1_000,
1431 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1433 connect_blocks(&nodes[0], revoked_htlc_timeout.lock_time.0 - nodes[0].best_block_info().1);
1434 expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(&nodes[0],
1435 [HTLCDestination::FailedPayment { payment_hash: failed_payment_hash }]);
1436 // As time goes on A may split its revocation claim transaction into multiple.
1437 let as_fewer_input_rbf = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1438 for tx in as_fewer_input_rbf.iter() {
1439 check_spends!(tx, revoked_local_txn[0]);
1442 // Connect a number of additional blocks to ensure we don't forget the HTLC output needs
1444 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
1445 let as_fewer_input_rbf = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1446 for tx in as_fewer_input_rbf.iter() {
1447 check_spends!(tx, revoked_local_txn[0]);
1450 mine_transaction(&nodes[0], &revoked_htlc_timeout);
1451 let as_second_htlc_claim_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1452 assert_eq!(as_second_htlc_claim_tx.len(), 2);
1454 check_spends!(as_second_htlc_claim_tx[0], revoked_htlc_timeout);
1455 check_spends!(as_second_htlc_claim_tx[1], revoked_local_txn[0]);
1457 // Connect blocks to finalize the HTLC resolution with the HTLC-Timeout transaction. In a
1458 // previous iteration of the revoked balance handling this would result in us "forgetting" that
1459 // the revoked HTLC output still needed to be claimed.
1460 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
1461 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1462 // to_self output in B's revoked commitment
1463 claimable_amount_satoshis: 10_000,
1464 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1465 claimable_amount_satoshis: 1_000,
1467 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1469 mine_transaction(&nodes[0], &as_second_htlc_claim_tx[0]);
1470 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1471 // to_self output in B's revoked commitment
1472 claimable_amount_satoshis: 10_000,
1473 }, Balance::ClaimableAwaitingConfirmations {
1474 claimable_amount_satoshis: as_second_htlc_claim_tx[0].output[0].value,
1475 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
1477 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1479 mine_transaction(&nodes[0], &as_second_htlc_claim_tx[1]);
1480 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1481 // to_self output in B's revoked commitment
1482 claimable_amount_satoshis: as_second_htlc_claim_tx[1].output[0].value,
1483 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
1484 }, Balance::ClaimableAwaitingConfirmations {
1485 claimable_amount_satoshis: as_second_htlc_claim_tx[0].output[0].value,
1486 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 2,
1488 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1490 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 2);
1491 test_spendable_output(&nodes[0], &as_second_htlc_claim_tx[0]);
1492 connect_blocks(&nodes[0], 1);
1493 test_spendable_output(&nodes[0], &as_second_htlc_claim_tx[1]);
1495 assert_eq!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances(), Vec::new());
1497 // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
1498 // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
1499 // monitor events or claimable balances.
1500 connect_blocks(&nodes[0], 6);
1501 connect_blocks(&nodes[0], 6);
1502 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1503 assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1507 fn test_revoked_counterparty_aggregated_claims() {
1508 // Tests `get_claimable_balances` for revoked counterparty commitment transactions when
1509 // claiming with an aggregated claim transaction.
1510 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1511 // We broadcast a second-to-latest commitment transaction, without providing the revocation
1512 // secret to the counterparty. However, because we always immediately take the revocation
1513 // secret from the keys_manager, we would panic at broadcast as we're trying to sign a
1514 // transaction which, from the point of view of our keys_manager, is revoked.
1515 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
1516 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1517 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1518 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1520 let (_, _, chan_id, funding_tx) =
1521 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 100_000_000);
1522 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
1523 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
1525 // We create two HTLCs, one which we will give A the preimage to to generate an HTLC-Success
1526 // transaction, and one which we will not, allowing B to claim the HTLC output in an aggregated
1527 // revocation-claim transaction.
1529 let (claimed_payment_preimage, claimed_payment_hash, ..) = route_payment(&nodes[1], &[&nodes[0]], 3_000_000);
1530 let revoked_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 4_000_000).1;
1532 let htlc_cltv_timeout = nodes[1].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
1534 // Cheat by giving A's ChannelMonitor the preimage to the to-be-claimed HTLC so that we have an
1535 // HTLC-claim transaction on the to-be-revoked state.
1536 get_monitor!(nodes[0], chan_id).provide_payment_preimage(&claimed_payment_hash, &claimed_payment_preimage,
1537 &node_cfgs[0].tx_broadcaster, &LowerBoundedFeeEstimator::new(node_cfgs[0].fee_estimator), &nodes[0].logger);
1539 // Now get the latest commitment transaction from A and then update the fee to revoke it
1540 let as_revoked_txn = get_local_commitment_txn!(nodes[0], chan_id);
1542 assert_eq!(as_revoked_txn.len(), 2);
1543 check_spends!(as_revoked_txn[0], funding_tx);
1544 check_spends!(as_revoked_txn[1], as_revoked_txn[0]); // The HTLC-Claim transaction
1546 let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
1547 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
1550 let mut feerate = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
1553 nodes[0].node.timer_tick_occurred();
1554 check_added_monitors!(nodes[0], 1);
1556 let fee_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1557 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &fee_update.update_fee.unwrap());
1558 commitment_signed_dance!(nodes[1], nodes[0], fee_update.commitment_signed, false);
1560 nodes[0].node.claim_funds(claimed_payment_preimage);
1561 expect_payment_claimed!(nodes[0], claimed_payment_hash, 3_000_000);
1562 check_added_monitors!(nodes[0], 1);
1563 let _a_htlc_msgs = get_htlc_update_msgs!(&nodes[0], nodes[1].node.get_our_node_id());
1565 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
1566 claimable_amount_satoshis: 100_000 - 4_000 - 3_000,
1567 }, Balance::MaybeTimeoutClaimableHTLC {
1568 claimable_amount_satoshis: 4_000,
1569 claimable_height: htlc_cltv_timeout,
1570 }, Balance::MaybeTimeoutClaimableHTLC {
1571 claimable_amount_satoshis: 3_000,
1572 claimable_height: htlc_cltv_timeout,
1574 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1576 mine_transaction(&nodes[1], &as_revoked_txn[0]);
1577 check_closed_broadcast!(nodes[1], true);
1578 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
1579 check_added_monitors!(nodes[1], 1);
1581 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();
1582 // Currently the revoked commitment outputs are all claimed in one aggregated transaction
1583 assert_eq!(claim_txn.len(), 1);
1584 assert_eq!(claim_txn[0].input.len(), 3);
1585 check_spends!(claim_txn[0], as_revoked_txn[0]);
1587 let to_remote_maturity = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
1589 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1590 // to_remote output in A's revoked commitment
1591 claimable_amount_satoshis: 100_000 - 4_000 - 3_000,
1592 confirmation_height: to_remote_maturity,
1593 }, Balance::CounterpartyRevokedOutputClaimable {
1594 // to_self output in A's revoked commitment
1595 claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1596 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1597 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1598 claimable_amount_satoshis: 4_000,
1599 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1600 claimable_amount_satoshis: 3_000,
1602 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1604 // Confirm A's HTLC-Success tranasction which presumably raced B's claim, causing B to create a
1606 mine_transaction(&nodes[1], &as_revoked_txn[1]);
1607 expect_payment_sent!(nodes[1], claimed_payment_preimage);
1608 let mut claim_txn_2: Vec<_> = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
1609 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 });
1610 // Once B sees the HTLC-Success transaction it splits its claim transaction into two, though in
1611 // theory it could re-aggregate the claims as well.
1612 assert_eq!(claim_txn_2.len(), 2);
1613 assert_eq!(claim_txn_2[0].input.len(), 2);
1614 check_spends!(claim_txn_2[0], as_revoked_txn[0]);
1615 assert_eq!(claim_txn_2[1].input.len(), 1);
1616 check_spends!(claim_txn_2[1], as_revoked_txn[1]);
1618 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1619 // to_remote output in A's revoked commitment
1620 claimable_amount_satoshis: 100_000 - 4_000 - 3_000,
1621 confirmation_height: to_remote_maturity,
1622 }, Balance::CounterpartyRevokedOutputClaimable {
1623 // to_self output in A's revoked commitment
1624 claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1625 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1626 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1627 claimable_amount_satoshis: 4_000,
1628 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1629 // The amount here is a bit of a misnomer, really its been reduced by the HTLC
1630 // transaction fee, but the claimable amount is always a bit of an overshoot for HTLCs
1631 // anyway, so its not a big change.
1632 claimable_amount_satoshis: 3_000,
1634 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1636 connect_blocks(&nodes[1], 5);
1637 test_spendable_output(&nodes[1], &as_revoked_txn[0]);
1639 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1640 // to_self output in A's revoked commitment
1641 claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1642 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1643 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1644 claimable_amount_satoshis: 4_000,
1645 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1646 // The amount here is a bit of a misnomer, really its been reduced by the HTLC
1647 // transaction fee, but the claimable amount is always a bit of an overshoot for HTLCs
1648 // anyway, so its not a big change.
1649 claimable_amount_satoshis: 3_000,
1651 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1653 mine_transaction(&nodes[1], &claim_txn_2[1]);
1654 let htlc_2_claim_maturity = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
1656 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1657 // to_self output in A's revoked commitment
1658 claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1659 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1660 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1661 claimable_amount_satoshis: 4_000,
1662 }, Balance::ClaimableAwaitingConfirmations { // HTLC 2
1663 claimable_amount_satoshis: claim_txn_2[1].output[0].value,
1664 confirmation_height: htlc_2_claim_maturity,
1666 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1668 connect_blocks(&nodes[1], 5);
1669 test_spendable_output(&nodes[1], &claim_txn_2[1]);
1671 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1672 // to_self output in A's revoked commitment
1673 claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1674 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1675 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1676 claimable_amount_satoshis: 4_000,
1678 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1680 mine_transaction(&nodes[1], &claim_txn_2[0]);
1681 let rest_claim_maturity = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
1683 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
1684 claimable_amount_satoshis: claim_txn_2[0].output[0].value,
1685 confirmation_height: rest_claim_maturity,
1687 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
1689 assert!(nodes[1].node.get_and_clear_pending_events().is_empty()); // We shouldn't fail the payment until we spend the output
1691 connect_blocks(&nodes[1], 5);
1692 expect_payment_failed!(nodes[1], revoked_payment_hash, false);
1693 test_spendable_output(&nodes[1], &claim_txn_2[0]);
1694 assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1696 // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
1697 // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
1698 // monitor events or claimable balances.
1699 connect_blocks(&nodes[1], 6);
1700 connect_blocks(&nodes[1], 6);
1701 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1702 assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1705 fn do_test_restored_packages_retry() {
1706 // Tests that we'll retry packages that were previously timelocked after we've restored them.
1707 let chanmon_cfgs = create_chanmon_cfgs(2);
1708 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1709 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1710 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1712 // Open a channel, lock in an HTLC, and immediately broadcast the commitment transaction. This
1713 // ensures that the HTLC timeout package is held until we reach its expiration height.
1714 let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 50_000_000);
1715 route_payment(&nodes[0], &[&nodes[1]], 10_000_000);
1717 nodes[0].node.force_close_broadcasting_latest_txn(&chan_id, &nodes[1].node.get_our_node_id()).unwrap();
1718 check_added_monitors(&nodes[0], 1);
1719 check_closed_broadcast(&nodes[0], 1, true);
1720 check_closed_event(&nodes[0], 1, ClosureReason::HolderForceClosed, false);
1722 let commitment_tx = {
1723 let mut txn = nodes[0].tx_broadcaster.txn_broadcast();
1724 assert_eq!(txn.len(), 1);
1725 assert_eq!(txn[0].output.len(), 3);
1726 check_spends!(txn[0], funding_tx);
1730 mine_transaction(&nodes[0], &commitment_tx);
1732 // Connect blocks until the HTLC's expiration is met, expecting a transaction broadcast.
1733 connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1);
1734 let htlc_timeout_tx = {
1735 let mut txn = nodes[0].tx_broadcaster.txn_broadcast();
1736 assert_eq!(txn.len(), 1);
1737 check_spends!(txn[0], commitment_tx);
1741 // Connecting more blocks should result in the HTLC transactions being rebroadcast.
1742 connect_blocks(&nodes[0], 6);
1744 let txn = nodes[0].tx_broadcaster.txn_broadcast();
1745 if !nodes[0].connect_style.borrow().skips_blocks() {
1746 assert_eq!(txn.len(), 6);
1748 assert!(txn.len() < 6);
1751 assert_eq!(tx.input.len(), htlc_timeout_tx.input.len());
1752 assert_eq!(tx.output.len(), htlc_timeout_tx.output.len());
1753 assert_eq!(tx.input[0].previous_output, htlc_timeout_tx.input[0].previous_output);
1754 assert_eq!(tx.output[0], htlc_timeout_tx.output[0]);
1760 fn test_restored_packages_retry() {
1761 do_test_restored_packages_retry();
1766 fn test_yield_anchors_events() {
1767 // Tests that two parties supporting anchor outputs can open a channel, route payments over
1768 // it, and finalize its resolution uncooperatively. Once the HTLCs are locked in, one side will
1769 // force close once the HTLCs expire. The force close should stem from an event emitted by LDK,
1770 // allowing the consumer to provide additional fees to the commitment transaction to be
1771 // broadcast. Once the commitment transaction confirms, events for the HTLC resolution should be
1772 // emitted by LDK, such that the consumer can attach fees to the zero fee HTLC transactions.
1773 let secp = Secp256k1::new();
1774 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1775 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1776 let mut anchors_config = UserConfig::default();
1777 anchors_config.channel_handshake_config.announced_channel = true;
1778 anchors_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
1779 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(anchors_config), Some(anchors_config)]);
1780 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1782 let chan_id = create_announced_chan_between_nodes_with_value(
1783 &nodes, 0, 1, 1_000_000, 500_000_000
1785 route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
1786 let (payment_preimage, payment_hash, _) = route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1788 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
1790 connect_blocks(&nodes[0], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1);
1791 check_closed_broadcast!(&nodes[0], true);
1792 assert!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().is_empty());
1794 get_monitor!(nodes[0], chan_id).provide_payment_preimage(
1795 &payment_hash, &payment_preimage, &node_cfgs[0].tx_broadcaster,
1796 &LowerBoundedFeeEstimator::new(node_cfgs[0].fee_estimator), &nodes[0].logger
1799 let mut holder_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
1800 assert_eq!(holder_events.len(), 1);
1801 let (commitment_tx, anchor_tx) = match holder_events.pop().unwrap() {
1802 Event::BumpTransaction(BumpTransactionEvent::ChannelClose { commitment_tx, anchor_descriptor, .. }) => {
1803 assert_eq!(commitment_tx.input.len(), 1);
1804 assert_eq!(commitment_tx.output.len(), 6);
1805 let mut anchor_tx = Transaction {
1807 lock_time: PackedLockTime::ZERO,
1809 TxIn { previous_output: anchor_descriptor.outpoint, ..Default::default() },
1810 TxIn { ..Default::default() },
1812 output: vec![TxOut {
1813 value: Amount::ONE_BTC.to_sat(),
1814 script_pubkey: Script::new_op_return(&[]),
1817 let signer = nodes[0].keys_manager.derive_channel_keys(
1818 anchor_descriptor.channel_value_satoshis, &anchor_descriptor.channel_keys_id,
1820 let funding_sig = signer.sign_holder_anchor_input(&mut anchor_tx, 0, &secp).unwrap();
1821 anchor_tx.input[0].witness = chan_utils::build_anchor_input_witness(
1822 &signer.pubkeys().funding_pubkey, &funding_sig
1824 (commitment_tx, anchor_tx)
1826 _ => panic!("Unexpected event"),
1829 mine_transactions(&nodes[0], &[&commitment_tx, &anchor_tx]);
1830 check_added_monitors!(nodes[0], 1);
1832 let mut holder_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
1833 // Certain block `ConnectStyle`s cause an extra `ChannelClose` event to be emitted since the
1834 // best block is updated before the confirmed transactions are notified.
1835 match *nodes[0].connect_style.borrow() {
1836 ConnectStyle::BestBlockFirst|ConnectStyle::BestBlockFirstReorgsOnlyTip|ConnectStyle::BestBlockFirstSkippingBlocks => {
1837 assert_eq!(holder_events.len(), 3);
1838 if let Event::BumpTransaction(BumpTransactionEvent::ChannelClose { .. }) = holder_events.remove(0) {}
1839 else { panic!("unexpected event"); }
1842 _ => assert_eq!(holder_events.len(), 2),
1844 let mut htlc_txs = Vec::with_capacity(2);
1845 for event in holder_events {
1847 Event::BumpTransaction(BumpTransactionEvent::HTLCResolution { htlc_descriptors, tx_lock_time, .. }) => {
1848 assert_eq!(htlc_descriptors.len(), 1);
1849 let htlc_descriptor = &htlc_descriptors[0];
1850 let signer = nodes[0].keys_manager.derive_channel_keys(
1851 htlc_descriptor.channel_value_satoshis, &htlc_descriptor.channel_keys_id
1853 let per_commitment_point = signer.get_per_commitment_point(htlc_descriptor.per_commitment_number, &secp);
1854 let mut htlc_tx = Transaction {
1856 lock_time: tx_lock_time,
1858 htlc_descriptor.unsigned_tx_input(), // HTLC input
1859 TxIn { ..Default::default() } // Fee input
1862 htlc_descriptor.tx_output(&per_commitment_point, &secp), // HTLC output
1863 TxOut { // Fee input change
1864 value: Amount::ONE_BTC.to_sat(),
1865 script_pubkey: Script::new_op_return(&[]),
1869 let our_sig = signer.sign_holder_htlc_transaction(&mut htlc_tx, 0, htlc_descriptor, &secp).unwrap();
1870 let witness_script = htlc_descriptor.witness_script(&per_commitment_point, &secp);
1871 htlc_tx.input[0].witness = htlc_descriptor.tx_input_witness(&our_sig, &witness_script);
1872 htlc_txs.push(htlc_tx);
1874 _ => panic!("Unexpected event"),
1878 mine_transactions(&nodes[0], &[&htlc_txs[0], &htlc_txs[1]]);
1879 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
1881 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1883 connect_blocks(&nodes[0], BREAKDOWN_TIMEOUT as u32);
1885 let holder_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
1886 assert_eq!(holder_events.len(), 3);
1887 for event in holder_events {
1889 Event::SpendableOutputs { .. } => {},
1890 _ => panic!("Unexpected event"),
1894 // Clear the remaining events as they're not relevant to what we're testing.
1895 nodes[0].node.get_and_clear_pending_events();
1900 fn test_anchors_aggregated_revoked_htlc_tx() {
1901 // Test that `ChannelMonitor`s can properly detect and claim funds from a counterparty claiming
1902 // multiple HTLCs from multiple channels in a single transaction via the success path from a
1903 // revoked commitment.
1904 let secp = Secp256k1::new();
1905 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1906 // Required to sign a revoked commitment transaction
1907 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
1908 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1909 let mut anchors_config = UserConfig::default();
1910 anchors_config.channel_handshake_config.announced_channel = true;
1911 anchors_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
1912 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(anchors_config), Some(anchors_config)]);
1914 let bob_persister: test_utils::TestPersister;
1915 let bob_chain_monitor: test_utils::TestChainMonitor;
1916 let bob_deserialized: ChannelManager<
1917 &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface,
1918 &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator,
1919 &test_utils::TestRouter, &test_utils::TestLogger,
1922 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1924 let chan_a = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 20_000_000);
1925 let chan_b = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 20_000_000);
1927 // Serialize Bob with the initial state of both channels, which we'll use later.
1928 let bob_serialized = nodes[1].node.encode();
1930 // Route two payments for each channel from Alice to Bob to lock in the HTLCs.
1931 let payment_a = route_payment(&nodes[0], &[&nodes[1]], 50_000_000);
1932 let payment_b = route_payment(&nodes[0], &[&nodes[1]], 50_000_000);
1933 let payment_c = route_payment(&nodes[0], &[&nodes[1]], 50_000_000);
1934 let payment_d = route_payment(&nodes[0], &[&nodes[1]], 50_000_000);
1936 // Serialize Bob's monitors with the HTLCs locked in. We'll restart Bob later on with the state
1937 // at this point such that he broadcasts a revoked commitment transaction with the HTLCs
1939 let bob_serialized_monitor_a = get_monitor!(nodes[1], chan_a.2).encode();
1940 let bob_serialized_monitor_b = get_monitor!(nodes[1], chan_b.2).encode();
1942 // Bob claims all the HTLCs...
1943 claim_payment(&nodes[0], &[&nodes[1]], payment_a.0);
1944 claim_payment(&nodes[0], &[&nodes[1]], payment_b.0);
1945 claim_payment(&nodes[0], &[&nodes[1]], payment_c.0);
1946 claim_payment(&nodes[0], &[&nodes[1]], payment_d.0);
1948 // ...and sends one back through each channel such that he has a motive to broadcast his
1950 send_payment(&nodes[1], &[&nodes[0]], 30_000_000);
1951 send_payment(&nodes[1], &[&nodes[0]], 30_000_000);
1953 // Restart Bob with the revoked state and provide the HTLC preimages he claimed.
1955 nodes[1], anchors_config, bob_serialized, &[&bob_serialized_monitor_a, &bob_serialized_monitor_b],
1956 bob_persister, bob_chain_monitor, bob_deserialized
1958 for chan_id in [chan_a.2, chan_b.2].iter() {
1959 let monitor = get_monitor!(nodes[1], chan_id);
1960 for payment in [payment_a, payment_b, payment_c, payment_d].iter() {
1961 monitor.provide_payment_preimage(
1962 &payment.1, &payment.0, &node_cfgs[1].tx_broadcaster,
1963 &LowerBoundedFeeEstimator::new(node_cfgs[1].fee_estimator), &nodes[1].logger
1968 // Bob force closes by restarting with the outdated state, prompting the ChannelMonitors to
1969 // broadcast the latest commitment transaction known to them, which in our case is the one with
1970 // the HTLCs still pending.
1971 nodes[1].node.timer_tick_occurred();
1972 check_added_monitors(&nodes[1], 2);
1973 check_closed_event!(&nodes[1], 2, ClosureReason::OutdatedChannelManager);
1974 let (revoked_commitment_a, revoked_commitment_b) = {
1975 let txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1976 assert_eq!(txn.len(), 2);
1977 assert_eq!(txn[0].output.len(), 6); // 2 HTLC outputs + 1 to_self output + 1 to_remote output + 2 anchor outputs
1978 assert_eq!(txn[1].output.len(), 6); // 2 HTLC outputs + 1 to_self output + 1 to_remote output + 2 anchor outputs
1979 if txn[0].input[0].previous_output.txid == chan_a.3.txid() {
1980 check_spends!(&txn[0], &chan_a.3);
1981 check_spends!(&txn[1], &chan_b.3);
1982 (txn[0].clone(), txn[1].clone())
1984 check_spends!(&txn[1], &chan_a.3);
1985 check_spends!(&txn[0], &chan_b.3);
1986 (txn[1].clone(), txn[0].clone())
1990 // Bob should now receive two events to bump his revoked commitment transaction fees.
1991 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1992 let events = nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events();
1993 assert_eq!(events.len(), 2);
1995 let secret_key = SecretKey::from_slice(&[1; 32]).unwrap();
1996 let public_key = PublicKey::new(secret_key.public_key(&secp));
1997 let fee_utxo_script = Script::new_v0_p2wpkh(&public_key.wpubkey_hash().unwrap());
1998 let coinbase_tx = Transaction {
2000 lock_time: PackedLockTime::ZERO,
2001 input: vec![TxIn { ..Default::default() }],
2002 output: vec![TxOut { // UTXO to attach fees to `anchor_tx`
2003 value: Amount::ONE_BTC.to_sat(),
2004 script_pubkey: fee_utxo_script.clone(),
2007 let mut anchor_tx = Transaction {
2009 lock_time: PackedLockTime::ZERO,
2012 previous_output: bitcoin::OutPoint { txid: coinbase_tx.txid(), vout: 0 },
2013 ..Default::default()
2016 output: vec![TxOut { // Fee input change
2017 value: coinbase_tx.output[0].value / 2 ,
2018 script_pubkey: Script::new_op_return(&[]),
2021 let mut signers = Vec::with_capacity(2);
2022 for event in events {
2024 Event::BumpTransaction(BumpTransactionEvent::ChannelClose { anchor_descriptor, .. }) => {
2025 anchor_tx.input.push(TxIn {
2026 previous_output: anchor_descriptor.outpoint,
2027 ..Default::default()
2029 let signer = nodes[1].keys_manager.derive_channel_keys(
2030 anchor_descriptor.channel_value_satoshis, &anchor_descriptor.channel_keys_id,
2032 signers.push(signer);
2034 _ => panic!("Unexpected event"),
2037 for (i, signer) in signers.into_iter().enumerate() {
2038 let anchor_idx = i + 1;
2039 let funding_sig = signer.sign_holder_anchor_input(&mut anchor_tx, anchor_idx, &secp).unwrap();
2040 anchor_tx.input[anchor_idx].witness = chan_utils::build_anchor_input_witness(
2041 &signer.pubkeys().funding_pubkey, &funding_sig
2044 let fee_utxo_sig = {
2045 let witness_script = Script::new_p2pkh(&public_key.pubkey_hash());
2046 let sighash = hash_to_message!(&SighashCache::new(&anchor_tx).segwit_signature_hash(
2047 0, &witness_script, coinbase_tx.output[0].value, EcdsaSighashType::All
2049 let sig = sign(&secp, &sighash, &secret_key);
2050 let mut sig = sig.serialize_der().to_vec();
2051 sig.push(EcdsaSighashType::All as u8);
2054 anchor_tx.input[0].witness = Witness::from_vec(vec![fee_utxo_sig, public_key.to_bytes()]);
2055 check_spends!(anchor_tx, coinbase_tx, revoked_commitment_a, revoked_commitment_b);
2059 for node in &nodes {
2060 mine_transactions(node, &[&revoked_commitment_a, &revoked_commitment_b, &anchor_tx]);
2062 check_added_monitors!(&nodes[0], 2);
2063 check_closed_broadcast(&nodes[0], 2, true);
2064 check_closed_event!(&nodes[0], 2, ClosureReason::CommitmentTxConfirmed);
2066 // Alice should detect the confirmed revoked commitments, and attempt to claim all of the
2069 let txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
2070 assert_eq!(txn.len(), 2);
2072 let (revoked_claim_a, revoked_claim_b) = if txn[0].input[0].previous_output.txid == revoked_commitment_a.txid() {
2078 // TODO: to_self claim must be separate from HTLC claims
2079 assert_eq!(revoked_claim_a.input.len(), 3); // Spends both HTLC outputs and to_self output
2080 assert_eq!(revoked_claim_a.output.len(), 1);
2081 check_spends!(revoked_claim_a, revoked_commitment_a);
2082 assert_eq!(revoked_claim_b.input.len(), 3); // Spends both HTLC outputs and to_self output
2083 assert_eq!(revoked_claim_b.output.len(), 1);
2084 check_spends!(revoked_claim_b, revoked_commitment_b);
2087 // Since Bob was able to confirm his revoked commitment, he'll now try to claim the HTLCs
2088 // through the success path.
2089 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
2090 let mut events = nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events();
2091 // Certain block `ConnectStyle`s cause an extra `ChannelClose` event to be emitted since the
2092 // best block is updated before the confirmed transactions are notified.
2093 match *nodes[1].connect_style.borrow() {
2094 ConnectStyle::BestBlockFirst|ConnectStyle::BestBlockFirstReorgsOnlyTip|ConnectStyle::BestBlockFirstSkippingBlocks => {
2095 assert_eq!(events.len(), 4);
2096 if let Event::BumpTransaction(BumpTransactionEvent::ChannelClose { .. }) = events.remove(0) {}
2097 else { panic!("unexpected event"); }
2098 if let Event::BumpTransaction(BumpTransactionEvent::ChannelClose { .. }) = events.remove(1) {}
2099 else { panic!("unexpected event"); }
2102 _ => assert_eq!(events.len(), 2),
2105 let secret_key = SecretKey::from_slice(&[1; 32]).unwrap();
2106 let public_key = PublicKey::new(secret_key.public_key(&secp));
2107 let fee_utxo_script = Script::new_v0_p2wpkh(&public_key.wpubkey_hash().unwrap());
2108 let coinbase_tx = Transaction {
2110 lock_time: PackedLockTime::ZERO,
2111 input: vec![TxIn { ..Default::default() }],
2112 output: vec![TxOut { // UTXO to attach fees to `htlc_tx`
2113 value: Amount::ONE_BTC.to_sat(),
2114 script_pubkey: fee_utxo_script.clone(),
2117 let mut htlc_tx = Transaction {
2119 lock_time: PackedLockTime::ZERO,
2120 input: vec![TxIn { // Fee input
2121 previous_output: bitcoin::OutPoint { txid: coinbase_tx.txid(), vout: 0 },
2122 ..Default::default()
2124 output: vec![TxOut { // Fee input change
2125 value: coinbase_tx.output[0].value / 2 ,
2126 script_pubkey: Script::new_op_return(&[]),
2129 let mut descriptors = Vec::with_capacity(4);
2130 for event in events {
2131 if let Event::BumpTransaction(BumpTransactionEvent::HTLCResolution { mut htlc_descriptors, tx_lock_time, .. }) = event {
2132 assert_eq!(htlc_descriptors.len(), 2);
2133 for htlc_descriptor in &htlc_descriptors {
2134 assert!(!htlc_descriptor.htlc.offered);
2135 let signer = nodes[1].keys_manager.derive_channel_keys(
2136 htlc_descriptor.channel_value_satoshis, &htlc_descriptor.channel_keys_id
2138 let per_commitment_point = signer.get_per_commitment_point(htlc_descriptor.per_commitment_number, &secp);
2139 htlc_tx.input.push(htlc_descriptor.unsigned_tx_input());
2140 htlc_tx.output.push(htlc_descriptor.tx_output(&per_commitment_point, &secp));
2142 descriptors.append(&mut htlc_descriptors);
2143 htlc_tx.lock_time = tx_lock_time;
2145 panic!("Unexpected event");
2148 for (idx, htlc_descriptor) in descriptors.into_iter().enumerate() {
2149 let htlc_input_idx = idx + 1;
2150 let signer = nodes[1].keys_manager.derive_channel_keys(
2151 htlc_descriptor.channel_value_satoshis, &htlc_descriptor.channel_keys_id
2153 let our_sig = signer.sign_holder_htlc_transaction(&htlc_tx, htlc_input_idx, &htlc_descriptor, &secp).unwrap();
2154 let per_commitment_point = signer.get_per_commitment_point(htlc_descriptor.per_commitment_number, &secp);
2155 let witness_script = htlc_descriptor.witness_script(&per_commitment_point, &secp);
2156 htlc_tx.input[htlc_input_idx].witness = htlc_descriptor.tx_input_witness(&our_sig, &witness_script);
2158 let fee_utxo_sig = {
2159 let witness_script = Script::new_p2pkh(&public_key.pubkey_hash());
2160 let sighash = hash_to_message!(&SighashCache::new(&htlc_tx).segwit_signature_hash(
2161 0, &witness_script, coinbase_tx.output[0].value, EcdsaSighashType::All
2163 let sig = sign(&secp, &sighash, &secret_key);
2164 let mut sig = sig.serialize_der().to_vec();
2165 sig.push(EcdsaSighashType::All as u8);
2168 htlc_tx.input[0].witness = Witness::from_vec(vec![fee_utxo_sig, public_key.to_bytes()]);
2169 check_spends!(htlc_tx, coinbase_tx, revoked_commitment_a, revoked_commitment_b);
2173 for node in &nodes {
2174 mine_transaction(node, &htlc_tx);
2177 // Alice should see that Bob is trying to claim to HTLCs, so she should now try to claim them at
2178 // the second level instead.
2179 let revoked_claims = {
2180 let txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
2181 assert_eq!(txn.len(), 4);
2183 let revoked_to_self_claim_a = txn.iter().find(|tx|
2184 tx.input.len() == 1 &&
2185 tx.output.len() == 1 &&
2186 tx.input[0].previous_output.txid == revoked_commitment_a.txid()
2188 check_spends!(revoked_to_self_claim_a, revoked_commitment_a);
2190 let revoked_to_self_claim_b = txn.iter().find(|tx|
2191 tx.input.len() == 1 &&
2192 tx.output.len() == 1 &&
2193 tx.input[0].previous_output.txid == revoked_commitment_b.txid()
2195 check_spends!(revoked_to_self_claim_b, revoked_commitment_b);
2197 let revoked_htlc_claims = txn.iter().filter(|tx|
2198 tx.input.len() == 2 &&
2199 tx.output.len() == 1 &&
2200 tx.input[0].previous_output.txid == htlc_tx.txid()
2201 ).collect::<Vec<_>>();
2202 assert_eq!(revoked_htlc_claims.len(), 2);
2203 for revoked_htlc_claim in revoked_htlc_claims {
2204 check_spends!(revoked_htlc_claim, htlc_tx);
2209 for node in &nodes {
2210 mine_transactions(node, &revoked_claims.iter().collect::<Vec<_>>());
2214 // Connect one block to make sure the HTLC events are not yielded while ANTI_REORG_DELAY has not
2216 connect_blocks(&nodes[0], 1);
2217 connect_blocks(&nodes[1], 1);
2219 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
2220 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
2222 // Connect the remaining blocks to reach ANTI_REORG_DELAY.
2223 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 2);
2224 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 2);
2226 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
2227 let spendable_output_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
2228 assert_eq!(spendable_output_events.len(), 4);
2229 for (idx, event) in spendable_output_events.iter().enumerate() {
2230 if let Event::SpendableOutputs { outputs } = event {
2231 assert_eq!(outputs.len(), 1);
2232 let spend_tx = nodes[0].keys_manager.backing.spend_spendable_outputs(
2233 &[&outputs[0]], Vec::new(), Script::new_op_return(&[]), 253, &Secp256k1::new(),
2235 check_spends!(spend_tx, revoked_claims[idx]);
2237 panic!("unexpected event");
2241 assert!(nodes[0].node.list_channels().is_empty());
2242 assert!(nodes[1].node.list_channels().is_empty());
2243 assert!(nodes[0].chain_monitor.chain_monitor.get_claimable_balances(&[]).is_empty());
2244 // TODO: From Bob's PoV, he still thinks he can claim the outputs from his revoked commitment.
2245 // This needs to be fixed before we enable pruning `ChannelMonitor`s once they don't have any
2246 // balances to claim.
2248 // The 6 claimable balances correspond to his `to_self` outputs and the 2 HTLC outputs in each
2249 // revoked commitment which Bob has the preimage for.
2250 assert_eq!(nodes[1].chain_monitor.chain_monitor.get_claimable_balances(&[]).len(), 6);