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;
34 use crate::util::ser::Writeable;
36 use crate::util::test_utils;
39 use bitcoin::blockdata::transaction::EcdsaSighashType;
40 use bitcoin::blockdata::script::Builder;
41 use bitcoin::blockdata::opcodes;
42 use bitcoin::secp256k1::Secp256k1;
44 use bitcoin::secp256k1::SecretKey;
46 use bitcoin::{Amount, PublicKey, Script, TxIn, TxOut, PackedLockTime, Witness};
47 use bitcoin::Transaction;
49 use bitcoin::util::sighash::SighashCache;
51 use crate::prelude::*;
53 use crate::ln::functional_test_utils::*;
56 fn chanmon_fail_from_stale_commitment() {
57 // If we forward an HTLC to our counterparty, but we force-closed the channel before our
58 // counterparty provides us an updated commitment transaction, we'll end up with a commitment
59 // transaction that does not contain the HTLC which we attempted to forward. In this case, we
60 // need to wait `ANTI_REORG_DELAY` blocks and then fail back the HTLC as there is no way for us
61 // to learn the preimage and the confirmed commitment transaction paid us the value of the
64 // However, previously, we did not do this, ignoring the HTLC entirely.
66 // This could lead to channel closure if the sender we received the HTLC from decides to go on
67 // chain to get their HTLC back before it times out.
69 // Here, we check exactly this case, forwarding a payment from A, through B, to C, before B
70 // broadcasts its latest commitment transaction, which should result in it eventually failing
71 // the HTLC back off-chain to A.
72 let chanmon_cfgs = create_chanmon_cfgs(3);
73 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
74 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
75 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
77 create_announced_chan_between_nodes(&nodes, 0, 1);
78 let (update_a, _, chan_id_2, _) = create_announced_chan_between_nodes(&nodes, 1, 2);
80 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 1_000_000);
81 nodes[0].node.send_payment_with_route(&route, payment_hash,
82 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
83 check_added_monitors!(nodes[0], 1);
85 let bs_txn = get_local_commitment_txn!(nodes[1], chan_id_2);
87 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
88 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
89 commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
91 expect_pending_htlcs_forwardable!(nodes[1]);
92 get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
93 check_added_monitors!(nodes[1], 1);
95 // Don't bother delivering the new HTLC add/commits, instead confirming the pre-HTLC commitment
96 // transaction for nodes[1].
97 mine_transaction(&nodes[1], &bs_txn[0]);
98 check_added_monitors!(nodes[1], 1);
99 check_closed_broadcast!(nodes[1], true);
100 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
101 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
103 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
104 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 }]);
105 check_added_monitors!(nodes[1], 1);
106 let fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
108 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates.update_fail_htlcs[0]);
109 commitment_signed_dance!(nodes[0], nodes[1], fail_updates.commitment_signed, true, true);
110 expect_payment_failed_with_update!(nodes[0], payment_hash, false, update_a.contents.short_channel_id, true);
113 fn test_spendable_output<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, spendable_tx: &Transaction) {
114 let mut spendable = node.chain_monitor.chain_monitor.get_and_clear_pending_events();
115 assert_eq!(spendable.len(), 1);
116 if let Event::SpendableOutputs { outputs } = spendable.pop().unwrap() {
117 assert_eq!(outputs.len(), 1);
118 let spend_tx = node.keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
119 Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &Secp256k1::new()).unwrap();
120 check_spends!(spend_tx, spendable_tx);
125 fn revoked_output_htlc_resolution_timing() {
126 // Tests that HTLCs which were present in a broadcasted remote revoked commitment transaction
127 // are resolved only after a spend of the HTLC output reaches six confirmations. Preivously
128 // they would resolve after the revoked commitment transaction itself reaches six
130 let chanmon_cfgs = create_chanmon_cfgs(2);
131 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
132 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
133 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
135 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000);
137 let payment_hash_1 = route_payment(&nodes[1], &[&nodes[0]], 1_000_000).1;
139 // Get a commitment transaction which contains the HTLC we care about, but which we'll revoke
140 // before forwarding.
141 let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan.2);
142 assert_eq!(revoked_local_txn.len(), 1);
144 // Route a dust payment to revoke the above commitment transaction
145 route_payment(&nodes[0], &[&nodes[1]], 1_000);
147 // Confirm the revoked commitment transaction, closing the channel.
148 mine_transaction(&nodes[1], &revoked_local_txn[0]);
149 check_added_monitors!(nodes[1], 1);
150 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
151 check_closed_broadcast!(nodes[1], true);
153 let bs_spend_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
154 assert_eq!(bs_spend_txn.len(), 1);
155 check_spends!(bs_spend_txn[0], revoked_local_txn[0]);
157 // After the commitment transaction confirms, we should still wait on the HTLC spend
158 // transaction to confirm before resolving the HTLC.
159 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
160 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
161 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
163 // Spend the HTLC output, generating a HTLC failure event after ANTI_REORG_DELAY confirmations.
164 mine_transaction(&nodes[1], &bs_spend_txn[0]);
165 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
166 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
168 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
169 expect_payment_failed!(nodes[1], payment_hash_1, false);
173 fn chanmon_claim_value_coop_close() {
174 // Tests `get_claimable_balances` returns the correct values across a simple cooperative claim.
175 // Specifically, this tests that the channel non-HTLC balances show up in
176 // `get_claimable_balances` until the cooperative claims have confirmed and generated a
177 // `SpendableOutputs` event, and no longer.
178 let chanmon_cfgs = create_chanmon_cfgs(2);
179 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
180 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
181 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
183 let (_, _, chan_id, funding_tx) =
184 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 1_000_000);
185 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
186 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
188 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
189 let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
191 assert_eq!(vec![Balance::ClaimableOnChannelClose {
192 claimable_amount_satoshis: 1_000_000 - 1_000 - chan_feerate * channel::commitment_tx_base_weight(opt_anchors) / 1000
194 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
195 assert_eq!(vec![Balance::ClaimableOnChannelClose { claimable_amount_satoshis: 1_000, }],
196 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
198 nodes[0].node.close_channel(&chan_id, &nodes[1].node.get_our_node_id()).unwrap();
199 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
200 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
201 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
202 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
204 let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
205 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
206 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
207 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
208 let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
209 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
210 let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
211 assert!(node_1_none.is_none());
213 let shutdown_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
214 assert_eq!(shutdown_tx, nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0));
215 assert_eq!(shutdown_tx.len(), 1);
217 mine_transaction(&nodes[0], &shutdown_tx[0]);
218 mine_transaction(&nodes[1], &shutdown_tx[0]);
220 assert!(nodes[0].node.list_channels().is_empty());
221 assert!(nodes[1].node.list_channels().is_empty());
223 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
224 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
226 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
227 claimable_amount_satoshis: 1_000_000 - 1_000 - chan_feerate * channel::commitment_tx_base_weight(opt_anchors) / 1000,
228 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
230 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
231 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
232 claimable_amount_satoshis: 1000,
233 confirmation_height: nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1,
235 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
237 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
238 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
240 assert_eq!(Vec::<Balance>::new(),
241 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
242 assert_eq!(Vec::<Balance>::new(),
243 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
245 test_spendable_output(&nodes[0], &shutdown_tx[0]);
246 test_spendable_output(&nodes[1], &shutdown_tx[0]);
248 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure);
249 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure);
252 fn sorted_vec<T: Ord>(mut v: Vec<T>) -> Vec<T> {
257 /// Asserts that `a` and `b` are close, but maybe off by up to 5.
258 /// This is useful when checking fees and weights on transactions as things may vary by a few based
259 /// on signature size and signature size estimation being non-exact.
260 fn fuzzy_assert_eq<V: core::convert::TryInto<u64>>(a: V, b: V) {
261 let a_u64 = a.try_into().map_err(|_| ()).unwrap();
262 let b_u64 = b.try_into().map_err(|_| ()).unwrap();
263 eprintln!("Checking {} and {} for fuzzy equality", a_u64, b_u64);
264 assert!(a_u64 >= b_u64 - 5);
265 assert!(b_u64 >= a_u64 - 5);
268 fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
269 // Tests `get_claimable_balances` with an HTLC across a force-close.
270 // We build a channel with an HTLC pending, then force close the channel and check that the
271 // `get_claimable_balances` return value is correct as transactions confirm on-chain.
272 let mut chanmon_cfgs = create_chanmon_cfgs(2);
273 if prev_commitment_tx {
274 // We broadcast a second-to-latest commitment transaction, without providing the revocation
275 // secret to the counterparty. However, because we always immediately take the revocation
276 // secret from the keys_manager, we would panic at broadcast as we're trying to sign a
277 // transaction which, from the point of view of our keys_manager, is revoked.
278 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
280 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
281 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
282 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
284 let (_, _, chan_id, funding_tx) =
285 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 1_000_000);
286 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
287 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
289 // This HTLC is immediately claimed, giving node B the preimage
290 let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 3_000_000);
291 // This HTLC is allowed to time out, letting A claim it. However, in order to test claimable
292 // balances more fully we also give B the preimage for this HTLC.
293 let (timeout_payment_preimage, timeout_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 4_000_000);
294 // This HTLC will be dust, and not be claimable at all:
295 let (dust_payment_preimage, dust_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 3_000);
297 let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
299 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
300 let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
302 let remote_txn = get_local_commitment_txn!(nodes[1], chan_id);
303 // Before B receives the payment preimage, it only suggests the push_msat value of 1_000 sats
304 // as claimable. A lists both its to-self balance and the (possibly-claimable) HTLCs.
305 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
306 claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
307 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
308 }, Balance::MaybeTimeoutClaimableHTLC {
309 claimable_amount_satoshis: 3_000,
310 claimable_height: htlc_cltv_timeout,
311 }, Balance::MaybeTimeoutClaimableHTLC {
312 claimable_amount_satoshis: 4_000,
313 claimable_height: htlc_cltv_timeout,
315 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
316 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
317 claimable_amount_satoshis: 1_000,
318 }, Balance::MaybePreimageClaimableHTLC {
319 claimable_amount_satoshis: 3_000,
320 expiry_height: htlc_cltv_timeout,
321 }, Balance::MaybePreimageClaimableHTLC {
322 claimable_amount_satoshis: 4_000,
323 expiry_height: htlc_cltv_timeout,
325 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
327 nodes[1].node.claim_funds(payment_preimage);
328 check_added_monitors!(nodes[1], 1);
329 expect_payment_claimed!(nodes[1], payment_hash, 3_000_000);
331 let b_htlc_msgs = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
332 // We claim the dust payment here as well, but it won't impact our claimable balances as its
333 // dust and thus doesn't appear on chain at all.
334 nodes[1].node.claim_funds(dust_payment_preimage);
335 check_added_monitors!(nodes[1], 1);
336 expect_payment_claimed!(nodes[1], dust_payment_hash, 3_000);
338 nodes[1].node.claim_funds(timeout_payment_preimage);
339 check_added_monitors!(nodes[1], 1);
340 expect_payment_claimed!(nodes[1], timeout_payment_hash, 4_000_000);
342 if prev_commitment_tx {
343 // To build a previous commitment transaction, deliver one round of commitment messages.
344 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &b_htlc_msgs.update_fulfill_htlcs[0]);
345 expect_payment_sent_without_paths!(nodes[0], payment_preimage);
346 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &b_htlc_msgs.commitment_signed);
347 check_added_monitors!(nodes[0], 1);
348 let (as_raa, as_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
349 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
350 let _htlc_updates = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
351 check_added_monitors!(nodes[1], 1);
352 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs);
353 let _bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
354 check_added_monitors!(nodes[1], 1);
357 // Once B has received the payment preimage, it includes the value of the HTLC in its
358 // "claimable if you were to close the channel" balance.
359 let mut a_expected_balances = vec![Balance::ClaimableOnChannelClose {
360 claimable_amount_satoshis: 1_000_000 - // Channel funding value in satoshis
361 4_000 - // The to-be-failed HTLC value in satoshis
362 3_000 - // The claimed HTLC value in satoshis
363 1_000 - // The push_msat value in satoshis
364 3 - // The dust HTLC value in satoshis
365 // The commitment transaction fee with two HTLC outputs:
366 chan_feerate * (channel::commitment_tx_base_weight(opt_anchors) +
367 if prev_commitment_tx { 1 } else { 2 } *
368 channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
369 }, Balance::MaybeTimeoutClaimableHTLC {
370 claimable_amount_satoshis: 4_000,
371 claimable_height: htlc_cltv_timeout,
373 if !prev_commitment_tx {
374 a_expected_balances.push(Balance::MaybeTimeoutClaimableHTLC {
375 claimable_amount_satoshis: 3_000,
376 claimable_height: htlc_cltv_timeout,
379 assert_eq!(sorted_vec(a_expected_balances),
380 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
381 assert_eq!(vec![Balance::ClaimableOnChannelClose {
382 claimable_amount_satoshis: 1_000 + 3_000 + 4_000,
384 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
386 // Broadcast the closing transaction (which has both pending HTLCs in it) and get B's
387 // broadcasted HTLC claim transaction with preimage.
388 let node_b_commitment_claimable = nodes[1].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
389 mine_transaction(&nodes[0], &remote_txn[0]);
390 mine_transaction(&nodes[1], &remote_txn[0]);
392 let b_broadcast_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
393 assert_eq!(b_broadcast_txn.len(), 2);
394 // b_broadcast_txn should spend the HTLCs output of the commitment tx for 3_000 and 4_000 sats
395 check_spends!(b_broadcast_txn[0], remote_txn[0]);
396 check_spends!(b_broadcast_txn[1], remote_txn[0]);
397 assert_eq!(b_broadcast_txn[0].input.len(), 1);
398 assert_eq!(b_broadcast_txn[1].input.len(), 1);
399 assert_eq!(remote_txn[0].output[b_broadcast_txn[0].input[0].previous_output.vout as usize].value, 3_000);
400 assert_eq!(remote_txn[0].output[b_broadcast_txn[1].input[0].previous_output.vout as usize].value, 4_000);
402 assert!(nodes[0].node.list_channels().is_empty());
403 check_closed_broadcast!(nodes[0], true);
404 check_added_monitors!(nodes[0], 1);
405 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
406 assert!(nodes[1].node.list_channels().is_empty());
407 check_closed_broadcast!(nodes[1], true);
408 check_added_monitors!(nodes[1], 1);
409 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
410 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
411 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
413 // Once the commitment transaction confirms, we will wait until ANTI_REORG_DELAY until we
414 // generate any `SpendableOutputs` events. Thus, the same balances will still be listed
415 // available in `get_claimable_balances`. However, both will swap from `ClaimableOnClose` to
416 // other Balance variants, as close has already happened.
417 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
418 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
420 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
421 claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
422 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
423 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
424 }, Balance::MaybeTimeoutClaimableHTLC {
425 claimable_amount_satoshis: 3_000,
426 claimable_height: htlc_cltv_timeout,
427 }, Balance::MaybeTimeoutClaimableHTLC {
428 claimable_amount_satoshis: 4_000,
429 claimable_height: htlc_cltv_timeout,
431 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
432 // The main non-HTLC balance is just awaiting confirmations, but the claimable height is the
433 // CSV delay, not ANTI_REORG_DELAY.
434 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
435 claimable_amount_satoshis: 1_000,
436 confirmation_height: node_b_commitment_claimable,
438 // Both HTLC balances are "contentious" as our counterparty could claim them if we wait too
440 Balance::ContentiousClaimable {
441 claimable_amount_satoshis: 3_000,
442 timeout_height: htlc_cltv_timeout,
443 }, Balance::ContentiousClaimable {
444 claimable_amount_satoshis: 4_000,
445 timeout_height: htlc_cltv_timeout,
447 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
449 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
450 expect_payment_failed!(nodes[0], dust_payment_hash, false);
451 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
453 // After ANTI_REORG_DELAY, A will consider its balance fully spendable and generate a
454 // `SpendableOutputs` event. However, B still has to wait for the CSV delay.
455 assert_eq!(sorted_vec(vec![Balance::MaybeTimeoutClaimableHTLC {
456 claimable_amount_satoshis: 3_000,
457 claimable_height: htlc_cltv_timeout,
458 }, Balance::MaybeTimeoutClaimableHTLC {
459 claimable_amount_satoshis: 4_000,
460 claimable_height: htlc_cltv_timeout,
462 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
463 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
464 claimable_amount_satoshis: 1_000,
465 confirmation_height: node_b_commitment_claimable,
466 }, Balance::ContentiousClaimable {
467 claimable_amount_satoshis: 3_000,
468 timeout_height: htlc_cltv_timeout,
469 }, Balance::ContentiousClaimable {
470 claimable_amount_satoshis: 4_000,
471 timeout_height: htlc_cltv_timeout,
473 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
475 test_spendable_output(&nodes[0], &remote_txn[0]);
476 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
478 // After broadcasting the HTLC claim transaction, node A will still consider the HTLC
479 // possibly-claimable up to ANTI_REORG_DELAY, at which point it will drop it.
480 mine_transaction(&nodes[0], &b_broadcast_txn[0]);
481 if prev_commitment_tx {
482 expect_payment_path_successful!(nodes[0]);
484 expect_payment_sent!(nodes[0], payment_preimage);
486 assert_eq!(sorted_vec(vec![Balance::MaybeTimeoutClaimableHTLC {
487 claimable_amount_satoshis: 3_000,
488 claimable_height: htlc_cltv_timeout,
489 }, Balance::MaybeTimeoutClaimableHTLC {
490 claimable_amount_satoshis: 4_000,
491 claimable_height: htlc_cltv_timeout,
493 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
494 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
495 assert_eq!(vec![Balance::MaybeTimeoutClaimableHTLC {
496 claimable_amount_satoshis: 4_000,
497 claimable_height: htlc_cltv_timeout,
499 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
501 // When the HTLC timeout output is spendable in the next block, A should broadcast it
502 connect_blocks(&nodes[0], htlc_cltv_timeout - nodes[0].best_block_info().1 - 1);
503 let a_broadcast_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
504 assert_eq!(a_broadcast_txn.len(), 2);
505 assert_eq!(a_broadcast_txn[0].input.len(), 1);
506 check_spends!(a_broadcast_txn[0], remote_txn[0]);
507 assert_eq!(a_broadcast_txn[1].input.len(), 1);
508 check_spends!(a_broadcast_txn[1], remote_txn[0]);
509 assert_ne!(a_broadcast_txn[0].input[0].previous_output.vout,
510 a_broadcast_txn[1].input[0].previous_output.vout);
511 // a_broadcast_txn [0] and [1] should spend the HTLC outputs of the commitment tx
512 assert_eq!(remote_txn[0].output[a_broadcast_txn[0].input[0].previous_output.vout as usize].value, 3_000);
513 assert_eq!(remote_txn[0].output[a_broadcast_txn[1].input[0].previous_output.vout as usize].value, 4_000);
515 // Once the HTLC-Timeout transaction confirms, A will no longer consider the HTLC
516 // "MaybeClaimable", but instead move it to "AwaitingConfirmations".
517 mine_transaction(&nodes[0], &a_broadcast_txn[1]);
518 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
519 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
520 claimable_amount_satoshis: 4_000,
521 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
523 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
524 // After ANTI_REORG_DELAY, A will generate a SpendableOutputs event and drop the claimable
526 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
527 assert_eq!(Vec::<Balance>::new(),
528 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
529 expect_payment_failed!(nodes[0], timeout_payment_hash, false);
531 test_spendable_output(&nodes[0], &a_broadcast_txn[1]);
533 // Node B will no longer consider the HTLC "contentious" after the HTLC claim transaction
534 // confirms, and consider it simply "awaiting confirmations". Note that it has to wait for the
535 // standard revocable transaction CSV delay before receiving a `SpendableOutputs`.
536 let node_b_htlc_claimable = nodes[1].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
537 mine_transaction(&nodes[1], &b_broadcast_txn[0]);
539 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
540 claimable_amount_satoshis: 1_000,
541 confirmation_height: node_b_commitment_claimable,
542 }, Balance::ClaimableAwaitingConfirmations {
543 claimable_amount_satoshis: 3_000,
544 confirmation_height: node_b_htlc_claimable,
545 }, Balance::ContentiousClaimable {
546 claimable_amount_satoshis: 4_000,
547 timeout_height: htlc_cltv_timeout,
549 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
551 // After reaching the commitment output CSV, we'll get a SpendableOutputs event for it and have
552 // only the HTLCs claimable on node B.
553 connect_blocks(&nodes[1], node_b_commitment_claimable - nodes[1].best_block_info().1);
554 test_spendable_output(&nodes[1], &remote_txn[0]);
556 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
557 claimable_amount_satoshis: 3_000,
558 confirmation_height: node_b_htlc_claimable,
559 }, Balance::ContentiousClaimable {
560 claimable_amount_satoshis: 4_000,
561 timeout_height: htlc_cltv_timeout,
563 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
565 // After reaching the claimed HTLC output CSV, we'll get a SpendableOutptus event for it and
566 // have only one HTLC output left spendable.
567 connect_blocks(&nodes[1], node_b_htlc_claimable - nodes[1].best_block_info().1);
568 test_spendable_output(&nodes[1], &b_broadcast_txn[0]);
570 assert_eq!(vec![Balance::ContentiousClaimable {
571 claimable_amount_satoshis: 4_000,
572 timeout_height: htlc_cltv_timeout,
574 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
576 // Finally, mine the HTLC timeout transaction that A broadcasted (even though B should be able
577 // to claim this HTLC with the preimage it knows!). It will remain listed as a claimable HTLC
578 // until ANTI_REORG_DELAY confirmations on the spend.
579 mine_transaction(&nodes[1], &a_broadcast_txn[1]);
580 assert_eq!(vec![Balance::ContentiousClaimable {
581 claimable_amount_satoshis: 4_000,
582 timeout_height: htlc_cltv_timeout,
584 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
585 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
586 assert_eq!(Vec::<Balance>::new(),
587 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
589 // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
590 // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
591 // monitor events or claimable balances.
592 for node in nodes.iter() {
593 connect_blocks(node, 6);
594 connect_blocks(node, 6);
595 assert!(node.chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
596 assert!(node.chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
601 fn test_claim_value_force_close() {
602 do_test_claim_value_force_close(true);
603 do_test_claim_value_force_close(false);
607 fn test_balances_on_local_commitment_htlcs() {
608 // Previously, when handling the broadcast of a local commitment transactions (with associated
609 // CSV delays prior to spendability), we incorrectly handled the CSV delays on HTLC
610 // transactions. This caused us to miss spendable outputs for HTLCs which were awaiting a CSV
611 // delay prior to spendability.
613 // Further, because of this, we could hit an assertion as `get_claimable_balances` asserted
614 // that HTLCs were resolved after the funding spend was resolved, which was not true if the
615 // HTLC did not have a CSV delay attached (due to the above bug or due to it being an HTLC
616 // claim by our counterparty).
617 let chanmon_cfgs = create_chanmon_cfgs(2);
618 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
619 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
620 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
622 // Create a single channel with two pending HTLCs from nodes[0] to nodes[1], one which nodes[1]
623 // knows the preimage for, one which it does not.
624 let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
625 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
627 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 10_000_000);
628 let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
629 nodes[0].node.send_payment_with_route(&route, payment_hash,
630 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
631 check_added_monitors!(nodes[0], 1);
633 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
634 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
635 commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
637 expect_pending_htlcs_forwardable!(nodes[1]);
638 expect_payment_claimable!(nodes[1], payment_hash, payment_secret, 10_000_000);
640 let (route_2, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 20_000_000);
641 nodes[0].node.send_payment_with_route(&route_2, payment_hash_2,
642 RecipientOnionFields::secret_only(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
643 check_added_monitors!(nodes[0], 1);
645 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
646 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
647 commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
649 expect_pending_htlcs_forwardable!(nodes[1]);
650 expect_payment_claimable!(nodes[1], payment_hash_2, payment_secret_2, 20_000_000);
651 nodes[1].node.claim_funds(payment_preimage_2);
652 get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
653 check_added_monitors!(nodes[1], 1);
654 expect_payment_claimed!(nodes[1], payment_hash_2, 20_000_000);
656 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
657 let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
659 // Get nodes[0]'s commitment transaction and HTLC-Timeout transactions
660 let as_txn = get_local_commitment_txn!(nodes[0], chan_id);
661 assert_eq!(as_txn.len(), 3);
662 check_spends!(as_txn[1], as_txn[0]);
663 check_spends!(as_txn[2], as_txn[0]);
664 check_spends!(as_txn[0], funding_tx);
666 // First confirm the commitment transaction on nodes[0], which should leave us with three
667 // claimable balances.
668 let node_a_commitment_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
669 mine_transaction(&nodes[0], &as_txn[0]);
670 check_added_monitors!(nodes[0], 1);
671 check_closed_broadcast!(nodes[0], true);
672 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
674 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
675 claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
676 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
677 confirmation_height: node_a_commitment_claimable,
678 }, Balance::MaybeTimeoutClaimableHTLC {
679 claimable_amount_satoshis: 10_000,
680 claimable_height: htlc_cltv_timeout,
681 }, Balance::MaybeTimeoutClaimableHTLC {
682 claimable_amount_satoshis: 20_000,
683 claimable_height: htlc_cltv_timeout,
685 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
687 // Get nodes[1]'s HTLC claim tx for the second HTLC
688 mine_transaction(&nodes[1], &as_txn[0]);
689 check_added_monitors!(nodes[1], 1);
690 check_closed_broadcast!(nodes[1], true);
691 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
692 let bs_htlc_claim_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
693 assert_eq!(bs_htlc_claim_txn.len(), 1);
694 check_spends!(bs_htlc_claim_txn[0], as_txn[0]);
696 // Connect blocks until the HTLCs expire, allowing us to (validly) broadcast the HTLC-Timeout
698 connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1);
699 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
700 claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
701 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
702 confirmation_height: node_a_commitment_claimable,
703 }, Balance::MaybeTimeoutClaimableHTLC {
704 claimable_amount_satoshis: 10_000,
705 claimable_height: htlc_cltv_timeout,
706 }, Balance::MaybeTimeoutClaimableHTLC {
707 claimable_amount_satoshis: 20_000,
708 claimable_height: htlc_cltv_timeout,
710 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
711 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
713 // Now confirm nodes[0]'s HTLC-Timeout transaction, which changes the claimable balance to an
714 // "awaiting confirmations" one.
715 let node_a_htlc_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
716 mine_transaction(&nodes[0], &as_txn[1]);
717 // Note that prior to the fix in the commit which introduced this test, this (and the next
718 // balance) check failed. With this check removed, the code panicked in the `connect_blocks`
719 // call, as described, two hunks down.
720 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
721 claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
722 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
723 confirmation_height: node_a_commitment_claimable,
724 }, Balance::ClaimableAwaitingConfirmations {
725 claimable_amount_satoshis: 10_000,
726 confirmation_height: node_a_htlc_claimable,
727 }, Balance::MaybeTimeoutClaimableHTLC {
728 claimable_amount_satoshis: 20_000,
729 claimable_height: htlc_cltv_timeout,
731 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
733 // Now confirm nodes[1]'s HTLC claim, giving nodes[0] the preimage. Note that the "maybe
734 // claimable" balance remains until we see ANTI_REORG_DELAY blocks.
735 mine_transaction(&nodes[0], &bs_htlc_claim_txn[0]);
736 expect_payment_sent!(nodes[0], payment_preimage_2);
737 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
738 claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
739 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
740 confirmation_height: node_a_commitment_claimable,
741 }, Balance::ClaimableAwaitingConfirmations {
742 claimable_amount_satoshis: 10_000,
743 confirmation_height: node_a_htlc_claimable,
744 }, Balance::MaybeTimeoutClaimableHTLC {
745 claimable_amount_satoshis: 20_000,
746 claimable_height: htlc_cltv_timeout,
748 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
750 // Finally make the HTLC transactions have ANTI_REORG_DELAY blocks. This call previously
751 // panicked as described in the test introduction. This will remove the "maybe claimable"
752 // spendable output as nodes[1] has fully claimed the second HTLC.
753 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
754 expect_payment_failed!(nodes[0], payment_hash, false);
756 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
757 claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
758 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
759 confirmation_height: node_a_commitment_claimable,
760 }, Balance::ClaimableAwaitingConfirmations {
761 claimable_amount_satoshis: 10_000,
762 confirmation_height: node_a_htlc_claimable,
764 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
766 // Connect blocks until the commitment transaction's CSV expires, providing us the relevant
767 // `SpendableOutputs` event and removing the claimable balance entry.
768 connect_blocks(&nodes[0], node_a_commitment_claimable - nodes[0].best_block_info().1);
769 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
770 claimable_amount_satoshis: 10_000,
771 confirmation_height: node_a_htlc_claimable,
773 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
774 test_spendable_output(&nodes[0], &as_txn[0]);
776 // Connect blocks until the HTLC-Timeout's CSV expires, providing us the relevant
777 // `SpendableOutputs` event and removing the claimable balance entry.
778 connect_blocks(&nodes[0], node_a_htlc_claimable - nodes[0].best_block_info().1);
779 assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
780 test_spendable_output(&nodes[0], &as_txn[1]);
782 // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
783 // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
784 // monitor events or claimable balances.
785 connect_blocks(&nodes[0], 6);
786 connect_blocks(&nodes[0], 6);
787 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
788 assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
792 fn test_no_preimage_inbound_htlc_balances() {
793 // Tests that MaybePreimageClaimableHTLC are generated for inbound HTLCs for which we do not
795 let chanmon_cfgs = create_chanmon_cfgs(2);
796 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
797 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
798 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
800 let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000);
801 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
803 // Send two HTLCs, one from A to B, and one from B to A.
804 let to_b_failed_payment_hash = route_payment(&nodes[0], &[&nodes[1]], 10_000_000).1;
805 let to_a_failed_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 20_000_000).1;
806 let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
808 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
809 let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
811 // Both A and B will have an HTLC that's claimable on timeout and one that's claimable if they
812 // receive the preimage. These will remain the same through the channel closure and until the
813 // HTLC output is spent.
815 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
816 claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
817 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
818 }, Balance::MaybePreimageClaimableHTLC {
819 claimable_amount_satoshis: 20_000,
820 expiry_height: htlc_cltv_timeout,
821 }, Balance::MaybeTimeoutClaimableHTLC {
822 claimable_amount_satoshis: 10_000,
823 claimable_height: htlc_cltv_timeout,
825 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
827 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
828 claimable_amount_satoshis: 500_000 - 20_000,
829 }, Balance::MaybePreimageClaimableHTLC {
830 claimable_amount_satoshis: 10_000,
831 expiry_height: htlc_cltv_timeout,
832 }, Balance::MaybeTimeoutClaimableHTLC {
833 claimable_amount_satoshis: 20_000,
834 claimable_height: htlc_cltv_timeout,
836 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
838 // Get nodes[0]'s commitment transaction and HTLC-Timeout transaction
839 let as_txn = get_local_commitment_txn!(nodes[0], chan_id);
840 assert_eq!(as_txn.len(), 2);
841 check_spends!(as_txn[1], as_txn[0]);
842 check_spends!(as_txn[0], funding_tx);
844 // Now close the channel by confirming A's commitment transaction on both nodes, checking the
845 // claimable balances remain the same except for the non-HTLC balance changing variant.
846 let node_a_commitment_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
847 let as_pre_spend_claims = sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
848 claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
849 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
850 confirmation_height: node_a_commitment_claimable,
851 }, Balance::MaybePreimageClaimableHTLC {
852 claimable_amount_satoshis: 20_000,
853 expiry_height: htlc_cltv_timeout,
854 }, Balance::MaybeTimeoutClaimableHTLC {
855 claimable_amount_satoshis: 10_000,
856 claimable_height: htlc_cltv_timeout,
859 mine_transaction(&nodes[0], &as_txn[0]);
860 nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
861 check_added_monitors!(nodes[0], 1);
862 check_closed_broadcast!(nodes[0], true);
863 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
865 assert_eq!(as_pre_spend_claims,
866 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
868 mine_transaction(&nodes[1], &as_txn[0]);
869 check_added_monitors!(nodes[1], 1);
870 check_closed_broadcast!(nodes[1], true);
871 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
873 let node_b_commitment_claimable = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
874 let mut bs_pre_spend_claims = sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
875 claimable_amount_satoshis: 500_000 - 20_000,
876 confirmation_height: node_b_commitment_claimable,
877 }, Balance::MaybePreimageClaimableHTLC {
878 claimable_amount_satoshis: 10_000,
879 expiry_height: htlc_cltv_timeout,
880 }, Balance::MaybeTimeoutClaimableHTLC {
881 claimable_amount_satoshis: 20_000,
882 claimable_height: htlc_cltv_timeout,
884 assert_eq!(bs_pre_spend_claims,
885 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
887 // We'll broadcast the HTLC-Timeout transaction one block prior to the htlc's expiration (as it
888 // is confirmable in the next block), but will still include the same claimable balances as no
889 // HTLC has been spent, even after the HTLC expires. We'll also fail the inbound HTLC, but it
890 // won't do anything as the channel is already closed.
892 connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1);
893 let as_htlc_timeout_claim = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
894 assert_eq!(as_htlc_timeout_claim.len(), 1);
895 check_spends!(as_htlc_timeout_claim[0], as_txn[0]);
896 expect_pending_htlcs_forwardable_conditions!(nodes[0],
897 [HTLCDestination::FailedPayment { payment_hash: to_a_failed_payment_hash }]);
899 assert_eq!(as_pre_spend_claims,
900 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
902 connect_blocks(&nodes[0], 1);
903 assert_eq!(as_pre_spend_claims,
904 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
906 // For node B, we'll get the non-HTLC funds claimable after ANTI_REORG_DELAY confirmations
907 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
908 test_spendable_output(&nodes[1], &as_txn[0]);
909 bs_pre_spend_claims.retain(|e| if let Balance::ClaimableAwaitingConfirmations { .. } = e { false } else { true });
911 // The next few blocks for B look the same as for A, though for the opposite HTLC
912 nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
913 connect_blocks(&nodes[1], TEST_FINAL_CLTV - (ANTI_REORG_DELAY - 1) - 1);
914 expect_pending_htlcs_forwardable_conditions!(nodes[1],
915 [HTLCDestination::FailedPayment { payment_hash: to_b_failed_payment_hash }]);
916 let bs_htlc_timeout_claim = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
917 assert_eq!(bs_htlc_timeout_claim.len(), 1);
918 check_spends!(bs_htlc_timeout_claim[0], as_txn[0]);
920 assert_eq!(bs_pre_spend_claims,
921 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
923 connect_blocks(&nodes[1], 1);
924 assert_eq!(bs_pre_spend_claims,
925 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
927 // Now confirm the two HTLC timeout transactions for A, checking that the inbound HTLC resolves
928 // after ANTI_REORG_DELAY confirmations and the other takes BREAKDOWN_TIMEOUT confirmations.
929 mine_transaction(&nodes[0], &as_htlc_timeout_claim[0]);
930 let as_timeout_claimable_height = nodes[0].best_block_info().1 + (BREAKDOWN_TIMEOUT as u32) - 1;
931 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
932 claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
933 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
934 confirmation_height: node_a_commitment_claimable,
935 }, Balance::MaybePreimageClaimableHTLC {
936 claimable_amount_satoshis: 20_000,
937 expiry_height: htlc_cltv_timeout,
938 }, Balance::ClaimableAwaitingConfirmations {
939 claimable_amount_satoshis: 10_000,
940 confirmation_height: as_timeout_claimable_height,
942 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
944 mine_transaction(&nodes[0], &bs_htlc_timeout_claim[0]);
945 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
946 claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
947 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
948 confirmation_height: node_a_commitment_claimable,
949 }, Balance::MaybePreimageClaimableHTLC {
950 claimable_amount_satoshis: 20_000,
951 expiry_height: htlc_cltv_timeout,
952 }, Balance::ClaimableAwaitingConfirmations {
953 claimable_amount_satoshis: 10_000,
954 confirmation_height: as_timeout_claimable_height,
956 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
958 // Once as_htlc_timeout_claim[0] reaches ANTI_REORG_DELAY confirmations, we should get a
959 // payment failure event.
960 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 2);
961 expect_payment_failed!(nodes[0], to_b_failed_payment_hash, false);
963 connect_blocks(&nodes[0], 1);
964 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
965 claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
966 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
967 confirmation_height: node_a_commitment_claimable,
968 }, Balance::ClaimableAwaitingConfirmations {
969 claimable_amount_satoshis: 10_000,
970 confirmation_height: core::cmp::max(as_timeout_claimable_height, htlc_cltv_timeout),
972 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
974 connect_blocks(&nodes[0], node_a_commitment_claimable - nodes[0].best_block_info().1);
975 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
976 claimable_amount_satoshis: 10_000,
977 confirmation_height: core::cmp::max(as_timeout_claimable_height, htlc_cltv_timeout),
979 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
980 test_spendable_output(&nodes[0], &as_txn[0]);
982 connect_blocks(&nodes[0], as_timeout_claimable_height - nodes[0].best_block_info().1);
983 assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
984 test_spendable_output(&nodes[0], &as_htlc_timeout_claim[0]);
986 // The process for B should be completely identical as well, noting that the non-HTLC-balance
987 // was already claimed.
988 mine_transaction(&nodes[1], &bs_htlc_timeout_claim[0]);
989 let bs_timeout_claimable_height = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
990 assert_eq!(sorted_vec(vec![Balance::MaybePreimageClaimableHTLC {
991 claimable_amount_satoshis: 10_000,
992 expiry_height: htlc_cltv_timeout,
993 }, Balance::ClaimableAwaitingConfirmations {
994 claimable_amount_satoshis: 20_000,
995 confirmation_height: bs_timeout_claimable_height,
997 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
999 mine_transaction(&nodes[1], &as_htlc_timeout_claim[0]);
1000 assert_eq!(sorted_vec(vec![Balance::MaybePreimageClaimableHTLC {
1001 claimable_amount_satoshis: 10_000,
1002 expiry_height: htlc_cltv_timeout,
1003 }, Balance::ClaimableAwaitingConfirmations {
1004 claimable_amount_satoshis: 20_000,
1005 confirmation_height: bs_timeout_claimable_height,
1007 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1009 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 2);
1010 expect_payment_failed!(nodes[1], to_a_failed_payment_hash, false);
1012 assert_eq!(vec![Balance::MaybePreimageClaimableHTLC {
1013 claimable_amount_satoshis: 10_000,
1014 expiry_height: htlc_cltv_timeout,
1016 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
1017 test_spendable_output(&nodes[1], &bs_htlc_timeout_claim[0]);
1019 connect_blocks(&nodes[1], 1);
1020 assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1022 // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
1023 // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
1024 // monitor events or claimable balances.
1025 connect_blocks(&nodes[1], 6);
1026 connect_blocks(&nodes[1], 6);
1027 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1028 assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1031 fn sorted_vec_with_additions<T: Ord + Clone>(v_orig: &Vec<T>, extra_ts: &[&T]) -> Vec<T> {
1032 let mut v = v_orig.clone();
1034 v.push((*t).clone());
1040 fn do_test_revoked_counterparty_commitment_balances(confirm_htlc_spend_first: bool) {
1041 // Tests `get_claimable_balances` for revoked counterparty commitment transactions.
1042 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1043 // We broadcast a second-to-latest commitment transaction, without providing the revocation
1044 // secret to the counterparty. However, because we always immediately take the revocation
1045 // secret from the keys_manager, we would panic at broadcast as we're trying to sign a
1046 // transaction which, from the point of view of our keys_manager, is revoked.
1047 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
1048 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1049 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1050 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1052 let (_, _, chan_id, funding_tx) =
1053 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 100_000_000);
1054 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
1055 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
1057 // We create five HTLCs for B to claim against A's revoked commitment transaction:
1059 // (1) one for which A is the originator and B knows the preimage
1060 // (2) one for which B is the originator where the HTLC has since timed-out
1061 // (3) one for which B is the originator but where the HTLC has not yet timed-out
1062 // (4) one dust HTLC which is lost in the channel closure
1063 // (5) one that actually isn't in the revoked commitment transaction at all, but was added in
1064 // later commitment transaction updates
1066 // Though they could all be claimed in a single claim transaction, due to CLTV timeouts they
1067 // are all currently claimed in separate transactions, which helps us test as we can claim
1068 // HTLCs individually.
1070 let (claimed_payment_preimage, claimed_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 3_000_000);
1071 let timeout_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 4_000_000).1;
1072 let dust_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 3_000).1;
1074 let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
1076 connect_blocks(&nodes[0], 10);
1077 connect_blocks(&nodes[1], 10);
1079 let live_htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
1080 let live_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 5_000_000).1;
1082 // Get the latest commitment transaction from A and then update the fee to revoke it
1083 let as_revoked_txn = get_local_commitment_txn!(nodes[0], chan_id);
1084 let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
1086 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
1088 let missing_htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
1089 let missing_htlc_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 2_000_000).1;
1091 nodes[1].node.claim_funds(claimed_payment_preimage);
1092 expect_payment_claimed!(nodes[1], claimed_payment_hash, 3_000_000);
1093 check_added_monitors!(nodes[1], 1);
1094 let _b_htlc_msgs = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
1096 connect_blocks(&nodes[0], htlc_cltv_timeout + 1 - 10);
1097 check_closed_broadcast!(nodes[0], true);
1098 check_added_monitors!(nodes[0], 1);
1100 let mut events = nodes[0].node.get_and_clear_pending_events();
1101 assert_eq!(events.len(), 6);
1102 let mut failed_payments: HashSet<_> =
1103 [timeout_payment_hash, dust_payment_hash, live_payment_hash, missing_htlc_payment_hash]
1104 .iter().map(|a| *a).collect();
1105 events.retain(|ev| {
1107 Event::HTLCHandlingFailed { failed_next_destination: HTLCDestination::NextHopChannel { node_id, channel_id }, .. } => {
1108 assert_eq!(*channel_id, chan_id);
1109 assert_eq!(*node_id, Some(nodes[1].node.get_our_node_id()));
1112 Event::HTLCHandlingFailed { failed_next_destination: HTLCDestination::FailedPayment { payment_hash }, .. } => {
1113 assert!(failed_payments.remove(payment_hash));
1119 assert!(failed_payments.is_empty());
1120 if let Event::PendingHTLCsForwardable { .. } = events[0] {} else { panic!(); }
1122 Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {},
1126 connect_blocks(&nodes[1], htlc_cltv_timeout + 1 - 10);
1127 check_closed_broadcast!(nodes[1], true);
1128 check_added_monitors!(nodes[1], 1);
1129 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
1131 // Prior to channel closure, B considers the preimage HTLC as its own, and otherwise only
1132 // lists the two on-chain timeout-able HTLCs as claimable balances.
1133 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
1134 claimable_amount_satoshis: 100_000 - 5_000 - 4_000 - 3 - 2_000 + 3_000,
1135 }, Balance::MaybeTimeoutClaimableHTLC {
1136 claimable_amount_satoshis: 2_000,
1137 claimable_height: missing_htlc_cltv_timeout,
1138 }, Balance::MaybeTimeoutClaimableHTLC {
1139 claimable_amount_satoshis: 4_000,
1140 claimable_height: htlc_cltv_timeout,
1141 }, Balance::MaybeTimeoutClaimableHTLC {
1142 claimable_amount_satoshis: 5_000,
1143 claimable_height: live_htlc_cltv_timeout,
1145 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1147 mine_transaction(&nodes[1], &as_revoked_txn[0]);
1148 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();
1149 // Currently the revoked commitment is claimed in four transactions as the HTLCs all expire
1151 assert_eq!(claim_txn.len(), 4);
1152 claim_txn.sort_unstable_by_key(|tx| tx.output.iter().map(|output| output.value).sum::<u64>());
1154 // The following constants were determined experimentally
1155 const BS_TO_SELF_CLAIM_EXP_WEIGHT: usize = 483;
1156 const OUTBOUND_HTLC_CLAIM_EXP_WEIGHT: usize = 571;
1157 const INBOUND_HTLC_CLAIM_EXP_WEIGHT: usize = 578;
1159 // Check that the weight is close to the expected weight. Note that signature sizes vary
1160 // somewhat so it may not always be exact.
1161 fuzzy_assert_eq(claim_txn[0].weight(), OUTBOUND_HTLC_CLAIM_EXP_WEIGHT);
1162 fuzzy_assert_eq(claim_txn[1].weight(), INBOUND_HTLC_CLAIM_EXP_WEIGHT);
1163 fuzzy_assert_eq(claim_txn[2].weight(), INBOUND_HTLC_CLAIM_EXP_WEIGHT);
1164 fuzzy_assert_eq(claim_txn[3].weight(), BS_TO_SELF_CLAIM_EXP_WEIGHT);
1166 // The expected balance for the next three checks, with the largest-HTLC and to_self output
1167 // claim balances separated out.
1168 let expected_balance = vec![Balance::ClaimableAwaitingConfirmations {
1169 // to_remote output in A's revoked commitment
1170 claimable_amount_satoshis: 100_000 - 5_000 - 4_000 - 3,
1171 confirmation_height: nodes[1].best_block_info().1 + 5,
1172 }, Balance::CounterpartyRevokedOutputClaimable {
1173 claimable_amount_satoshis: 3_000,
1174 }, Balance::CounterpartyRevokedOutputClaimable {
1175 claimable_amount_satoshis: 4_000,
1178 let to_self_unclaimed_balance = Balance::CounterpartyRevokedOutputClaimable {
1179 claimable_amount_satoshis: 1_000_000 - 100_000 - 3_000 - chan_feerate *
1180 (channel::commitment_tx_base_weight(opt_anchors) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1182 let to_self_claimed_avail_height;
1183 let largest_htlc_unclaimed_balance = Balance::CounterpartyRevokedOutputClaimable {
1184 claimable_amount_satoshis: 5_000,
1186 let largest_htlc_claimed_avail_height;
1188 // Once the channel has been closed by A, B now considers all of the commitment transactions'
1189 // outputs as `CounterpartyRevokedOutputClaimable`.
1190 assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_unclaimed_balance, &largest_htlc_unclaimed_balance]),
1191 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1193 if confirm_htlc_spend_first {
1194 mine_transaction(&nodes[1], &claim_txn[2]);
1195 largest_htlc_claimed_avail_height = nodes[1].best_block_info().1 + 5;
1196 to_self_claimed_avail_height = nodes[1].best_block_info().1 + 6; // will be claimed in the next block
1198 // Connect the to_self output claim, taking all of A's non-HTLC funds
1199 mine_transaction(&nodes[1], &claim_txn[3]);
1200 to_self_claimed_avail_height = nodes[1].best_block_info().1 + 5;
1201 largest_htlc_claimed_avail_height = nodes[1].best_block_info().1 + 6; // will be claimed in the next block
1204 let largest_htlc_claimed_balance = Balance::ClaimableAwaitingConfirmations {
1205 claimable_amount_satoshis: 5_000 - chan_feerate * INBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
1206 confirmation_height: largest_htlc_claimed_avail_height,
1208 let to_self_claimed_balance = Balance::ClaimableAwaitingConfirmations {
1209 claimable_amount_satoshis: 1_000_000 - 100_000 - 3_000 - chan_feerate *
1210 (channel::commitment_tx_base_weight(opt_anchors) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
1211 - chan_feerate * claim_txn[3].weight() as u64 / 1000,
1212 confirmation_height: to_self_claimed_avail_height,
1215 if confirm_htlc_spend_first {
1216 assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_unclaimed_balance, &largest_htlc_claimed_balance]),
1217 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1219 assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_claimed_balance, &largest_htlc_unclaimed_balance]),
1220 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1223 if confirm_htlc_spend_first {
1224 mine_transaction(&nodes[1], &claim_txn[3]);
1226 mine_transaction(&nodes[1], &claim_txn[2]);
1228 assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_claimed_balance, &largest_htlc_claimed_balance]),
1229 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1231 // Finally, connect the last two remaining HTLC spends and check that they move to
1232 // `ClaimableAwaitingConfirmations`
1233 mine_transaction(&nodes[1], &claim_txn[0]);
1234 mine_transaction(&nodes[1], &claim_txn[1]);
1236 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1237 // to_remote output in A's revoked commitment
1238 claimable_amount_satoshis: 100_000 - 5_000 - 4_000 - 3,
1239 confirmation_height: nodes[1].best_block_info().1 + 1,
1240 }, Balance::ClaimableAwaitingConfirmations {
1241 claimable_amount_satoshis: 1_000_000 - 100_000 - 3_000 - chan_feerate *
1242 (channel::commitment_tx_base_weight(opt_anchors) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
1243 - chan_feerate * claim_txn[3].weight() as u64 / 1000,
1244 confirmation_height: to_self_claimed_avail_height,
1245 }, Balance::ClaimableAwaitingConfirmations {
1246 claimable_amount_satoshis: 3_000 - chan_feerate * OUTBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
1247 confirmation_height: nodes[1].best_block_info().1 + 4,
1248 }, Balance::ClaimableAwaitingConfirmations {
1249 claimable_amount_satoshis: 4_000 - chan_feerate * INBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
1250 confirmation_height: nodes[1].best_block_info().1 + 5,
1251 }, Balance::ClaimableAwaitingConfirmations {
1252 claimable_amount_satoshis: 5_000 - chan_feerate * INBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
1253 confirmation_height: largest_htlc_claimed_avail_height,
1255 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1257 connect_blocks(&nodes[1], 1);
1258 test_spendable_output(&nodes[1], &as_revoked_txn[0]);
1260 let mut payment_failed_events = nodes[1].node.get_and_clear_pending_events();
1261 expect_payment_failed_conditions_event(payment_failed_events[..2].to_vec(),
1262 missing_htlc_payment_hash, false, PaymentFailedConditions::new());
1263 expect_payment_failed_conditions_event(payment_failed_events[2..].to_vec(),
1264 dust_payment_hash, false, PaymentFailedConditions::new());
1266 connect_blocks(&nodes[1], 1);
1267 test_spendable_output(&nodes[1], &claim_txn[if confirm_htlc_spend_first { 2 } else { 3 }]);
1268 connect_blocks(&nodes[1], 1);
1269 test_spendable_output(&nodes[1], &claim_txn[if confirm_htlc_spend_first { 3 } else { 2 }]);
1270 expect_payment_failed!(nodes[1], live_payment_hash, false);
1271 connect_blocks(&nodes[1], 1);
1272 test_spendable_output(&nodes[1], &claim_txn[0]);
1273 connect_blocks(&nodes[1], 1);
1274 test_spendable_output(&nodes[1], &claim_txn[1]);
1275 expect_payment_failed!(nodes[1], timeout_payment_hash, false);
1276 assert_eq!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances(), Vec::new());
1278 // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
1279 // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
1280 // monitor events or claimable balances.
1281 connect_blocks(&nodes[1], 6);
1282 connect_blocks(&nodes[1], 6);
1283 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1284 assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1288 fn test_revoked_counterparty_commitment_balances() {
1289 do_test_revoked_counterparty_commitment_balances(true);
1290 do_test_revoked_counterparty_commitment_balances(false);
1294 fn test_revoked_counterparty_htlc_tx_balances() {
1295 // Tests `get_claimable_balances` for revocation spends of HTLC transactions.
1296 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1297 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
1298 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1299 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1300 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1302 // Create some initial channels
1303 let (_, _, chan_id, funding_tx) =
1304 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 11_000_000);
1305 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
1306 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
1308 let payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 3_000_000).0;
1309 let failed_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 1_000_000).1;
1310 let revoked_local_txn = get_local_commitment_txn!(nodes[1], chan_id);
1311 assert_eq!(revoked_local_txn[0].input.len(), 1);
1312 assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, funding_tx.txid());
1314 // The to-be-revoked commitment tx should have two HTLCs and an output for both sides
1315 assert_eq!(revoked_local_txn[0].output.len(), 4);
1317 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
1319 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
1320 let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
1322 // B will generate an HTLC-Success from its revoked commitment tx
1323 mine_transaction(&nodes[1], &revoked_local_txn[0]);
1324 check_closed_broadcast!(nodes[1], true);
1325 check_added_monitors!(nodes[1], 1);
1326 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
1327 let revoked_htlc_success_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1329 assert_eq!(revoked_htlc_success_txn.len(), 1);
1330 assert_eq!(revoked_htlc_success_txn[0].input.len(), 1);
1331 assert_eq!(revoked_htlc_success_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
1332 check_spends!(revoked_htlc_success_txn[0], revoked_local_txn[0]);
1334 connect_blocks(&nodes[1], TEST_FINAL_CLTV);
1335 let revoked_htlc_timeout_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1336 assert_eq!(revoked_htlc_timeout_txn.len(), 1);
1337 check_spends!(revoked_htlc_timeout_txn[0], revoked_local_txn[0]);
1338 assert_ne!(revoked_htlc_success_txn[0].input[0].previous_output, revoked_htlc_timeout_txn[0].input[0].previous_output);
1339 assert_eq!(revoked_htlc_success_txn[0].lock_time.0, 0);
1340 assert_ne!(revoked_htlc_timeout_txn[0].lock_time.0, 0);
1342 // A will generate justice tx from B's revoked commitment/HTLC tx
1343 mine_transaction(&nodes[0], &revoked_local_txn[0]);
1344 check_closed_broadcast!(nodes[0], true);
1345 check_added_monitors!(nodes[0], 1);
1346 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
1347 let to_remote_conf_height = nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1;
1349 let as_commitment_claim_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1350 assert_eq!(as_commitment_claim_txn.len(), 1);
1351 check_spends!(as_commitment_claim_txn[0], revoked_local_txn[0]);
1353 // The next two checks have the same balance set for A - even though we confirm a revoked HTLC
1354 // transaction our balance tracking doesn't use the on-chain value so the
1355 // `CounterpartyRevokedOutputClaimable` entry doesn't change.
1356 let as_balances = sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1357 // to_remote output in B's revoked commitment
1358 claimable_amount_satoshis: 1_000_000 - 11_000 - 3_000 - chan_feerate *
1359 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1360 confirmation_height: to_remote_conf_height,
1361 }, Balance::CounterpartyRevokedOutputClaimable {
1362 // to_self output in B's revoked commitment
1363 claimable_amount_satoshis: 10_000,
1364 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1365 claimable_amount_satoshis: 3_000,
1366 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1367 claimable_amount_satoshis: 1_000,
1369 assert_eq!(as_balances,
1370 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1372 mine_transaction(&nodes[0], &revoked_htlc_success_txn[0]);
1373 let as_htlc_claim_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1374 assert_eq!(as_htlc_claim_tx.len(), 2);
1375 check_spends!(as_htlc_claim_tx[0], revoked_htlc_success_txn[0]);
1376 check_spends!(as_htlc_claim_tx[1], revoked_local_txn[0]); // A has to generate a new claim for the remaining revoked
1377 // outputs (which no longer includes the spent HTLC output)
1379 assert_eq!(as_balances,
1380 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1382 assert_eq!(as_htlc_claim_tx[0].output.len(), 1);
1383 fuzzy_assert_eq(as_htlc_claim_tx[0].output[0].value,
1384 3_000 - chan_feerate * (revoked_htlc_success_txn[0].weight() + as_htlc_claim_tx[0].weight()) as u64 / 1000);
1386 mine_transaction(&nodes[0], &as_htlc_claim_tx[0]);
1387 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1388 // to_remote output in B's revoked commitment
1389 claimable_amount_satoshis: 1_000_000 - 11_000 - 3_000 - chan_feerate *
1390 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1391 confirmation_height: to_remote_conf_height,
1392 }, Balance::CounterpartyRevokedOutputClaimable {
1393 // to_self output in B's revoked commitment
1394 claimable_amount_satoshis: 10_000,
1395 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1396 claimable_amount_satoshis: 1_000,
1397 }, Balance::ClaimableAwaitingConfirmations {
1398 claimable_amount_satoshis: as_htlc_claim_tx[0].output[0].value,
1399 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
1401 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1403 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 3);
1404 test_spendable_output(&nodes[0], &revoked_local_txn[0]);
1405 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1406 // to_self output to B
1407 claimable_amount_satoshis: 10_000,
1408 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1409 claimable_amount_satoshis: 1_000,
1410 }, Balance::ClaimableAwaitingConfirmations {
1411 claimable_amount_satoshis: as_htlc_claim_tx[0].output[0].value,
1412 confirmation_height: nodes[0].best_block_info().1 + 2,
1414 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1416 connect_blocks(&nodes[0], 2);
1417 test_spendable_output(&nodes[0], &as_htlc_claim_tx[0]);
1418 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1419 // to_self output in B's revoked commitment
1420 claimable_amount_satoshis: 10_000,
1421 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1422 claimable_amount_satoshis: 1_000,
1424 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1426 connect_blocks(&nodes[0], revoked_htlc_timeout_txn[0].lock_time.0 - nodes[0].best_block_info().1);
1427 expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(&nodes[0],
1428 [HTLCDestination::FailedPayment { payment_hash: failed_payment_hash }]);
1429 // As time goes on A may split its revocation claim transaction into multiple.
1430 let as_fewer_input_rbf = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1431 for tx in as_fewer_input_rbf.iter() {
1432 check_spends!(tx, revoked_local_txn[0]);
1435 // Connect a number of additional blocks to ensure we don't forget the HTLC output needs
1437 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
1438 let as_fewer_input_rbf = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1439 for tx in as_fewer_input_rbf.iter() {
1440 check_spends!(tx, revoked_local_txn[0]);
1443 mine_transaction(&nodes[0], &revoked_htlc_timeout_txn[0]);
1444 let as_second_htlc_claim_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1445 assert_eq!(as_second_htlc_claim_tx.len(), 2);
1447 check_spends!(as_second_htlc_claim_tx[0], revoked_htlc_timeout_txn[0]);
1448 check_spends!(as_second_htlc_claim_tx[1], revoked_local_txn[0]);
1450 // Connect blocks to finalize the HTLC resolution with the HTLC-Timeout transaction. In a
1451 // previous iteration of the revoked balance handling this would result in us "forgetting" that
1452 // the revoked HTLC output still needed to be claimed.
1453 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
1454 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1455 // to_self output in B's revoked commitment
1456 claimable_amount_satoshis: 10_000,
1457 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1458 claimable_amount_satoshis: 1_000,
1460 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1462 mine_transaction(&nodes[0], &as_second_htlc_claim_tx[0]);
1463 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1464 // to_self output in B's revoked commitment
1465 claimable_amount_satoshis: 10_000,
1466 }, Balance::ClaimableAwaitingConfirmations {
1467 claimable_amount_satoshis: as_second_htlc_claim_tx[0].output[0].value,
1468 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
1470 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1472 mine_transaction(&nodes[0], &as_second_htlc_claim_tx[1]);
1473 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1474 // to_self output in B's revoked commitment
1475 claimable_amount_satoshis: as_second_htlc_claim_tx[1].output[0].value,
1476 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
1477 }, Balance::ClaimableAwaitingConfirmations {
1478 claimable_amount_satoshis: as_second_htlc_claim_tx[0].output[0].value,
1479 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 2,
1481 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1483 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 2);
1484 test_spendable_output(&nodes[0], &as_second_htlc_claim_tx[0]);
1485 connect_blocks(&nodes[0], 1);
1486 test_spendable_output(&nodes[0], &as_second_htlc_claim_tx[1]);
1488 assert_eq!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances(), Vec::new());
1490 // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
1491 // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
1492 // monitor events or claimable balances.
1493 connect_blocks(&nodes[0], 6);
1494 connect_blocks(&nodes[0], 6);
1495 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1496 assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1500 fn test_revoked_counterparty_aggregated_claims() {
1501 // Tests `get_claimable_balances` for revoked counterparty commitment transactions when
1502 // claiming with an aggregated claim transaction.
1503 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1504 // We broadcast a second-to-latest commitment transaction, without providing the revocation
1505 // secret to the counterparty. However, because we always immediately take the revocation
1506 // secret from the keys_manager, we would panic at broadcast as we're trying to sign a
1507 // transaction which, from the point of view of our keys_manager, is revoked.
1508 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
1509 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1510 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1511 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1513 let (_, _, chan_id, funding_tx) =
1514 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 100_000_000);
1515 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
1516 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
1518 // We create two HTLCs, one which we will give A the preimage to to generate an HTLC-Success
1519 // transaction, and one which we will not, allowing B to claim the HTLC output in an aggregated
1520 // revocation-claim transaction.
1522 let (claimed_payment_preimage, claimed_payment_hash, ..) = route_payment(&nodes[1], &[&nodes[0]], 3_000_000);
1523 let revoked_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 4_000_000).1;
1525 let htlc_cltv_timeout = nodes[1].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
1527 // Cheat by giving A's ChannelMonitor the preimage to the to-be-claimed HTLC so that we have an
1528 // HTLC-claim transaction on the to-be-revoked state.
1529 get_monitor!(nodes[0], chan_id).provide_payment_preimage(&claimed_payment_hash, &claimed_payment_preimage,
1530 &node_cfgs[0].tx_broadcaster, &LowerBoundedFeeEstimator::new(node_cfgs[0].fee_estimator), &nodes[0].logger);
1532 // Now get the latest commitment transaction from A and then update the fee to revoke it
1533 let as_revoked_txn = get_local_commitment_txn!(nodes[0], chan_id);
1535 assert_eq!(as_revoked_txn.len(), 2);
1536 check_spends!(as_revoked_txn[0], funding_tx);
1537 check_spends!(as_revoked_txn[1], as_revoked_txn[0]); // The HTLC-Claim transaction
1539 let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
1540 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
1543 let mut feerate = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
1546 nodes[0].node.timer_tick_occurred();
1547 check_added_monitors!(nodes[0], 1);
1549 let fee_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1550 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &fee_update.update_fee.unwrap());
1551 commitment_signed_dance!(nodes[1], nodes[0], fee_update.commitment_signed, false);
1553 nodes[0].node.claim_funds(claimed_payment_preimage);
1554 expect_payment_claimed!(nodes[0], claimed_payment_hash, 3_000_000);
1555 check_added_monitors!(nodes[0], 1);
1556 let _a_htlc_msgs = get_htlc_update_msgs!(&nodes[0], nodes[1].node.get_our_node_id());
1558 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
1559 claimable_amount_satoshis: 100_000 - 4_000 - 3_000,
1560 }, Balance::MaybeTimeoutClaimableHTLC {
1561 claimable_amount_satoshis: 4_000,
1562 claimable_height: htlc_cltv_timeout,
1563 }, Balance::MaybeTimeoutClaimableHTLC {
1564 claimable_amount_satoshis: 3_000,
1565 claimable_height: htlc_cltv_timeout,
1567 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1569 mine_transaction(&nodes[1], &as_revoked_txn[0]);
1570 check_closed_broadcast!(nodes[1], true);
1571 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
1572 check_added_monitors!(nodes[1], 1);
1574 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();
1575 // Currently the revoked commitment outputs are all claimed in one aggregated transaction
1576 assert_eq!(claim_txn.len(), 1);
1577 assert_eq!(claim_txn[0].input.len(), 3);
1578 check_spends!(claim_txn[0], as_revoked_txn[0]);
1580 let to_remote_maturity = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
1582 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1583 // to_remote output in A's revoked commitment
1584 claimable_amount_satoshis: 100_000 - 4_000 - 3_000,
1585 confirmation_height: to_remote_maturity,
1586 }, Balance::CounterpartyRevokedOutputClaimable {
1587 // to_self output in A's revoked commitment
1588 claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1589 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1590 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1591 claimable_amount_satoshis: 4_000,
1592 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1593 claimable_amount_satoshis: 3_000,
1595 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1597 // Confirm A's HTLC-Success tranasction which presumably raced B's claim, causing B to create a
1599 mine_transaction(&nodes[1], &as_revoked_txn[1]);
1600 expect_payment_sent!(nodes[1], claimed_payment_preimage);
1601 let mut claim_txn_2: Vec<_> = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
1602 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 });
1603 // Once B sees the HTLC-Success transaction it splits its claim transaction into two, though in
1604 // theory it could re-aggregate the claims as well.
1605 assert_eq!(claim_txn_2.len(), 2);
1606 assert_eq!(claim_txn_2[0].input.len(), 2);
1607 check_spends!(claim_txn_2[0], as_revoked_txn[0]);
1608 assert_eq!(claim_txn_2[1].input.len(), 1);
1609 check_spends!(claim_txn_2[1], as_revoked_txn[1]);
1611 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1612 // to_remote output in A's revoked commitment
1613 claimable_amount_satoshis: 100_000 - 4_000 - 3_000,
1614 confirmation_height: to_remote_maturity,
1615 }, Balance::CounterpartyRevokedOutputClaimable {
1616 // to_self output in A's revoked commitment
1617 claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1618 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1619 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1620 claimable_amount_satoshis: 4_000,
1621 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1622 // The amount here is a bit of a misnomer, really its been reduced by the HTLC
1623 // transaction fee, but the claimable amount is always a bit of an overshoot for HTLCs
1624 // anyway, so its not a big change.
1625 claimable_amount_satoshis: 3_000,
1627 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1629 connect_blocks(&nodes[1], 5);
1630 test_spendable_output(&nodes[1], &as_revoked_txn[0]);
1632 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1633 // to_self output in A's revoked commitment
1634 claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1635 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1636 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1637 claimable_amount_satoshis: 4_000,
1638 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1639 // The amount here is a bit of a misnomer, really its been reduced by the HTLC
1640 // transaction fee, but the claimable amount is always a bit of an overshoot for HTLCs
1641 // anyway, so its not a big change.
1642 claimable_amount_satoshis: 3_000,
1644 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1646 mine_transaction(&nodes[1], &claim_txn_2[1]);
1647 let htlc_2_claim_maturity = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
1649 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1650 // to_self output in A's revoked commitment
1651 claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1652 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1653 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1654 claimable_amount_satoshis: 4_000,
1655 }, Balance::ClaimableAwaitingConfirmations { // HTLC 2
1656 claimable_amount_satoshis: claim_txn_2[1].output[0].value,
1657 confirmation_height: htlc_2_claim_maturity,
1659 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1661 connect_blocks(&nodes[1], 5);
1662 test_spendable_output(&nodes[1], &claim_txn_2[1]);
1664 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1665 // to_self output in A's revoked commitment
1666 claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1667 (channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1668 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1669 claimable_amount_satoshis: 4_000,
1671 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1673 mine_transaction(&nodes[1], &claim_txn_2[0]);
1674 let rest_claim_maturity = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
1676 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
1677 claimable_amount_satoshis: claim_txn_2[0].output[0].value,
1678 confirmation_height: rest_claim_maturity,
1680 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
1682 assert!(nodes[1].node.get_and_clear_pending_events().is_empty()); // We shouldn't fail the payment until we spend the output
1684 connect_blocks(&nodes[1], 5);
1685 expect_payment_failed!(nodes[1], revoked_payment_hash, false);
1686 test_spendable_output(&nodes[1], &claim_txn_2[0]);
1687 assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1689 // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
1690 // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
1691 // monitor events or claimable balances.
1692 connect_blocks(&nodes[1], 6);
1693 connect_blocks(&nodes[1], 6);
1694 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1695 assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1700 fn test_yield_anchors_events() {
1701 // Tests that two parties supporting anchor outputs can open a channel, route payments over
1702 // it, and finalize its resolution uncooperatively. Once the HTLCs are locked in, one side will
1703 // force close once the HTLCs expire. The force close should stem from an event emitted by LDK,
1704 // allowing the consumer to provide additional fees to the commitment transaction to be
1705 // broadcast. Once the commitment transaction confirms, events for the HTLC resolution should be
1706 // emitted by LDK, such that the consumer can attach fees to the zero fee HTLC transactions.
1707 let secp = Secp256k1::new();
1708 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1709 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1710 let mut anchors_config = UserConfig::default();
1711 anchors_config.channel_handshake_config.announced_channel = true;
1712 anchors_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
1713 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(anchors_config), Some(anchors_config)]);
1714 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1716 let chan_id = create_announced_chan_between_nodes_with_value(
1717 &nodes, 0, 1, 1_000_000, 500_000_000
1719 route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
1720 let (payment_preimage, payment_hash, _) = route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1722 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
1724 connect_blocks(&nodes[0], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1);
1725 check_closed_broadcast!(&nodes[0], true);
1726 assert!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().is_empty());
1728 get_monitor!(nodes[0], chan_id).provide_payment_preimage(
1729 &payment_hash, &payment_preimage, &node_cfgs[0].tx_broadcaster,
1730 &LowerBoundedFeeEstimator::new(node_cfgs[0].fee_estimator), &nodes[0].logger
1733 let mut holder_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
1734 assert_eq!(holder_events.len(), 1);
1735 let (commitment_tx, anchor_tx) = match holder_events.pop().unwrap() {
1736 Event::BumpTransaction(BumpTransactionEvent::ChannelClose { commitment_tx, anchor_descriptor, .. }) => {
1737 assert_eq!(commitment_tx.input.len(), 1);
1738 assert_eq!(commitment_tx.output.len(), 6);
1739 let mut anchor_tx = Transaction {
1741 lock_time: PackedLockTime::ZERO,
1743 TxIn { previous_output: anchor_descriptor.outpoint, ..Default::default() },
1744 TxIn { ..Default::default() },
1746 output: vec![TxOut {
1747 value: Amount::ONE_BTC.to_sat(),
1748 script_pubkey: Script::new_op_return(&[]),
1751 let signer = nodes[0].keys_manager.derive_channel_keys(
1752 anchor_descriptor.channel_value_satoshis, &anchor_descriptor.channel_keys_id,
1754 let funding_sig = signer.sign_holder_anchor_input(&mut anchor_tx, 0, &secp).unwrap();
1755 anchor_tx.input[0].witness = chan_utils::build_anchor_input_witness(
1756 &signer.pubkeys().funding_pubkey, &funding_sig
1758 (commitment_tx, anchor_tx)
1760 _ => panic!("Unexpected event"),
1763 mine_transactions(&nodes[0], &[&commitment_tx, &anchor_tx]);
1764 check_added_monitors!(nodes[0], 1);
1766 let mut holder_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
1767 // Certain block `ConnectStyle`s cause an extra `ChannelClose` event to be emitted since the
1768 // best block is updated before the confirmed transactions are notified.
1769 match *nodes[0].connect_style.borrow() {
1770 ConnectStyle::BestBlockFirst|ConnectStyle::BestBlockFirstReorgsOnlyTip|ConnectStyle::BestBlockFirstSkippingBlocks => {
1771 assert_eq!(holder_events.len(), 3);
1772 if let Event::BumpTransaction(BumpTransactionEvent::ChannelClose { .. }) = holder_events.remove(0) {}
1773 else { panic!("unexpected event"); }
1776 _ => assert_eq!(holder_events.len(), 2),
1778 let mut htlc_txs = Vec::with_capacity(2);
1779 for event in holder_events {
1781 Event::BumpTransaction(BumpTransactionEvent::HTLCResolution { htlc_descriptors, tx_lock_time, .. }) => {
1782 assert_eq!(htlc_descriptors.len(), 1);
1783 let htlc_descriptor = &htlc_descriptors[0];
1784 let signer = nodes[0].keys_manager.derive_channel_keys(
1785 htlc_descriptor.channel_value_satoshis, &htlc_descriptor.channel_keys_id
1787 let per_commitment_point = signer.get_per_commitment_point(htlc_descriptor.per_commitment_number, &secp);
1788 let mut htlc_tx = Transaction {
1790 lock_time: tx_lock_time,
1792 htlc_descriptor.unsigned_tx_input(), // HTLC input
1793 TxIn { ..Default::default() } // Fee input
1796 htlc_descriptor.tx_output(&per_commitment_point, &secp), // HTLC output
1797 TxOut { // Fee input change
1798 value: Amount::ONE_BTC.to_sat(),
1799 script_pubkey: Script::new_op_return(&[]),
1803 let our_sig = signer.sign_holder_htlc_transaction(&mut htlc_tx, 0, htlc_descriptor, &secp).unwrap();
1804 let witness_script = htlc_descriptor.witness_script(&per_commitment_point, &secp);
1805 htlc_tx.input[0].witness = htlc_descriptor.tx_input_witness(&our_sig, &witness_script);
1806 htlc_txs.push(htlc_tx);
1808 _ => panic!("Unexpected event"),
1812 mine_transactions(&nodes[0], &[&htlc_txs[0], &htlc_txs[1]]);
1813 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
1815 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1817 connect_blocks(&nodes[0], BREAKDOWN_TIMEOUT as u32);
1819 let holder_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
1820 assert_eq!(holder_events.len(), 3);
1821 for event in holder_events {
1823 Event::SpendableOutputs { .. } => {},
1824 _ => panic!("Unexpected event"),
1828 // Clear the remaining events as they're not relevant to what we're testing.
1829 nodes[0].node.get_and_clear_pending_events();
1834 fn test_anchors_aggregated_revoked_htlc_tx() {
1835 // Test that `ChannelMonitor`s can properly detect and claim funds from a counterparty claiming
1836 // multiple HTLCs from multiple channels in a single transaction via the success path from a
1837 // revoked commitment.
1838 let secp = Secp256k1::new();
1839 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1840 // Required to sign a revoked commitment transaction
1841 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
1842 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1843 let mut anchors_config = UserConfig::default();
1844 anchors_config.channel_handshake_config.announced_channel = true;
1845 anchors_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
1846 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(anchors_config), Some(anchors_config)]);
1848 let bob_persister: test_utils::TestPersister;
1849 let bob_chain_monitor: test_utils::TestChainMonitor;
1850 let bob_deserialized: ChannelManager<
1851 &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface,
1852 &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator,
1853 &test_utils::TestRouter, &test_utils::TestLogger,
1856 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1858 let chan_a = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 20_000_000);
1859 let chan_b = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 20_000_000);
1861 // Serialize Bob with the initial state of both channels, which we'll use later.
1862 let bob_serialized = nodes[1].node.encode();
1864 // Route two payments for each channel from Alice to Bob to lock in the HTLCs.
1865 let payment_a = route_payment(&nodes[0], &[&nodes[1]], 50_000_000);
1866 let payment_b = route_payment(&nodes[0], &[&nodes[1]], 50_000_000);
1867 let payment_c = route_payment(&nodes[0], &[&nodes[1]], 50_000_000);
1868 let payment_d = route_payment(&nodes[0], &[&nodes[1]], 50_000_000);
1870 // Serialize Bob's monitors with the HTLCs locked in. We'll restart Bob later on with the state
1871 // at this point such that he broadcasts a revoked commitment transaction with the HTLCs
1873 let bob_serialized_monitor_a = get_monitor!(nodes[1], chan_a.2).encode();
1874 let bob_serialized_monitor_b = get_monitor!(nodes[1], chan_b.2).encode();
1876 // Bob claims all the HTLCs...
1877 claim_payment(&nodes[0], &[&nodes[1]], payment_a.0);
1878 claim_payment(&nodes[0], &[&nodes[1]], payment_b.0);
1879 claim_payment(&nodes[0], &[&nodes[1]], payment_c.0);
1880 claim_payment(&nodes[0], &[&nodes[1]], payment_d.0);
1882 // ...and sends one back through each channel such that he has a motive to broadcast his
1884 send_payment(&nodes[1], &[&nodes[0]], 30_000_000);
1885 send_payment(&nodes[1], &[&nodes[0]], 30_000_000);
1887 // Restart Bob with the revoked state and provide the HTLC preimages he claimed.
1889 nodes[1], anchors_config, bob_serialized, &[&bob_serialized_monitor_a, &bob_serialized_monitor_b],
1890 bob_persister, bob_chain_monitor, bob_deserialized
1892 for chan_id in [chan_a.2, chan_b.2].iter() {
1893 let monitor = get_monitor!(nodes[1], chan_id);
1894 for payment in [payment_a, payment_b, payment_c, payment_d].iter() {
1895 monitor.provide_payment_preimage(
1896 &payment.1, &payment.0, &node_cfgs[1].tx_broadcaster,
1897 &LowerBoundedFeeEstimator::new(node_cfgs[1].fee_estimator), &nodes[1].logger
1902 // Bob force closes by restarting with the outdated state, prompting the ChannelMonitors to
1903 // broadcast the latest commitment transaction known to them, which in our case is the one with
1904 // the HTLCs still pending.
1905 nodes[1].node.timer_tick_occurred();
1906 check_added_monitors(&nodes[1], 2);
1907 check_closed_event!(&nodes[1], 2, ClosureReason::OutdatedChannelManager);
1908 let (revoked_commitment_a, revoked_commitment_b) = {
1909 let txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1910 assert_eq!(txn.len(), 2);
1911 assert_eq!(txn[0].output.len(), 6); // 2 HTLC outputs + 1 to_self output + 1 to_remote output + 2 anchor outputs
1912 assert_eq!(txn[1].output.len(), 6); // 2 HTLC outputs + 1 to_self output + 1 to_remote output + 2 anchor outputs
1913 if txn[0].input[0].previous_output.txid == chan_a.3.txid() {
1914 check_spends!(&txn[0], &chan_a.3);
1915 check_spends!(&txn[1], &chan_b.3);
1916 (txn[0].clone(), txn[1].clone())
1918 check_spends!(&txn[1], &chan_a.3);
1919 check_spends!(&txn[0], &chan_b.3);
1920 (txn[1].clone(), txn[0].clone())
1924 // Bob should now receive two events to bump his revoked commitment transaction fees.
1925 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1926 let events = nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events();
1927 assert_eq!(events.len(), 2);
1929 let secret_key = SecretKey::from_slice(&[1; 32]).unwrap();
1930 let public_key = PublicKey::new(secret_key.public_key(&secp));
1931 let fee_utxo_script = Script::new_v0_p2wpkh(&public_key.wpubkey_hash().unwrap());
1932 let coinbase_tx = Transaction {
1934 lock_time: PackedLockTime::ZERO,
1935 input: vec![TxIn { ..Default::default() }],
1936 output: vec![TxOut { // UTXO to attach fees to `anchor_tx`
1937 value: Amount::ONE_BTC.to_sat(),
1938 script_pubkey: fee_utxo_script.clone(),
1941 let mut anchor_tx = Transaction {
1943 lock_time: PackedLockTime::ZERO,
1946 previous_output: bitcoin::OutPoint { txid: coinbase_tx.txid(), vout: 0 },
1947 ..Default::default()
1950 output: vec![TxOut { // Fee input change
1951 value: coinbase_tx.output[0].value / 2 ,
1952 script_pubkey: Script::new_op_return(&[]),
1955 let mut signers = Vec::with_capacity(2);
1956 for event in events {
1958 Event::BumpTransaction(BumpTransactionEvent::ChannelClose { anchor_descriptor, .. }) => {
1959 anchor_tx.input.push(TxIn {
1960 previous_output: anchor_descriptor.outpoint,
1961 ..Default::default()
1963 let signer = nodes[1].keys_manager.derive_channel_keys(
1964 anchor_descriptor.channel_value_satoshis, &anchor_descriptor.channel_keys_id,
1966 signers.push(signer);
1968 _ => panic!("Unexpected event"),
1971 for (i, signer) in signers.into_iter().enumerate() {
1972 let anchor_idx = i + 1;
1973 let funding_sig = signer.sign_holder_anchor_input(&mut anchor_tx, anchor_idx, &secp).unwrap();
1974 anchor_tx.input[anchor_idx].witness = chan_utils::build_anchor_input_witness(
1975 &signer.pubkeys().funding_pubkey, &funding_sig
1978 let fee_utxo_sig = {
1979 let witness_script = Script::new_p2pkh(&public_key.pubkey_hash());
1980 let sighash = hash_to_message!(&SighashCache::new(&anchor_tx).segwit_signature_hash(
1981 0, &witness_script, coinbase_tx.output[0].value, EcdsaSighashType::All
1983 let sig = sign(&secp, &sighash, &secret_key);
1984 let mut sig = sig.serialize_der().to_vec();
1985 sig.push(EcdsaSighashType::All as u8);
1988 anchor_tx.input[0].witness = Witness::from_vec(vec![fee_utxo_sig, public_key.to_bytes()]);
1989 check_spends!(anchor_tx, coinbase_tx, revoked_commitment_a, revoked_commitment_b);
1993 for node in &nodes {
1994 mine_transactions(node, &[&revoked_commitment_a, &revoked_commitment_b, &anchor_tx]);
1996 check_added_monitors!(&nodes[0], 2);
1997 check_closed_broadcast(&nodes[0], 2, true);
1998 check_closed_event!(&nodes[0], 2, ClosureReason::CommitmentTxConfirmed);
2000 // Alice should detect the confirmed revoked commitments, and attempt to claim all of the
2003 let txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
2004 assert_eq!(txn.len(), 2);
2006 let (revoked_claim_a, revoked_claim_b) = if txn[0].input[0].previous_output.txid == revoked_commitment_a.txid() {
2012 // TODO: to_self claim must be separate from HTLC claims
2013 assert_eq!(revoked_claim_a.input.len(), 3); // Spends both HTLC outputs and to_self output
2014 assert_eq!(revoked_claim_a.output.len(), 1);
2015 check_spends!(revoked_claim_a, revoked_commitment_a);
2016 assert_eq!(revoked_claim_b.input.len(), 3); // Spends both HTLC outputs and to_self output
2017 assert_eq!(revoked_claim_b.output.len(), 1);
2018 check_spends!(revoked_claim_b, revoked_commitment_b);
2021 // Since Bob was able to confirm his revoked commitment, he'll now try to claim the HTLCs
2022 // through the success path.
2023 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
2024 let mut events = nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events();
2025 // Certain block `ConnectStyle`s cause an extra `ChannelClose` event to be emitted since the
2026 // best block is updated before the confirmed transactions are notified.
2027 match *nodes[1].connect_style.borrow() {
2028 ConnectStyle::BestBlockFirst|ConnectStyle::BestBlockFirstReorgsOnlyTip|ConnectStyle::BestBlockFirstSkippingBlocks => {
2029 assert_eq!(events.len(), 4);
2030 if let Event::BumpTransaction(BumpTransactionEvent::ChannelClose { .. }) = events.remove(0) {}
2031 else { panic!("unexpected event"); }
2032 if let Event::BumpTransaction(BumpTransactionEvent::ChannelClose { .. }) = events.remove(1) {}
2033 else { panic!("unexpected event"); }
2036 _ => assert_eq!(events.len(), 2),
2039 let secret_key = SecretKey::from_slice(&[1; 32]).unwrap();
2040 let public_key = PublicKey::new(secret_key.public_key(&secp));
2041 let fee_utxo_script = Script::new_v0_p2wpkh(&public_key.wpubkey_hash().unwrap());
2042 let coinbase_tx = Transaction {
2044 lock_time: PackedLockTime::ZERO,
2045 input: vec![TxIn { ..Default::default() }],
2046 output: vec![TxOut { // UTXO to attach fees to `htlc_tx`
2047 value: Amount::ONE_BTC.to_sat(),
2048 script_pubkey: fee_utxo_script.clone(),
2051 let mut htlc_tx = Transaction {
2053 lock_time: PackedLockTime::ZERO,
2054 input: vec![TxIn { // Fee input
2055 previous_output: bitcoin::OutPoint { txid: coinbase_tx.txid(), vout: 0 },
2056 ..Default::default()
2058 output: vec![TxOut { // Fee input change
2059 value: coinbase_tx.output[0].value / 2 ,
2060 script_pubkey: Script::new_op_return(&[]),
2063 let mut descriptors = Vec::with_capacity(4);
2064 for event in events {
2065 if let Event::BumpTransaction(BumpTransactionEvent::HTLCResolution { mut htlc_descriptors, tx_lock_time, .. }) = event {
2066 assert_eq!(htlc_descriptors.len(), 2);
2067 for htlc_descriptor in &htlc_descriptors {
2068 assert!(!htlc_descriptor.htlc.offered);
2069 let signer = nodes[1].keys_manager.derive_channel_keys(
2070 htlc_descriptor.channel_value_satoshis, &htlc_descriptor.channel_keys_id
2072 let per_commitment_point = signer.get_per_commitment_point(htlc_descriptor.per_commitment_number, &secp);
2073 htlc_tx.input.push(htlc_descriptor.unsigned_tx_input());
2074 htlc_tx.output.push(htlc_descriptor.tx_output(&per_commitment_point, &secp));
2076 descriptors.append(&mut htlc_descriptors);
2077 htlc_tx.lock_time = tx_lock_time;
2079 panic!("Unexpected event");
2082 for (idx, htlc_descriptor) in descriptors.into_iter().enumerate() {
2083 let htlc_input_idx = idx + 1;
2084 let signer = nodes[1].keys_manager.derive_channel_keys(
2085 htlc_descriptor.channel_value_satoshis, &htlc_descriptor.channel_keys_id
2087 let our_sig = signer.sign_holder_htlc_transaction(&htlc_tx, htlc_input_idx, &htlc_descriptor, &secp).unwrap();
2088 let per_commitment_point = signer.get_per_commitment_point(htlc_descriptor.per_commitment_number, &secp);
2089 let witness_script = htlc_descriptor.witness_script(&per_commitment_point, &secp);
2090 htlc_tx.input[htlc_input_idx].witness = htlc_descriptor.tx_input_witness(&our_sig, &witness_script);
2092 let fee_utxo_sig = {
2093 let witness_script = Script::new_p2pkh(&public_key.pubkey_hash());
2094 let sighash = hash_to_message!(&SighashCache::new(&htlc_tx).segwit_signature_hash(
2095 0, &witness_script, coinbase_tx.output[0].value, EcdsaSighashType::All
2097 let sig = sign(&secp, &sighash, &secret_key);
2098 let mut sig = sig.serialize_der().to_vec();
2099 sig.push(EcdsaSighashType::All as u8);
2102 htlc_tx.input[0].witness = Witness::from_vec(vec![fee_utxo_sig, public_key.to_bytes()]);
2103 check_spends!(htlc_tx, coinbase_tx, revoked_commitment_a, revoked_commitment_b);
2107 for node in &nodes {
2108 mine_transaction(node, &htlc_tx);
2111 // Alice should see that Bob is trying to claim to HTLCs, so she should now try to claim them at
2112 // the second level instead.
2113 let revoked_claims = {
2114 let txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
2115 assert_eq!(txn.len(), 4);
2117 let revoked_to_self_claim_a = txn.iter().find(|tx|
2118 tx.input.len() == 1 &&
2119 tx.output.len() == 1 &&
2120 tx.input[0].previous_output.txid == revoked_commitment_a.txid()
2122 check_spends!(revoked_to_self_claim_a, revoked_commitment_a);
2124 let revoked_to_self_claim_b = txn.iter().find(|tx|
2125 tx.input.len() == 1 &&
2126 tx.output.len() == 1 &&
2127 tx.input[0].previous_output.txid == revoked_commitment_b.txid()
2129 check_spends!(revoked_to_self_claim_b, revoked_commitment_b);
2131 let revoked_htlc_claims = txn.iter().filter(|tx|
2132 tx.input.len() == 2 &&
2133 tx.output.len() == 1 &&
2134 tx.input[0].previous_output.txid == htlc_tx.txid()
2135 ).collect::<Vec<_>>();
2136 assert_eq!(revoked_htlc_claims.len(), 2);
2137 for revoked_htlc_claim in revoked_htlc_claims {
2138 check_spends!(revoked_htlc_claim, htlc_tx);
2143 for node in &nodes {
2144 mine_transactions(node, &revoked_claims.iter().collect::<Vec<_>>());
2148 // Connect one block to make sure the HTLC events are not yielded while ANTI_REORG_DELAY has not
2150 connect_blocks(&nodes[0], 1);
2151 connect_blocks(&nodes[1], 1);
2153 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
2154 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
2156 // Connect the remaining blocks to reach ANTI_REORG_DELAY.
2157 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 2);
2158 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 2);
2160 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
2161 let spendable_output_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
2162 assert_eq!(spendable_output_events.len(), 4);
2163 for (idx, event) in spendable_output_events.iter().enumerate() {
2164 if let Event::SpendableOutputs { outputs } = event {
2165 assert_eq!(outputs.len(), 1);
2166 let spend_tx = nodes[0].keys_manager.backing.spend_spendable_outputs(
2167 &[&outputs[0]], Vec::new(), Script::new_op_return(&[]), 253, &Secp256k1::new(),
2169 check_spends!(spend_tx, revoked_claims[idx]);
2171 panic!("unexpected event");
2175 assert!(nodes[0].node.list_channels().is_empty());
2176 assert!(nodes[1].node.list_channels().is_empty());
2177 assert!(nodes[0].chain_monitor.chain_monitor.get_claimable_balances(&[]).is_empty());
2178 // TODO: From Bob's PoV, he still thinks he can claim the outputs from his revoked commitment.
2179 // This needs to be fixed before we enable pruning `ChannelMonitor`s once they don't have any
2180 // balances to claim.
2182 // The 6 claimable balances correspond to his `to_self` outputs and the 2 HTLC outputs in each
2183 // revoked commitment which Bob has the preimage for.
2184 assert_eq!(nodes[1].chain_monitor.chain_monitor.get_claimable_balances(&[]).len(), 6);