1 // This file is Copyright its original authors, visible in version control
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
10 //! Further functional tests which test blockchain reorganizations.
12 use crate::sign::{ChannelSigner, EcdsaChannelSigner};
13 use crate::chain::channelmonitor::{ANTI_REORG_DELAY, LATENCY_GRACE_PERIOD_BLOCKS, Balance};
14 use crate::chain::transaction::OutPoint;
15 use crate::chain::chaininterface::LowerBoundedFeeEstimator;
16 use crate::events::bump_transaction::BumpTransactionEvent;
17 use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination};
18 use crate::ln::channel;
19 use crate::ln::chan_utils;
20 use crate::ln::channelmanager::{BREAKDOWN_TIMEOUT, ChannelManager, PaymentId, RecipientOnionFields};
21 use crate::ln::msgs::ChannelMessageHandler;
22 use crate::util::config::UserConfig;
23 use crate::util::crypto::sign;
24 use crate::util::ser::Writeable;
25 use crate::util::test_utils;
27 use bitcoin::blockdata::transaction::EcdsaSighashType;
28 use bitcoin::blockdata::script::Builder;
29 use bitcoin::blockdata::opcodes;
30 use bitcoin::secp256k1::{Secp256k1, SecretKey};
31 use bitcoin::{Amount, PublicKey, Script, Transaction, TxIn, TxOut, PackedLockTime, Witness};
32 use bitcoin::util::sighash::SighashCache;
34 use crate::prelude::*;
36 use crate::ln::functional_test_utils::*;
39 fn chanmon_fail_from_stale_commitment() {
40 // If we forward an HTLC to our counterparty, but we force-closed the channel before our
41 // counterparty provides us an updated commitment transaction, we'll end up with a commitment
42 // transaction that does not contain the HTLC which we attempted to forward. In this case, we
43 // need to wait `ANTI_REORG_DELAY` blocks and then fail back the HTLC as there is no way for us
44 // to learn the preimage and the confirmed commitment transaction paid us the value of the
47 // However, previously, we did not do this, ignoring the HTLC entirely.
49 // This could lead to channel closure if the sender we received the HTLC from decides to go on
50 // chain to get their HTLC back before it times out.
52 // Here, we check exactly this case, forwarding a payment from A, through B, to C, before B
53 // broadcasts its latest commitment transaction, which should result in it eventually failing
54 // the HTLC back off-chain to A.
55 let chanmon_cfgs = create_chanmon_cfgs(3);
56 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
57 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
58 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
60 create_announced_chan_between_nodes(&nodes, 0, 1);
61 let (update_a, _, chan_id_2, _) = create_announced_chan_between_nodes(&nodes, 1, 2);
63 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 1_000_000);
64 nodes[0].node.send_payment_with_route(&route, payment_hash,
65 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
66 check_added_monitors!(nodes[0], 1);
68 let bs_txn = get_local_commitment_txn!(nodes[1], chan_id_2);
70 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
71 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
72 commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
74 expect_pending_htlcs_forwardable!(nodes[1]);
75 get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
76 check_added_monitors!(nodes[1], 1);
78 // Don't bother delivering the new HTLC add/commits, instead confirming the pre-HTLC commitment
79 // transaction for nodes[1].
80 mine_transaction(&nodes[1], &bs_txn[0]);
81 check_added_monitors!(nodes[1], 1);
82 check_closed_broadcast!(nodes[1], true);
83 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
84 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
86 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
87 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 }]);
88 check_added_monitors!(nodes[1], 1);
89 let fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
91 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates.update_fail_htlcs[0]);
92 commitment_signed_dance!(nodes[0], nodes[1], fail_updates.commitment_signed, true, true);
93 expect_payment_failed_with_update!(nodes[0], payment_hash, false, update_a.contents.short_channel_id, true);
96 fn test_spendable_output<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, spendable_tx: &Transaction) {
97 let mut spendable = node.chain_monitor.chain_monitor.get_and_clear_pending_events();
98 assert_eq!(spendable.len(), 1);
99 if let Event::SpendableOutputs { outputs } = spendable.pop().unwrap() {
100 assert_eq!(outputs.len(), 1);
101 let spend_tx = node.keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(),
102 Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, None, &Secp256k1::new()).unwrap();
103 check_spends!(spend_tx, spendable_tx);
108 fn revoked_output_htlc_resolution_timing() {
109 // Tests that HTLCs which were present in a broadcasted remote revoked commitment transaction
110 // are resolved only after a spend of the HTLC output reaches six confirmations. Preivously
111 // they would resolve after the revoked commitment transaction itself reaches six
113 let chanmon_cfgs = create_chanmon_cfgs(2);
114 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
115 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
116 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
118 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000);
120 let payment_hash_1 = route_payment(&nodes[1], &[&nodes[0]], 1_000_000).1;
122 // Get a commitment transaction which contains the HTLC we care about, but which we'll revoke
123 // before forwarding.
124 let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan.2);
125 assert_eq!(revoked_local_txn.len(), 1);
127 // Route a dust payment to revoke the above commitment transaction
128 route_payment(&nodes[0], &[&nodes[1]], 1_000);
130 // Confirm the revoked commitment transaction, closing the channel.
131 mine_transaction(&nodes[1], &revoked_local_txn[0]);
132 check_added_monitors!(nodes[1], 1);
133 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
134 check_closed_broadcast!(nodes[1], true);
136 let bs_spend_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
137 assert_eq!(bs_spend_txn.len(), 1);
138 check_spends!(bs_spend_txn[0], revoked_local_txn[0]);
140 // After the commitment transaction confirms, we should still wait on the HTLC spend
141 // transaction to confirm before resolving the HTLC.
142 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
143 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
144 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
146 // Spend the HTLC output, generating a HTLC failure event after ANTI_REORG_DELAY confirmations.
147 mine_transaction(&nodes[1], &bs_spend_txn[0]);
148 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
149 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
151 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
152 expect_payment_failed!(nodes[1], payment_hash_1, false);
156 fn chanmon_claim_value_coop_close() {
157 // Tests `get_claimable_balances` returns the correct values across a simple cooperative claim.
158 // Specifically, this tests that the channel non-HTLC balances show up in
159 // `get_claimable_balances` until the cooperative claims have confirmed and generated a
160 // `SpendableOutputs` event, and no longer.
161 let chanmon_cfgs = create_chanmon_cfgs(2);
162 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
163 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
164 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
166 let (_, _, chan_id, funding_tx) =
167 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 1_000_000);
168 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
169 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
171 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
172 let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan_id);
174 assert_eq!(vec![Balance::ClaimableOnChannelClose {
175 claimable_amount_satoshis: 1_000_000 - 1_000 - chan_feerate * channel::commitment_tx_base_weight(&channel_type_features) / 1000
177 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
178 assert_eq!(vec![Balance::ClaimableOnChannelClose { claimable_amount_satoshis: 1_000, }],
179 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
181 nodes[0].node.close_channel(&chan_id, &nodes[1].node.get_our_node_id()).unwrap();
182 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
183 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown);
184 let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
185 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown);
187 let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
188 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
189 let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
190 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
191 let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
192 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap());
193 let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
194 assert!(node_1_none.is_none());
196 let shutdown_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
197 assert_eq!(shutdown_tx, nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0));
198 assert_eq!(shutdown_tx.len(), 1);
200 mine_transaction(&nodes[0], &shutdown_tx[0]);
201 mine_transaction(&nodes[1], &shutdown_tx[0]);
203 assert!(nodes[0].node.list_channels().is_empty());
204 assert!(nodes[1].node.list_channels().is_empty());
206 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
207 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
209 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
210 claimable_amount_satoshis: 1_000_000 - 1_000 - chan_feerate * channel::commitment_tx_base_weight(&channel_type_features) / 1000,
211 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
213 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
214 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
215 claimable_amount_satoshis: 1000,
216 confirmation_height: nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1,
218 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
220 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
221 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
223 assert_eq!(Vec::<Balance>::new(),
224 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
225 assert_eq!(Vec::<Balance>::new(),
226 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
228 test_spendable_output(&nodes[0], &shutdown_tx[0]);
229 test_spendable_output(&nodes[1], &shutdown_tx[0]);
231 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure);
232 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure);
235 fn sorted_vec<T: Ord>(mut v: Vec<T>) -> Vec<T> {
240 /// Asserts that `a` and `b` are close, but maybe off by up to 5.
241 /// This is useful when checking fees and weights on transactions as things may vary by a few based
242 /// on signature size and signature size estimation being non-exact.
243 fn fuzzy_assert_eq<V: core::convert::TryInto<u64>>(a: V, b: V) {
244 let a_u64 = a.try_into().map_err(|_| ()).unwrap();
245 let b_u64 = b.try_into().map_err(|_| ()).unwrap();
246 eprintln!("Checking {} and {} for fuzzy equality", a_u64, b_u64);
247 assert!(a_u64 >= b_u64 - 5);
248 assert!(b_u64 >= a_u64 - 5);
251 fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
252 // Tests `get_claimable_balances` with an HTLC across a force-close.
253 // We build a channel with an HTLC pending, then force close the channel and check that the
254 // `get_claimable_balances` return value is correct as transactions confirm on-chain.
255 let mut chanmon_cfgs = create_chanmon_cfgs(2);
256 if prev_commitment_tx {
257 // We broadcast a second-to-latest commitment transaction, without providing the revocation
258 // secret to the counterparty. However, because we always immediately take the revocation
259 // secret from the keys_manager, we would panic at broadcast as we're trying to sign a
260 // transaction which, from the point of view of our keys_manager, is revoked.
261 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
263 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
264 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
265 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
267 let (_, _, chan_id, funding_tx) =
268 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 1_000_000);
269 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
270 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
272 // This HTLC is immediately claimed, giving node B the preimage
273 let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 3_000_000);
274 // This HTLC is allowed to time out, letting A claim it. However, in order to test claimable
275 // balances more fully we also give B the preimage for this HTLC.
276 let (timeout_payment_preimage, timeout_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 4_000_000);
277 // This HTLC will be dust, and not be claimable at all:
278 let (dust_payment_preimage, dust_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 3_000);
280 let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
282 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
283 let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan_id);
285 let remote_txn = get_local_commitment_txn!(nodes[1], chan_id);
286 let sent_htlc_balance = Balance::MaybeTimeoutClaimableHTLC {
287 claimable_amount_satoshis: 3_000,
288 claimable_height: htlc_cltv_timeout,
291 let sent_htlc_timeout_balance = Balance::MaybeTimeoutClaimableHTLC {
292 claimable_amount_satoshis: 4_000,
293 claimable_height: htlc_cltv_timeout,
294 payment_hash: timeout_payment_hash,
296 let received_htlc_balance = Balance::MaybePreimageClaimableHTLC {
297 claimable_amount_satoshis: 3_000,
298 expiry_height: htlc_cltv_timeout,
301 let received_htlc_timeout_balance = Balance::MaybePreimageClaimableHTLC {
302 claimable_amount_satoshis: 4_000,
303 expiry_height: htlc_cltv_timeout,
304 payment_hash: timeout_payment_hash,
306 let received_htlc_claiming_balance = Balance::ContentiousClaimable {
307 claimable_amount_satoshis: 3_000,
308 timeout_height: htlc_cltv_timeout,
312 let received_htlc_timeout_claiming_balance = Balance::ContentiousClaimable {
313 claimable_amount_satoshis: 4_000,
314 timeout_height: htlc_cltv_timeout,
315 payment_hash: timeout_payment_hash,
316 payment_preimage: timeout_payment_preimage,
319 // Before B receives the payment preimage, it only suggests the push_msat value of 1_000 sats
320 // as claimable. A lists both its to-self balance and the (possibly-claimable) HTLCs.
321 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
322 claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
323 (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
324 }, sent_htlc_balance.clone(), sent_htlc_timeout_balance.clone()]),
325 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
326 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
327 claimable_amount_satoshis: 1_000,
328 }, received_htlc_balance.clone(), received_htlc_timeout_balance.clone()]),
329 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
331 nodes[1].node.claim_funds(payment_preimage);
332 check_added_monitors!(nodes[1], 1);
333 expect_payment_claimed!(nodes[1], payment_hash, 3_000_000);
335 let b_htlc_msgs = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
336 // We claim the dust payment here as well, but it won't impact our claimable balances as its
337 // dust and thus doesn't appear on chain at all.
338 nodes[1].node.claim_funds(dust_payment_preimage);
339 check_added_monitors!(nodes[1], 1);
340 expect_payment_claimed!(nodes[1], dust_payment_hash, 3_000);
342 nodes[1].node.claim_funds(timeout_payment_preimage);
343 check_added_monitors!(nodes[1], 1);
344 expect_payment_claimed!(nodes[1], timeout_payment_hash, 4_000_000);
346 if prev_commitment_tx {
347 // To build a previous commitment transaction, deliver one round of commitment messages.
348 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &b_htlc_msgs.update_fulfill_htlcs[0]);
349 expect_payment_sent_without_paths!(nodes[0], payment_preimage);
350 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &b_htlc_msgs.commitment_signed);
351 check_added_monitors!(nodes[0], 1);
352 let (as_raa, as_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
353 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
354 let _htlc_updates = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
355 check_added_monitors!(nodes[1], 1);
356 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs);
357 let _bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
358 check_added_monitors!(nodes[1], 1);
361 // Once B has received the payment preimage, it includes the value of the HTLC in its
362 // "claimable if you were to close the channel" balance.
363 let mut a_expected_balances = vec![Balance::ClaimableOnChannelClose {
364 claimable_amount_satoshis: 1_000_000 - // Channel funding value in satoshis
365 4_000 - // The to-be-failed HTLC value in satoshis
366 3_000 - // The claimed HTLC value in satoshis
367 1_000 - // The push_msat value in satoshis
368 3 - // The dust HTLC value in satoshis
369 // The commitment transaction fee with two HTLC outputs:
370 chan_feerate * (channel::commitment_tx_base_weight(&channel_type_features) +
371 if prev_commitment_tx { 1 } else { 2 } *
372 channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
373 }, sent_htlc_timeout_balance.clone()];
374 if !prev_commitment_tx {
375 a_expected_balances.push(sent_htlc_balance.clone());
377 assert_eq!(sorted_vec(a_expected_balances),
378 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
379 assert_eq!(vec![Balance::ClaimableOnChannelClose {
380 claimable_amount_satoshis: 1_000 + 3_000 + 4_000,
382 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
384 // Broadcast the closing transaction (which has both pending HTLCs in it) and get B's
385 // broadcasted HTLC claim transaction with preimage.
386 let node_b_commitment_claimable = nodes[1].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
387 mine_transaction(&nodes[0], &remote_txn[0]);
388 mine_transaction(&nodes[1], &remote_txn[0]);
390 let b_broadcast_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
391 assert_eq!(b_broadcast_txn.len(), 2);
392 // b_broadcast_txn should spend the HTLCs output of the commitment tx for 3_000 and 4_000 sats
393 check_spends!(b_broadcast_txn[0], remote_txn[0]);
394 check_spends!(b_broadcast_txn[1], remote_txn[0]);
395 assert_eq!(b_broadcast_txn[0].input.len(), 1);
396 assert_eq!(b_broadcast_txn[1].input.len(), 1);
397 assert_eq!(remote_txn[0].output[b_broadcast_txn[0].input[0].previous_output.vout as usize].value, 3_000);
398 assert_eq!(remote_txn[0].output[b_broadcast_txn[1].input[0].previous_output.vout as usize].value, 4_000);
400 assert!(nodes[0].node.list_channels().is_empty());
401 check_closed_broadcast!(nodes[0], true);
402 check_added_monitors!(nodes[0], 1);
403 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
404 assert!(nodes[1].node.list_channels().is_empty());
405 check_closed_broadcast!(nodes[1], true);
406 check_added_monitors!(nodes[1], 1);
407 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
408 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
409 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
411 // Once the commitment transaction confirms, we will wait until ANTI_REORG_DELAY until we
412 // generate any `SpendableOutputs` events. Thus, the same balances will still be listed
413 // available in `get_claimable_balances`. However, both will swap from `ClaimableOnClose` to
414 // other Balance variants, as close has already happened.
415 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
416 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
418 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
419 claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
420 (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
421 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
422 }, sent_htlc_balance.clone(), sent_htlc_timeout_balance.clone()]),
423 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
424 // The main non-HTLC balance is just awaiting confirmations, but the claimable height is the
425 // CSV delay, not ANTI_REORG_DELAY.
426 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
427 claimable_amount_satoshis: 1_000,
428 confirmation_height: node_b_commitment_claimable,
430 // Both HTLC balances are "contentious" as our counterparty could claim them if we wait too
432 received_htlc_claiming_balance.clone(), received_htlc_timeout_claiming_balance.clone()]),
433 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
435 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
436 expect_payment_failed!(nodes[0], dust_payment_hash, false);
437 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
439 // After ANTI_REORG_DELAY, A will consider its balance fully spendable and generate a
440 // `SpendableOutputs` event. However, B still has to wait for the CSV delay.
441 assert_eq!(sorted_vec(vec![sent_htlc_balance.clone(), sent_htlc_timeout_balance.clone()]),
442 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
443 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
444 claimable_amount_satoshis: 1_000,
445 confirmation_height: node_b_commitment_claimable,
446 }, received_htlc_claiming_balance.clone(), received_htlc_timeout_claiming_balance.clone()]),
447 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
449 test_spendable_output(&nodes[0], &remote_txn[0]);
450 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
452 // After broadcasting the HTLC claim transaction, node A will still consider the HTLC
453 // possibly-claimable up to ANTI_REORG_DELAY, at which point it will drop it.
454 mine_transaction(&nodes[0], &b_broadcast_txn[0]);
455 if prev_commitment_tx {
456 expect_payment_path_successful!(nodes[0]);
458 expect_payment_sent!(nodes[0], payment_preimage);
460 assert_eq!(sorted_vec(vec![sent_htlc_balance.clone(), sent_htlc_timeout_balance.clone()]),
461 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
462 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
463 assert_eq!(vec![sent_htlc_timeout_balance.clone()],
464 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
466 // When the HTLC timeout output is spendable in the next block, A should broadcast it
467 connect_blocks(&nodes[0], htlc_cltv_timeout - nodes[0].best_block_info().1);
468 let a_broadcast_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
469 assert_eq!(a_broadcast_txn.len(), 2);
470 assert_eq!(a_broadcast_txn[0].input.len(), 1);
471 check_spends!(a_broadcast_txn[0], remote_txn[0]);
472 assert_eq!(a_broadcast_txn[1].input.len(), 1);
473 check_spends!(a_broadcast_txn[1], remote_txn[0]);
474 assert_ne!(a_broadcast_txn[0].input[0].previous_output.vout,
475 a_broadcast_txn[1].input[0].previous_output.vout);
476 // a_broadcast_txn [0] and [1] should spend the HTLC outputs of the commitment tx
477 assert_eq!(remote_txn[0].output[a_broadcast_txn[0].input[0].previous_output.vout as usize].value, 3_000);
478 assert_eq!(remote_txn[0].output[a_broadcast_txn[1].input[0].previous_output.vout as usize].value, 4_000);
480 // Once the HTLC-Timeout transaction confirms, A will no longer consider the HTLC
481 // "MaybeClaimable", but instead move it to "AwaitingConfirmations".
482 mine_transaction(&nodes[0], &a_broadcast_txn[1]);
483 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
484 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
485 claimable_amount_satoshis: 4_000,
486 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
488 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
489 // After ANTI_REORG_DELAY, A will generate a SpendableOutputs event and drop the claimable
491 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
492 assert_eq!(Vec::<Balance>::new(),
493 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
494 expect_payment_failed!(nodes[0], timeout_payment_hash, false);
496 test_spendable_output(&nodes[0], &a_broadcast_txn[1]);
498 // Node B will no longer consider the HTLC "contentious" after the HTLC claim transaction
499 // confirms, and consider it simply "awaiting confirmations". Note that it has to wait for the
500 // standard revocable transaction CSV delay before receiving a `SpendableOutputs`.
501 let node_b_htlc_claimable = nodes[1].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
502 mine_transaction(&nodes[1], &b_broadcast_txn[0]);
504 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
505 claimable_amount_satoshis: 1_000,
506 confirmation_height: node_b_commitment_claimable,
507 }, Balance::ClaimableAwaitingConfirmations {
508 claimable_amount_satoshis: 3_000,
509 confirmation_height: node_b_htlc_claimable,
510 }, received_htlc_timeout_claiming_balance.clone()]),
511 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
513 // After reaching the commitment output CSV, we'll get a SpendableOutputs event for it and have
514 // only the HTLCs claimable on node B.
515 connect_blocks(&nodes[1], node_b_commitment_claimable - nodes[1].best_block_info().1);
516 test_spendable_output(&nodes[1], &remote_txn[0]);
518 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
519 claimable_amount_satoshis: 3_000,
520 confirmation_height: node_b_htlc_claimable,
521 }, received_htlc_timeout_claiming_balance.clone()]),
522 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
524 // After reaching the claimed HTLC output CSV, we'll get a SpendableOutptus event for it and
525 // have only one HTLC output left spendable.
526 connect_blocks(&nodes[1], node_b_htlc_claimable - nodes[1].best_block_info().1);
527 test_spendable_output(&nodes[1], &b_broadcast_txn[0]);
529 assert_eq!(vec![received_htlc_timeout_claiming_balance.clone()],
530 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
532 // Finally, mine the HTLC timeout transaction that A broadcasted (even though B should be able
533 // to claim this HTLC with the preimage it knows!). It will remain listed as a claimable HTLC
534 // until ANTI_REORG_DELAY confirmations on the spend.
535 mine_transaction(&nodes[1], &a_broadcast_txn[1]);
536 assert_eq!(vec![received_htlc_timeout_claiming_balance.clone()],
537 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
538 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
539 assert_eq!(Vec::<Balance>::new(),
540 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
542 // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
543 // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
544 // monitor events or claimable balances.
545 for node in nodes.iter() {
546 connect_blocks(node, 6);
547 connect_blocks(node, 6);
548 assert!(node.chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
549 assert!(node.chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
554 fn test_claim_value_force_close() {
555 do_test_claim_value_force_close(true);
556 do_test_claim_value_force_close(false);
560 fn test_balances_on_local_commitment_htlcs() {
561 // Previously, when handling the broadcast of a local commitment transactions (with associated
562 // CSV delays prior to spendability), we incorrectly handled the CSV delays on HTLC
563 // transactions. This caused us to miss spendable outputs for HTLCs which were awaiting a CSV
564 // delay prior to spendability.
566 // Further, because of this, we could hit an assertion as `get_claimable_balances` asserted
567 // that HTLCs were resolved after the funding spend was resolved, which was not true if the
568 // HTLC did not have a CSV delay attached (due to the above bug or due to it being an HTLC
569 // claim by our counterparty).
570 let chanmon_cfgs = create_chanmon_cfgs(2);
571 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
572 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
573 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
575 // Create a single channel with two pending HTLCs from nodes[0] to nodes[1], one which nodes[1]
576 // knows the preimage for, one which it does not.
577 let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
578 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
580 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 10_000_000);
581 let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
582 nodes[0].node.send_payment_with_route(&route, payment_hash,
583 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
584 check_added_monitors!(nodes[0], 1);
586 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
587 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
588 commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
590 expect_pending_htlcs_forwardable!(nodes[1]);
591 expect_payment_claimable!(nodes[1], payment_hash, payment_secret, 10_000_000);
593 let (route_2, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 20_000_000);
594 nodes[0].node.send_payment_with_route(&route_2, payment_hash_2,
595 RecipientOnionFields::secret_only(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
596 check_added_monitors!(nodes[0], 1);
598 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
599 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
600 commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
602 expect_pending_htlcs_forwardable!(nodes[1]);
603 expect_payment_claimable!(nodes[1], payment_hash_2, payment_secret_2, 20_000_000);
604 nodes[1].node.claim_funds(payment_preimage_2);
605 get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
606 check_added_monitors!(nodes[1], 1);
607 expect_payment_claimed!(nodes[1], payment_hash_2, 20_000_000);
609 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
610 let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan_id);
612 // Get nodes[0]'s commitment transaction and HTLC-Timeout transactions
613 let as_txn = get_local_commitment_txn!(nodes[0], chan_id);
614 assert_eq!(as_txn.len(), 3);
615 check_spends!(as_txn[1], as_txn[0]);
616 check_spends!(as_txn[2], as_txn[0]);
617 check_spends!(as_txn[0], funding_tx);
619 // First confirm the commitment transaction on nodes[0], which should leave us with three
620 // claimable balances.
621 let node_a_commitment_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
622 mine_transaction(&nodes[0], &as_txn[0]);
623 check_added_monitors!(nodes[0], 1);
624 check_closed_broadcast!(nodes[0], true);
625 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
627 let htlc_balance_known_preimage = Balance::MaybeTimeoutClaimableHTLC {
628 claimable_amount_satoshis: 10_000,
629 claimable_height: htlc_cltv_timeout,
632 let htlc_balance_unknown_preimage = Balance::MaybeTimeoutClaimableHTLC {
633 claimable_amount_satoshis: 20_000,
634 claimable_height: htlc_cltv_timeout,
635 payment_hash: payment_hash_2,
638 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
639 claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
640 (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
641 confirmation_height: node_a_commitment_claimable,
642 }, htlc_balance_known_preimage.clone(), htlc_balance_unknown_preimage.clone()]),
643 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
645 // Get nodes[1]'s HTLC claim tx for the second HTLC
646 mine_transaction(&nodes[1], &as_txn[0]);
647 check_added_monitors!(nodes[1], 1);
648 check_closed_broadcast!(nodes[1], true);
649 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
650 let bs_htlc_claim_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
651 assert_eq!(bs_htlc_claim_txn.len(), 1);
652 check_spends!(bs_htlc_claim_txn[0], as_txn[0]);
654 // Connect blocks until the HTLCs expire, allowing us to (validly) broadcast the HTLC-Timeout
656 connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1);
657 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
658 claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
659 (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
660 confirmation_height: node_a_commitment_claimable,
661 }, htlc_balance_known_preimage.clone(), htlc_balance_unknown_preimage.clone()]),
662 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
663 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
665 // Now confirm nodes[0]'s HTLC-Timeout transaction, which changes the claimable balance to an
666 // "awaiting confirmations" one.
667 let node_a_htlc_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
668 mine_transaction(&nodes[0], &as_txn[1]);
669 // Note that prior to the fix in the commit which introduced this test, this (and the next
670 // balance) check failed. With this check removed, the code panicked in the `connect_blocks`
671 // call, as described, two hunks down.
672 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
673 claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
674 (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
675 confirmation_height: node_a_commitment_claimable,
676 }, Balance::ClaimableAwaitingConfirmations {
677 claimable_amount_satoshis: 10_000,
678 confirmation_height: node_a_htlc_claimable,
679 }, htlc_balance_unknown_preimage.clone()]),
680 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
682 // Now confirm nodes[1]'s HTLC claim, giving nodes[0] the preimage. Note that the "maybe
683 // claimable" balance remains until we see ANTI_REORG_DELAY blocks.
684 mine_transaction(&nodes[0], &bs_htlc_claim_txn[0]);
685 expect_payment_sent!(nodes[0], payment_preimage_2);
686 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
687 claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
688 (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
689 confirmation_height: node_a_commitment_claimable,
690 }, Balance::ClaimableAwaitingConfirmations {
691 claimable_amount_satoshis: 10_000,
692 confirmation_height: node_a_htlc_claimable,
693 }, htlc_balance_unknown_preimage.clone()]),
694 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
696 // Finally make the HTLC transactions have ANTI_REORG_DELAY blocks. This call previously
697 // panicked as described in the test introduction. This will remove the "maybe claimable"
698 // spendable output as nodes[1] has fully claimed the second HTLC.
699 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
700 expect_payment_failed!(nodes[0], payment_hash, false);
702 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
703 claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
704 (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
705 confirmation_height: node_a_commitment_claimable,
706 }, Balance::ClaimableAwaitingConfirmations {
707 claimable_amount_satoshis: 10_000,
708 confirmation_height: node_a_htlc_claimable,
710 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
712 // Connect blocks until the commitment transaction's CSV expires, providing us the relevant
713 // `SpendableOutputs` event and removing the claimable balance entry.
714 connect_blocks(&nodes[0], node_a_commitment_claimable - nodes[0].best_block_info().1);
715 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
716 claimable_amount_satoshis: 10_000,
717 confirmation_height: node_a_htlc_claimable,
719 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
720 test_spendable_output(&nodes[0], &as_txn[0]);
722 // Connect blocks until the HTLC-Timeout's CSV expires, providing us the relevant
723 // `SpendableOutputs` event and removing the claimable balance entry.
724 connect_blocks(&nodes[0], node_a_htlc_claimable - nodes[0].best_block_info().1);
725 assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
726 test_spendable_output(&nodes[0], &as_txn[1]);
728 // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
729 // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
730 // monitor events or claimable balances.
731 connect_blocks(&nodes[0], 6);
732 connect_blocks(&nodes[0], 6);
733 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
734 assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
738 fn test_no_preimage_inbound_htlc_balances() {
739 // Tests that MaybePreimageClaimableHTLC are generated for inbound HTLCs for which we do not
741 let chanmon_cfgs = create_chanmon_cfgs(2);
742 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
743 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
744 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
746 let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000);
747 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
749 // Send two HTLCs, one from A to B, and one from B to A.
750 let to_b_failed_payment_hash = route_payment(&nodes[0], &[&nodes[1]], 10_000_000).1;
751 let to_a_failed_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 20_000_000).1;
752 let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
754 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
755 let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan_id);
757 let a_sent_htlc_balance = Balance::MaybeTimeoutClaimableHTLC {
758 claimable_amount_satoshis: 10_000,
759 claimable_height: htlc_cltv_timeout,
760 payment_hash: to_b_failed_payment_hash,
762 let a_received_htlc_balance = Balance::MaybePreimageClaimableHTLC {
763 claimable_amount_satoshis: 20_000,
764 expiry_height: htlc_cltv_timeout,
765 payment_hash: to_a_failed_payment_hash,
767 let b_received_htlc_balance = Balance::MaybePreimageClaimableHTLC {
768 claimable_amount_satoshis: 10_000,
769 expiry_height: htlc_cltv_timeout,
770 payment_hash: to_b_failed_payment_hash,
772 let b_sent_htlc_balance = Balance::MaybeTimeoutClaimableHTLC {
773 claimable_amount_satoshis: 20_000,
774 claimable_height: htlc_cltv_timeout,
775 payment_hash: to_a_failed_payment_hash,
778 // Both A and B will have an HTLC that's claimable on timeout and one that's claimable if they
779 // receive the preimage. These will remain the same through the channel closure and until the
780 // HTLC output is spent.
782 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
783 claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
784 (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
785 }, a_received_htlc_balance.clone(), a_sent_htlc_balance.clone()]),
786 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
788 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
789 claimable_amount_satoshis: 500_000 - 20_000,
790 }, b_received_htlc_balance.clone(), b_sent_htlc_balance.clone()]),
791 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
793 // Get nodes[0]'s commitment transaction and HTLC-Timeout transaction
794 let as_txn = get_local_commitment_txn!(nodes[0], chan_id);
795 assert_eq!(as_txn.len(), 2);
796 check_spends!(as_txn[1], as_txn[0]);
797 check_spends!(as_txn[0], funding_tx);
799 // Now close the channel by confirming A's commitment transaction on both nodes, checking the
800 // claimable balances remain the same except for the non-HTLC balance changing variant.
801 let node_a_commitment_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
802 let as_pre_spend_claims = sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
803 claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
804 (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
805 confirmation_height: node_a_commitment_claimable,
806 }, a_received_htlc_balance.clone(), a_sent_htlc_balance.clone()]);
808 mine_transaction(&nodes[0], &as_txn[0]);
809 nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
810 check_added_monitors!(nodes[0], 1);
811 check_closed_broadcast!(nodes[0], true);
812 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
814 assert_eq!(as_pre_spend_claims,
815 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
817 mine_transaction(&nodes[1], &as_txn[0]);
818 check_added_monitors!(nodes[1], 1);
819 check_closed_broadcast!(nodes[1], true);
820 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
822 let node_b_commitment_claimable = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
823 let mut bs_pre_spend_claims = sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
824 claimable_amount_satoshis: 500_000 - 20_000,
825 confirmation_height: node_b_commitment_claimable,
826 }, b_received_htlc_balance.clone(), b_sent_htlc_balance.clone()]);
827 assert_eq!(bs_pre_spend_claims,
828 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
830 // We'll broadcast the HTLC-Timeout transaction one block prior to the htlc's expiration (as it
831 // is confirmable in the next block), but will still include the same claimable balances as no
832 // HTLC has been spent, even after the HTLC expires. We'll also fail the inbound HTLC, but it
833 // won't do anything as the channel is already closed.
835 connect_blocks(&nodes[0], TEST_FINAL_CLTV);
836 let as_htlc_timeout_claim = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
837 assert_eq!(as_htlc_timeout_claim.len(), 1);
838 check_spends!(as_htlc_timeout_claim[0], as_txn[0]);
839 expect_pending_htlcs_forwardable_conditions!(nodes[0],
840 [HTLCDestination::FailedPayment { payment_hash: to_a_failed_payment_hash }]);
842 assert_eq!(as_pre_spend_claims,
843 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
845 connect_blocks(&nodes[0], 1);
846 assert_eq!(as_pre_spend_claims,
847 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
849 // For node B, we'll get the non-HTLC funds claimable after ANTI_REORG_DELAY confirmations
850 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
851 test_spendable_output(&nodes[1], &as_txn[0]);
852 bs_pre_spend_claims.retain(|e| if let Balance::ClaimableAwaitingConfirmations { .. } = e { false } else { true });
854 // The next few blocks for B look the same as for A, though for the opposite HTLC
855 nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
856 connect_blocks(&nodes[1], TEST_FINAL_CLTV - (ANTI_REORG_DELAY - 1));
857 expect_pending_htlcs_forwardable_conditions!(nodes[1],
858 [HTLCDestination::FailedPayment { payment_hash: to_b_failed_payment_hash }]);
859 let bs_htlc_timeout_claim = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
860 assert_eq!(bs_htlc_timeout_claim.len(), 1);
861 check_spends!(bs_htlc_timeout_claim[0], as_txn[0]);
863 assert_eq!(bs_pre_spend_claims,
864 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
866 connect_blocks(&nodes[1], 1);
867 assert_eq!(bs_pre_spend_claims,
868 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
870 // Now confirm the two HTLC timeout transactions for A, checking that the inbound HTLC resolves
871 // after ANTI_REORG_DELAY confirmations and the other takes BREAKDOWN_TIMEOUT confirmations.
872 mine_transaction(&nodes[0], &as_htlc_timeout_claim[0]);
873 let as_timeout_claimable_height = nodes[0].best_block_info().1 + (BREAKDOWN_TIMEOUT as u32) - 1;
874 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
875 claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
876 (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
877 confirmation_height: node_a_commitment_claimable,
878 }, a_received_htlc_balance.clone(), Balance::ClaimableAwaitingConfirmations {
879 claimable_amount_satoshis: 10_000,
880 confirmation_height: as_timeout_claimable_height,
882 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
884 mine_transaction(&nodes[0], &bs_htlc_timeout_claim[0]);
885 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
886 claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
887 (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
888 confirmation_height: node_a_commitment_claimable,
889 }, a_received_htlc_balance.clone(), Balance::ClaimableAwaitingConfirmations {
890 claimable_amount_satoshis: 10_000,
891 confirmation_height: as_timeout_claimable_height,
893 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
895 // Once as_htlc_timeout_claim[0] reaches ANTI_REORG_DELAY confirmations, we should get a
896 // payment failure event.
897 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 2);
898 expect_payment_failed!(nodes[0], to_b_failed_payment_hash, false);
900 connect_blocks(&nodes[0], 1);
901 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
902 claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
903 (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
904 confirmation_height: node_a_commitment_claimable,
905 }, Balance::ClaimableAwaitingConfirmations {
906 claimable_amount_satoshis: 10_000,
907 confirmation_height: core::cmp::max(as_timeout_claimable_height, htlc_cltv_timeout),
909 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
911 connect_blocks(&nodes[0], node_a_commitment_claimable - nodes[0].best_block_info().1);
912 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
913 claimable_amount_satoshis: 10_000,
914 confirmation_height: core::cmp::max(as_timeout_claimable_height, htlc_cltv_timeout),
916 nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
917 test_spendable_output(&nodes[0], &as_txn[0]);
919 connect_blocks(&nodes[0], as_timeout_claimable_height - nodes[0].best_block_info().1);
920 assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
921 test_spendable_output(&nodes[0], &as_htlc_timeout_claim[0]);
923 // The process for B should be completely identical as well, noting that the non-HTLC-balance
924 // was already claimed.
925 mine_transaction(&nodes[1], &bs_htlc_timeout_claim[0]);
926 let bs_timeout_claimable_height = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
927 assert_eq!(sorted_vec(vec![b_received_htlc_balance.clone(), Balance::ClaimableAwaitingConfirmations {
928 claimable_amount_satoshis: 20_000,
929 confirmation_height: bs_timeout_claimable_height,
931 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
933 mine_transaction(&nodes[1], &as_htlc_timeout_claim[0]);
934 assert_eq!(sorted_vec(vec![b_received_htlc_balance.clone(), Balance::ClaimableAwaitingConfirmations {
935 claimable_amount_satoshis: 20_000,
936 confirmation_height: bs_timeout_claimable_height,
938 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
940 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 2);
941 expect_payment_failed!(nodes[1], to_a_failed_payment_hash, false);
943 assert_eq!(vec![b_received_htlc_balance.clone()],
944 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
945 test_spendable_output(&nodes[1], &bs_htlc_timeout_claim[0]);
947 connect_blocks(&nodes[1], 1);
948 assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
950 // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
951 // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
952 // monitor events or claimable balances.
953 connect_blocks(&nodes[1], 6);
954 connect_blocks(&nodes[1], 6);
955 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
956 assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
959 fn sorted_vec_with_additions<T: Ord + Clone>(v_orig: &Vec<T>, extra_ts: &[&T]) -> Vec<T> {
960 let mut v = v_orig.clone();
962 v.push((*t).clone());
968 fn do_test_revoked_counterparty_commitment_balances(confirm_htlc_spend_first: bool) {
969 // Tests `get_claimable_balances` for revoked counterparty commitment transactions.
970 let mut chanmon_cfgs = create_chanmon_cfgs(2);
971 // We broadcast a second-to-latest commitment transaction, without providing the revocation
972 // secret to the counterparty. However, because we always immediately take the revocation
973 // secret from the keys_manager, we would panic at broadcast as we're trying to sign a
974 // transaction which, from the point of view of our keys_manager, is revoked.
975 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
976 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
977 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
978 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
980 let (_, _, chan_id, funding_tx) =
981 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 100_000_000);
982 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
983 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
985 // We create five HTLCs for B to claim against A's revoked commitment transaction:
987 // (1) one for which A is the originator and B knows the preimage
988 // (2) one for which B is the originator where the HTLC has since timed-out
989 // (3) one for which B is the originator but where the HTLC has not yet timed-out
990 // (4) one dust HTLC which is lost in the channel closure
991 // (5) one that actually isn't in the revoked commitment transaction at all, but was added in
992 // later commitment transaction updates
994 // Though they could all be claimed in a single claim transaction, due to CLTV timeouts they
995 // are all currently claimed in separate transactions, which helps us test as we can claim
996 // HTLCs individually.
998 let (claimed_payment_preimage, claimed_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 3_000_000);
999 let timeout_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 4_000_000).1;
1000 let dust_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 3_000).1;
1002 let htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
1004 connect_blocks(&nodes[0], 10);
1005 connect_blocks(&nodes[1], 10);
1007 let live_htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
1008 let live_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 5_000_000).1;
1010 // Get the latest commitment transaction from A and then update the fee to revoke it
1011 let as_revoked_txn = get_local_commitment_txn!(nodes[0], chan_id);
1012 let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan_id);
1014 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
1016 let missing_htlc_cltv_timeout = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
1017 let missing_htlc_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 2_000_000).1;
1019 nodes[1].node.claim_funds(claimed_payment_preimage);
1020 expect_payment_claimed!(nodes[1], claimed_payment_hash, 3_000_000);
1021 check_added_monitors!(nodes[1], 1);
1022 let _b_htlc_msgs = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
1024 connect_blocks(&nodes[0], htlc_cltv_timeout + 1 - 10);
1025 check_closed_broadcast!(nodes[0], true);
1026 check_added_monitors!(nodes[0], 1);
1028 let mut events = nodes[0].node.get_and_clear_pending_events();
1029 assert_eq!(events.len(), 6);
1030 let mut failed_payments: HashSet<_> =
1031 [timeout_payment_hash, dust_payment_hash, live_payment_hash, missing_htlc_payment_hash]
1032 .iter().map(|a| *a).collect();
1033 events.retain(|ev| {
1035 Event::HTLCHandlingFailed { failed_next_destination: HTLCDestination::NextHopChannel { node_id, channel_id }, .. } => {
1036 assert_eq!(*channel_id, chan_id);
1037 assert_eq!(*node_id, Some(nodes[1].node.get_our_node_id()));
1040 Event::HTLCHandlingFailed { failed_next_destination: HTLCDestination::FailedPayment { payment_hash }, .. } => {
1041 assert!(failed_payments.remove(payment_hash));
1047 assert!(failed_payments.is_empty());
1048 if let Event::PendingHTLCsForwardable { .. } = events[0] {} else { panic!(); }
1050 Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {},
1054 connect_blocks(&nodes[1], htlc_cltv_timeout + 1 - 10);
1055 check_closed_broadcast!(nodes[1], true);
1056 check_added_monitors!(nodes[1], 1);
1057 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
1059 // Prior to channel closure, B considers the preimage HTLC as its own, and otherwise only
1060 // lists the two on-chain timeout-able HTLCs as claimable balances.
1061 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
1062 claimable_amount_satoshis: 100_000 - 5_000 - 4_000 - 3 - 2_000 + 3_000,
1063 }, Balance::MaybeTimeoutClaimableHTLC {
1064 claimable_amount_satoshis: 2_000,
1065 claimable_height: missing_htlc_cltv_timeout,
1066 payment_hash: missing_htlc_payment_hash,
1067 }, Balance::MaybeTimeoutClaimableHTLC {
1068 claimable_amount_satoshis: 4_000,
1069 claimable_height: htlc_cltv_timeout,
1070 payment_hash: timeout_payment_hash,
1071 }, Balance::MaybeTimeoutClaimableHTLC {
1072 claimable_amount_satoshis: 5_000,
1073 claimable_height: live_htlc_cltv_timeout,
1074 payment_hash: live_payment_hash,
1076 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1078 mine_transaction(&nodes[1], &as_revoked_txn[0]);
1079 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();
1080 // Currently the revoked commitment is claimed in four transactions as the HTLCs all expire
1082 assert_eq!(claim_txn.len(), 4);
1083 claim_txn.sort_unstable_by_key(|tx| tx.output.iter().map(|output| output.value).sum::<u64>());
1085 // The following constants were determined experimentally
1086 const BS_TO_SELF_CLAIM_EXP_WEIGHT: usize = 483;
1087 const OUTBOUND_HTLC_CLAIM_EXP_WEIGHT: usize = 571;
1088 const INBOUND_HTLC_CLAIM_EXP_WEIGHT: usize = 578;
1090 // Check that the weight is close to the expected weight. Note that signature sizes vary
1091 // somewhat so it may not always be exact.
1092 fuzzy_assert_eq(claim_txn[0].weight(), OUTBOUND_HTLC_CLAIM_EXP_WEIGHT);
1093 fuzzy_assert_eq(claim_txn[1].weight(), INBOUND_HTLC_CLAIM_EXP_WEIGHT);
1094 fuzzy_assert_eq(claim_txn[2].weight(), INBOUND_HTLC_CLAIM_EXP_WEIGHT);
1095 fuzzy_assert_eq(claim_txn[3].weight(), BS_TO_SELF_CLAIM_EXP_WEIGHT);
1097 // The expected balance for the next three checks, with the largest-HTLC and to_self output
1098 // claim balances separated out.
1099 let expected_balance = vec![Balance::ClaimableAwaitingConfirmations {
1100 // to_remote output in A's revoked commitment
1101 claimable_amount_satoshis: 100_000 - 5_000 - 4_000 - 3,
1102 confirmation_height: nodes[1].best_block_info().1 + 5,
1103 }, Balance::CounterpartyRevokedOutputClaimable {
1104 claimable_amount_satoshis: 3_000,
1105 }, Balance::CounterpartyRevokedOutputClaimable {
1106 claimable_amount_satoshis: 4_000,
1109 let to_self_unclaimed_balance = Balance::CounterpartyRevokedOutputClaimable {
1110 claimable_amount_satoshis: 1_000_000 - 100_000 - 3_000 - chan_feerate *
1111 (channel::commitment_tx_base_weight(&channel_type_features) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1113 let to_self_claimed_avail_height;
1114 let largest_htlc_unclaimed_balance = Balance::CounterpartyRevokedOutputClaimable {
1115 claimable_amount_satoshis: 5_000,
1117 let largest_htlc_claimed_avail_height;
1119 // Once the channel has been closed by A, B now considers all of the commitment transactions'
1120 // outputs as `CounterpartyRevokedOutputClaimable`.
1121 assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_unclaimed_balance, &largest_htlc_unclaimed_balance]),
1122 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1124 if confirm_htlc_spend_first {
1125 mine_transaction(&nodes[1], &claim_txn[2]);
1126 largest_htlc_claimed_avail_height = nodes[1].best_block_info().1 + 5;
1127 to_self_claimed_avail_height = nodes[1].best_block_info().1 + 6; // will be claimed in the next block
1129 // Connect the to_self output claim, taking all of A's non-HTLC funds
1130 mine_transaction(&nodes[1], &claim_txn[3]);
1131 to_self_claimed_avail_height = nodes[1].best_block_info().1 + 5;
1132 largest_htlc_claimed_avail_height = nodes[1].best_block_info().1 + 6; // will be claimed in the next block
1135 let largest_htlc_claimed_balance = Balance::ClaimableAwaitingConfirmations {
1136 claimable_amount_satoshis: 5_000 - chan_feerate * INBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
1137 confirmation_height: largest_htlc_claimed_avail_height,
1139 let to_self_claimed_balance = Balance::ClaimableAwaitingConfirmations {
1140 claimable_amount_satoshis: 1_000_000 - 100_000 - 3_000 - chan_feerate *
1141 (channel::commitment_tx_base_weight(&channel_type_features) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
1142 - chan_feerate * claim_txn[3].weight() as u64 / 1000,
1143 confirmation_height: to_self_claimed_avail_height,
1146 if confirm_htlc_spend_first {
1147 assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_unclaimed_balance, &largest_htlc_claimed_balance]),
1148 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1150 assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_claimed_balance, &largest_htlc_unclaimed_balance]),
1151 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1154 if confirm_htlc_spend_first {
1155 mine_transaction(&nodes[1], &claim_txn[3]);
1157 mine_transaction(&nodes[1], &claim_txn[2]);
1159 assert_eq!(sorted_vec_with_additions(&expected_balance, &[&to_self_claimed_balance, &largest_htlc_claimed_balance]),
1160 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1162 // Finally, connect the last two remaining HTLC spends and check that they move to
1163 // `ClaimableAwaitingConfirmations`
1164 mine_transaction(&nodes[1], &claim_txn[0]);
1165 mine_transaction(&nodes[1], &claim_txn[1]);
1167 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1168 // to_remote output in A's revoked commitment
1169 claimable_amount_satoshis: 100_000 - 5_000 - 4_000 - 3,
1170 confirmation_height: nodes[1].best_block_info().1 + 1,
1171 }, Balance::ClaimableAwaitingConfirmations {
1172 claimable_amount_satoshis: 1_000_000 - 100_000 - 3_000 - chan_feerate *
1173 (channel::commitment_tx_base_weight(&channel_type_features) + 3 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
1174 - chan_feerate * claim_txn[3].weight() as u64 / 1000,
1175 confirmation_height: to_self_claimed_avail_height,
1176 }, Balance::ClaimableAwaitingConfirmations {
1177 claimable_amount_satoshis: 3_000 - chan_feerate * OUTBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
1178 confirmation_height: nodes[1].best_block_info().1 + 4,
1179 }, Balance::ClaimableAwaitingConfirmations {
1180 claimable_amount_satoshis: 4_000 - chan_feerate * INBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
1181 confirmation_height: nodes[1].best_block_info().1 + 5,
1182 }, Balance::ClaimableAwaitingConfirmations {
1183 claimable_amount_satoshis: 5_000 - chan_feerate * INBOUND_HTLC_CLAIM_EXP_WEIGHT as u64 / 1000,
1184 confirmation_height: largest_htlc_claimed_avail_height,
1186 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1188 connect_blocks(&nodes[1], 1);
1189 test_spendable_output(&nodes[1], &as_revoked_txn[0]);
1191 let mut payment_failed_events = nodes[1].node.get_and_clear_pending_events();
1192 expect_payment_failed_conditions_event(payment_failed_events[..2].to_vec(),
1193 missing_htlc_payment_hash, false, PaymentFailedConditions::new());
1194 expect_payment_failed_conditions_event(payment_failed_events[2..].to_vec(),
1195 dust_payment_hash, false, PaymentFailedConditions::new());
1197 connect_blocks(&nodes[1], 1);
1198 test_spendable_output(&nodes[1], &claim_txn[if confirm_htlc_spend_first { 2 } else { 3 }]);
1199 connect_blocks(&nodes[1], 1);
1200 test_spendable_output(&nodes[1], &claim_txn[if confirm_htlc_spend_first { 3 } else { 2 }]);
1201 expect_payment_failed!(nodes[1], live_payment_hash, false);
1202 connect_blocks(&nodes[1], 1);
1203 test_spendable_output(&nodes[1], &claim_txn[0]);
1204 connect_blocks(&nodes[1], 1);
1205 test_spendable_output(&nodes[1], &claim_txn[1]);
1206 expect_payment_failed!(nodes[1], timeout_payment_hash, false);
1207 assert_eq!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances(), Vec::new());
1209 // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
1210 // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
1211 // monitor events or claimable balances.
1212 connect_blocks(&nodes[1], 6);
1213 connect_blocks(&nodes[1], 6);
1214 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1215 assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1219 fn test_revoked_counterparty_commitment_balances() {
1220 do_test_revoked_counterparty_commitment_balances(true);
1221 do_test_revoked_counterparty_commitment_balances(false);
1225 fn test_revoked_counterparty_htlc_tx_balances() {
1226 // Tests `get_claimable_balances` for revocation spends of HTLC transactions.
1227 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1228 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
1229 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1230 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1231 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1233 // Create some initial channels
1234 let (_, _, chan_id, funding_tx) =
1235 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 11_000_000);
1236 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
1237 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
1239 let payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 3_000_000).0;
1240 let failed_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 1_000_000).1;
1241 let revoked_local_txn = get_local_commitment_txn!(nodes[1], chan_id);
1242 assert_eq!(revoked_local_txn[0].input.len(), 1);
1243 assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, funding_tx.txid());
1245 // The to-be-revoked commitment tx should have two HTLCs and an output for both sides
1246 assert_eq!(revoked_local_txn[0].output.len(), 4);
1248 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
1250 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
1251 let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan_id);
1253 // B will generate an HTLC-Success from its revoked commitment tx
1254 mine_transaction(&nodes[1], &revoked_local_txn[0]);
1255 check_closed_broadcast!(nodes[1], true);
1256 check_added_monitors!(nodes[1], 1);
1257 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
1258 let revoked_htlc_success = {
1259 let mut txn = nodes[1].tx_broadcaster.txn_broadcast();
1260 assert_eq!(txn.len(), 1);
1261 assert_eq!(txn[0].input.len(), 1);
1262 assert_eq!(txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
1263 check_spends!(txn[0], revoked_local_txn[0]);
1267 connect_blocks(&nodes[1], TEST_FINAL_CLTV);
1268 let revoked_htlc_timeout = {
1269 let mut txn = nodes[1].tx_broadcaster.unique_txn_broadcast();
1270 assert_eq!(txn.len(), 2);
1271 if txn[0].input[0].previous_output == revoked_htlc_success.input[0].previous_output {
1277 check_spends!(revoked_htlc_timeout, revoked_local_txn[0]);
1278 assert_ne!(revoked_htlc_success.input[0].previous_output, revoked_htlc_timeout.input[0].previous_output);
1279 assert_eq!(revoked_htlc_success.lock_time.0, 0);
1280 assert_ne!(revoked_htlc_timeout.lock_time.0, 0);
1282 // A will generate justice tx from B's revoked commitment/HTLC tx
1283 mine_transaction(&nodes[0], &revoked_local_txn[0]);
1284 check_closed_broadcast!(nodes[0], true);
1285 check_added_monitors!(nodes[0], 1);
1286 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
1287 let to_remote_conf_height = nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1;
1289 let as_commitment_claim_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1290 assert_eq!(as_commitment_claim_txn.len(), 1);
1291 check_spends!(as_commitment_claim_txn[0], revoked_local_txn[0]);
1293 // The next two checks have the same balance set for A - even though we confirm a revoked HTLC
1294 // transaction our balance tracking doesn't use the on-chain value so the
1295 // `CounterpartyRevokedOutputClaimable` entry doesn't change.
1296 let as_balances = sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1297 // to_remote output in B's revoked commitment
1298 claimable_amount_satoshis: 1_000_000 - 11_000 - 3_000 - chan_feerate *
1299 (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1300 confirmation_height: to_remote_conf_height,
1301 }, Balance::CounterpartyRevokedOutputClaimable {
1302 // to_self output in B's revoked commitment
1303 claimable_amount_satoshis: 10_000,
1304 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1305 claimable_amount_satoshis: 3_000,
1306 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1307 claimable_amount_satoshis: 1_000,
1309 assert_eq!(as_balances,
1310 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1312 mine_transaction(&nodes[0], &revoked_htlc_success);
1313 let as_htlc_claim_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1314 assert_eq!(as_htlc_claim_tx.len(), 2);
1315 check_spends!(as_htlc_claim_tx[0], revoked_htlc_success);
1316 check_spends!(as_htlc_claim_tx[1], revoked_local_txn[0]); // A has to generate a new claim for the remaining revoked
1317 // outputs (which no longer includes the spent HTLC output)
1319 assert_eq!(as_balances,
1320 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1322 assert_eq!(as_htlc_claim_tx[0].output.len(), 1);
1323 fuzzy_assert_eq(as_htlc_claim_tx[0].output[0].value,
1324 3_000 - chan_feerate * (revoked_htlc_success.weight() + as_htlc_claim_tx[0].weight()) as u64 / 1000);
1326 mine_transaction(&nodes[0], &as_htlc_claim_tx[0]);
1327 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1328 // to_remote output in B's revoked commitment
1329 claimable_amount_satoshis: 1_000_000 - 11_000 - 3_000 - chan_feerate *
1330 (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1331 confirmation_height: to_remote_conf_height,
1332 }, Balance::CounterpartyRevokedOutputClaimable {
1333 // to_self output in B's revoked commitment
1334 claimable_amount_satoshis: 10_000,
1335 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1336 claimable_amount_satoshis: 1_000,
1337 }, Balance::ClaimableAwaitingConfirmations {
1338 claimable_amount_satoshis: as_htlc_claim_tx[0].output[0].value,
1339 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
1341 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1343 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 3);
1344 test_spendable_output(&nodes[0], &revoked_local_txn[0]);
1345 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1346 // to_self output to B
1347 claimable_amount_satoshis: 10_000,
1348 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1349 claimable_amount_satoshis: 1_000,
1350 }, Balance::ClaimableAwaitingConfirmations {
1351 claimable_amount_satoshis: as_htlc_claim_tx[0].output[0].value,
1352 confirmation_height: nodes[0].best_block_info().1 + 2,
1354 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1356 connect_blocks(&nodes[0], 2);
1357 test_spendable_output(&nodes[0], &as_htlc_claim_tx[0]);
1358 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1359 // to_self output in B's revoked commitment
1360 claimable_amount_satoshis: 10_000,
1361 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1362 claimable_amount_satoshis: 1_000,
1364 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1366 connect_blocks(&nodes[0], revoked_htlc_timeout.lock_time.0 - nodes[0].best_block_info().1);
1367 expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(&nodes[0],
1368 [HTLCDestination::FailedPayment { payment_hash: failed_payment_hash }]);
1369 // As time goes on A may split its revocation claim transaction into multiple.
1370 let as_fewer_input_rbf = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1371 for tx in as_fewer_input_rbf.iter() {
1372 check_spends!(tx, revoked_local_txn[0]);
1375 // Connect a number of additional blocks to ensure we don't forget the HTLC output needs
1377 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
1378 let as_fewer_input_rbf = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1379 for tx in as_fewer_input_rbf.iter() {
1380 check_spends!(tx, revoked_local_txn[0]);
1383 mine_transaction(&nodes[0], &revoked_htlc_timeout);
1384 let as_second_htlc_claim_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
1385 assert_eq!(as_second_htlc_claim_tx.len(), 2);
1387 check_spends!(as_second_htlc_claim_tx[0], revoked_htlc_timeout);
1388 check_spends!(as_second_htlc_claim_tx[1], revoked_local_txn[0]);
1390 // Connect blocks to finalize the HTLC resolution with the HTLC-Timeout transaction. In a
1391 // previous iteration of the revoked balance handling this would result in us "forgetting" that
1392 // the revoked HTLC output still needed to be claimed.
1393 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
1394 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1395 // to_self output in B's revoked commitment
1396 claimable_amount_satoshis: 10_000,
1397 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1398 claimable_amount_satoshis: 1_000,
1400 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1402 mine_transaction(&nodes[0], &as_second_htlc_claim_tx[0]);
1403 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1404 // to_self output in B's revoked commitment
1405 claimable_amount_satoshis: 10_000,
1406 }, Balance::ClaimableAwaitingConfirmations {
1407 claimable_amount_satoshis: as_second_htlc_claim_tx[0].output[0].value,
1408 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
1410 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1412 mine_transaction(&nodes[0], &as_second_htlc_claim_tx[1]);
1413 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1414 // to_self output in B's revoked commitment
1415 claimable_amount_satoshis: as_second_htlc_claim_tx[1].output[0].value,
1416 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
1417 }, Balance::ClaimableAwaitingConfirmations {
1418 claimable_amount_satoshis: as_second_htlc_claim_tx[0].output[0].value,
1419 confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 2,
1421 sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1423 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 2);
1424 test_spendable_output(&nodes[0], &as_second_htlc_claim_tx[0]);
1425 connect_blocks(&nodes[0], 1);
1426 test_spendable_output(&nodes[0], &as_second_htlc_claim_tx[1]);
1428 assert_eq!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances(), Vec::new());
1430 // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
1431 // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
1432 // monitor events or claimable balances.
1433 connect_blocks(&nodes[0], 6);
1434 connect_blocks(&nodes[0], 6);
1435 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1436 assert!(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1440 fn test_revoked_counterparty_aggregated_claims() {
1441 // Tests `get_claimable_balances` for revoked counterparty commitment transactions when
1442 // claiming with an aggregated claim transaction.
1443 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1444 // We broadcast a second-to-latest commitment transaction, without providing the revocation
1445 // secret to the counterparty. However, because we always immediately take the revocation
1446 // secret from the keys_manager, we would panic at broadcast as we're trying to sign a
1447 // transaction which, from the point of view of our keys_manager, is revoked.
1448 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
1449 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1450 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1451 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1453 let (_, _, chan_id, funding_tx) =
1454 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 100_000_000);
1455 let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 };
1456 assert_eq!(funding_outpoint.to_channel_id(), chan_id);
1458 // We create two HTLCs, one which we will give A the preimage to to generate an HTLC-Success
1459 // transaction, and one which we will not, allowing B to claim the HTLC output in an aggregated
1460 // revocation-claim transaction.
1462 let (claimed_payment_preimage, claimed_payment_hash, ..) = route_payment(&nodes[1], &[&nodes[0]], 3_000_000);
1463 let revoked_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 4_000_000).1;
1465 let htlc_cltv_timeout = nodes[1].best_block_info().1 + TEST_FINAL_CLTV + 1; // Note ChannelManager adds one to CLTV timeouts for safety
1467 // Cheat by giving A's ChannelMonitor the preimage to the to-be-claimed HTLC so that we have an
1468 // HTLC-claim transaction on the to-be-revoked state.
1469 get_monitor!(nodes[0], chan_id).provide_payment_preimage(&claimed_payment_hash, &claimed_payment_preimage,
1470 &node_cfgs[0].tx_broadcaster, &LowerBoundedFeeEstimator::new(node_cfgs[0].fee_estimator), &nodes[0].logger);
1472 // Now get the latest commitment transaction from A and then update the fee to revoke it
1473 let as_revoked_txn = get_local_commitment_txn!(nodes[0], chan_id);
1475 assert_eq!(as_revoked_txn.len(), 2);
1476 check_spends!(as_revoked_txn[0], funding_tx);
1477 check_spends!(as_revoked_txn[1], as_revoked_txn[0]); // The HTLC-Claim transaction
1479 let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan_id);
1480 let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
1483 let mut feerate = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
1486 nodes[0].node.timer_tick_occurred();
1487 check_added_monitors!(nodes[0], 1);
1489 let fee_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1490 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &fee_update.update_fee.unwrap());
1491 commitment_signed_dance!(nodes[1], nodes[0], fee_update.commitment_signed, false);
1493 nodes[0].node.claim_funds(claimed_payment_preimage);
1494 expect_payment_claimed!(nodes[0], claimed_payment_hash, 3_000_000);
1495 check_added_monitors!(nodes[0], 1);
1496 let _a_htlc_msgs = get_htlc_update_msgs!(&nodes[0], nodes[1].node.get_our_node_id());
1498 assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
1499 claimable_amount_satoshis: 100_000 - 4_000 - 3_000,
1500 }, Balance::MaybeTimeoutClaimableHTLC {
1501 claimable_amount_satoshis: 4_000,
1502 claimable_height: htlc_cltv_timeout,
1503 payment_hash: revoked_payment_hash,
1504 }, Balance::MaybeTimeoutClaimableHTLC {
1505 claimable_amount_satoshis: 3_000,
1506 claimable_height: htlc_cltv_timeout,
1507 payment_hash: claimed_payment_hash,
1509 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1511 mine_transaction(&nodes[1], &as_revoked_txn[0]);
1512 check_closed_broadcast!(nodes[1], true);
1513 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
1514 check_added_monitors!(nodes[1], 1);
1516 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();
1517 // Currently the revoked commitment outputs are all claimed in one aggregated transaction
1518 assert_eq!(claim_txn.len(), 1);
1519 assert_eq!(claim_txn[0].input.len(), 3);
1520 check_spends!(claim_txn[0], as_revoked_txn[0]);
1522 let to_remote_maturity = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
1524 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1525 // to_remote output in A's revoked commitment
1526 claimable_amount_satoshis: 100_000 - 4_000 - 3_000,
1527 confirmation_height: to_remote_maturity,
1528 }, Balance::CounterpartyRevokedOutputClaimable {
1529 // to_self output in A's revoked commitment
1530 claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1531 (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1532 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1533 claimable_amount_satoshis: 4_000,
1534 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1535 claimable_amount_satoshis: 3_000,
1537 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1539 // Confirm A's HTLC-Success tranasction which presumably raced B's claim, causing B to create a
1541 mine_transaction(&nodes[1], &as_revoked_txn[1]);
1542 expect_payment_sent!(nodes[1], claimed_payment_preimage);
1543 let mut claim_txn_2: Vec<_> = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
1544 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 });
1545 // Once B sees the HTLC-Success transaction it splits its claim transaction into two, though in
1546 // theory it could re-aggregate the claims as well.
1547 assert_eq!(claim_txn_2.len(), 2);
1548 assert_eq!(claim_txn_2[0].input.len(), 2);
1549 check_spends!(claim_txn_2[0], as_revoked_txn[0]);
1550 assert_eq!(claim_txn_2[1].input.len(), 1);
1551 check_spends!(claim_txn_2[1], as_revoked_txn[1]);
1553 assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
1554 // to_remote output in A's revoked commitment
1555 claimable_amount_satoshis: 100_000 - 4_000 - 3_000,
1556 confirmation_height: to_remote_maturity,
1557 }, Balance::CounterpartyRevokedOutputClaimable {
1558 // to_self output in A's revoked commitment
1559 claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1560 (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1561 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1562 claimable_amount_satoshis: 4_000,
1563 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1564 // The amount here is a bit of a misnomer, really its been reduced by the HTLC
1565 // transaction fee, but the claimable amount is always a bit of an overshoot for HTLCs
1566 // anyway, so its not a big change.
1567 claimable_amount_satoshis: 3_000,
1569 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1571 connect_blocks(&nodes[1], 5);
1572 test_spendable_output(&nodes[1], &as_revoked_txn[0]);
1574 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1575 // to_self output in A's revoked commitment
1576 claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1577 (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1578 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1579 claimable_amount_satoshis: 4_000,
1580 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 2
1581 // The amount here is a bit of a misnomer, really its been reduced by the HTLC
1582 // transaction fee, but the claimable amount is always a bit of an overshoot for HTLCs
1583 // anyway, so its not a big change.
1584 claimable_amount_satoshis: 3_000,
1586 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1588 mine_transaction(&nodes[1], &claim_txn_2[1]);
1589 let htlc_2_claim_maturity = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
1591 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1592 // to_self output in A's revoked commitment
1593 claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1594 (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1595 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1596 claimable_amount_satoshis: 4_000,
1597 }, Balance::ClaimableAwaitingConfirmations { // HTLC 2
1598 claimable_amount_satoshis: claim_txn_2[1].output[0].value,
1599 confirmation_height: htlc_2_claim_maturity,
1601 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1603 connect_blocks(&nodes[1], 5);
1604 test_spendable_output(&nodes[1], &claim_txn_2[1]);
1606 assert_eq!(sorted_vec(vec![Balance::CounterpartyRevokedOutputClaimable {
1607 // to_self output in A's revoked commitment
1608 claimable_amount_satoshis: 1_000_000 - 100_000 - chan_feerate *
1609 (channel::commitment_tx_base_weight(&channel_type_features) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
1610 }, Balance::CounterpartyRevokedOutputClaimable { // HTLC 1
1611 claimable_amount_satoshis: 4_000,
1613 sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
1615 mine_transaction(&nodes[1], &claim_txn_2[0]);
1616 let rest_claim_maturity = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
1618 assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
1619 claimable_amount_satoshis: claim_txn_2[0].output[0].value,
1620 confirmation_height: rest_claim_maturity,
1622 nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
1624 assert!(nodes[1].node.get_and_clear_pending_events().is_empty()); // We shouldn't fail the payment until we spend the output
1626 connect_blocks(&nodes[1], 5);
1627 expect_payment_failed!(nodes[1], revoked_payment_hash, false);
1628 test_spendable_output(&nodes[1], &claim_txn_2[0]);
1629 assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1631 // Ensure that even if we connect more blocks, potentially replaying the entire chain if we're
1632 // using `ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks`, we don't get new
1633 // monitor events or claimable balances.
1634 connect_blocks(&nodes[1], 6);
1635 connect_blocks(&nodes[1], 6);
1636 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1637 assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
1640 fn do_test_restored_packages_retry(check_old_monitor_retries_after_upgrade: bool) {
1641 // Tests that we'll retry packages that were previously timelocked after we've restored them.
1643 let new_chain_monitor;
1644 let node_deserialized;
1646 let chanmon_cfgs = create_chanmon_cfgs(2);
1647 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1648 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1649 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1651 // Open a channel, lock in an HTLC, and immediately broadcast the commitment transaction. This
1652 // ensures that the HTLC timeout package is held until we reach its expiration height.
1653 let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 50_000_000);
1654 route_payment(&nodes[0], &[&nodes[1]], 10_000_000);
1656 nodes[0].node.force_close_broadcasting_latest_txn(&chan_id, &nodes[1].node.get_our_node_id()).unwrap();
1657 check_added_monitors(&nodes[0], 1);
1658 check_closed_broadcast(&nodes[0], 1, true);
1659 check_closed_event(&nodes[0], 1, ClosureReason::HolderForceClosed, false);
1661 let commitment_tx = {
1662 let mut txn = nodes[0].tx_broadcaster.txn_broadcast();
1663 assert_eq!(txn.len(), 1);
1664 assert_eq!(txn[0].output.len(), 3);
1665 check_spends!(txn[0], funding_tx);
1669 mine_transaction(&nodes[0], &commitment_tx);
1671 // Connect blocks until the HTLC's expiration is met, expecting a transaction broadcast.
1672 connect_blocks(&nodes[0], TEST_FINAL_CLTV);
1673 let htlc_timeout_tx = {
1674 let mut txn = nodes[0].tx_broadcaster.txn_broadcast();
1675 assert_eq!(txn.len(), 1);
1676 check_spends!(txn[0], commitment_tx);
1680 // Check that we can still rebroadcast these packages/transactions if we're upgrading from an
1681 // old `ChannelMonitor` that did not exercise said rebroadcasting logic.
1682 if check_old_monitor_retries_after_upgrade {
1683 let serialized_monitor = hex::decode(
1684 "0101fffffffffffffffff9550f22c95100160014d5a9aa98b89acc215fc3d23d6fec0ad59ca3665f00002200204c5f18e5e95b184f34d02ba6de8a2a4e36ae3d4ec87299ad81f3284dc7195c6302d7dde8e10a5a22c9bd0d7ef5494d85683ac050253b917615d4f97af633f0a8e2035f5e9d58b4328566223c107d86cf853e6b9fae1d26ff6d969be0178d1423c4ea0016001467822698d782e8421ebdf96d010de99382b7ec2300160014caf6d80fe2bab80473b021f57588a9c384bf23170000000000000000000000004d49e5da0000000000000000000000000000002a0270b20ad0f2c2bb30a55590fc77778495bc1b38c96476901145dda57491237f0f74c52ab4f11296d62b66a6dba9513b04a3e7fb5a09a30cee22fce7294ab55b7e00000022002034c0cc0ad0dd5fe61dcf7ef58f995e3d34f8dbd24aa2a6fae68fefe102bf025c21391732ce658e1fe167300bb689a81e7db5399b9ee4095e217b0e997e8dd3d17a0000000000000000004a002103adde8029d3ee281a32e9db929b39f503ff9d7e93cd308eb157955344dc6def84022103205087e2dc1f6b9937e887dfa712c5bdfa950b01dbda3ebac4c85efdde48ee6a04020090004752210307a78def56cba9fc4db22a25928181de538ee59ba1a475ae113af7790acd0db32103c21e841cbc0b48197d060c71e116c185fa0ac281b7d0aa5924f535154437ca3b52ae00000000000186a0ffffffffffff0291e7c0a3232fb8650a6b4089568a81062b48a768780e5a74bb4a4a74e33aec2c029d5760248ec86c4a76d9df8308555785a06a65472fb995f5b392d520bbd000650090c1c94b11625690c9d84c5daa67b6ad19fcc7f9f23e194384140b08fcab9e8e810000ffffffffffff000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000002167c86cc0e598a6b541f7c9bf9ef17222e4a76f636e2d22185aeadd2b02d029c0000000000000000391732ce658e1fe167300bb689a81e7db5399b9ee4095e217b0e997e8dd3d17a00000000000000010000000000009896800000005166687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f29250500000000a0009d00202d704fbfe342a9ff6eaca14d80a24aaed0e680bbbdd36157b6f2798c61d906910120f9fe5e552aa0fc45020f0505efde432a4e373e5d393863973a6899f8c26d33d102080000000000989680044d4c00210355f8d2238a322d16b602bd0ceaad5b01019fb055971eaadcc9b29226a4da6c2302090007000000000241000408000001000000000006020000080800000000009896800a04000000460000000000000000000000000000000166687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925fffffffffffe01e3002004f8eda5676356f539169a8e9a1e86c7f125283328d6f4bded1b939b52a6a7e30108000000000000c299022103a1f98e85886df54add6908b4fc1ff515e44aedefe9eb9c02879c89994298fa79042103a650bf03971df0176c7b412247390ef717853e8bd487b204dccc2fe2078bb75206210390bbbcebe9f70ba5dfd98866a79f72f75e0a6ea550ef73b202dd87cd6477350a08210284152d57908488e666e872716a286eb670b3d06cbeebf3f2e4ad350e01ec5e5b0a2102295e2de39eb3dcc2882f8cc266df7882a8b6d2c32aa08799f49b693aad3be28e0c04000000fd0e00fd0202002045cfd42d0989e55b953f516ac7fd152bd90ec4438a2fc636f97ddd32a0c8fe0d01080000000000009b5e0221035f5e9d58b4328566223c107d86cf853e6b9fae1d26ff6d969be0178d1423c4ea04210230fde9c031f487db95ff55b7c0acbe0c7c26a8d82615e9184416bd350101616706210225afb4e88eac8b47b67adeaf085f5eb5d37d936f56138f0848de3d104edf113208210208e4687a95c172b86b920c3bc5dbd5f023094ec2cb0abdb74f9b624f45740df90a2102d7dde8e10a5a22c9bd0d7ef5494d85683ac050253b917615d4f97af633f0a8e20c04000000fd0efd011d3b00010102080000000000989680040400000051062066687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925080400000000417e2650c201383711eed2a7cb8652c3e77ee6a395e81849c5c222217ed68b333c0ca9f1e900662ae68a7359efa7ef9d90613f2a62f7c3ff90f8c25e2cc974c9d3a0009d00202d704fbfe342a9ff6eaca14d80a24aaed0e680bbbdd36157b6f2798c61d906910120f9fe5e552aa0fc45020f0505efde432a4e373e5d393863973a6899f8c26d33d102080000000000989680044d4c00210355f8d2238a322d16b602bd0ceaad5b01019fb055971eaadcc9b29226a4da6c2302090007000000000241000408000001000000000006020000080800000000009896800a0400000046fffffffffffefffffffffffe000000000000000000000000000000000000000000000000f1600ef6ea657b8d411d553516ae35cedfe86b0cd48d1f91b32772facbae757d0000000b0000000000000002fd01da002045cfd42d0989e55b953f516ac7fd152bd90ec4438a2fc636f97ddd32a0c8fe0d01fd01840200000000010174c52ab4f11296d62b66a6dba9513b04a3e7fb5a09a30cee22fce7294ab55b7e00000000000f55f9800310270000000000002200208309b406e3b96e76cde414fbb8f5159f5b25b24075656c6382cec797854d53495e9b0000000000002200204c5f18e5e95b184f34d02ba6de8a2a4e36ae3d4ec87299ad81f3284dc7195c6350c300000000000016001425df8ec4a074f80579fed67d4707d5ec8ed7e8d304004730440220671c9badf26bd3a1ebd2d17020c6be20587d7822530daacc52c28839875eaec602204b575a21729ed27311f6d79fdf6fe8702b0a798f7d842e39ede1b56f249a613401473044022016a0da36f70cbf5d889586af88f238982889dc161462c56557125c7acfcb69e9022036ae10c6cc8cbc3b27d9e9ef6babb556086585bc819f252208bd175286699fdd014752210307a78def56cba9fc4db22a25928181de538ee59ba1a475ae113af7790acd0db32103c21e841cbc0b48197d060c71e116c185fa0ac281b7d0aa5924f535154437ca3b52ae50c9222002040000000b0320f1600ef6ea657b8d411d553516ae35cedfe86b0cd48d1f91b32772facbae757d0406030400020090fd02a1002045cfd42d0989e55b953f516ac7fd152bd90ec4438a2fc636f97ddd32a0c8fe0d01fd01840200000000010174c52ab4f11296d62b66a6dba9513b04a3e7fb5a09a30cee22fce7294ab55b7e00000000000f55f9800310270000000000002200208309b406e3b96e76cde414fbb8f5159f5b25b24075656c6382cec797854d53495e9b0000000000002200204c5f18e5e95b184f34d02ba6de8a2a4e36ae3d4ec87299ad81f3284dc7195c6350c300000000000016001425df8ec4a074f80579fed67d4707d5ec8ed7e8d304004730440220671c9badf26bd3a1ebd2d17020c6be20587d7822530daacc52c28839875eaec602204b575a21729ed27311f6d79fdf6fe8702b0a798f7d842e39ede1b56f249a613401473044022016a0da36f70cbf5d889586af88f238982889dc161462c56557125c7acfcb69e9022036ae10c6cc8cbc3b27d9e9ef6babb556086585bc819f252208bd175286699fdd014752210307a78def56cba9fc4db22a25928181de538ee59ba1a475ae113af7790acd0db32103c21e841cbc0b48197d060c71e116c185fa0ac281b7d0aa5924f535154437ca3b52ae50c9222002040000000b0320f1600ef6ea657b8d411d553516ae35cedfe86b0cd48d1f91b32772facbae757d04cd01cb00c901c7002245cfd42d0989e55b953f516ac7fd152bd90ec4438a2fc636f97ddd32a0c8fe0d0001022102d7dde8e10a5a22c9bd0d7ef5494d85683ac050253b917615d4f97af633f0a8e204020090062b5e9b0000000000002200204c5f18e5e95b184f34d02ba6de8a2a4e36ae3d4ec87299ad81f3284dc7195c630821035f5e9d58b4328566223c107d86cf853e6b9fae1d26ff6d969be0178d1423c4ea0a200000000000000000000000004d49e5da0000000000000000000000000000002a0c0800000000000186a0000000000000000274c52ab4f11296d62b66a6dba9513b04a3e7fb5a09a30cee22fce7294ab55b7e0000000000000001000000000022002034c0cc0ad0dd5fe61dcf7ef58f995e3d34f8dbd24aa2a6fae68fefe102bf025c45cfd42d0989e55b953f516ac7fd152bd90ec4438a2fc636f97ddd32a0c8fe0d000000000000000100000000002200208309b406e3b96e76cde414fbb8f5159f5b25b24075656c6382cec797854d5349010100160014d5a9aa98b89acc215fc3d23d6fec0ad59ca3665ffd027100fd01e6fd01e300080000fffffffffffe02080000000000009b5e0408000000000000c3500604000000fd08b0af002102d7dde8e10a5a22c9bd0d7ef5494d85683ac050253b917615d4f97af633f0a8e20221035f5e9d58b4328566223c107d86cf853e6b9fae1d26ff6d969be0178d1423c4ea04210230fde9c031f487db95ff55b7c0acbe0c7c26a8d82615e9184416bd350101616706210225afb4e88eac8b47b67adeaf085f5eb5d37d936f56138f0848de3d104edf113208210208e4687a95c172b86b920c3bc5dbd5f023094ec2cb0abdb74f9b624f45740df90acdcc00a8020000000174c52ab4f11296d62b66a6dba9513b04a3e7fb5a09a30cee22fce7294ab55b7e00000000000f55f9800310270000000000002200208309b406e3b96e76cde414fbb8f5159f5b25b24075656c6382cec797854d53495e9b0000000000002200204c5f18e5e95b184f34d02ba6de8a2a4e36ae3d4ec87299ad81f3284dc7195c6350c300000000000016001425df8ec4a074f80579fed67d4707d5ec8ed7e8d350c92220022045cfd42d0989e55b953f516ac7fd152bd90ec4438a2fc636f97ddd32a0c8fe0d0c3c3b00010102080000000000989680040400000051062066687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f29250804000000000240671c9badf26bd3a1ebd2d17020c6be20587d7822530daacc52c28839875eaec64b575a21729ed27311f6d79fdf6fe8702b0a798f7d842e39ede1b56f249a613404010006407e2650c201383711eed2a7cb8652c3e77ee6a395e81849c5c222217ed68b333c0ca9f1e900662ae68a7359efa7ef9d90613f2a62f7c3ff90f8c25e2cc974c9d3010000000000000001010000000000000000090b2a953d93a124c600ecb1a0ccfed420169cdd37f538ad94a3e4e6318c93c14adf59cdfbb40bdd40950c9f8dd547d29d75a173e1376a7850743394c46dea2dfd01cefd01ca00fd017ffd017c00080000ffffffffffff0208000000000000c2990408000000000000c3500604000000fd08b0af002102295e2de39eb3dcc2882f8cc266df7882a8b6d2c32aa08799f49b693aad3be28e022103a1f98e85886df54add6908b4fc1ff515e44aedefe9eb9c02879c89994298fa79042103a650bf03971df0176c7b412247390ef717853e8bd487b204dccc2fe2078bb75206210390bbbcebe9f70ba5dfd98866a79f72f75e0a6ea550ef73b202dd87cd6477350a08210284152d57908488e666e872716a286eb670b3d06cbeebf3f2e4ad350e01ec5e5b0aa2a1007d020000000174c52ab4f11296d62b66a6dba9513b04a3e7fb5a09a30cee22fce7294ab55b7e00000000000f55f9800299c2000000000000220020740e108cfbc93967b6ab242a351ebee7de51814cf78d366adefd78b10281f17e50c300000000000016001425df8ec4a074f80579fed67d4707d5ec8ed7e8d351c92220022004f8eda5676356f539169a8e9a1e86c7f125283328d6f4bded1b939b52a6a7e30c00024045cb2485594bb1ec08e7bb6af4f89c912bd53f006d7876ea956773e04a4aad4a40e2b8d4fc612102f0b54061b3c1239fb78783053e8e6f9d92b1b99f81ae9ec2040100060000fd019600b0af002103c21e841cbc0b48197d060c71e116c185fa0ac281b7d0aa5924f535154437ca3b02210270b20ad0f2c2bb30a55590fc77778495bc1b38c96476901145dda57491237f0f042103b4e59df102747edc3a3e2283b42b88a8c8218ffd0dcfb52f2524b371d64cadaa062103d902b7b8b3434076d2b210e912c76645048b71e28995aad227a465a65ccd817608210301e9a52f923c157941de4a7692e601f758660969dcf5abdb67817efe84cce2ef0202009004010106b7b600b0af00210307a78def56cba9fc4db22a25928181de538ee59ba1a475ae113af7790acd0db30221034d0f817cb19b4a3bd144b615459bd06cbab3b4bdc96d73e18549a992cee80e8104210380542b59a9679890cba529fe155a9508ef57dac7416d035b23666e3fb98c3814062103adde8029d3ee281a32e9db929b39f503ff9d7e93cd308eb157955344dc6def84082103205087e2dc1f6b9937e887dfa712c5bdfa950b01dbda3ebac4c85efdde48ee6a02020090082274c52ab4f11296d62b66a6dba9513b04a3e7fb5a09a30cee22fce7294ab55b7e000000000287010108d30df34e3a1e00ecdd03a2c843db062479a81752c4dfd0cc4baef0f81e7bc7ef8820990daf8d8e8d30a3b4b08af12c9f5cd71e45c7238103e0c80ca13850862e4fd2c56b69b7195312518de1bfe9aed63c80bb7760d70b2a870d542d815895fd12423d11e2adb0cdf55d776dac8f487c9b3b7ea12f1b150eb15889cf41333ade465692bf1cdc360b9c2a19bf8c1ca4fed7639d8bc953d36c10d8c6c9a8c0a57608788979bcf145e61b308006896e21d03e92084f93bd78740c20639134a7a8fd019afd019600b0af002103c21e841cbc0b48197d060c71e116c185fa0ac281b7d0aa5924f535154437ca3b02210270b20ad0f2c2bb30a55590fc77778495bc1b38c96476901145dda57491237f0f042103b4e59df102747edc3a3e2283b42b88a8c8218ffd0dcfb52f2524b371d64cadaa062103d902b7b8b3434076d2b210e912c76645048b71e28995aad227a465a65ccd817608210301e9a52f923c157941de4a7692e601f758660969dcf5abdb67817efe84cce2ef0202009004010106b7b600b0af00210307a78def56cba9fc4db22a25928181de538ee59ba1a475ae113af7790acd0db30221034d0f817cb19b4a3bd144b615459bd06cbab3b4bdc96d73e18549a992cee80e8104210380542b59a9679890cba529fe155a9508ef57dac7416d035b23666e3fb98c3814062103adde8029d3ee281a32e9db929b39f503ff9d7e93cd308eb157955344dc6def84082103205087e2dc1f6b9937e887dfa712c5bdfa950b01dbda3ebac4c85efdde48ee6a02020090082274c52ab4f11296d62b66a6dba9513b04a3e7fb5a09a30cee22fce7294ab55b7e000000000000000186a00000000000000000000000004d49e5da0000000000000000000000000000002a00000000000000000000000000000000000000000000000001000000510000000000000001000000000000000145cfd42d0989e55b953f516ac7fd152bd90ec4438a2fc636f97ddd32a0c8fe0d00000000041000080000000000989680020400000051160004000000510208000000000000000004040000000b0000000000000000000101300300050007010109210355f8d2238a322d16b602bd0ceaad5b01019fb055971eaadcc9b29226a4da6c230d000f020000",
1686 reload_node!(nodes[0], &nodes[0].node.encode(), &[&serialized_monitor], persister, new_chain_monitor, node_deserialized);
1689 // Connecting more blocks should result in the HTLC transactions being rebroadcast.
1690 connect_blocks(&nodes[0], 6);
1691 if check_old_monitor_retries_after_upgrade {
1692 check_added_monitors(&nodes[0], 1);
1695 let txn = nodes[0].tx_broadcaster.txn_broadcast();
1696 if !nodes[0].connect_style.borrow().skips_blocks() {
1697 assert_eq!(txn.len(), 6);
1699 assert!(txn.len() < 6);
1702 assert_eq!(tx.input.len(), htlc_timeout_tx.input.len());
1703 assert_eq!(tx.output.len(), htlc_timeout_tx.output.len());
1704 assert_eq!(tx.input[0].previous_output, htlc_timeout_tx.input[0].previous_output);
1705 assert_eq!(tx.output[0], htlc_timeout_tx.output[0]);
1711 fn test_restored_packages_retry() {
1712 do_test_restored_packages_retry(false);
1713 do_test_restored_packages_retry(true);
1716 fn do_test_monitor_rebroadcast_pending_claims(anchors: bool) {
1717 // Test that we will retry broadcasting pending claims for a force-closed channel on every
1718 // `ChainMonitor::rebroadcast_pending_claims` call.
1719 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1720 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1721 let mut config = test_default_channel_config();
1723 config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
1724 config.manually_accept_inbound_channels = true;
1726 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(config), Some(config)]);
1727 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1729 let (_, _, _, chan_id, funding_tx) = create_chan_between_nodes_with_value(
1730 &nodes[0], &nodes[1], 1_000_000, 500_000_000
1732 const HTLC_AMT_MSAT: u64 = 1_000_000;
1733 const HTLC_AMT_SAT: u64 = HTLC_AMT_MSAT / 1000;
1734 route_payment(&nodes[0], &[&nodes[1]], HTLC_AMT_MSAT);
1736 let htlc_expiry = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1;
1738 let commitment_txn = get_local_commitment_txn!(&nodes[0], &chan_id);
1739 assert_eq!(commitment_txn.len(), if anchors { 1 /* commitment tx only */} else { 2 /* commitment and htlc timeout tx */ });
1740 check_spends!(&commitment_txn[0], &funding_tx);
1741 mine_transaction(&nodes[0], &commitment_txn[0]);
1742 check_closed_broadcast!(&nodes[0], true);
1743 check_closed_event(&nodes[0], 1, ClosureReason::CommitmentTxConfirmed, false);
1744 check_added_monitors(&nodes[0], 1);
1746 // Set up a helper closure we'll use throughout our test. We should only expect retries without
1747 // bumps if fees have not increased after a block has been connected (assuming the height timer
1748 // re-evaluates at every block) or after `ChainMonitor::rebroadcast_pending_claims` is called.
1749 let mut prev_htlc_tx_feerate = None;
1750 let mut check_htlc_retry = |should_retry: bool, should_bump: bool| -> Option<Transaction> {
1751 let (htlc_tx, htlc_tx_feerate) = if anchors {
1752 assert!(nodes[0].tx_broadcaster.txn_broadcast().is_empty());
1753 let mut events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
1754 assert_eq!(events.len(), if should_retry { 1 } else { 0 });
1758 #[allow(unused_assignments)]
1759 let mut tx = Transaction {
1761 lock_time: bitcoin::PackedLockTime::ZERO,
1765 #[allow(unused_assignments)]
1766 let mut feerate = 0;
1767 feerate = if let Event::BumpTransaction(BumpTransactionEvent::HTLCResolution {
1768 target_feerate_sat_per_1000_weight, mut htlc_descriptors, tx_lock_time, ..
1769 }) = events.pop().unwrap() {
1770 let secp = Secp256k1::new();
1771 assert_eq!(htlc_descriptors.len(), 1);
1772 let descriptor = htlc_descriptors.pop().unwrap();
1773 assert_eq!(descriptor.commitment_txid, commitment_txn[0].txid());
1774 let htlc_output_idx = descriptor.htlc.transaction_output_index.unwrap() as usize;
1775 assert!(htlc_output_idx < commitment_txn[0].output.len());
1776 tx.lock_time = tx_lock_time;
1777 // Note that we don't care about actually making the HTLC transaction meet the
1778 // feerate for the test, we just want to make sure the feerates we receive from
1779 // the events never decrease.
1780 tx.input.push(descriptor.unsigned_tx_input());
1781 tx.output.push(descriptor.tx_output(&secp));
1782 let signer = descriptor.derive_channel_signer(&nodes[0].keys_manager);
1783 let our_sig = signer.sign_holder_htlc_transaction(&mut tx, 0, &descriptor, &secp).unwrap();
1784 let witness_script = descriptor.witness_script(&secp);
1785 tx.input[0].witness = descriptor.tx_input_witness(&our_sig, &witness_script);
1786 target_feerate_sat_per_1000_weight as u64
1787 } else { panic!("unexpected event"); };
1790 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1791 let mut txn = nodes[0].tx_broadcaster.txn_broadcast();
1792 assert_eq!(txn.len(), if should_retry { 1 } else { 0 });
1796 let htlc_tx = txn.pop().unwrap();
1797 check_spends!(htlc_tx, commitment_txn[0]);
1798 let htlc_tx_fee = HTLC_AMT_SAT - htlc_tx.output[0].value;
1799 let htlc_tx_feerate = htlc_tx_fee * 1000 / htlc_tx.weight() as u64;
1800 (htlc_tx, htlc_tx_feerate)
1803 assert!(htlc_tx_feerate > prev_htlc_tx_feerate.take().unwrap());
1804 } else if let Some(prev_feerate) = prev_htlc_tx_feerate.take() {
1805 assert_eq!(htlc_tx_feerate, prev_feerate);
1807 prev_htlc_tx_feerate = Some(htlc_tx_feerate);
1811 // Connect blocks up to one before the HTLC expires. This should not result in a claim/retry.
1812 connect_blocks(&nodes[0], htlc_expiry - nodes[0].best_block_info().1 - 1);
1813 check_htlc_retry(false, false);
1815 // Connect one more block, producing our first claim.
1816 connect_blocks(&nodes[0], 1);
1817 check_htlc_retry(true, false);
1819 // Connect one more block, expecting a retry with a fee bump. Unfortunately, we cannot bump HTLC
1820 // transactions pre-anchors.
1821 connect_blocks(&nodes[0], 1);
1822 check_htlc_retry(true, anchors);
1824 // Trigger a call and we should have another retry, but without a bump.
1825 nodes[0].chain_monitor.chain_monitor.rebroadcast_pending_claims();
1826 check_htlc_retry(true, false);
1828 // Double the feerate and trigger a call, expecting a fee-bumped retry.
1829 *nodes[0].fee_estimator.sat_per_kw.lock().unwrap() *= 2;
1830 nodes[0].chain_monitor.chain_monitor.rebroadcast_pending_claims();
1831 check_htlc_retry(true, anchors);
1833 // Connect one more block, expecting a retry with a fee bump. Unfortunately, we cannot bump HTLC
1834 // transactions pre-anchors.
1835 connect_blocks(&nodes[0], 1);
1836 let htlc_tx = check_htlc_retry(true, anchors).unwrap();
1838 // Mine the HTLC transaction to ensure we don't retry claims while they're confirmed.
1839 mine_transaction(&nodes[0], &htlc_tx);
1840 // If we have a `ConnectStyle` that advertises the new block first without the transasctions,
1841 // we'll receive an extra bumped claim.
1842 if nodes[0].connect_style.borrow().updates_best_block_first() {
1843 check_htlc_retry(true, anchors);
1845 nodes[0].chain_monitor.chain_monitor.rebroadcast_pending_claims();
1846 check_htlc_retry(false, false);
1850 fn test_monitor_timer_based_claim() {
1851 do_test_monitor_rebroadcast_pending_claims(false);
1852 do_test_monitor_rebroadcast_pending_claims(true);
1856 fn test_yield_anchors_events() {
1857 // Tests that two parties supporting anchor outputs can open a channel, route payments over
1858 // it, and finalize its resolution uncooperatively. Once the HTLCs are locked in, one side will
1859 // force close once the HTLCs expire. The force close should stem from an event emitted by LDK,
1860 // allowing the consumer to provide additional fees to the commitment transaction to be
1861 // broadcast. Once the commitment transaction confirms, events for the HTLC resolution should be
1862 // emitted by LDK, such that the consumer can attach fees to the zero fee HTLC transactions.
1863 let secp = Secp256k1::new();
1864 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1865 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1866 let mut anchors_config = UserConfig::default();
1867 anchors_config.channel_handshake_config.announced_channel = true;
1868 anchors_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
1869 anchors_config.manually_accept_inbound_channels = true;
1870 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(anchors_config), Some(anchors_config)]);
1871 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1873 let chan_id = create_announced_chan_between_nodes_with_value(
1874 &nodes, 0, 1, 1_000_000, 500_000_000
1876 route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
1877 let (payment_preimage, payment_hash, _) = route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1879 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
1881 *nodes[0].fee_estimator.sat_per_kw.lock().unwrap() *= 2;
1882 connect_blocks(&nodes[0], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1);
1883 check_closed_broadcast!(&nodes[0], true);
1884 assert!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().is_empty());
1886 get_monitor!(nodes[0], chan_id).provide_payment_preimage(
1887 &payment_hash, &payment_preimage, &node_cfgs[0].tx_broadcaster,
1888 &LowerBoundedFeeEstimator::new(node_cfgs[0].fee_estimator), &nodes[0].logger
1891 let mut holder_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
1892 assert_eq!(holder_events.len(), 1);
1893 let (commitment_tx, anchor_tx) = match holder_events.pop().unwrap() {
1894 Event::BumpTransaction(BumpTransactionEvent::ChannelClose { commitment_tx, anchor_descriptor, .. }) => {
1895 assert_eq!(commitment_tx.input.len(), 1);
1896 assert_eq!(commitment_tx.output.len(), 6);
1897 let mut anchor_tx = Transaction {
1899 lock_time: PackedLockTime::ZERO,
1901 TxIn { previous_output: anchor_descriptor.outpoint, ..Default::default() },
1902 TxIn { ..Default::default() },
1904 output: vec![TxOut {
1905 value: Amount::ONE_BTC.to_sat(),
1906 script_pubkey: Script::new_op_return(&[]),
1909 let signer = anchor_descriptor.derive_channel_signer(&nodes[0].keys_manager);
1910 let funding_sig = signer.sign_holder_anchor_input(&mut anchor_tx, 0, &secp).unwrap();
1911 anchor_tx.input[0].witness = chan_utils::build_anchor_input_witness(
1912 &signer.pubkeys().funding_pubkey, &funding_sig
1914 (commitment_tx, anchor_tx)
1916 _ => panic!("Unexpected event"),
1919 mine_transactions(&nodes[0], &[&commitment_tx, &anchor_tx]);
1920 check_added_monitors!(nodes[0], 1);
1922 let mut holder_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
1923 // Certain block `ConnectStyle`s cause an extra `ChannelClose` event to be emitted since the
1924 // best block is updated before the confirmed transactions are notified.
1925 match *nodes[0].connect_style.borrow() {
1926 ConnectStyle::BestBlockFirst|ConnectStyle::BestBlockFirstReorgsOnlyTip|ConnectStyle::BestBlockFirstSkippingBlocks => {
1927 assert_eq!(holder_events.len(), 3);
1928 if let Event::BumpTransaction(BumpTransactionEvent::ChannelClose { .. }) = holder_events.remove(0) {}
1929 else { panic!("unexpected event"); }
1932 _ => assert_eq!(holder_events.len(), 2),
1934 let mut htlc_txs = Vec::with_capacity(2);
1935 for event in holder_events {
1937 Event::BumpTransaction(BumpTransactionEvent::HTLCResolution { htlc_descriptors, tx_lock_time, .. }) => {
1938 assert_eq!(htlc_descriptors.len(), 1);
1939 let htlc_descriptor = &htlc_descriptors[0];
1940 let mut htlc_tx = Transaction {
1942 lock_time: tx_lock_time,
1944 htlc_descriptor.unsigned_tx_input(), // HTLC input
1945 TxIn { ..Default::default() } // Fee input
1948 htlc_descriptor.tx_output(&secp), // HTLC output
1949 TxOut { // Fee input change
1950 value: Amount::ONE_BTC.to_sat(),
1951 script_pubkey: Script::new_op_return(&[]),
1955 let signer = htlc_descriptor.derive_channel_signer(&nodes[0].keys_manager);
1956 let our_sig = signer.sign_holder_htlc_transaction(&mut htlc_tx, 0, htlc_descriptor, &secp).unwrap();
1957 let witness_script = htlc_descriptor.witness_script(&secp);
1958 htlc_tx.input[0].witness = htlc_descriptor.tx_input_witness(&our_sig, &witness_script);
1959 htlc_txs.push(htlc_tx);
1961 _ => panic!("Unexpected event"),
1965 mine_transactions(&nodes[0], &[&htlc_txs[0], &htlc_txs[1]]);
1966 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
1968 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1970 connect_blocks(&nodes[0], BREAKDOWN_TIMEOUT as u32);
1972 let holder_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
1973 assert_eq!(holder_events.len(), 3);
1974 for event in holder_events {
1976 Event::SpendableOutputs { .. } => {},
1977 _ => panic!("Unexpected event"),
1981 // Clear the remaining events as they're not relevant to what we're testing.
1982 nodes[0].node.get_and_clear_pending_events();
1986 fn test_anchors_aggregated_revoked_htlc_tx() {
1987 // Test that `ChannelMonitor`s can properly detect and claim funds from a counterparty claiming
1988 // multiple HTLCs from multiple channels in a single transaction via the success path from a
1989 // revoked commitment.
1990 let secp = Secp256k1::new();
1991 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1992 // Required to sign a revoked commitment transaction
1993 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
1994 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1995 let mut anchors_config = UserConfig::default();
1996 anchors_config.channel_handshake_config.announced_channel = true;
1997 anchors_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
1998 anchors_config.manually_accept_inbound_channels = true;
1999 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(anchors_config), Some(anchors_config)]);
2001 let bob_persister: test_utils::TestPersister;
2002 let bob_chain_monitor: test_utils::TestChainMonitor;
2003 let bob_deserialized: ChannelManager<
2004 &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface,
2005 &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator,
2006 &test_utils::TestRouter, &test_utils::TestLogger,
2009 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2011 let chan_a = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 20_000_000);
2012 let chan_b = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 20_000_000);
2014 // Serialize Bob with the initial state of both channels, which we'll use later.
2015 let bob_serialized = nodes[1].node.encode();
2017 // Route two payments for each channel from Alice to Bob to lock in the HTLCs.
2018 let payment_a = route_payment(&nodes[0], &[&nodes[1]], 50_000_000);
2019 let payment_b = route_payment(&nodes[0], &[&nodes[1]], 50_000_000);
2020 let payment_c = route_payment(&nodes[0], &[&nodes[1]], 50_000_000);
2021 let payment_d = route_payment(&nodes[0], &[&nodes[1]], 50_000_000);
2023 // Serialize Bob's monitors with the HTLCs locked in. We'll restart Bob later on with the state
2024 // at this point such that he broadcasts a revoked commitment transaction with the HTLCs
2026 let bob_serialized_monitor_a = get_monitor!(nodes[1], chan_a.2).encode();
2027 let bob_serialized_monitor_b = get_monitor!(nodes[1], chan_b.2).encode();
2029 // Bob claims all the HTLCs...
2030 claim_payment(&nodes[0], &[&nodes[1]], payment_a.0);
2031 claim_payment(&nodes[0], &[&nodes[1]], payment_b.0);
2032 claim_payment(&nodes[0], &[&nodes[1]], payment_c.0);
2033 claim_payment(&nodes[0], &[&nodes[1]], payment_d.0);
2035 // ...and sends one back through each channel such that he has a motive to broadcast his
2037 send_payment(&nodes[1], &[&nodes[0]], 30_000_000);
2038 send_payment(&nodes[1], &[&nodes[0]], 30_000_000);
2040 // Restart Bob with the revoked state and provide the HTLC preimages he claimed.
2042 nodes[1], anchors_config, bob_serialized, &[&bob_serialized_monitor_a, &bob_serialized_monitor_b],
2043 bob_persister, bob_chain_monitor, bob_deserialized
2045 for chan_id in [chan_a.2, chan_b.2].iter() {
2046 let monitor = get_monitor!(nodes[1], chan_id);
2047 for payment in [payment_a, payment_b, payment_c, payment_d].iter() {
2048 monitor.provide_payment_preimage(
2049 &payment.1, &payment.0, &node_cfgs[1].tx_broadcaster,
2050 &LowerBoundedFeeEstimator::new(node_cfgs[1].fee_estimator), &nodes[1].logger
2055 // Bob force closes by restarting with the outdated state, prompting the ChannelMonitors to
2056 // broadcast the latest commitment transaction known to them, which in our case is the one with
2057 // the HTLCs still pending.
2058 *nodes[1].fee_estimator.sat_per_kw.lock().unwrap() *= 2;
2059 nodes[1].node.timer_tick_occurred();
2060 check_added_monitors(&nodes[1], 2);
2061 check_closed_event!(&nodes[1], 2, ClosureReason::OutdatedChannelManager);
2062 let (revoked_commitment_a, revoked_commitment_b) = {
2063 let txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
2064 assert_eq!(txn.len(), 2);
2065 assert_eq!(txn[0].output.len(), 6); // 2 HTLC outputs + 1 to_self output + 1 to_remote output + 2 anchor outputs
2066 assert_eq!(txn[1].output.len(), 6); // 2 HTLC outputs + 1 to_self output + 1 to_remote output + 2 anchor outputs
2067 if txn[0].input[0].previous_output.txid == chan_a.3.txid() {
2068 check_spends!(&txn[0], &chan_a.3);
2069 check_spends!(&txn[1], &chan_b.3);
2070 (txn[0].clone(), txn[1].clone())
2072 check_spends!(&txn[1], &chan_a.3);
2073 check_spends!(&txn[0], &chan_b.3);
2074 (txn[1].clone(), txn[0].clone())
2078 // Bob should now receive two events to bump his revoked commitment transaction fees.
2079 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
2080 let events = nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events();
2081 assert_eq!(events.len(), 2);
2083 let secret_key = SecretKey::from_slice(&[1; 32]).unwrap();
2084 let public_key = PublicKey::new(secret_key.public_key(&secp));
2085 let fee_utxo_script = Script::new_v0_p2wpkh(&public_key.wpubkey_hash().unwrap());
2086 let coinbase_tx = Transaction {
2088 lock_time: PackedLockTime::ZERO,
2089 input: vec![TxIn { ..Default::default() }],
2090 output: vec![TxOut { // UTXO to attach fees to `anchor_tx`
2091 value: Amount::ONE_BTC.to_sat(),
2092 script_pubkey: fee_utxo_script.clone(),
2095 let mut anchor_tx = Transaction {
2097 lock_time: PackedLockTime::ZERO,
2100 previous_output: bitcoin::OutPoint { txid: coinbase_tx.txid(), vout: 0 },
2101 ..Default::default()
2104 output: vec![TxOut { // Fee input change
2105 value: coinbase_tx.output[0].value / 2 ,
2106 script_pubkey: Script::new_op_return(&[]),
2109 let mut signers = Vec::with_capacity(2);
2110 for event in events {
2112 Event::BumpTransaction(BumpTransactionEvent::ChannelClose { anchor_descriptor, .. }) => {
2113 anchor_tx.input.push(TxIn {
2114 previous_output: anchor_descriptor.outpoint,
2115 ..Default::default()
2117 let signer = anchor_descriptor.derive_channel_signer(&nodes[1].keys_manager);
2118 signers.push(signer);
2120 _ => panic!("Unexpected event"),
2123 for (i, signer) in signers.into_iter().enumerate() {
2124 let anchor_idx = i + 1;
2125 let funding_sig = signer.sign_holder_anchor_input(&mut anchor_tx, anchor_idx, &secp).unwrap();
2126 anchor_tx.input[anchor_idx].witness = chan_utils::build_anchor_input_witness(
2127 &signer.pubkeys().funding_pubkey, &funding_sig
2130 let fee_utxo_sig = {
2131 let witness_script = Script::new_p2pkh(&public_key.pubkey_hash());
2132 let sighash = hash_to_message!(&SighashCache::new(&anchor_tx).segwit_signature_hash(
2133 0, &witness_script, coinbase_tx.output[0].value, EcdsaSighashType::All
2135 let sig = sign(&secp, &sighash, &secret_key);
2136 let mut sig = sig.serialize_der().to_vec();
2137 sig.push(EcdsaSighashType::All as u8);
2140 anchor_tx.input[0].witness = Witness::from_vec(vec![fee_utxo_sig, public_key.to_bytes()]);
2141 check_spends!(anchor_tx, coinbase_tx, revoked_commitment_a, revoked_commitment_b);
2145 for node in &nodes {
2146 mine_transactions(node, &[&revoked_commitment_a, &revoked_commitment_b, &anchor_tx]);
2148 check_added_monitors!(&nodes[0], 2);
2149 check_closed_broadcast(&nodes[0], 2, true);
2150 check_closed_event!(&nodes[0], 2, ClosureReason::CommitmentTxConfirmed);
2152 // Alice should detect the confirmed revoked commitments, and attempt to claim all of the
2155 let txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
2156 assert_eq!(txn.len(), 4);
2158 let (revoked_htlc_claim_a, revoked_htlc_claim_b) = if txn[0].input[0].previous_output.txid == revoked_commitment_a.txid() {
2159 (if txn[0].input.len() == 2 { &txn[0] } else { &txn[1] }, if txn[2].input.len() == 2 { &txn[2] } else { &txn[3] })
2161 (if txn[2].input.len() == 2 { &txn[2] } else { &txn[3] }, if txn[0].input.len() == 2 { &txn[0] } else { &txn[1] })
2164 assert_eq!(revoked_htlc_claim_a.input.len(), 2); // Spends both HTLC outputs
2165 assert_eq!(revoked_htlc_claim_a.output.len(), 1);
2166 check_spends!(revoked_htlc_claim_a, revoked_commitment_a);
2167 assert_eq!(revoked_htlc_claim_b.input.len(), 2); // Spends both HTLC outputs
2168 assert_eq!(revoked_htlc_claim_b.output.len(), 1);
2169 check_spends!(revoked_htlc_claim_b, revoked_commitment_b);
2172 // Since Bob was able to confirm his revoked commitment, he'll now try to claim the HTLCs
2173 // through the success path.
2174 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
2175 let mut events = nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events();
2176 // Certain block `ConnectStyle`s cause an extra `ChannelClose` event to be emitted since the
2177 // best block is updated before the confirmed transactions are notified.
2178 match *nodes[1].connect_style.borrow() {
2179 ConnectStyle::BestBlockFirst|ConnectStyle::BestBlockFirstReorgsOnlyTip|ConnectStyle::BestBlockFirstSkippingBlocks => {
2180 assert_eq!(events.len(), 4);
2181 if let Event::BumpTransaction(BumpTransactionEvent::ChannelClose { .. }) = events.remove(0) {}
2182 else { panic!("unexpected event"); }
2183 if let Event::BumpTransaction(BumpTransactionEvent::ChannelClose { .. }) = events.remove(1) {}
2184 else { panic!("unexpected event"); }
2187 _ => assert_eq!(events.len(), 2),
2190 let secret_key = SecretKey::from_slice(&[1; 32]).unwrap();
2191 let public_key = PublicKey::new(secret_key.public_key(&secp));
2192 let fee_utxo_script = Script::new_v0_p2wpkh(&public_key.wpubkey_hash().unwrap());
2193 let coinbase_tx = Transaction {
2195 lock_time: PackedLockTime::ZERO,
2196 input: vec![TxIn { ..Default::default() }],
2197 output: vec![TxOut { // UTXO to attach fees to `htlc_tx`
2198 value: Amount::ONE_BTC.to_sat(),
2199 script_pubkey: fee_utxo_script.clone(),
2202 let mut htlc_tx = Transaction {
2204 lock_time: PackedLockTime::ZERO,
2205 input: vec![TxIn { // Fee input
2206 previous_output: bitcoin::OutPoint { txid: coinbase_tx.txid(), vout: 0 },
2207 ..Default::default()
2209 output: vec![TxOut { // Fee input change
2210 value: coinbase_tx.output[0].value / 2 ,
2211 script_pubkey: Script::new_op_return(&[]),
2214 let mut descriptors = Vec::with_capacity(4);
2215 for event in events {
2216 if let Event::BumpTransaction(BumpTransactionEvent::HTLCResolution { mut htlc_descriptors, tx_lock_time, .. }) = event {
2217 assert_eq!(htlc_descriptors.len(), 2);
2218 for htlc_descriptor in &htlc_descriptors {
2219 assert!(!htlc_descriptor.htlc.offered);
2220 htlc_tx.input.push(htlc_descriptor.unsigned_tx_input());
2221 htlc_tx.output.push(htlc_descriptor.tx_output(&secp));
2223 descriptors.append(&mut htlc_descriptors);
2224 htlc_tx.lock_time = tx_lock_time;
2226 panic!("Unexpected event");
2229 for (idx, htlc_descriptor) in descriptors.into_iter().enumerate() {
2230 let htlc_input_idx = idx + 1;
2231 let signer = htlc_descriptor.derive_channel_signer(&nodes[1].keys_manager);
2232 let our_sig = signer.sign_holder_htlc_transaction(&htlc_tx, htlc_input_idx, &htlc_descriptor, &secp).unwrap();
2233 let witness_script = htlc_descriptor.witness_script(&secp);
2234 htlc_tx.input[htlc_input_idx].witness = htlc_descriptor.tx_input_witness(&our_sig, &witness_script);
2236 let fee_utxo_sig = {
2237 let witness_script = Script::new_p2pkh(&public_key.pubkey_hash());
2238 let sighash = hash_to_message!(&SighashCache::new(&htlc_tx).segwit_signature_hash(
2239 0, &witness_script, coinbase_tx.output[0].value, EcdsaSighashType::All
2241 let sig = sign(&secp, &sighash, &secret_key);
2242 let mut sig = sig.serialize_der().to_vec();
2243 sig.push(EcdsaSighashType::All as u8);
2246 htlc_tx.input[0].witness = Witness::from_vec(vec![fee_utxo_sig, public_key.to_bytes()]);
2247 check_spends!(htlc_tx, coinbase_tx, revoked_commitment_a, revoked_commitment_b);
2251 for node in &nodes {
2252 mine_transaction(node, &htlc_tx);
2255 // Alice should see that Bob is trying to claim to HTLCs, so she should now try to claim them at
2256 // the second level instead.
2257 let revoked_claims = {
2258 let txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
2259 assert_eq!(txn.len(), 2);
2261 let revoked_htlc_claims = txn.iter().filter(|tx|
2262 tx.input.len() == 2 &&
2263 tx.output.len() == 1 &&
2264 tx.input[0].previous_output.txid == htlc_tx.txid()
2265 ).collect::<Vec<_>>();
2266 assert_eq!(revoked_htlc_claims.len(), 2);
2267 for revoked_htlc_claim in revoked_htlc_claims {
2268 check_spends!(revoked_htlc_claim, htlc_tx);
2273 for node in &nodes {
2274 mine_transactions(node, &revoked_claims.iter().collect::<Vec<_>>());
2278 // Connect one block to make sure the HTLC events are not yielded while ANTI_REORG_DELAY has not
2280 connect_blocks(&nodes[0], 1);
2281 connect_blocks(&nodes[1], 1);
2283 assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
2284 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
2286 // Connect the remaining blocks to reach ANTI_REORG_DELAY.
2287 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 2);
2288 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 2);
2290 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
2291 let spendable_output_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
2292 assert_eq!(spendable_output_events.len(), 2);
2293 for (idx, event) in spendable_output_events.iter().enumerate() {
2294 if let Event::SpendableOutputs { outputs } = event {
2295 assert_eq!(outputs.len(), 1);
2296 let spend_tx = nodes[0].keys_manager.backing.spend_spendable_outputs(
2297 &[&outputs[0]], Vec::new(), Script::new_op_return(&[]), 253, None, &Secp256k1::new(),
2299 check_spends!(spend_tx, revoked_claims[idx]);
2301 panic!("unexpected event");
2305 assert!(nodes[0].node.list_channels().is_empty());
2306 assert!(nodes[1].node.list_channels().is_empty());
2307 // On the Alice side, the individual to_self_claim are still pending confirmation.
2308 assert_eq!(nodes[0].chain_monitor.chain_monitor.get_claimable_balances(&[]).len(), 2);
2309 // TODO: From Bob's PoV, he still thinks he can claim the outputs from his revoked commitment.
2310 // This needs to be fixed before we enable pruning `ChannelMonitor`s once they don't have any
2311 // balances to claim.
2313 // The 6 claimable balances correspond to his `to_self` outputs and the 2 HTLC outputs in each
2314 // revoked commitment which Bob has the preimage for.
2315 assert_eq!(nodes[1].chain_monitor.chain_monitor.get_claimable_balances(&[]).len(), 6);