Skip to content

Commit 3f82143

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 6893371 commit 3f82143

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

0 commit comments

Comments
 (0)