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