22
22
23
23
use bitcoin:: blockdata:: block:: BlockHeader ;
24
24
use bitcoin:: blockdata:: transaction:: { TxOut , Transaction } ;
25
+ use bitcoin:: blockdata:: transaction:: OutPoint as BitcoinOutPoint ;
25
26
use bitcoin:: blockdata:: script:: { Script , Builder } ;
26
27
use bitcoin:: blockdata:: opcodes;
27
28
@@ -382,6 +383,9 @@ enum OnchainEvent {
382
383
on_local_output_csv : Option < u16 > ,
383
384
/// If the funding spend transaction was a known remote commitment transaction, we track
384
385
/// the output index and amount of the counterparty's `to_self` output here.
386
+ ///
387
+ /// This allows us to generate a [`Balance::CounterpartyRevokedOutputClaimable`] for the
388
+ /// counterparty output.
385
389
commitment_tx_to_counterparty_output : CommitmentTxCounterpartyOutputInfo ,
386
390
} ,
387
391
/// A spend of a commitment transaction HTLC output, set in the cases where *no* `HTLCUpdate`
@@ -582,6 +586,18 @@ pub enum Balance {
582
586
/// done so.
583
587
claimable_height : u32 ,
584
588
} ,
589
+ /// The channel has been closed, and our counterparty broadcasted a revoked commitment
590
+ /// transaction.
591
+ ///
592
+ /// Thus, we're able to claim all outputs in the commitment transaction, one of which has the
593
+ /// following amount.
594
+ CounterpartyRevokedOutputClaimable {
595
+ /// The amount, in satoshis, of the output which we can claim.
596
+ ///
597
+ /// Note that for outputs from HTLC balances this may be excluding some on-chain fees that
598
+ /// were already spent.
599
+ claimable_amount_satoshis : u64 ,
600
+ } ,
585
601
}
586
602
587
603
/// An HTLC which has been irrevocably resolved on-chain, and has reached ANTI_REORG_DELAY.
@@ -1412,9 +1428,9 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1412
1428
/// balance, or until our counterparty has claimed the balance and accrued several
1413
1429
/// confirmations on the claim transaction.
1414
1430
///
1415
- /// Note that the balances available when you or your counterparty have broadcasted revoked
1416
- /// state(s) may not be fully captured here.
1417
- // TODO, fix that ^
1431
+ /// Note that for `ChannelMonitors` which track a channel which went on-chain with versions of
1432
+ /// LDK prior to 0.0.108, balances may not be fully captured if our counterparty broadcasted
1433
+ /// a revoked state.
1418
1434
///
1419
1435
/// See [`Balance`] for additional details on the types of claimable balances which
1420
1436
/// may be returned here and their meanings.
@@ -1423,9 +1439,13 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1423
1439
let us = self . inner . lock ( ) . unwrap ( ) ;
1424
1440
1425
1441
let mut confirmed_txid = us. funding_spend_confirmed ;
1442
+ let mut confirmed_counterparty_output = us. confirmed_commitment_tx_counterparty_output ;
1426
1443
let mut pending_commitment_tx_conf_thresh = None ;
1427
1444
let funding_spend_pending = us. onchain_events_awaiting_threshold_conf . iter ( ) . find_map ( |event| {
1428
- if let OnchainEvent :: FundingSpendConfirmation { .. } = event. event {
1445
+ if let OnchainEvent :: FundingSpendConfirmation { commitment_tx_to_counterparty_output, .. } =
1446
+ event. event
1447
+ {
1448
+ confirmed_counterparty_output = commitment_tx_to_counterparty_output;
1429
1449
Some ( ( event. txid , event. confirmation_threshold ( ) ) )
1430
1450
} else { None }
1431
1451
} ) ;
@@ -1437,22 +1457,27 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1437
1457
}
1438
1458
1439
1459
macro_rules! walk_htlcs {
1440
- ( $holder_commitment: expr, $htlc_iter: expr) => {
1460
+ ( $holder_commitment: expr, $counterparty_revoked_commitment : expr , $ htlc_iter: expr) => {
1441
1461
for htlc in $htlc_iter {
1442
1462
if let Some ( htlc_commitment_tx_output_idx) = htlc. transaction_output_index {
1463
+ let mut htlc_spend_txid_opt = None ;
1443
1464
let mut htlc_update_pending = None ;
1444
1465
let mut htlc_spend_pending = None ;
1445
1466
let mut delayed_output_pending = None ;
1446
1467
for event in us. onchain_events_awaiting_threshold_conf. iter( ) {
1447
1468
match event. event {
1448
1469
OnchainEvent :: HTLCUpdate { commitment_tx_output_idx, htlc_value_satoshis, .. }
1449
1470
if commitment_tx_output_idx == Some ( htlc_commitment_tx_output_idx) => {
1471
+ debug_assert!( htlc_spend_txid_opt. is_none( ) ) ;
1472
+ htlc_spend_txid_opt = event. transaction. as_ref( ) . map( |tx| tx. txid( ) ) ;
1450
1473
debug_assert!( htlc_update_pending. is_none( ) ) ;
1451
1474
debug_assert_eq!( htlc_value_satoshis. unwrap( ) , htlc. amount_msat / 1000 ) ;
1452
1475
htlc_update_pending = Some ( event. confirmation_threshold( ) ) ;
1453
1476
} ,
1454
1477
OnchainEvent :: HTLCSpendConfirmation { commitment_tx_output_idx, preimage, .. }
1455
1478
if commitment_tx_output_idx == htlc_commitment_tx_output_idx => {
1479
+ debug_assert!( htlc_spend_txid_opt. is_none( ) ) ;
1480
+ htlc_spend_txid_opt = event. transaction. as_ref( ) . map( |tx| tx. txid( ) ) ;
1456
1481
debug_assert!( htlc_spend_pending. is_none( ) ) ;
1457
1482
htlc_spend_pending = Some ( ( event. confirmation_threshold( ) , preimage. is_some( ) ) ) ;
1458
1483
} ,
@@ -1466,22 +1491,69 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1466
1491
}
1467
1492
}
1468
1493
let htlc_resolved = us. htlcs_resolved_on_chain. iter( )
1469
- . find( |v| v. commitment_tx_output_idx == htlc_commitment_tx_output_idx) ;
1494
+ . find( |v| if v. commitment_tx_output_idx == htlc_commitment_tx_output_idx {
1495
+ debug_assert!( htlc_spend_txid_opt. is_none( ) ) ;
1496
+ htlc_spend_txid_opt = v. resolving_txid;
1497
+ true
1498
+ } else { false } ) ;
1470
1499
debug_assert!( htlc_update_pending. is_some( ) as u8 + htlc_spend_pending. is_some( ) as u8 + htlc_resolved. is_some( ) as u8 <= 1 ) ;
1471
1500
1501
+ let htlc_output_to_spend =
1502
+ if let Some ( txid) = htlc_spend_txid_opt {
1503
+ debug_assert!(
1504
+ us. onchain_tx_handler. channel_transaction_parameters. opt_anchors. is_none( ) ,
1505
+ "This code needs updating for anchors" ) ;
1506
+ BitcoinOutPoint :: new( txid, 0 )
1507
+ } else {
1508
+ BitcoinOutPoint :: new( confirmed_txid. unwrap( ) , htlc_commitment_tx_output_idx)
1509
+ } ;
1510
+ let htlc_output_needs_spending = us. onchain_tx_handler. is_output_spend_pending( & htlc_output_to_spend) ;
1511
+
1472
1512
if let Some ( conf_thresh) = delayed_output_pending {
1473
1513
debug_assert!( $holder_commitment) ;
1474
1514
res. push( Balance :: ClaimableAwaitingConfirmations {
1475
1515
claimable_amount_satoshis: htlc. amount_msat / 1000 ,
1476
1516
confirmation_height: conf_thresh,
1477
1517
} ) ;
1478
- } else if htlc_resolved. is_some( ) {
1518
+ } else if htlc_resolved. is_some( ) && !htlc_output_needs_spending {
1479
1519
// Funding transaction spends should be fully confirmed by the time any
1480
1520
// HTLC transactions are resolved, unless we're talking about a holder
1481
1521
// commitment tx, whose resolution is delayed until the CSV timeout is
1482
1522
// reached, even though HTLCs may be resolved after only
1483
1523
// ANTI_REORG_DELAY confirmations.
1484
1524
debug_assert!( $holder_commitment || us. funding_spend_confirmed. is_some( ) ) ;
1525
+ } else if $counterparty_revoked_commitment {
1526
+ let htlc_output_claim_pending = us. onchain_events_awaiting_threshold_conf. iter( ) . find_map( |event| {
1527
+ if let OnchainEvent :: MaturingOutput {
1528
+ descriptor: SpendableOutputDescriptor :: StaticOutput { .. }
1529
+ } = & event. event {
1530
+ if event. transaction. as_ref( ) . map( |tx| tx. input. iter( ) . any( |inp| {
1531
+ if let Some ( htlc_spend_txid) = htlc_spend_txid_opt {
1532
+ Some ( tx. txid( ) ) == htlc_spend_txid_opt ||
1533
+ inp. previous_output. txid == htlc_spend_txid
1534
+ } else {
1535
+ Some ( inp. previous_output. txid) == confirmed_txid &&
1536
+ inp. previous_output. vout == htlc_commitment_tx_output_idx
1537
+ }
1538
+ } ) ) . unwrap_or( false ) {
1539
+ Some ( ( ) )
1540
+ } else { None }
1541
+ } else { None }
1542
+ } ) ;
1543
+ if htlc_output_claim_pending. is_some( ) {
1544
+ // We already push `Balance`s onto the `res` list for every
1545
+ // `StaticOutput` in a `MaturingOutput` in the revoked
1546
+ // counterparty commitment transaction case generally, so don't
1547
+ // need to do so again here.
1548
+ } else {
1549
+ debug_assert!( htlc_update_pending. is_none( ) ,
1550
+ "HTLCUpdate OnchainEvents should never appear for preimage claims" ) ;
1551
+ debug_assert!( !htlc. offered || htlc_spend_pending. is_none( ) || !htlc_spend_pending. unwrap( ) . 1 ,
1552
+ "We don't (currently) generate preimage claims against revoked outputs, where did you get one?!" ) ;
1553
+ res. push( Balance :: CounterpartyRevokedOutputClaimable {
1554
+ claimable_amount_satoshis: htlc. amount_msat / 1000 ,
1555
+ } ) ;
1556
+ }
1485
1557
} else {
1486
1558
if htlc. offered == $holder_commitment {
1487
1559
// If the payment was outbound, check if there's an HTLCUpdate
@@ -1525,8 +1597,8 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1525
1597
1526
1598
if let Some ( txid) = confirmed_txid {
1527
1599
let mut found_commitment_tx = false ;
1528
- if Some ( txid ) == us. current_counterparty_commitment_txid || Some ( txid) == us . prev_counterparty_commitment_txid {
1529
- walk_htlcs ! ( false , us . counterparty_claimable_outpoints . get ( & txid ) . unwrap ( ) . iter ( ) . map ( | ( a , _ ) | a ) ) ;
1600
+ if let Some ( counterparty_tx_htlcs ) = us. counterparty_claimable_outpoints . get ( & txid) {
1601
+ // First look for the to_remote output back to us.
1530
1602
if let Some ( conf_thresh) = pending_commitment_tx_conf_thresh {
1531
1603
if let Some ( value) = us. onchain_events_awaiting_threshold_conf . iter ( ) . find_map ( |event| {
1532
1604
if let OnchainEvent :: MaturingOutput {
@@ -1545,9 +1617,50 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1545
1617
// confirmation with the same height or have never met our dust amount.
1546
1618
}
1547
1619
}
1620
+ if Some ( txid) == us. current_counterparty_commitment_txid || Some ( txid) == us. prev_counterparty_commitment_txid {
1621
+ walk_htlcs ! ( false , false , counterparty_tx_htlcs. iter( ) . map( |( a, _) | a) ) ;
1622
+ } else {
1623
+ walk_htlcs ! ( false , true , counterparty_tx_htlcs. iter( ) . map( |( a, _) | a) ) ;
1624
+ // The counterparty broadcasted a revoked state!
1625
+ // Look for any StaticOutputs first, generating claimable balances for those.
1626
+ // If any match the confirmed counterparty revoked to_self output, skip
1627
+ // generating a CounterpartyRevokedOutputClaimable.
1628
+ let mut spent_counterparty_output = false ;
1629
+ for event in us. onchain_events_awaiting_threshold_conf . iter ( ) {
1630
+ if let OnchainEvent :: MaturingOutput {
1631
+ descriptor : SpendableOutputDescriptor :: StaticOutput { output, .. }
1632
+ } = & event. event {
1633
+ res. push ( Balance :: ClaimableAwaitingConfirmations {
1634
+ claimable_amount_satoshis : output. value ,
1635
+ confirmation_height : event. confirmation_threshold ( ) ,
1636
+ } ) ;
1637
+ if let Some ( confirmed_to_self_idx) = confirmed_counterparty_output. map ( |( idx, _) | idx) {
1638
+ if event. transaction . as_ref ( ) . map ( |tx|
1639
+ tx. input . iter ( ) . any ( |inp| inp. previous_output . vout == confirmed_to_self_idx)
1640
+ ) . unwrap_or ( false ) {
1641
+ spent_counterparty_output = true ;
1642
+ }
1643
+ }
1644
+ }
1645
+ }
1646
+
1647
+ if spent_counterparty_output {
1648
+ } else if let Some ( ( confirmed_to_self_idx, amt) ) = confirmed_counterparty_output {
1649
+ let output_spendable = us. onchain_tx_handler
1650
+ . is_output_spend_pending ( & BitcoinOutPoint :: new ( txid, confirmed_to_self_idx) ) ;
1651
+ if output_spendable {
1652
+ res. push ( Balance :: CounterpartyRevokedOutputClaimable {
1653
+ claimable_amount_satoshis : amt,
1654
+ } ) ;
1655
+ }
1656
+ } else {
1657
+ // Counterparty output is missing, either it was broadcasted on a
1658
+ // previous version of LDK or the counterparty hadn't met dust.
1659
+ }
1660
+ }
1548
1661
found_commitment_tx = true ;
1549
1662
} else if txid == us. current_holder_commitment_tx . txid {
1550
- walk_htlcs ! ( true , us. current_holder_commitment_tx. htlc_outputs. iter( ) . map( |( a, _, _) | a) ) ;
1663
+ walk_htlcs ! ( true , false , us. current_holder_commitment_tx. htlc_outputs. iter( ) . map( |( a, _, _) | a) ) ;
1551
1664
if let Some ( conf_thresh) = pending_commitment_tx_conf_thresh {
1552
1665
res. push ( Balance :: ClaimableAwaitingConfirmations {
1553
1666
claimable_amount_satoshis : us. current_holder_commitment_tx . to_self_value_sat ,
@@ -1557,7 +1670,7 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1557
1670
found_commitment_tx = true ;
1558
1671
} else if let Some ( prev_commitment) = & us. prev_holder_signed_commitment_tx {
1559
1672
if txid == prev_commitment. txid {
1560
- walk_htlcs ! ( true , prev_commitment. htlc_outputs. iter( ) . map( |( a, _, _) | a) ) ;
1673
+ walk_htlcs ! ( true , false , prev_commitment. htlc_outputs. iter( ) . map( |( a, _, _) | a) ) ;
1561
1674
if let Some ( conf_thresh) = pending_commitment_tx_conf_thresh {
1562
1675
res. push ( Balance :: ClaimableAwaitingConfirmations {
1563
1676
claimable_amount_satoshis : prev_commitment. to_self_value_sat ,
@@ -1578,8 +1691,6 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1578
1691
} ) ;
1579
1692
}
1580
1693
}
1581
- // TODO: Add logic to provide claimable balances for counterparty broadcasting revoked
1582
- // outputs.
1583
1694
} else {
1584
1695
let mut claimable_inbound_htlc_value_sat = 0 ;
1585
1696
for ( htlc, _, _) in us. current_holder_commitment_tx . htlc_outputs . iter ( ) {
0 commit comments