@@ -1440,67 +1440,82 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1440
1440
( $holder_commitment: expr, $htlc_iter: expr) => {
1441
1441
for htlc in $htlc_iter {
1442
1442
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 {
1448
1473
debug_assert!( $holder_commitment) ;
1449
1474
res. push( Balance :: ClaimableAwaitingConfirmations {
1450
1475
claimable_amount_satoshis: htlc. amount_msat / 1000 ,
1451
1476
confirmation_height: conf_thresh,
1452
1477
} ) ;
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 ( ) {
1454
1479
// Funding transaction spends should be fully confirmed by the time any
1455
1480
// HTLC transactions are resolved, unless we're talking about a holder
1456
1481
// commitment tx, whose resolution is delayed until the CSV timeout is
1457
1482
// reached, even though HTLCs may be resolved after only
1458
1483
// ANTI_REORG_DELAY confirmations.
1459
1484
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
+ }
1504
1519
}
1505
1520
}
1506
1521
}
0 commit comments