Skip to content

Commit 1080696

Browse files
committed
Rewrite closure check_htlc_retry
Async closures are complicated. Preparatory commit.
1 parent 361ff01 commit 1080696

File tree

1 file changed

+55
-50
lines changed

1 file changed

+55
-50
lines changed

lightning/src/ln/monitor_tests.rs

+55-50
Original file line numberDiff line numberDiff line change
@@ -2437,81 +2437,86 @@ 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> {
2441-
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();
2444-
assert_eq!(events.len(), if should_retry { 1 } else { 0 });
2445-
if !should_retry {
2446-
return None;
2447-
}
2448-
match &events[0] {
2449-
Event::BumpTransaction(event) => {
2450-
nodes[0].bump_tx_handler.handle_event(&event);
2451-
let mut txn = nodes[0].tx_broadcaster.unique_txn_broadcast();
2452-
assert_eq!(txn.len(), 1);
2453-
let htlc_tx = txn.pop().unwrap();
2454-
check_spends!(&htlc_tx, &commitment_txn[0], &coinbase_tx);
2455-
let htlc_tx_fee = HTLC_AMT_SAT + coinbase_tx.output[0].value.to_sat() -
2456-
htlc_tx.output.iter().map(|output| output.value.to_sat()).sum::<u64>();
2457-
let htlc_tx_weight = htlc_tx.weight().to_wu();
2458-
(htlc_tx, compute_feerate_sat_per_1000_weight(htlc_tx_fee, htlc_tx_weight))
2459-
}
2460-
_ => panic!("Unexpected event"),
2461-
}
2462-
} 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();
2465-
assert_eq!(txn.len(), if should_retry { 1 } else { 0 });
2466-
if !should_retry {
2467-
return None;
2468-
}
2469-
let htlc_tx = txn.pop().unwrap();
2470-
check_spends!(htlc_tx, commitment_txn[0]);
2471-
let htlc_tx_fee = HTLC_AMT_SAT - htlc_tx.output[0].value.to_sat();
2472-
let htlc_tx_weight = htlc_tx.weight().to_wu();
2473-
(htlc_tx, compute_feerate_sat_per_1000_weight(htlc_tx_fee, htlc_tx_weight))
2474-
};
2475-
if should_bump {
2476-
assert!(htlc_tx_feerate > prev_htlc_tx_feerate.take().unwrap());
2477-
} else if let Some(prev_feerate) = prev_htlc_tx_feerate.take() {
2478-
assert_eq!(htlc_tx_feerate, prev_feerate);
2479-
}
2480-
prev_htlc_tx_feerate = Some(htlc_tx_feerate);
2481-
Some(htlc_tx)
2482-
};
24832440

24842441
// Connect blocks up to one before the HTLC expires. This should not result in a claim/retry.
24852442
connect_blocks(&nodes[0], htlc_expiry - nodes[0].best_block_info().1 - 1);
2486-
check_htlc_retry(false, false);
2443+
check_htlc_retry(&nodes[0], anchors, false, false, &commitment_txn, &coinbase_tx, &mut prev_htlc_tx_feerate, HTLC_AMT_SAT);
24872444

24882445
// Connect one more block, producing our first claim.
24892446
connect_blocks(&nodes[0], 1);
2490-
check_htlc_retry(true, false);
2447+
check_htlc_retry(&nodes[0], anchors, true, false, &commitment_txn, &coinbase_tx, &mut prev_htlc_tx_feerate, HTLC_AMT_SAT);
24912448

24922449
// Connect a few more blocks, expecting a retry with a fee bump. Unfortunately, we cannot bump
24932450
// HTLC transactions pre-anchors.
24942451
connect_blocks(&nodes[0], crate::chain::package::LOW_FREQUENCY_BUMP_INTERVAL);
2495-
check_htlc_retry(true, anchors);
2452+
check_htlc_retry(&nodes[0], anchors, true, anchors, &commitment_txn, &coinbase_tx, &mut prev_htlc_tx_feerate, HTLC_AMT_SAT);
24962453

24972454
// Trigger a call and we should have another retry, but without a bump.
24982455
nodes[0].chain_monitor.chain_monitor.rebroadcast_pending_claims();
2499-
check_htlc_retry(true, false);
2456+
check_htlc_retry(&nodes[0], anchors, true, false, &commitment_txn, &coinbase_tx, &mut prev_htlc_tx_feerate, HTLC_AMT_SAT);
25002457

25012458
// Double the feerate and trigger a call, expecting a fee-bumped retry.
25022459
*nodes[0].fee_estimator.sat_per_kw.lock().unwrap() *= 2;
25032460
nodes[0].chain_monitor.chain_monitor.rebroadcast_pending_claims();
2504-
check_htlc_retry(true, anchors);
2461+
check_htlc_retry(&nodes[0], anchors, true, anchors, &commitment_txn, &coinbase_tx, &mut prev_htlc_tx_feerate, HTLC_AMT_SAT);
25052462

25062463
// Connect a few more blocks, expecting a retry with a fee bump. Unfortunately, we cannot bump
25072464
// HTLC transactions pre-anchors.
25082465
connect_blocks(&nodes[0], crate::chain::package::LOW_FREQUENCY_BUMP_INTERVAL);
2509-
let htlc_tx = check_htlc_retry(true, anchors).unwrap();
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();
25102467

25112468
// Mine the HTLC transaction to ensure we don't retry claims while they're confirmed.
25122469
mine_transaction(&nodes[0], &htlc_tx);
25132470
nodes[0].chain_monitor.chain_monitor.rebroadcast_pending_claims();
2514-
check_htlc_retry(false, false);
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<'_, '_, '_>, anchors: bool, should_retry: bool, should_bump: bool,
2476+
commitment_txn: &[Transaction], coinbase_tx: &Transaction,
2477+
prev_htlc_tx_feerate: &mut Option<u32>, htlc_amt_sat: u64,
2478+
) -> Option<Transaction> {
2479+
let (htlc_tx, htlc_tx_feerate) = if anchors {
2480+
assert!(node.tx_broadcaster.txn_broadcast().is_empty());
2481+
let events = node.chain_monitor.chain_monitor.get_and_clear_pending_events();
2482+
assert_eq!(events.len(), if should_retry { 1 } else { 0 });
2483+
if !should_retry {
2484+
return None;
2485+
}
2486+
match &events[0] {
2487+
Event::BumpTransaction(event) => {
2488+
node.bump_tx_handler.handle_event(&event);
2489+
let mut txn = node.tx_broadcaster.unique_txn_broadcast();
2490+
assert_eq!(txn.len(), 1);
2491+
let htlc_tx = txn.pop().unwrap();
2492+
check_spends!(&htlc_tx, &commitment_txn[0], &coinbase_tx);
2493+
let htlc_tx_fee = htlc_amt_sat + coinbase_tx.output[0].value.to_sat()
2494+
- htlc_tx.output.iter().map(|output| output.value.to_sat()).sum::<u64>();
2495+
let htlc_tx_weight = htlc_tx.weight().to_wu();
2496+
(htlc_tx, compute_feerate_sat_per_1000_weight(htlc_tx_fee, htlc_tx_weight))
2497+
},
2498+
_ => panic!("Unexpected event"),
2499+
}
2500+
} else {
2501+
assert!(node.chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
2502+
let mut txn = node.tx_broadcaster.txn_broadcast();
2503+
assert_eq!(txn.len(), if should_retry { 1 } else { 0 });
2504+
if !should_retry {
2505+
return None;
2506+
}
2507+
let htlc_tx = txn.pop().unwrap();
2508+
check_spends!(htlc_tx, commitment_txn[0]);
2509+
let htlc_tx_fee = htlc_amt_sat - htlc_tx.output[0].value.to_sat();
2510+
let htlc_tx_weight = htlc_tx.weight().to_wu();
2511+
(htlc_tx, compute_feerate_sat_per_1000_weight(htlc_tx_fee, htlc_tx_weight))
2512+
};
2513+
if should_bump {
2514+
assert!(htlc_tx_feerate > prev_htlc_tx_feerate.take().unwrap());
2515+
} else if let Some(prev_feerate) = prev_htlc_tx_feerate.take() {
2516+
assert_eq!(htlc_tx_feerate, prev_feerate);
2517+
}
2518+
*prev_htlc_tx_feerate = Some(htlc_tx_feerate);
2519+
Some(htlc_tx)
25152520
}
25162521

25172522
#[test]

0 commit comments

Comments
 (0)