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