Skip to content

Commit 707d045

Browse files
committed
Rewrite closure check_htlc_retry
Async closures are complicated. Preparatory commit.
1 parent d9ba80f commit 707d045

File tree

1 file changed

+53
-43
lines changed

1 file changed

+53
-43
lines changed

lightning/src/ln/monitor_tests.rs

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2437,38 +2437,81 @@ fn do_test_monitor_rebroadcast_pending_claims(anchors: bool) {
24372437
// bumps if fees have not increased after a block has been connected (assuming the height timer
24382438
// re-evaluates at every block) or after `ChainMonitor::rebroadcast_pending_claims` is called.
24392439
let mut prev_htlc_tx_feerate = None;
2440-
let mut check_htlc_retry = |should_retry: bool, should_bump: bool| -> Option<Transaction> {
2440+
2441+
// Connect blocks up to one before the HTLC expires. This should not result in a claim/retry.
2442+
connect_blocks(&nodes[0], htlc_expiry - nodes[0].best_block_info().1 - 1);
2443+
check_htlc_retry(&nodes[0], anchors, false, false, &commitment_txn, &coinbase_tx, &mut prev_htlc_tx_feerate, HTLC_AMT_SAT);
2444+
2445+
// Connect one more block, producing our first claim.
2446+
connect_blocks(&nodes[0], 1);
2447+
check_htlc_retry(&nodes[0], anchors, true, false, &commitment_txn, &coinbase_tx, &mut prev_htlc_tx_feerate, HTLC_AMT_SAT);
2448+
2449+
// Connect a few more blocks, expecting a retry with a fee bump. Unfortunately, we cannot bump
2450+
// HTLC transactions pre-anchors.
2451+
connect_blocks(&nodes[0], crate::chain::package::LOW_FREQUENCY_BUMP_INTERVAL);
2452+
check_htlc_retry(&nodes[0], anchors, true, anchors, &commitment_txn, &coinbase_tx, &mut prev_htlc_tx_feerate, HTLC_AMT_SAT);
2453+
2454+
// Trigger a call and we should have another retry, but without a bump.
2455+
nodes[0].chain_monitor.chain_monitor.rebroadcast_pending_claims();
2456+
check_htlc_retry(&nodes[0], anchors, true, false, &commitment_txn, &coinbase_tx, &mut prev_htlc_tx_feerate, HTLC_AMT_SAT);
2457+
2458+
// Double the feerate and trigger a call, expecting a fee-bumped retry.
2459+
*nodes[0].fee_estimator.sat_per_kw.lock().unwrap() *= 2;
2460+
nodes[0].chain_monitor.chain_monitor.rebroadcast_pending_claims();
2461+
check_htlc_retry(&nodes[0], anchors, true, anchors, &commitment_txn, &coinbase_tx, &mut prev_htlc_tx_feerate, HTLC_AMT_SAT);
2462+
2463+
// Connect a few more blocks, expecting a retry with a fee bump. Unfortunately, we cannot bump
2464+
// HTLC transactions pre-anchors.
2465+
connect_blocks(&nodes[0], crate::chain::package::LOW_FREQUENCY_BUMP_INTERVAL);
2466+
let htlc_tx = check_htlc_retry(&nodes[0], anchors, true, anchors, &commitment_txn, &coinbase_tx, &mut prev_htlc_tx_feerate, HTLC_AMT_SAT).unwrap();
2467+
2468+
// Mine the HTLC transaction to ensure we don't retry claims while they're confirmed.
2469+
mine_transaction(&nodes[0], &htlc_tx);
2470+
nodes[0].chain_monitor.chain_monitor.rebroadcast_pending_claims();
2471+
check_htlc_retry(&nodes[0], anchors, false, false, &commitment_txn, &coinbase_tx, &mut prev_htlc_tx_feerate, HTLC_AMT_SAT);
2472+
}
2473+
2474+
fn check_htlc_retry(
2475+
node: &Node<'_, '_, '_>,
2476+
anchors: bool,
2477+
should_retry: bool,
2478+
should_bump: bool,
2479+
commitment_txn: &[Transaction],
2480+
coinbase_tx: &Transaction,
2481+
prev_htlc_tx_feerate: &mut Option<u32>,
2482+
htlc_amt_sat: u64,
2483+
) -> Option<Transaction> {
24412484
let (htlc_tx, htlc_tx_feerate) = if anchors {
2442-
assert!(nodes[0].tx_broadcaster.txn_broadcast().is_empty());
2443-
let events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
2485+
assert!(node.tx_broadcaster.txn_broadcast().is_empty());
2486+
let events = node.chain_monitor.chain_monitor.get_and_clear_pending_events();
24442487
assert_eq!(events.len(), if should_retry { 1 } else { 0 });
24452488
if !should_retry {
24462489
return None;
24472490
}
24482491
match &events[0] {
24492492
Event::BumpTransaction(event) => {
2450-
nodes[0].bump_tx_handler.handle_event(&event);
2451-
let mut txn = nodes[0].tx_broadcaster.unique_txn_broadcast();
2493+
node.bump_tx_handler.handle_event(&event);
2494+
let mut txn = node.tx_broadcaster.unique_txn_broadcast();
24522495
assert_eq!(txn.len(), 1);
24532496
let htlc_tx = txn.pop().unwrap();
24542497
check_spends!(&htlc_tx, &commitment_txn[0], &coinbase_tx);
2455-
let htlc_tx_fee = HTLC_AMT_SAT + coinbase_tx.output[0].value.to_sat() -
2498+
let htlc_tx_fee = htlc_amt_sat + coinbase_tx.output[0].value.to_sat() -
24562499
htlc_tx.output.iter().map(|output| output.value.to_sat()).sum::<u64>();
24572500
let htlc_tx_weight = htlc_tx.weight().to_wu();
24582501
(htlc_tx, compute_feerate_sat_per_1000_weight(htlc_tx_fee, htlc_tx_weight))
24592502
}
24602503
_ => panic!("Unexpected event"),
24612504
}
24622505
} else {
2463-
assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
2464-
let mut txn = nodes[0].tx_broadcaster.txn_broadcast();
2506+
assert!(node.chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
2507+
let mut txn = node.tx_broadcaster.txn_broadcast();
24652508
assert_eq!(txn.len(), if should_retry { 1 } else { 0 });
24662509
if !should_retry {
24672510
return None;
24682511
}
24692512
let htlc_tx = txn.pop().unwrap();
24702513
check_spends!(htlc_tx, commitment_txn[0]);
2471-
let htlc_tx_fee = HTLC_AMT_SAT - htlc_tx.output[0].value.to_sat();
2514+
let htlc_tx_fee = htlc_amt_sat - htlc_tx.output[0].value.to_sat();
24722515
let htlc_tx_weight = htlc_tx.weight().to_wu();
24732516
(htlc_tx, compute_feerate_sat_per_1000_weight(htlc_tx_fee, htlc_tx_weight))
24742517
};
@@ -2477,41 +2520,8 @@ fn do_test_monitor_rebroadcast_pending_claims(anchors: bool) {
24772520
} else if let Some(prev_feerate) = prev_htlc_tx_feerate.take() {
24782521
assert_eq!(htlc_tx_feerate, prev_feerate);
24792522
}
2480-
prev_htlc_tx_feerate = Some(htlc_tx_feerate);
2523+
*prev_htlc_tx_feerate = Some(htlc_tx_feerate);
24812524
Some(htlc_tx)
2482-
};
2483-
2484-
// Connect blocks up to one before the HTLC expires. This should not result in a claim/retry.
2485-
connect_blocks(&nodes[0], htlc_expiry - nodes[0].best_block_info().1 - 1);
2486-
check_htlc_retry(false, false);
2487-
2488-
// Connect one more block, producing our first claim.
2489-
connect_blocks(&nodes[0], 1);
2490-
check_htlc_retry(true, false);
2491-
2492-
// Connect a few more blocks, expecting a retry with a fee bump. Unfortunately, we cannot bump
2493-
// HTLC transactions pre-anchors.
2494-
connect_blocks(&nodes[0], crate::chain::package::LOW_FREQUENCY_BUMP_INTERVAL);
2495-
check_htlc_retry(true, anchors);
2496-
2497-
// Trigger a call and we should have another retry, but without a bump.
2498-
nodes[0].chain_monitor.chain_monitor.rebroadcast_pending_claims();
2499-
check_htlc_retry(true, false);
2500-
2501-
// Double the feerate and trigger a call, expecting a fee-bumped retry.
2502-
*nodes[0].fee_estimator.sat_per_kw.lock().unwrap() *= 2;
2503-
nodes[0].chain_monitor.chain_monitor.rebroadcast_pending_claims();
2504-
check_htlc_retry(true, anchors);
2505-
2506-
// Connect a few more blocks, expecting a retry with a fee bump. Unfortunately, we cannot bump
2507-
// HTLC transactions pre-anchors.
2508-
connect_blocks(&nodes[0], crate::chain::package::LOW_FREQUENCY_BUMP_INTERVAL);
2509-
let htlc_tx = check_htlc_retry(true, anchors).unwrap();
2510-
2511-
// Mine the HTLC transaction to ensure we don't retry claims while they're confirmed.
2512-
mine_transaction(&nodes[0], &htlc_tx);
2513-
nodes[0].chain_monitor.chain_monitor.rebroadcast_pending_claims();
2514-
check_htlc_retry(false, false);
25152525
}
25162526

25172527
#[test]

0 commit comments

Comments
 (0)