+fn do_test_monitor_rebroadcast_pending_claims(anchors: bool) {
+ // Test that we will retry broadcasting pending claims for a force-closed channel on every
+ // `ChainMonitor::rebroadcast_pending_claims` call.
+ let mut chanmon_cfgs = create_chanmon_cfgs(2);
+ let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+ let mut config = test_default_channel_config();
+ if anchors {
+ config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
+ config.manually_accept_inbound_channels = true;
+ }
+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(config), Some(config)]);
+ let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+ let (_, _, _, chan_id, funding_tx) = create_chan_between_nodes_with_value(
+ &nodes[0], &nodes[1], 1_000_000, 500_000_000
+ );
+ const HTLC_AMT_MSAT: u64 = 1_000_000;
+ const HTLC_AMT_SAT: u64 = HTLC_AMT_MSAT / 1000;
+ route_payment(&nodes[0], &[&nodes[1]], HTLC_AMT_MSAT);
+
+ let htlc_expiry = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1;
+
+ let commitment_txn = get_local_commitment_txn!(&nodes[0], &chan_id);
+ assert_eq!(commitment_txn.len(), if anchors { 1 /* commitment tx only */} else { 2 /* commitment and htlc timeout tx */ });
+ check_spends!(&commitment_txn[0], &funding_tx);
+ mine_transaction(&nodes[0], &commitment_txn[0]);
+ check_closed_broadcast!(&nodes[0], true);
+ check_closed_event!(&nodes[0], 1, ClosureReason::CommitmentTxConfirmed,
+ false, [nodes[1].node.get_our_node_id()], 1000000);
+ check_added_monitors(&nodes[0], 1);
+
+ let coinbase_tx = Transaction {
+ version: 2,
+ lock_time: PackedLockTime::ZERO,
+ input: vec![TxIn { ..Default::default() }],
+ output: vec![TxOut { // UTXO to attach fees to `htlc_tx` on anchors
+ value: Amount::ONE_BTC.to_sat(),
+ script_pubkey: nodes[0].wallet_source.get_change_script().unwrap(),
+ }],
+ };
+ nodes[0].wallet_source.add_utxo(bitcoin::OutPoint { txid: coinbase_tx.txid(), vout: 0 }, coinbase_tx.output[0].value);
+
+ // Set up a helper closure we'll use throughout our test. We should only expect retries without
+ // bumps if fees have not increased after a block has been connected (assuming the height timer
+ // re-evaluates at every block) or after `ChainMonitor::rebroadcast_pending_claims` is called.
+ let mut prev_htlc_tx_feerate = None;
+ let mut check_htlc_retry = |should_retry: bool, should_bump: bool| -> Option<Transaction> {
+ let (htlc_tx, htlc_tx_feerate) = if anchors {
+ assert!(nodes[0].tx_broadcaster.txn_broadcast().is_empty());
+ let events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
+ assert_eq!(events.len(), if should_retry { 1 } else { 0 });
+ if !should_retry {
+ return None;
+ }
+ match &events[0] {
+ Event::BumpTransaction(event) => {
+ nodes[0].bump_tx_handler.handle_event(&event);
+ let mut txn = nodes[0].tx_broadcaster.unique_txn_broadcast();
+ assert_eq!(txn.len(), 1);
+ let htlc_tx = txn.pop().unwrap();
+ check_spends!(&htlc_tx, &commitment_txn[0], &coinbase_tx);
+ let htlc_tx_fee = HTLC_AMT_SAT + coinbase_tx.output[0].value -
+ htlc_tx.output.iter().map(|output| output.value).sum::<u64>();
+ let htlc_tx_weight = htlc_tx.weight() as u64;
+ (htlc_tx, compute_feerate_sat_per_1000_weight(htlc_tx_fee, htlc_tx_weight))
+ }
+ _ => panic!("Unexpected event"),
+ }
+ } else {
+ assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
+ let mut txn = nodes[0].tx_broadcaster.txn_broadcast();
+ assert_eq!(txn.len(), if should_retry { 1 } else { 0 });
+ if !should_retry {
+ return None;
+ }
+ let htlc_tx = txn.pop().unwrap();
+ check_spends!(htlc_tx, commitment_txn[0]);
+ let htlc_tx_fee = HTLC_AMT_SAT - htlc_tx.output[0].value;
+ let htlc_tx_weight = htlc_tx.weight() as u64;
+ (htlc_tx, compute_feerate_sat_per_1000_weight(htlc_tx_fee, htlc_tx_weight))
+ };
+ if should_bump {
+ assert!(htlc_tx_feerate > prev_htlc_tx_feerate.take().unwrap());
+ } else if let Some(prev_feerate) = prev_htlc_tx_feerate.take() {
+ assert_eq!(htlc_tx_feerate, prev_feerate);
+ }
+ prev_htlc_tx_feerate = Some(htlc_tx_feerate);
+ Some(htlc_tx)
+ };
+
+ // Connect blocks up to one before the HTLC expires. This should not result in a claim/retry.
+ connect_blocks(&nodes[0], htlc_expiry - nodes[0].best_block_info().1 - 1);
+ check_htlc_retry(false, false);
+
+ // Connect one more block, producing our first claim.
+ connect_blocks(&nodes[0], 1);
+ check_htlc_retry(true, false);
+
+ // Connect one more block, expecting a retry with a fee bump. Unfortunately, we cannot bump HTLC
+ // transactions pre-anchors.
+ connect_blocks(&nodes[0], 1);
+ check_htlc_retry(true, anchors);
+
+ // Trigger a call and we should have another retry, but without a bump.
+ nodes[0].chain_monitor.chain_monitor.rebroadcast_pending_claims();
+ check_htlc_retry(true, false);
+
+ // Double the feerate and trigger a call, expecting a fee-bumped retry.
+ *nodes[0].fee_estimator.sat_per_kw.lock().unwrap() *= 2;
+ nodes[0].chain_monitor.chain_monitor.rebroadcast_pending_claims();
+ check_htlc_retry(true, anchors);
+
+ // Connect one more block, expecting a retry with a fee bump. Unfortunately, we cannot bump HTLC
+ // transactions pre-anchors.
+ connect_blocks(&nodes[0], 1);
+ let htlc_tx = check_htlc_retry(true, anchors).unwrap();
+
+ // Mine the HTLC transaction to ensure we don't retry claims while they're confirmed.
+ mine_transaction(&nodes[0], &htlc_tx);
+ // If we have a `ConnectStyle` that advertises the new block first without the transactions,
+ // we'll receive an extra bumped claim.
+ if nodes[0].connect_style.borrow().updates_best_block_first() {
+ nodes[0].wallet_source.add_utxo(bitcoin::OutPoint { txid: coinbase_tx.txid(), vout: 0 }, coinbase_tx.output[0].value);
+ nodes[0].wallet_source.remove_utxo(bitcoin::OutPoint { txid: htlc_tx.txid(), vout: 1 });
+ check_htlc_retry(true, anchors);
+ }
+ nodes[0].chain_monitor.chain_monitor.rebroadcast_pending_claims();
+ check_htlc_retry(false, false);
+}
+
+#[test]
+fn test_monitor_timer_based_claim() {
+ do_test_monitor_rebroadcast_pending_claims(false);
+ do_test_monitor_rebroadcast_pending_claims(true);
+}
+