Skip to content

Commit 284c801

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 f47b208 commit 284c801

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

0 commit comments

Comments
 (0)