@@ -535,6 +535,56 @@ impl_writeable_tlv_based_enum_upgradable!(ChannelMonitorUpdateStep,
535
535
} ,
536
536
) ;
537
537
538
+ /// Details about the balance available for claim once the channel appears on chain.
539
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
540
+ #[ cfg_attr( test, derive( PartialOrd , Ord ) ) ]
541
+ pub enum ClaimableBalance {
542
+ /// The channel is not yet closed (or the initial commitment or closing transaction has not yet
543
+ /// been confirmed). The given balance is claimable (less on-chain fees) if the channel is
544
+ /// force-closed now.
545
+ ClaimableOnChannelClose {
546
+ /// The amount available to claim, in satoshis, ignoring the on-chain fees which will be
547
+ /// required to do so.
548
+ claimable_amount_satoshis : u64 ,
549
+ } ,
550
+ /// The channel has been closed, and the given balance is ours but awaiting confirmations until
551
+ /// we consider it spendable.
552
+ ClaimableAwaitingConfirmations {
553
+ /// The amount available to claim, in satoshis, possibly ignoring the on-chain fees which
554
+ /// were spent in broadcasting the transaction.
555
+ claimable_amount_satoshis : u64 ,
556
+ /// The height at which an [`Event::SpendableOutputs`] event will be generated for this
557
+ /// amount.
558
+ confirmation_height : u32 ,
559
+ } ,
560
+ /// The channel has been closed, and the given balance should be ours but awaiting spending
561
+ /// transaction confirmation. If the spending transaction does not confirm in time, it is
562
+ /// possible our counterparty can take the funds by broadcasting an HTLC timeout on-chain.
563
+ ///
564
+ /// Once the spending transaction confirms, before it has reached enough confirmations to be
565
+ /// considered safe from chain reorganizations, the balance will instead be provided via
566
+ /// [`ClaimableBalance::ClaimableAwaitingConfirmations`].
567
+ ContentiousClaimable {
568
+ /// The amount available to claim, in satoshis, ignoring the on-chain fees which will be
569
+ /// required to do so.
570
+ claimable_amount_satoshis : u64 ,
571
+ /// The height at which the counterparty may be able to claim the balance if we have not
572
+ /// done so.
573
+ timeout_height : u32 ,
574
+ } ,
575
+ /// HTLCs which we sent to our counterparty which are claimable after a timeout (less on-chain
576
+ /// fees) if the counterparty does not know the preimage for the HTLCs. These are somewhat
577
+ /// likely to be claimed by our counterparty before we do.
578
+ PossiblyClaimableHTLCAwaitingTimeout {
579
+ /// The amount available to claim, in satoshis, ignoring the on-chain fees which will be
580
+ /// required to do so.
581
+ claimable_amount_satoshis : u64 ,
582
+ /// The height at which we will be able to claim the balance if our counterparty has not
583
+ /// done so.
584
+ claimable_height : u32 ,
585
+ } ,
586
+ }
587
+
538
588
/// An HTLC which has been irrevocably resolved on-chain, and has reached ANTI_REORG_DELAY.
539
589
#[ derive( PartialEq ) ]
540
590
struct HTLCIrrevocablyResolved {
@@ -1301,6 +1351,107 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1301
1351
pub fn current_best_block ( & self ) -> BestBlock {
1302
1352
self . inner . lock ( ) . unwrap ( ) . best_block . clone ( )
1303
1353
}
1354
+
1355
+ /// Gets the balances in this channel which are either claimable by us if we were to
1356
+ /// force-close the channel now or which are claimable on-chain or claims which are awaiting
1357
+ /// confirmation.
1358
+ ///
1359
+ /// Any balances in the channel which are available on-chain (ignoring on-chain fees) are
1360
+ /// included here until an [`Event::SpendableOutputs`] event has been generated for the
1361
+ /// balance, or until our counterparty has claimed the balance and accrued several
1362
+ /// confirmations on the claim transaction.
1363
+ ///
1364
+ /// See [`ClaimableBalance`] for additional details on the types of claimable balances which
1365
+ /// may be returned here and their meanings.
1366
+ pub fn get_claimable_balances ( & self ) -> Vec < ClaimableBalance > {
1367
+ let mut res = Vec :: new ( ) ;
1368
+ let us = self . inner . lock ( ) . unwrap ( ) ;
1369
+
1370
+ let mut confirmed_txid = us. funding_spend_confirmed ;
1371
+ if let Some ( ( txid, conf_thresh) ) = us. onchain_events_awaiting_threshold_conf . iter ( ) . find_map ( |event| {
1372
+ if let OnchainEvent :: FundingSpendConfirmation { txid, .. } = event. event {
1373
+ Some ( ( txid, event. confirmation_threshold ( ) ) )
1374
+ } else { None }
1375
+ } ) {
1376
+ debug_assert ! ( us. funding_spend_confirmed. is_none( ) , "We have a pending funding spend awaiting confirmation, we can't have confirmed it already!" ) ;
1377
+ confirmed_txid = Some ( txid) ;
1378
+ res. push ( ClaimableBalance :: ClaimableAwaitingConfirmations {
1379
+ claimable_amount_satoshis : us. current_holder_commitment_tx . to_self_value_sat ,
1380
+ confirmation_height : conf_thresh,
1381
+ } ) ;
1382
+ }
1383
+
1384
+ macro_rules! walk_htlcs {
1385
+ ( $holder_commitment: expr, $htlc_iter: expr) => {
1386
+ for htlc in $htlc_iter {
1387
+ if let Some ( htlc_input_idx) = htlc. transaction_output_index {
1388
+ if us. htlcs_resolved_on_chain. iter( ) . any( |v| v. input_idx == htlc_input_idx) {
1389
+ assert!( us. funding_spend_confirmed. is_some( ) ) ;
1390
+ } else if htlc. offered == $holder_commitment {
1391
+ res. push( ClaimableBalance :: PossiblyClaimableHTLCAwaitingTimeout {
1392
+ claimable_amount_satoshis: htlc. amount_msat / 1000 ,
1393
+ claimable_height: htlc. cltv_expiry,
1394
+ } ) ;
1395
+ } else {
1396
+ if us. payment_preimages. get( & htlc. payment_hash) . is_some( ) {
1397
+ if let Some ( conf_thresh) = us. onchain_events_awaiting_threshold_conf. iter( ) . find_map( |event| {
1398
+ if let OnchainEvent :: HTLCSpendConfirmation { input_idx, .. } = event. event {
1399
+ if input_idx == htlc_input_idx { Some ( event. confirmation_threshold( ) ) } else { None }
1400
+ } else { None }
1401
+ } ) {
1402
+ res. push( ClaimableBalance :: ClaimableAwaitingConfirmations {
1403
+ claimable_amount_satoshis: htlc. amount_msat / 1000 ,
1404
+ confirmation_height: conf_thresh,
1405
+ } ) ;
1406
+ } else {
1407
+ res. push( ClaimableBalance :: ContentiousClaimable {
1408
+ claimable_amount_satoshis: htlc. amount_msat / 1000 ,
1409
+ timeout_height: htlc. cltv_expiry,
1410
+ } ) ;
1411
+ }
1412
+ }
1413
+ }
1414
+ }
1415
+ }
1416
+ }
1417
+ }
1418
+
1419
+ if let Some ( txid) = confirmed_txid {
1420
+ if Some ( txid) == us. current_counterparty_commitment_txid || Some ( txid) == us. prev_counterparty_commitment_txid {
1421
+ walk_htlcs ! ( false , us. counterparty_claimable_outpoints. get( & txid) . unwrap( ) . iter( ) . map( |( a, _) | a) ) ;
1422
+ } else if txid == us. current_holder_commitment_tx . txid {
1423
+ walk_htlcs ! ( true , us. current_holder_commitment_tx. htlc_outputs. iter( ) . map( |( a, _, _) | a) ) ;
1424
+ } else if let Some ( prev_commitment) = & us. prev_holder_signed_commitment_tx {
1425
+ if txid == prev_commitment. txid {
1426
+ walk_htlcs ! ( true , prev_commitment. htlc_outputs. iter( ) . map( |( a, _, _) | a) ) ;
1427
+ }
1428
+ }
1429
+ // TODO: Add logic to provide claimable balances for counterparty broadcasting revoked
1430
+ // outputs.
1431
+ // Otherwise assume we closed with a cooperative close which only needs the
1432
+ // `ClaimableAwaitingConfirmations` balance pushed first.
1433
+ } else {
1434
+ let mut claimable_inbound_htlc_value_sat = 0 ;
1435
+ for ( htlc, _, _) in us. current_holder_commitment_tx . htlc_outputs . iter ( ) {
1436
+ if htlc. transaction_output_index . is_none ( ) { continue ; }
1437
+ if htlc. offered {
1438
+ res. push ( ClaimableBalance :: PossiblyClaimableHTLCAwaitingTimeout {
1439
+ claimable_amount_satoshis : htlc. amount_msat / 1000 ,
1440
+ claimable_height : htlc. cltv_expiry ,
1441
+ } ) ;
1442
+ } else {
1443
+ if us. payment_preimages . get ( & htlc. payment_hash ) . is_some ( ) {
1444
+ claimable_inbound_htlc_value_sat += htlc. amount_msat / 1000 ;
1445
+ }
1446
+ }
1447
+ }
1448
+ res. push ( ClaimableBalance :: ClaimableOnChannelClose {
1449
+ claimable_amount_satoshis : us. current_holder_commitment_tx . to_self_value_sat + claimable_inbound_htlc_value_sat,
1450
+ } ) ;
1451
+ }
1452
+
1453
+ res
1454
+ }
1304
1455
}
1305
1456
1306
1457
/// Compares a broadcasted commitment transaction's HTLCs with those in the latest state,
0 commit comments