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