@@ -620,6 +620,10 @@ pub enum Balance {
620
620
claimable_height : u32 ,
621
621
/// The payment hash whose preimage our counterparty needs to claim this HTLC.
622
622
payment_hash : PaymentHash ,
623
+ /// Whether this HTLC represents a payment which was sent outbound from us. Otherwise it
624
+ /// represents an HTLC which was forwarded (and should, thus, have a corresponding inbound
625
+ /// edge on another channel).
626
+ outbound_payment : bool ,
623
627
} ,
624
628
/// HTLCs which we received from our counterparty which are claimable with a preimage which we
625
629
/// do not currently have. This will only be claimable if we receive the preimage from the node
@@ -662,9 +666,9 @@ impl Balance {
662
666
Balance :: ContentiousClaimable { amount_satoshis, .. } |
663
667
Balance :: CounterpartyRevokedOutputClaimable { amount_satoshis, .. }
664
668
=> * amount_satoshis,
665
- Balance :: MaybeTimeoutClaimableHTLC { .. } |
666
- Balance :: MaybePreimageClaimableHTLC { .. }
667
- => 0 ,
669
+ Balance :: MaybeTimeoutClaimableHTLC { amount_satoshis , outbound_payment , .. }
670
+ => if * outbound_payment { 0 } else { * amount_satoshis } ,
671
+ Balance :: MaybePreimageClaimableHTLC { .. } => 0 ,
668
672
}
669
673
}
670
674
}
@@ -1674,9 +1678,10 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
1674
1678
impl < Signer : WriteableEcdsaChannelSigner > ChannelMonitorImpl < Signer > {
1675
1679
/// Helper for get_claimable_balances which does the work for an individual HTLC, generating up
1676
1680
/// to one `Balance` for the HTLC.
1677
- fn get_htlc_balance ( & self , htlc : & HTLCOutputInCommitment , holder_commitment : bool ,
1678
- counterparty_revoked_commitment : bool , confirmed_txid : Option < Txid > )
1679
- -> Option < Balance > {
1681
+ fn get_htlc_balance ( & self , htlc : & HTLCOutputInCommitment , source : Option < & HTLCSource > ,
1682
+ holder_commitment : bool , counterparty_revoked_commitment : bool ,
1683
+ confirmed_txid : Option < Txid >
1684
+ ) -> Option < Balance > {
1680
1685
let htlc_commitment_tx_output_idx =
1681
1686
if let Some ( v) = htlc. transaction_output_index { v } else { return None ; } ;
1682
1687
@@ -1801,10 +1806,19 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
1801
1806
confirmation_height : conf_thresh,
1802
1807
} ) ;
1803
1808
} else {
1809
+ let outbound_payment = match source {
1810
+ None => {
1811
+ debug_assert ! ( false , "Outbound HTLCs should have a source" ) ;
1812
+ true
1813
+ } ,
1814
+ Some ( & HTLCSource :: PreviousHopData ( _) ) => false ,
1815
+ Some ( & HTLCSource :: OutboundRoute { .. } ) => true ,
1816
+ } ;
1804
1817
return Some ( Balance :: MaybeTimeoutClaimableHTLC {
1805
1818
amount_satoshis : htlc. amount_msat / 1000 ,
1806
1819
claimable_height : htlc. cltv_expiry ,
1807
1820
payment_hash : htlc. payment_hash ,
1821
+ outbound_payment,
1808
1822
} ) ;
1809
1823
}
1810
1824
} else if let Some ( payment_preimage) = self . payment_preimages . get ( & htlc. payment_hash ) {
@@ -1878,10 +1892,12 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
1878
1892
1879
1893
macro_rules! walk_htlcs {
1880
1894
( $holder_commitment: expr, $counterparty_revoked_commitment: expr, $htlc_iter: expr) => {
1881
- for htlc in $htlc_iter {
1895
+ for ( htlc, source ) in $htlc_iter {
1882
1896
if htlc. transaction_output_index. is_some( ) {
1883
1897
1884
- if let Some ( bal) = us. get_htlc_balance( htlc, $holder_commitment, $counterparty_revoked_commitment, confirmed_txid) {
1898
+ if let Some ( bal) = us. get_htlc_balance(
1899
+ htlc, source, $holder_commitment, $counterparty_revoked_commitment, confirmed_txid
1900
+ ) {
1885
1901
res. push( bal) ;
1886
1902
}
1887
1903
}
@@ -1912,9 +1928,9 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
1912
1928
}
1913
1929
}
1914
1930
if Some ( txid) == us. current_counterparty_commitment_txid || Some ( txid) == us. prev_counterparty_commitment_txid {
1915
- walk_htlcs ! ( false , false , counterparty_tx_htlcs. iter( ) . map( |( a, _ ) | a ) ) ;
1931
+ walk_htlcs ! ( false , false , counterparty_tx_htlcs. iter( ) . map( |( a, b ) | ( a , b . as_ref ( ) . map ( |b| & * * b ) ) ) ) ;
1916
1932
} else {
1917
- walk_htlcs ! ( false , true , counterparty_tx_htlcs. iter( ) . map( |( a, _ ) | a ) ) ;
1933
+ walk_htlcs ! ( false , true , counterparty_tx_htlcs. iter( ) . map( |( a, b ) | ( a , b . as_ref ( ) . map ( |b| & * * b ) ) ) ) ;
1918
1934
// The counterparty broadcasted a revoked state!
1919
1935
// Look for any StaticOutputs first, generating claimable balances for those.
1920
1936
// If any match the confirmed counterparty revoked to_self output, skip
@@ -1954,7 +1970,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
1954
1970
}
1955
1971
found_commitment_tx = true ;
1956
1972
} else if txid == us. current_holder_commitment_tx . txid {
1957
- walk_htlcs ! ( true , false , us. current_holder_commitment_tx. htlc_outputs. iter( ) . map( |( a, _, _ ) | a ) ) ;
1973
+ walk_htlcs ! ( true , false , us. current_holder_commitment_tx. htlc_outputs. iter( ) . map( |( a, _, c ) | ( a , c . as_ref ( ) ) ) ) ;
1958
1974
if let Some ( conf_thresh) = pending_commitment_tx_conf_thresh {
1959
1975
res. push ( Balance :: ClaimableAwaitingConfirmations {
1960
1976
amount_satoshis : us. current_holder_commitment_tx . to_self_value_sat ,
@@ -1964,7 +1980,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
1964
1980
found_commitment_tx = true ;
1965
1981
} else if let Some ( prev_commitment) = & us. prev_holder_signed_commitment_tx {
1966
1982
if txid == prev_commitment. txid {
1967
- walk_htlcs ! ( true , false , prev_commitment. htlc_outputs. iter( ) . map( |( a, _, _ ) | a ) ) ;
1983
+ walk_htlcs ! ( true , false , prev_commitment. htlc_outputs. iter( ) . map( |( a, _, c ) | ( a , c . as_ref ( ) ) ) ) ;
1968
1984
if let Some ( conf_thresh) = pending_commitment_tx_conf_thresh {
1969
1985
res. push ( Balance :: ClaimableAwaitingConfirmations {
1970
1986
amount_satoshis : prev_commitment. to_self_value_sat ,
@@ -1987,13 +2003,22 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
1987
2003
}
1988
2004
} else {
1989
2005
let mut claimable_inbound_htlc_value_sat = 0 ;
1990
- for ( htlc, _, _ ) in us. current_holder_commitment_tx . htlc_outputs . iter ( ) {
2006
+ for ( htlc, _, source ) in us. current_holder_commitment_tx . htlc_outputs . iter ( ) {
1991
2007
if htlc. transaction_output_index . is_none ( ) { continue ; }
1992
2008
if htlc. offered {
2009
+ let outbound_payment = match source {
2010
+ None => {
2011
+ debug_assert ! ( false , "Outbound HTLCs should have a source" ) ;
2012
+ true
2013
+ } ,
2014
+ Some ( HTLCSource :: PreviousHopData ( _) ) => false ,
2015
+ Some ( HTLCSource :: OutboundRoute { .. } ) => true ,
2016
+ } ;
1993
2017
res. push ( Balance :: MaybeTimeoutClaimableHTLC {
1994
2018
amount_satoshis : htlc. amount_msat / 1000 ,
1995
2019
claimable_height : htlc. cltv_expiry ,
1996
2020
payment_hash : htlc. payment_hash ,
2021
+ outbound_payment,
1997
2022
} ) ;
1998
2023
} else if us. payment_preimages . get ( & htlc. payment_hash ) . is_some ( ) {
1999
2024
claimable_inbound_htlc_value_sat += htlc. amount_msat / 1000 ;
0 commit comments