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
@@ -590,6 +591,15 @@ pub enum Balance {
590
591
/// done so.
591
592
claimable_height : u32 ,
592
593
} ,
594
+ /// The channel has been closed, and our counterparty broadcasted a revoked commitment
595
+ /// transaction.
596
+ ///
597
+ /// Thus, we're able to claim all outputs in the commitment transaction, one of which has the
598
+ /// following amount.
599
+ CounterpartyRevokedOutputClaimable {
600
+ /// The amount, in satoshis, of the output which we can claim.
601
+ claimable_amount_satoshis : u64 ,
602
+ } ,
593
603
}
594
604
595
605
/// An HTLC which has been irrevocably resolved on-chain, and has reached ANTI_REORG_DELAY.
@@ -1410,9 +1420,9 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1410
1420
/// balance, or until our counterparty has claimed the balance and accrued several
1411
1421
/// confirmations on the claim transaction.
1412
1422
///
1413
- /// Note that the balances available when you or your counterparty have broadcasted revoked
1414
- /// state(s) may not be fully captured here.
1415
- // TODO, fix that ^
1423
+ /// Note that for `ChannelMonitors` which track a channel which went on-chain with versions of
1424
+ /// LDK prior to 0.0.108, balances may not be fully captured if our counterparty broadcasted
1425
+ /// a revoked state.
1416
1426
///
1417
1427
/// See [`Balance`] for additional details on the types of claimable balances which
1418
1428
/// may be returned here and their meanings.
@@ -1421,9 +1431,13 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1421
1431
let us = self . inner . lock ( ) . unwrap ( ) ;
1422
1432
1423
1433
let mut confirmed_txid = us. funding_spend_confirmed ;
1434
+ let mut confirmed_counterparty_output = us. confirmed_commitment_tx_counterparty_output ;
1424
1435
let mut pending_commitment_tx_conf_thresh = None ;
1425
1436
let funding_spend_pending = us. onchain_events_awaiting_threshold_conf . iter ( ) . find_map ( |event| {
1426
- if let OnchainEvent :: FundingSpendConfirmation { .. } = event. event {
1437
+ if let OnchainEvent :: FundingSpendConfirmation { commitment_tx_to_counterparty_output, .. } =
1438
+ event. event
1439
+ {
1440
+ confirmed_counterparty_output = commitment_tx_to_counterparty_output;
1427
1441
Some ( ( event. txid , event. confirmation_threshold ( ) ) )
1428
1442
} else { None }
1429
1443
} ) ;
@@ -1435,9 +1449,10 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1435
1449
}
1436
1450
1437
1451
macro_rules! walk_htlcs {
1438
- ( $holder_commitment: expr, $htlc_iter: expr) => {
1452
+ ( $holder_commitment: expr, $counterparty_revoked_commitment : expr , $ htlc_iter: expr) => {
1439
1453
for htlc in $htlc_iter {
1440
1454
if let Some ( htlc_commitment_tx_output_idx) = htlc. transaction_output_index {
1455
+ let mut htlc_spend_txid_opt = None ;
1441
1456
let mut htlc_update_pending = None ;
1442
1457
let mut htlc_spend_pending = None ;
1443
1458
let mut delayed_output_pending = None ;
@@ -1446,6 +1461,7 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1446
1461
OnchainEvent :: HTLCUpdate {
1447
1462
commitment_tx_output_idx, onchain_value_satoshis, htlc_value_satoshis, .. }
1448
1463
if commitment_tx_output_idx == Some ( htlc_commitment_tx_output_idx) => {
1464
+ htlc_spend_txid_opt = event. transaction. as_ref( ) . map( |tx| tx. txid( ) ) ;
1449
1465
debug_assert!( htlc_update_pending. is_none( ) ) ;
1450
1466
htlc_update_pending = Some ( (
1451
1467
onchain_value_satoshis. unwrap_or( htlc_value_satoshis. unwrap( ) ) ,
@@ -1454,6 +1470,7 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1454
1470
OnchainEvent :: HTLCSpendConfirmation {
1455
1471
commitment_tx_output_idx, preimage, onchain_value_satoshis, .. }
1456
1472
if commitment_tx_output_idx == htlc_commitment_tx_output_idx => {
1473
+ htlc_spend_txid_opt = event. transaction. as_ref( ) . map( |tx| tx. txid( ) ) ;
1457
1474
debug_assert!( htlc_spend_pending. is_none( ) ) ;
1458
1475
htlc_spend_pending = Some ( ( event. confirmation_threshold( ) ,
1459
1476
preimage. is_some( ) , onchain_value_satoshis) ) ;
@@ -1468,22 +1485,80 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1468
1485
}
1469
1486
}
1470
1487
let htlc_resolved = us. htlcs_resolved_on_chain. iter( )
1471
- . find( |v| v. commitment_tx_output_idx == htlc_commitment_tx_output_idx) ;
1488
+ . find( |v| if v. commitment_tx_output_idx == htlc_commitment_tx_output_idx {
1489
+ if v. resolving_txid != confirmed_txid {
1490
+ htlc_spend_txid_opt = v. resolving_txid;
1491
+ }
1492
+ true
1493
+ } else { false } ) ;
1472
1494
debug_assert!( htlc_update_pending. is_some( ) as u8 + htlc_spend_pending. is_some( ) as u8 + htlc_resolved. is_some( ) as u8 <= 1 ) ;
1473
1495
1496
+ let htlc_output_needs_spending =
1497
+ us. onchain_tx_handler. is_output_spend_pending( &
1498
+ if let Some ( txid) = htlc_spend_txid_opt {
1499
+ debug_assert!(
1500
+ us. onchain_tx_handler. channel_transaction_parameters. opt_anchors. is_none( ) ,
1501
+ "This code needs updating for anchors" ) ;
1502
+ BitcoinOutPoint :: new( txid, 0 )
1503
+ } else {
1504
+ BitcoinOutPoint :: new( confirmed_txid. unwrap( ) , htlc_commitment_tx_output_idx)
1505
+ } ) ;
1506
+
1474
1507
if let Some ( conf_thresh) = delayed_output_pending {
1475
1508
debug_assert!( $holder_commitment) ;
1476
1509
res. push( Balance :: ClaimableAwaitingConfirmations {
1477
1510
claimable_amount_satoshis: htlc. amount_msat / 1000 ,
1478
1511
confirmation_height: conf_thresh,
1479
1512
} ) ;
1480
- } else if htlc_resolved. is_some( ) {
1513
+ } else if htlc_resolved. is_some( ) && !htlc_output_needs_spending {
1481
1514
// Funding transaction spends should be fully confirmed by the time any
1482
1515
// HTLC transactions are resolved, unless we're talking about a holder
1483
1516
// commitment tx, whose resolution is delayed until the CSV timeout is
1484
1517
// reached, even though HTLCs may be resolved after only
1485
1518
// ANTI_REORG_DELAY confirmations.
1486
1519
debug_assert!( $holder_commitment || us. funding_spend_confirmed. is_some( ) ) ;
1520
+ } else if $counterparty_revoked_commitment {
1521
+ let htlc_output_claim_pending = us. onchain_events_awaiting_threshold_conf. iter( ) . find_map( |event| {
1522
+ if let OnchainEvent :: MaturingOutput {
1523
+ descriptor: SpendableOutputDescriptor :: StaticOutput { .. }
1524
+ } = & event. event {
1525
+ if event. transaction. as_ref( ) . map( |tx| tx. input. iter( ) . any( |inp| {
1526
+ if let Some ( htlc_spend_txid) = htlc_spend_txid_opt {
1527
+ Some ( tx. txid( ) ) == htlc_spend_txid_opt ||
1528
+ inp. previous_output. txid == htlc_spend_txid
1529
+ } else {
1530
+ Some ( inp. previous_output. txid) == confirmed_txid &&
1531
+ inp. previous_output. vout == htlc_commitment_tx_output_idx
1532
+ }
1533
+ } ) ) . unwrap_or( false ) {
1534
+ Some ( ( ) )
1535
+ } else { None }
1536
+ } else { None }
1537
+ } ) ;
1538
+ if htlc_output_claim_pending. is_some( ) {
1539
+ // We already push `Balance`s onto the `res` list for every
1540
+ // `StaticOutput` in a `MaturingOutput` in the revoked
1541
+ // counterparty commitment transaction case generally, so don't
1542
+ // need to do so again here.
1543
+ } else if let Some ( ( _, _, amount_sats_option) ) = htlc_spend_pending {
1544
+ if let Some ( amount_sats) = amount_sats_option {
1545
+ res. push( Balance :: CounterpartyRevokedOutputClaimable {
1546
+ claimable_amount_satoshis: amount_sats,
1547
+ } ) ;
1548
+ }
1549
+ } else if let Some ( Some ( val) ) = htlc_resolved. map( |v| v. onchain_value_satoshis) {
1550
+ res. push( Balance :: CounterpartyRevokedOutputClaimable {
1551
+ claimable_amount_satoshis: val,
1552
+ } ) ;
1553
+ } else {
1554
+ debug_assert!( htlc_update_pending. is_none( ) ,
1555
+ "HTLCUpdate OnchainEvents should never appear for preimage claims" ) ;
1556
+ debug_assert!( htlc_spend_pending. is_none( ) || !htlc_spend_pending. unwrap( ) . 1 ,
1557
+ "We don't (currently) generate preimage claims against revoked outputs, where did you get one?!" ) ;
1558
+ res. push( Balance :: CounterpartyRevokedOutputClaimable {
1559
+ claimable_amount_satoshis: htlc. amount_msat / 1000 ,
1560
+ } ) ;
1561
+ }
1487
1562
} else {
1488
1563
if htlc. offered == $holder_commitment {
1489
1564
// If the payment was outbound, check if there's an HTLCUpdate
@@ -1527,8 +1602,8 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1527
1602
1528
1603
if let Some ( txid) = confirmed_txid {
1529
1604
let mut found_commitment_tx = false ;
1530
- if Some ( txid ) == us. current_counterparty_commitment_txid || Some ( txid) == us . prev_counterparty_commitment_txid {
1531
- walk_htlcs ! ( false , us . counterparty_claimable_outpoints . get ( & txid ) . unwrap ( ) . iter ( ) . map ( | ( a , _ ) | a ) ) ;
1605
+ if let Some ( counterparty_tx_htlcs ) = us. counterparty_claimable_outpoints . get ( & txid) {
1606
+ // First look for the to_remote output back to us.
1532
1607
if let Some ( conf_thresh) = pending_commitment_tx_conf_thresh {
1533
1608
if let Some ( value) = us. onchain_events_awaiting_threshold_conf . iter ( ) . find_map ( |event| {
1534
1609
if let OnchainEvent :: MaturingOutput {
@@ -1547,9 +1622,50 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1547
1622
// confirmation with the same height or have never met our dust amount.
1548
1623
}
1549
1624
}
1625
+ if Some ( txid) == us. current_counterparty_commitment_txid || Some ( txid) == us. prev_counterparty_commitment_txid {
1626
+ walk_htlcs ! ( false , false , counterparty_tx_htlcs. iter( ) . map( |( a, _) | a) ) ;
1627
+ } else {
1628
+ walk_htlcs ! ( false , true , counterparty_tx_htlcs. iter( ) . map( |( a, _) | a) ) ;
1629
+ // The counterparty broadcasted a revoked state!
1630
+ // Look for a StaticOutput spend first, as it should be spending the full set
1631
+ // of commitment transaction outputs, if we see one, assume that it did and
1632
+ // just return that.
1633
+ let mut spent_counterparty_output = false ;
1634
+ for event in us. onchain_events_awaiting_threshold_conf . iter ( ) {
1635
+ if let OnchainEvent :: MaturingOutput {
1636
+ descriptor : SpendableOutputDescriptor :: StaticOutput { output, .. }
1637
+ } = & event. event {
1638
+ res. push ( Balance :: ClaimableAwaitingConfirmations {
1639
+ claimable_amount_satoshis : output. value ,
1640
+ confirmation_height : event. confirmation_threshold ( ) ,
1641
+ } ) ;
1642
+ if let Some ( confirmed_to_self_idx) = confirmed_counterparty_output. map ( |( idx, _) | idx) {
1643
+ if event. transaction . as_ref ( ) . map ( |tx|
1644
+ tx. input . iter ( ) . any ( |inp| inp. previous_output . vout == confirmed_to_self_idx)
1645
+ ) . unwrap_or ( false ) {
1646
+ spent_counterparty_output = true ;
1647
+ }
1648
+ }
1649
+ }
1650
+ }
1651
+
1652
+ if spent_counterparty_output {
1653
+ } else if let Some ( ( confirmed_to_self_idx, amt) ) = confirmed_counterparty_output {
1654
+ let output_spendable = us. onchain_tx_handler
1655
+ . is_output_spend_pending ( & BitcoinOutPoint :: new ( txid, confirmed_to_self_idx) ) ;
1656
+ if output_spendable {
1657
+ res. push ( Balance :: CounterpartyRevokedOutputClaimable {
1658
+ claimable_amount_satoshis : amt,
1659
+ } ) ;
1660
+ }
1661
+ } else {
1662
+ // Counterparty output is missing, either it was broadcasted on a
1663
+ // previous version of LDK or the counterparty hadn't met dust.
1664
+ }
1665
+ }
1550
1666
found_commitment_tx = true ;
1551
1667
} else if txid == us. current_holder_commitment_tx . txid {
1552
- walk_htlcs ! ( true , us. current_holder_commitment_tx. htlc_outputs. iter( ) . map( |( a, _, _) | a) ) ;
1668
+ walk_htlcs ! ( true , false , us. current_holder_commitment_tx. htlc_outputs. iter( ) . map( |( a, _, _) | a) ) ;
1553
1669
if let Some ( conf_thresh) = pending_commitment_tx_conf_thresh {
1554
1670
res. push ( Balance :: ClaimableAwaitingConfirmations {
1555
1671
claimable_amount_satoshis : us. current_holder_commitment_tx . to_self_value_sat ,
@@ -1559,7 +1675,7 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1559
1675
found_commitment_tx = true ;
1560
1676
} else if let Some ( prev_commitment) = & us. prev_holder_signed_commitment_tx {
1561
1677
if txid == prev_commitment. txid {
1562
- walk_htlcs ! ( true , prev_commitment. htlc_outputs. iter( ) . map( |( a, _, _) | a) ) ;
1678
+ walk_htlcs ! ( true , false , prev_commitment. htlc_outputs. iter( ) . map( |( a, _, _) | a) ) ;
1563
1679
if let Some ( conf_thresh) = pending_commitment_tx_conf_thresh {
1564
1680
res. push ( Balance :: ClaimableAwaitingConfirmations {
1565
1681
claimable_amount_satoshis : prev_commitment. to_self_value_sat ,
@@ -1580,8 +1696,6 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1580
1696
} ) ;
1581
1697
}
1582
1698
}
1583
- // TODO: Add logic to provide claimable balances for counterparty broadcasting revoked
1584
- // outputs.
1585
1699
} else {
1586
1700
let mut claimable_inbound_htlc_value_sat = 0 ;
1587
1701
for ( htlc, _, _) in us. current_holder_commitment_tx . htlc_outputs . iter ( ) {
0 commit comments