Skip to content

Commit acaaffd

Browse files
committed
Scan onchain_events_awaiting_threshold_conf once in balance calc
Instead of a series of different `onchain_events_awaiting_threshold_conf.iter()...` calls to scan for HTLC status in balance calculation, pull them all out into one `for ... { match ... }` to do it once and simplify the code somewhat.
1 parent 23c3cd2 commit acaaffd

File tree

1 file changed

+65
-50
lines changed

1 file changed

+65
-50
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 65 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,67 +1445,82 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
14451445
($holder_commitment: expr, $htlc_iter: expr) => {
14461446
for htlc in $htlc_iter {
14471447
if let Some(htlc_commitment_tx_output_idx) = htlc.transaction_output_index {
1448-
if let Some(conf_thresh) = us.onchain_events_awaiting_threshold_conf.iter().find_map(|event| {
1449-
if let OnchainEvent::MaturingOutput { descriptor: SpendableOutputDescriptor::DelayedPaymentOutput(descriptor) } = &event.event {
1450-
if descriptor.outpoint.index as u32 == htlc_commitment_tx_output_idx { Some(event.confirmation_threshold()) } else { None }
1451-
} else { None }
1452-
}) {
1448+
let mut htlc_update_pending = None;
1449+
let mut htlc_spend_pending = None;
1450+
let mut delayed_output_pending = None;
1451+
for event in us.onchain_events_awaiting_threshold_conf.iter() {
1452+
match event.event {
1453+
OnchainEvent::HTLCUpdate { commitment_tx_output_idx, htlc_value_satoshis, .. }
1454+
if commitment_tx_output_idx == Some(htlc_commitment_tx_output_idx) => {
1455+
debug_assert!(htlc_update_pending.is_none());
1456+
htlc_update_pending =
1457+
Some((htlc_value_satoshis.unwrap(), event.confirmation_threshold()));
1458+
},
1459+
OnchainEvent::HTLCSpendConfirmation { commitment_tx_output_idx, preimage, .. }
1460+
if commitment_tx_output_idx == htlc_commitment_tx_output_idx => {
1461+
debug_assert!(htlc_spend_pending.is_none());
1462+
htlc_spend_pending = Some((event.confirmation_threshold(), preimage.is_some()));
1463+
},
1464+
OnchainEvent::MaturingOutput {
1465+
descriptor: SpendableOutputDescriptor::DelayedPaymentOutput(ref descriptor) }
1466+
if descriptor.outpoint.index as u32 == htlc_commitment_tx_output_idx => {
1467+
debug_assert!(delayed_output_pending.is_none());
1468+
delayed_output_pending = Some(event.confirmation_threshold());
1469+
},
1470+
_ => {},
1471+
}
1472+
}
1473+
let htlc_resolved = us.htlcs_resolved_on_chain.iter()
1474+
.find(|v| v.commitment_tx_output_idx == htlc_commitment_tx_output_idx);
1475+
debug_assert!(htlc_update_pending.is_some() as u8 + htlc_spend_pending.is_some() as u8 + htlc_resolved.is_some() as u8 <= 1);
1476+
1477+
if let Some(conf_thresh) = delayed_output_pending {
14531478
debug_assert!($holder_commitment);
14541479
res.push(Balance::ClaimableAwaitingConfirmations {
14551480
claimable_amount_satoshis: htlc.amount_msat / 1000,
14561481
confirmation_height: conf_thresh,
14571482
});
1458-
} else if us.htlcs_resolved_on_chain.iter().any(|v| v.commitment_tx_output_idx == htlc_commitment_tx_output_idx) {
1483+
} else if htlc_resolved.is_some() {
14591484
// Funding transaction spends should be fully confirmed by the time any
14601485
// HTLC transactions are resolved, unless we're talking about a holder
14611486
// commitment tx, whose resolution is delayed until the CSV timeout is
14621487
// reached, even though HTLCs may be resolved after only
14631488
// ANTI_REORG_DELAY confirmations.
14641489
debug_assert!($holder_commitment || us.funding_spend_confirmed.is_some());
1465-
} else if htlc.offered == $holder_commitment {
1466-
// If the payment was outbound, check if there's an HTLCUpdate
1467-
// indicating we have spent this HTLC with a timeout, claiming it back
1468-
// and awaiting confirmations on it.
1469-
let htlc_update_pending = us.onchain_events_awaiting_threshold_conf.iter().find_map(|event| {
1470-
if let OnchainEvent::HTLCUpdate { commitment_tx_output_idx: Some(commitment_tx_output_idx), .. } = event.event {
1471-
if commitment_tx_output_idx == htlc_commitment_tx_output_idx {
1472-
Some(event.confirmation_threshold()) } else { None }
1473-
} else { None }
1474-
});
1475-
if let Some(conf_thresh) = htlc_update_pending {
1476-
res.push(Balance::ClaimableAwaitingConfirmations {
1477-
claimable_amount_satoshis: htlc.amount_msat / 1000,
1478-
confirmation_height: conf_thresh,
1479-
});
1480-
} else {
1481-
res.push(Balance::MaybeClaimableHTLCAwaitingTimeout {
1482-
claimable_amount_satoshis: htlc.amount_msat / 1000,
1483-
claimable_height: htlc.cltv_expiry,
1484-
});
1485-
}
1486-
} else if us.payment_preimages.get(&htlc.payment_hash).is_some() {
1487-
// Otherwise (the payment was inbound), only expose it as claimable if
1488-
// we know the preimage.
1489-
// Note that if there is a pending claim, but it did not use the
1490-
// preimage, we lost funds to our counterparty! We will then continue
1491-
// to show it as ContentiousClaimable until ANTI_REORG_DELAY.
1492-
let htlc_spend_pending = us.onchain_events_awaiting_threshold_conf.iter().find_map(|event| {
1493-
if let OnchainEvent::HTLCSpendConfirmation { commitment_tx_output_idx, preimage, .. } = event.event {
1494-
if commitment_tx_output_idx == htlc_commitment_tx_output_idx {
1495-
Some((event.confirmation_threshold(), preimage.is_some()))
1496-
} else { None }
1497-
} else { None }
1498-
});
1499-
if let Some((conf_thresh, true)) = htlc_spend_pending {
1500-
res.push(Balance::ClaimableAwaitingConfirmations {
1501-
claimable_amount_satoshis: htlc.amount_msat / 1000,
1502-
confirmation_height: conf_thresh,
1503-
});
1504-
} else {
1505-
res.push(Balance::ContentiousClaimable {
1506-
claimable_amount_satoshis: htlc.amount_msat / 1000,
1507-
timeout_height: htlc.cltv_expiry,
1508-
});
1490+
} else {
1491+
if htlc.offered == $holder_commitment {
1492+
// If the payment was outbound, check if there's an HTLCUpdate
1493+
// indicating we have spent this HTLC with a timeout, claiming it back
1494+
// and awaiting confirmations on it.
1495+
if let Some((value, conf_thresh)) = htlc_update_pending {
1496+
res.push(Balance::ClaimableAwaitingConfirmations {
1497+
claimable_amount_satoshis: value,
1498+
confirmation_height: conf_thresh,
1499+
});
1500+
} else {
1501+
res.push(Balance::MaybeClaimableHTLCAwaitingTimeout {
1502+
claimable_amount_satoshis: htlc.amount_msat / 1000,
1503+
claimable_height: htlc.cltv_expiry,
1504+
});
1505+
}
1506+
} else if us.payment_preimages.get(&htlc.payment_hash).is_some() {
1507+
// Otherwise (the payment was inbound), only expose it as claimable if
1508+
// we know the preimage.
1509+
// Note that if there is a pending claim, but it did not use the
1510+
// preimage, we lost funds to our counterparty! We will then continue
1511+
// to show it as ContentiousClaimable until ANTI_REORG_DELAY.
1512+
debug_assert!(htlc_update_pending.is_none());
1513+
if let Some((conf_thresh, true)) = htlc_spend_pending {
1514+
res.push(Balance::ClaimableAwaitingConfirmations {
1515+
claimable_amount_satoshis: htlc.amount_msat / 1000,
1516+
confirmation_height: conf_thresh,
1517+
});
1518+
} else {
1519+
res.push(Balance::ContentiousClaimable {
1520+
claimable_amount_satoshis: htlc.amount_msat / 1000,
1521+
timeout_height: htlc.cltv_expiry,
1522+
});
1523+
}
15091524
}
15101525
}
15111526
}

0 commit comments

Comments
 (0)