@@ -504,6 +504,56 @@ impl_writeable_tlv_based_enum_upgradable!(ChannelMonitorUpdateStep,
504
504
} ,
505
505
) ;
506
506
507
+ /// Details about the balance available for claim once the channel appears on chain.
508
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
509
+ #[ cfg_attr( test, derive( PartialOrd , Ord ) ) ]
510
+ pub enum ClaimableBalance {
511
+ /// The channel is not yet closed (or the initial commitment or closing transaction has not yet
512
+ /// been confirmed). The given balance is claimable (less on-chain fees) if the channel is
513
+ /// force-closed now.
514
+ ClaimableOnChannelClose {
515
+ /// The amount available to claim, in satoshis, ignoring the on-chain fees which will be
516
+ /// required to do so.
517
+ claimable_amount_satoshis : u64 ,
518
+ } ,
519
+ /// The channel has been closed, and the given balance is ours but awaiting confirmations until
520
+ /// we consider it spendable.
521
+ ClaimableAwaitingConfirmations {
522
+ /// The amount available to claim, in satoshis, possibly ignoring the on-chain fees which
523
+ /// were spent in broadcasting the transaction.
524
+ claimable_amount_satoshis : u64 ,
525
+ /// The height at which an [`Event::SpendableOutputs`] event will be generated for this
526
+ /// amount.
527
+ confirmation_height : u32 ,
528
+ } ,
529
+ /// The channel has been closed, and the given balance should be ours but awaiting spending
530
+ /// transaction confirmation. If the spending transaction does not confirm in time, it is
531
+ /// possible our counterparty can take the funds by broadcasting an HTLC timeout on-chain.
532
+ ///
533
+ /// Once the spending transaction confirms, before it has reached enough confirmations to be
534
+ /// considered safe from chain reorganizations, the balance will instead be provided via
535
+ /// [`ClaimableBalance::ClaimableAwaitingConfirmations`].
536
+ ContentiousClaimable {
537
+ /// The amount available to claim, in satoshis, ignoring the on-chain fees which will be
538
+ /// required to do so.
539
+ claimable_amount_satoshis : u64 ,
540
+ /// The height at which the counterparty may be able to claim the balance if we have not
541
+ /// done so.
542
+ timeout_height : u32 ,
543
+ } ,
544
+ /// HTLCs which we sent to our counterparty which are claimable after a timeout (less on-chain
545
+ /// fees) if the counterparty does not know the preimage for the HTLCs. These are somewhat
546
+ /// likely to be claimed by our counterparty before we do.
547
+ PossiblyClaimableHTLCAwaitingTimeout {
548
+ /// The amount available to claim, in satoshis, ignoring the on-chain fees which will be
549
+ /// required to do so.
550
+ claimable_amount_satoshis : u64 ,
551
+ /// The height at which we will be able to claim the balance if our counterparty has not
552
+ /// done so.
553
+ claimable_height : u32 ,
554
+ } ,
555
+ }
556
+
507
557
/// An HTLC which has been irrevocably resolved on-chain, and has reached ANTI_REORG_DELAY.
508
558
#[ derive( PartialEq ) ]
509
559
struct HTLCIrrevocablyResolved {
@@ -1266,6 +1316,107 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1266
1316
pub fn current_best_block ( & self ) -> BestBlock {
1267
1317
self . inner . lock ( ) . unwrap ( ) . best_block . clone ( )
1268
1318
}
1319
+
1320
+ /// Gets the balances in this channel which are either claimable by us if we were to
1321
+ /// force-close the channel now or which are claimable on-chain or claims which are awaiting
1322
+ /// confirmation.
1323
+ ///
1324
+ /// Any balances in the channel which are available on-chain (ignoring on-chain fees) are
1325
+ /// included here until an [`Event::SpendableOutputs`] event has been generated for the
1326
+ /// balance, or until our counterparty has claimed the balance and accrued several
1327
+ /// confirmations on the claim transaction.
1328
+ ///
1329
+ /// See [`ClaimableBalance`] for additional details on the types of claimable balances which
1330
+ /// may be returned here and their meanings.
1331
+ pub fn get_claimable_balances ( & self ) -> Vec < ClaimableBalance > {
1332
+ let mut res = Vec :: new ( ) ;
1333
+ let us = self . inner . lock ( ) . unwrap ( ) ;
1334
+
1335
+ let mut confirmed_txid = us. funding_spend_confirmed ;
1336
+ if let Some ( ( txid, conf_thresh) ) = us. onchain_events_awaiting_threshold_conf . iter ( ) . find_map ( |event| {
1337
+ if let OnchainEvent :: FundingSpendConfirmation { txid, .. } = event. event {
1338
+ Some ( ( txid, event. confirmation_threshold ( ) ) )
1339
+ } else { None }
1340
+ } ) {
1341
+ debug_assert ! ( us. funding_spend_confirmed. is_none( ) , "We have a pending funding spend awaiting confirmation, we can't have confirmed it already!" ) ;
1342
+ confirmed_txid = Some ( txid) ;
1343
+ res. push ( ClaimableBalance :: ClaimableAwaitingConfirmations {
1344
+ claimable_amount_satoshis : us. current_holder_commitment_tx . to_self_value_sat ,
1345
+ confirmation_height : conf_thresh,
1346
+ } ) ;
1347
+ }
1348
+
1349
+ macro_rules! walk_htlcs {
1350
+ ( $holder_commitment: expr, $htlc_iter: expr) => {
1351
+ for htlc in $htlc_iter {
1352
+ if let Some ( htlc_input_idx) = htlc. transaction_output_index {
1353
+ if us. htlcs_resolved_on_chain. iter( ) . any( |v| v. input_idx == htlc_input_idx) {
1354
+ assert!( us. funding_spend_confirmed. is_some( ) ) ;
1355
+ } else if htlc. offered == $holder_commitment {
1356
+ res. push( ClaimableBalance :: PossiblyClaimableHTLCAwaitingTimeout {
1357
+ claimable_amount_satoshis: htlc. amount_msat / 1000 ,
1358
+ claimable_height: htlc. cltv_expiry,
1359
+ } ) ;
1360
+ } else {
1361
+ if us. payment_preimages. get( & htlc. payment_hash) . is_some( ) {
1362
+ if let Some ( conf_thresh) = us. onchain_events_awaiting_threshold_conf. iter( ) . find_map( |event| {
1363
+ if let OnchainEvent :: HTLCSpendConfirmation { input_idx, .. } = event. event {
1364
+ if input_idx == htlc_input_idx { Some ( event. confirmation_threshold( ) ) } else { None }
1365
+ } else { None }
1366
+ } ) {
1367
+ res. push( ClaimableBalance :: ClaimableAwaitingConfirmations {
1368
+ claimable_amount_satoshis: htlc. amount_msat / 1000 ,
1369
+ confirmation_height: conf_thresh,
1370
+ } ) ;
1371
+ } else {
1372
+ res. push( ClaimableBalance :: ContentiousClaimable {
1373
+ claimable_amount_satoshis: htlc. amount_msat / 1000 ,
1374
+ timeout_height: htlc. cltv_expiry,
1375
+ } ) ;
1376
+ }
1377
+ }
1378
+ }
1379
+ }
1380
+ }
1381
+ }
1382
+ }
1383
+
1384
+ if let Some ( txid) = confirmed_txid {
1385
+ if Some ( txid) == us. current_counterparty_commitment_txid || Some ( txid) == us. prev_counterparty_commitment_txid {
1386
+ walk_htlcs ! ( false , us. counterparty_claimable_outpoints. get( & txid) . unwrap( ) . iter( ) . map( |( a, _) | a) ) ;
1387
+ } else if txid == us. current_holder_commitment_tx . txid {
1388
+ walk_htlcs ! ( true , us. current_holder_commitment_tx. htlc_outputs. iter( ) . map( |( a, _, _) | a) ) ;
1389
+ } else if let Some ( prev_commitment) = & us. prev_holder_signed_commitment_tx {
1390
+ if txid == prev_commitment. txid {
1391
+ walk_htlcs ! ( true , prev_commitment. htlc_outputs. iter( ) . map( |( a, _, _) | a) ) ;
1392
+ }
1393
+ }
1394
+ // TODO: Add logic to provide claimable balances for counterparty broadcasting revoked
1395
+ // outputs.
1396
+ // Otherwise assume we closed with a cooperative close which only needs the
1397
+ // `ClaimableAwaitingConfirmations` balance pushed first.
1398
+ } else {
1399
+ let mut claimable_inbound_htlc_value_sat = 0 ;
1400
+ for ( htlc, _, _) in us. current_holder_commitment_tx . htlc_outputs . iter ( ) {
1401
+ if htlc. transaction_output_index . is_none ( ) { continue ; }
1402
+ if htlc. offered {
1403
+ res. push ( ClaimableBalance :: PossiblyClaimableHTLCAwaitingTimeout {
1404
+ claimable_amount_satoshis : htlc. amount_msat / 1000 ,
1405
+ claimable_height : htlc. cltv_expiry ,
1406
+ } ) ;
1407
+ } else {
1408
+ if us. payment_preimages . get ( & htlc. payment_hash ) . is_some ( ) {
1409
+ claimable_inbound_htlc_value_sat += htlc. amount_msat / 1000 ;
1410
+ }
1411
+ }
1412
+ }
1413
+ res. push ( ClaimableBalance :: ClaimableOnChannelClose {
1414
+ claimable_amount_satoshis : us. current_holder_commitment_tx . to_self_value_sat + claimable_inbound_htlc_value_sat,
1415
+ } ) ;
1416
+ }
1417
+
1418
+ res
1419
+ }
1269
1420
}
1270
1421
1271
1422
/// Compares a broadcasted commitment transaction's HTLCs with those in the latest state,
0 commit comments