Skip to content

Commit f1ac752

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 a5cc59e commit f1ac752

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

0 commit comments

Comments
 (0)