@@ -535,6 +535,59 @@ impl_writeable_tlv_based_enum_upgradable!(ChannelMonitorUpdateStep,
535
535
} ,
536
536
) ;
537
537
538
+ /// Details about the balance(s) available for spending once the channel appears on chain.
539
+ ///
540
+ /// See [`ChannelMonitor::get_claimable_balances`] for more details on when these will or will not
541
+ /// be provided.
542
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
543
+ #[ cfg_attr( test, derive( PartialOrd , Ord ) ) ]
544
+ pub enum ClaimableBalance {
545
+ /// The channel is not yet closed (or the commitment or closing transaction has not yet
546
+ /// appeared in a block). The given balance is claimable (less on-chain fees) if the channel is
547
+ /// force-closed now.
548
+ ClaimableOnChannelClose {
549
+ /// The amount available to claim, in satoshis, ignoring the on-chain fees which will be
550
+ /// required to do so.
551
+ claimable_amount_satoshis : u64 ,
552
+ } ,
553
+ /// The channel has been closed, and the given balance is ours but awaiting confirmations until
554
+ /// we consider it spendable.
555
+ ClaimableAwaitingConfirmations {
556
+ /// The amount available to claim, in satoshis, possibly ignoring the on-chain fees which
557
+ /// were spent in broadcasting the transaction.
558
+ claimable_amount_satoshis : u64 ,
559
+ /// The height at which an [`Event::SpendableOutputs`] event will be generated for this
560
+ /// amount.
561
+ confirmation_height : u32 ,
562
+ } ,
563
+ /// The channel has been closed, and the given balance should be ours but awaiting spending
564
+ /// transaction confirmation. If the spending transaction does not confirm in time, it is
565
+ /// possible our counterparty can take the funds by broadcasting an HTLC timeout on-chain.
566
+ ///
567
+ /// Once the spending transaction confirms, before it has reached enough confirmations to be
568
+ /// considered safe from chain reorganizations, the balance will instead be provided via
569
+ /// [`ClaimableBalance::ClaimableAwaitingConfirmations`].
570
+ ContentiousClaimable {
571
+ /// The amount available to claim, in satoshis, ignoring the on-chain fees which will be
572
+ /// required to do so.
573
+ claimable_amount_satoshis : u64 ,
574
+ /// The height at which the counterparty may be able to claim the balance if we have not
575
+ /// done so.
576
+ timeout_height : u32 ,
577
+ } ,
578
+ /// HTLCs which we sent to our counterparty which are claimable after a timeout (less on-chain
579
+ /// fees) if the counterparty does not know the preimage for the HTLCs. These are somewhat
580
+ /// likely to be claimed by our counterparty before we do.
581
+ MaybeClaimableHTLCAwaitingTimeout {
582
+ /// The amount available to claim, in satoshis, ignoring the on-chain fees which will be
583
+ /// required to do so.
584
+ claimable_amount_satoshis : u64 ,
585
+ /// The height at which we will be able to claim the balance if our counterparty has not
586
+ /// done so.
587
+ claimable_height : u32 ,
588
+ } ,
589
+ }
590
+
538
591
/// An HTLC which has been irrevocably resolved on-chain, and has reached ANTI_REORG_DELAY.
539
592
#[ derive( PartialEq ) ]
540
593
struct HTLCIrrevocablyResolved {
@@ -1301,6 +1354,174 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1301
1354
pub fn current_best_block ( & self ) -> BestBlock {
1302
1355
self . inner . lock ( ) . unwrap ( ) . best_block . clone ( )
1303
1356
}
1357
+
1358
+ /// Gets the balances in this channel which are either claimable by us if we were to
1359
+ /// force-close the channel now or which are claimable on-chain or claims which are awaiting
1360
+ /// confirmation.
1361
+ ///
1362
+ /// Any balances in the channel which are available on-chain (ignoring on-chain fees) are
1363
+ /// included here until an [`Event::SpendableOutputs`] event has been generated for the
1364
+ /// balance, or until our counterparty has claimed the balance and accrued several
1365
+ /// confirmations on the claim transaction.
1366
+ ///
1367
+ /// Note that the balances available when you or your counterparty have broadcasted revoked
1368
+ /// state(s) may not be fully captured here.
1369
+ // TODO, fix that ^
1370
+ ///
1371
+ /// See [`ClaimableBalance`] for additional details on the types of claimable balances which
1372
+ /// may be returned here and their meanings.
1373
+ pub fn get_claimable_balances ( & self ) -> Vec < ClaimableBalance > {
1374
+ let mut res = Vec :: new ( ) ;
1375
+ let us = self . inner . lock ( ) . unwrap ( ) ;
1376
+
1377
+ let mut confirmed_txid = us. funding_spend_confirmed ;
1378
+ let mut pending_commitment_tx_conf_thresh = None ;
1379
+ if let Some ( ( txid, conf_thresh) ) = us. onchain_events_awaiting_threshold_conf . iter ( ) . find_map ( |event| {
1380
+ if let OnchainEvent :: FundingSpendConfirmation { txid, .. } = event. event {
1381
+ Some ( ( txid, event. confirmation_threshold ( ) ) )
1382
+ } else { None }
1383
+ } ) {
1384
+ debug_assert ! ( us. funding_spend_confirmed. is_none( ) , "We have a pending funding spend awaiting confirmation, we can't have confirmed it already!" ) ;
1385
+ confirmed_txid = Some ( txid) ;
1386
+ pending_commitment_tx_conf_thresh = Some ( conf_thresh) ;
1387
+ }
1388
+
1389
+ macro_rules! walk_htlcs {
1390
+ ( $holder_commitment: expr, $htlc_iter: expr) => {
1391
+ for htlc in $htlc_iter {
1392
+ if let Some ( htlc_input_idx) = htlc. transaction_output_index {
1393
+ if us. htlcs_resolved_on_chain. iter( ) . any( |v| v. input_idx == htlc_input_idx) {
1394
+ assert!( us. funding_spend_confirmed. is_some( ) ) ;
1395
+ } else if htlc. offered == $holder_commitment {
1396
+ // If the payment was outbound, check if there's an HTLCUpdate
1397
+ // indicating we have spent this HTLC with a timeout, claiming it back
1398
+ // and awaiting confirmations on it.
1399
+ if let Some ( conf_thresh) = us. onchain_events_awaiting_threshold_conf. iter( ) . find_map( |event| {
1400
+ if let OnchainEvent :: HTLCUpdate { input_idx: Some ( input_idx) , .. } = event. event {
1401
+ if input_idx == htlc_input_idx { Some ( event. confirmation_threshold( ) ) } else { None }
1402
+ } else { None }
1403
+ } ) {
1404
+ res. push( ClaimableBalance :: ClaimableAwaitingConfirmations {
1405
+ claimable_amount_satoshis: htlc. amount_msat / 1000 ,
1406
+ confirmation_height: conf_thresh,
1407
+ } ) ;
1408
+ } else {
1409
+ res. push( ClaimableBalance :: MaybeClaimableHTLCAwaitingTimeout {
1410
+ claimable_amount_satoshis: htlc. amount_msat / 1000 ,
1411
+ claimable_height: htlc. cltv_expiry,
1412
+ } ) ;
1413
+ }
1414
+ } else if us. payment_preimages. get( & htlc. payment_hash) . is_some( ) {
1415
+ // Otherwise (the payment was inbound), only expose it as claimable if
1416
+ // we know the preimage.
1417
+ // Note that if there is a pending claim, but it did not use the
1418
+ // preimage, we lost funds to our counterparty! We will then continue
1419
+ // to show it as ContentiousClaimable until ANTI_REORG_DELAY.
1420
+ if let Some ( ( conf_thresh, true ) ) =
1421
+ us. onchain_events_awaiting_threshold_conf. iter( ) . find_map( |event| {
1422
+ if let OnchainEvent :: HTLCSpendConfirmation { input_idx, preimage, .. } = event. event {
1423
+ if input_idx == htlc_input_idx {
1424
+ Some ( ( event. confirmation_threshold( ) , preimage. is_some( ) ) )
1425
+ } else { None }
1426
+ } else { None }
1427
+ }
1428
+ ) {
1429
+ res. push( ClaimableBalance :: ClaimableAwaitingConfirmations {
1430
+ claimable_amount_satoshis: htlc. amount_msat / 1000 ,
1431
+ confirmation_height: conf_thresh,
1432
+ } ) ;
1433
+ } else {
1434
+ res. push( ClaimableBalance :: ContentiousClaimable {
1435
+ claimable_amount_satoshis: htlc. amount_msat / 1000 ,
1436
+ timeout_height: htlc. cltv_expiry,
1437
+ } ) ;
1438
+ }
1439
+ }
1440
+ }
1441
+ }
1442
+ }
1443
+ }
1444
+
1445
+ if let Some ( txid) = confirmed_txid {
1446
+ let mut found_commitment_tx = false ;
1447
+ if Some ( txid) == us. current_counterparty_commitment_txid || Some ( txid) == us. prev_counterparty_commitment_txid {
1448
+ walk_htlcs ! ( false , us. counterparty_claimable_outpoints. get( & txid) . unwrap( ) . iter( ) . map( |( a, _) | a) ) ;
1449
+ if let Some ( conf_thresh) = pending_commitment_tx_conf_thresh {
1450
+ if let Some ( value) = us. onchain_events_awaiting_threshold_conf . iter ( ) . find_map ( |event| {
1451
+ if let OnchainEvent :: MaturingOutput {
1452
+ descriptor : SpendableOutputDescriptor :: StaticPaymentOutput ( descriptor)
1453
+ } = & event. event {
1454
+ Some ( descriptor. output . value )
1455
+ } else { None }
1456
+ } ) {
1457
+ res. push ( ClaimableBalance :: ClaimableAwaitingConfirmations {
1458
+ claimable_amount_satoshis : value,
1459
+ confirmation_height : conf_thresh,
1460
+ } ) ;
1461
+ } else {
1462
+ // If a counterparty commitment transaction is awaiting confirmation, we
1463
+ // should also have a StaticPaymentOutput MaturingOutput event awaiting
1464
+ // confirmation with the same height. Not having one implies something has
1465
+ // gone terribly wrong with our commitment txid tracking.
1466
+ debug_assert ! ( false ) ;
1467
+ }
1468
+ }
1469
+ found_commitment_tx = true ;
1470
+ } else if txid == us. current_holder_commitment_tx . txid {
1471
+ walk_htlcs ! ( true , us. current_holder_commitment_tx. htlc_outputs. iter( ) . map( |( a, _, _) | a) ) ;
1472
+ if let Some ( conf_thresh) = pending_commitment_tx_conf_thresh {
1473
+ res. push ( ClaimableBalance :: ClaimableAwaitingConfirmations {
1474
+ claimable_amount_satoshis : us. current_holder_commitment_tx . to_self_value_sat ,
1475
+ confirmation_height : conf_thresh,
1476
+ } ) ;
1477
+ }
1478
+ found_commitment_tx = true ;
1479
+ } else if let Some ( prev_commitment) = & us. prev_holder_signed_commitment_tx {
1480
+ if txid == prev_commitment. txid {
1481
+ walk_htlcs ! ( true , prev_commitment. htlc_outputs. iter( ) . map( |( a, _, _) | a) ) ;
1482
+ if let Some ( conf_thresh) = pending_commitment_tx_conf_thresh {
1483
+ res. push ( ClaimableBalance :: ClaimableAwaitingConfirmations {
1484
+ claimable_amount_satoshis : prev_commitment. to_self_value_sat ,
1485
+ confirmation_height : conf_thresh,
1486
+ } ) ;
1487
+ }
1488
+ found_commitment_tx = true ;
1489
+ }
1490
+ }
1491
+ if !found_commitment_tx {
1492
+ if let Some ( conf_thresh) = pending_commitment_tx_conf_thresh {
1493
+ // We blindly assume this is a cooperative close transaction here, and that the
1494
+ // counterparty didn't misbehave. At worst we've over-estimated the amount we can
1495
+ // claim as we'll punish a misbehaving counterparty (as long as we didn't
1496
+ // misbehave).
1497
+ res. push ( ClaimableBalance :: ClaimableAwaitingConfirmations {
1498
+ claimable_amount_satoshis : us. current_holder_commitment_tx . to_self_value_sat ,
1499
+ confirmation_height : conf_thresh,
1500
+ } ) ;
1501
+ }
1502
+ }
1503
+ // TODO: Add logic to provide claimable balances for counterparty broadcasting revoked
1504
+ // outputs.
1505
+ } else {
1506
+ let mut claimable_inbound_htlc_value_sat = 0 ;
1507
+ for ( htlc, _, _) in us. current_holder_commitment_tx . htlc_outputs . iter ( ) {
1508
+ if htlc. transaction_output_index . is_none ( ) { continue ; }
1509
+ if htlc. offered {
1510
+ res. push ( ClaimableBalance :: MaybeClaimableHTLCAwaitingTimeout {
1511
+ claimable_amount_satoshis : htlc. amount_msat / 1000 ,
1512
+ claimable_height : htlc. cltv_expiry ,
1513
+ } ) ;
1514
+ } else if us. payment_preimages . get ( & htlc. payment_hash ) . is_some ( ) {
1515
+ claimable_inbound_htlc_value_sat += htlc. amount_msat / 1000 ;
1516
+ }
1517
+ }
1518
+ res. push ( ClaimableBalance :: ClaimableOnChannelClose {
1519
+ claimable_amount_satoshis : us. current_holder_commitment_tx . to_self_value_sat + claimable_inbound_htlc_value_sat,
1520
+ } ) ;
1521
+ }
1522
+
1523
+ res
1524
+ }
1304
1525
}
1305
1526
1306
1527
/// Compares a broadcasted commitment transaction's HTLCs with those in the latest state,
0 commit comments