Skip to content

Commit 0a452be

Browse files
committed
Test new ConfirmationTarget selection based on HTLC set
This updates `test_yield_anchors_events` to test both anchor channels with and without HTLCs, and relies on overriding only the singular expected `ConfirmationTarget` used, testing the new `ConfirmationTarget::UrgentOnChainSweep` use.
1 parent f99c0be commit 0a452be

File tree

1 file changed

+60
-22
lines changed

1 file changed

+60
-22
lines changed

lightning/src/ln/monitor_tests.rs

+60-22
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use crate::sign::{ecdsa::EcdsaChannelSigner, OutputSpender, SpendableOutputDescriptor};
1313
use crate::chain::channelmonitor::{ANTI_REORG_DELAY, LATENCY_GRACE_PERIOD_BLOCKS, Balance, BalanceSource};
1414
use crate::chain::transaction::OutPoint;
15-
use crate::chain::chaininterface::{LowerBoundedFeeEstimator, compute_feerate_sat_per_1000_weight};
15+
use crate::chain::chaininterface::{ConfirmationTarget, LowerBoundedFeeEstimator, compute_feerate_sat_per_1000_weight};
1616
use crate::events::bump_transaction::{BumpTransactionEvent, WalletSource};
1717
use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination};
1818
use crate::ln::channel;
@@ -2458,8 +2458,7 @@ fn test_monitor_timer_based_claim() {
24582458
do_test_monitor_rebroadcast_pending_claims(true);
24592459
}
24602460

2461-
#[test]
2462-
fn test_yield_anchors_events() {
2461+
fn do_test_yield_anchors_events(have_htlcs: bool) {
24632462
// Tests that two parties supporting anchor outputs can open a channel, route payments over
24642463
// it, and finalize its resolution uncooperatively. Once the HTLCs are locked in, one side will
24652464
// force close once the HTLCs expire. The force close should stem from an event emitted by LDK,
@@ -2478,45 +2477,72 @@ fn test_yield_anchors_events() {
24782477
let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(
24792478
&nodes, 0, 1, 1_000_000, 500_000_000
24802479
);
2481-
let (payment_preimage_1, payment_hash_1, ..) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
2482-
let (payment_preimage_2, payment_hash_2, ..) = route_payment(&nodes[1], &[&nodes[0]], 2_000_000);
2480+
let mut payment_preimage_1 = None;
2481+
let mut payment_hash_1 = None;
2482+
let mut payment_preimage_2 = None;
2483+
let mut payment_hash_2 = None;
2484+
if have_htlcs {
2485+
let (preimage_1, hash_1, ..) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
2486+
let (preimage_2, hash_2, ..) = route_payment(&nodes[1], &[&nodes[0]], 2_000_000);
2487+
payment_preimage_1 = Some(preimage_1);
2488+
payment_hash_1 = Some(hash_1);
2489+
payment_preimage_2 = Some(preimage_2);
2490+
payment_hash_2 = Some(hash_2);
2491+
}
24832492

24842493
assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
24852494
assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
24862495

2487-
*nodes[0].fee_estimator.sat_per_kw.lock().unwrap() *= 2;
2496+
// Note that if we use the wrong target, we will immediately broadcast the commitment
2497+
// transaction as no bump is required.
2498+
if have_htlcs {
2499+
nodes[0].fee_estimator.target_override.lock().unwrap().insert(ConfirmationTarget::UrgentOnChainSweep, 500);
2500+
} else {
2501+
nodes[0].fee_estimator.target_override.lock().unwrap().insert(ConfirmationTarget::OutputSpendingFee, 500);
2502+
}
24882503

24892504
connect_blocks(&nodes[0], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1);
24902505
assert!(nodes[0].tx_broadcaster.txn_broadcast().is_empty());
24912506

24922507
connect_blocks(&nodes[1], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1);
2508+
if !have_htlcs {
2509+
nodes[1].node.force_close_broadcasting_latest_txn(&chan_id, &nodes[0].node.get_our_node_id(), "".to_string()).unwrap();
2510+
}
24932511
{
24942512
let txn = nodes[1].tx_broadcaster.txn_broadcast();
24952513
assert_eq!(txn.len(), 1);
24962514
check_spends!(txn[0], funding_tx);
24972515
}
24982516

2499-
get_monitor!(nodes[0], chan_id).provide_payment_preimage(
2500-
&payment_hash_2, &payment_preimage_2, &node_cfgs[0].tx_broadcaster,
2501-
&LowerBoundedFeeEstimator::new(node_cfgs[0].fee_estimator), &nodes[0].logger
2502-
);
2503-
get_monitor!(nodes[1], chan_id).provide_payment_preimage(
2504-
&payment_hash_1, &payment_preimage_1, &node_cfgs[1].tx_broadcaster,
2505-
&LowerBoundedFeeEstimator::new(node_cfgs[1].fee_estimator), &nodes[1].logger
2506-
);
2517+
if have_htlcs {
2518+
get_monitor!(nodes[0], chan_id).provide_payment_preimage(
2519+
&payment_hash_2.unwrap(), &payment_preimage_2.unwrap(), &node_cfgs[0].tx_broadcaster,
2520+
&LowerBoundedFeeEstimator::new(node_cfgs[0].fee_estimator), &nodes[0].logger
2521+
);
2522+
get_monitor!(nodes[1], chan_id).provide_payment_preimage(
2523+
&payment_hash_1.unwrap(), &payment_preimage_1.unwrap(), &node_cfgs[1].tx_broadcaster,
2524+
&LowerBoundedFeeEstimator::new(node_cfgs[1].fee_estimator), &nodes[1].logger
2525+
);
2526+
} else {
2527+
nodes[0].node.force_close_broadcasting_latest_txn(&chan_id, &nodes[1].node.get_our_node_id(), "".to_string()).unwrap();
2528+
}
25072529

25082530
check_closed_broadcast(&nodes[0], 1, true);
25092531
let a_events = nodes[0].node.get_and_clear_pending_events();
25102532
assert_eq!(a_events.len(), if have_htlcs { 3 } else { 1 });
2511-
assert!(a_events.iter().any(|ev| matches!(ev, Event::PendingHTLCsForwardable { .. })));
2512-
assert!(a_events.iter().any(|ev| matches!(ev, Event::HTLCHandlingFailed { .. })));
2533+
if have_htlcs {
2534+
assert!(a_events.iter().any(|ev| matches!(ev, Event::PendingHTLCsForwardable { .. })));
2535+
assert!(a_events.iter().any(|ev| matches!(ev, Event::HTLCHandlingFailed { .. })));
2536+
}
25132537
assert!(a_events.iter().any(|ev| matches!(ev, Event::ChannelClosed { .. })));
25142538

25152539
check_closed_broadcast(&nodes[1], 1, true);
25162540
let b_events = nodes[1].node.get_and_clear_pending_events();
25172541
assert_eq!(b_events.len(), if have_htlcs { 3 } else { 1 });
2518-
assert!(b_events.iter().any(|ev| matches!(ev, Event::PendingHTLCsForwardable { .. })));
2519-
assert!(b_events.iter().any(|ev| matches!(ev, Event::HTLCHandlingFailed { .. })));
2542+
if have_htlcs {
2543+
assert!(b_events.iter().any(|ev| matches!(ev, Event::PendingHTLCsForwardable { .. })));
2544+
assert!(b_events.iter().any(|ev| matches!(ev, Event::HTLCHandlingFailed { .. })));
2545+
}
25202546
assert!(b_events.iter().any(|ev| matches!(ev, Event::ChannelClosed { .. })));
25212547

25222548
let mut holder_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
@@ -2546,14 +2572,21 @@ fn test_yield_anchors_events() {
25462572
};
25472573
check_spends!(commitment_tx, funding_tx);
25482574

2549-
assert_eq!(commitment_tx.output[2].value.to_sat(), 1_000); // HTLC A -> B
2550-
assert_eq!(commitment_tx.output[3].value.to_sat(), 2_000); // HTLC B -> A
2575+
if have_htlcs {
2576+
assert_eq!(commitment_tx.output[2].value.to_sat(), 1_000); // HTLC A -> B
2577+
assert_eq!(commitment_tx.output[3].value.to_sat(), 2_000); // HTLC B -> A
2578+
}
25512579

25522580
mine_transactions(&nodes[0], &[&commitment_tx, &anchor_tx]);
25532581
check_added_monitors!(nodes[0], 1);
25542582
mine_transactions(&nodes[1], &[&commitment_tx, &anchor_tx]);
25552583
check_added_monitors!(nodes[1], 1);
25562584

2585+
if !have_htlcs {
2586+
// If we don't have any HTLCs, we're done, the rest of the test is about HTLC transactions
2587+
return;
2588+
}
2589+
25572590
{
25582591
let mut txn = nodes[1].tx_broadcaster.unique_txn_broadcast();
25592592
assert_eq!(txn.len(), if nodes[1].connect_style.borrow().updates_best_block_first() { 3 } else { 2 });
@@ -2568,8 +2601,8 @@ fn test_yield_anchors_events() {
25682601
assert_eq!(htlc_timeout_tx.input[0].previous_output.vout, 2);
25692602
check_spends!(htlc_timeout_tx, commitment_tx);
25702603

2571-
if let Some(commitment_tx) = txn.pop() {
2572-
check_spends!(commitment_tx, funding_tx);
2604+
if let Some(new_commitment_tx) = txn.pop() {
2605+
check_spends!(new_commitment_tx, funding_tx);
25732606
}
25742607
}
25752608

@@ -2614,7 +2647,12 @@ fn test_yield_anchors_events() {
26142647
_ => panic!("Unexpected event"),
26152648
}
26162649
}
2650+
}
26172651

2652+
#[test]
2653+
fn test_yield_anchors_events() {
2654+
do_test_yield_anchors_events(true);
2655+
do_test_yield_anchors_events(false);
26182656
}
26192657

26202658
#[test]

0 commit comments

Comments
 (0)