@@ -620,6 +620,10 @@ pub enum Balance {
620620 claimable_height : u32 ,
621621 /// The payment hash whose preimage our counterparty needs to claim this HTLC.
622622 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 ,
623627 } ,
624628 /// HTLCs which we received from our counterparty which are claimable with a preimage which we
625629 /// do not currently have. This will only be claimable if we receive the preimage from the node
@@ -662,9 +666,9 @@ impl Balance {
662666 Balance :: ContentiousClaimable { amount_satoshis, .. } |
663667 Balance :: CounterpartyRevokedOutputClaimable { amount_satoshis, .. }
664668 => * 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 ,
668672 }
669673 }
670674}
@@ -1674,9 +1678,10 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
16741678impl < Signer : WriteableEcdsaChannelSigner > ChannelMonitorImpl < Signer > {
16751679 /// Helper for get_claimable_balances which does the work for an individual HTLC, generating up
16761680 /// 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 > {
16801685 let htlc_commitment_tx_output_idx =
16811686 if let Some ( v) = htlc. transaction_output_index { v } else { return None ; } ;
16821687
@@ -1801,10 +1806,19 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
18011806 confirmation_height : conf_thresh,
18021807 } ) ;
18031808 } 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+ } ;
18041817 return Some ( Balance :: MaybeTimeoutClaimableHTLC {
18051818 amount_satoshis : htlc. amount_msat / 1000 ,
18061819 claimable_height : htlc. cltv_expiry ,
18071820 payment_hash : htlc. payment_hash ,
1821+ outbound_payment,
18081822 } ) ;
18091823 }
18101824 } else if let Some ( payment_preimage) = self . payment_preimages . get ( & htlc. payment_hash ) {
@@ -1878,10 +1892,12 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
18781892
18791893 macro_rules! walk_htlcs {
18801894 ( $holder_commitment: expr, $counterparty_revoked_commitment: expr, $htlc_iter: expr) => {
1881- for htlc in $htlc_iter {
1895+ for ( htlc, source ) in $htlc_iter {
18821896 if htlc. transaction_output_index. is_some( ) {
18831897
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+ ) {
18851901 res. push( bal) ;
18861902 }
18871903 }
@@ -1912,9 +1928,9 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
19121928 }
19131929 }
19141930 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 ) ) ) ) ;
19161932 } 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 ) ) ) ) ;
19181934 // The counterparty broadcasted a revoked state!
19191935 // Look for any StaticOutputs first, generating claimable balances for those.
19201936 // If any match the confirmed counterparty revoked to_self output, skip
@@ -1954,7 +1970,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
19541970 }
19551971 found_commitment_tx = true ;
19561972 } 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 ( ) ) ) ) ;
19581974 if let Some ( conf_thresh) = pending_commitment_tx_conf_thresh {
19591975 res. push ( Balance :: ClaimableAwaitingConfirmations {
19601976 amount_satoshis : us. current_holder_commitment_tx . to_self_value_sat ,
@@ -1964,7 +1980,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
19641980 found_commitment_tx = true ;
19651981 } else if let Some ( prev_commitment) = & us. prev_holder_signed_commitment_tx {
19661982 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 ( ) ) ) ) ;
19681984 if let Some ( conf_thresh) = pending_commitment_tx_conf_thresh {
19691985 res. push ( Balance :: ClaimableAwaitingConfirmations {
19701986 amount_satoshis : prev_commitment. to_self_value_sat ,
@@ -1987,13 +2003,22 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
19872003 }
19882004 } else {
19892005 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 ( ) {
19912007 if htlc. transaction_output_index . is_none ( ) { continue ; }
19922008 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+ } ;
19932017 res. push ( Balance :: MaybeTimeoutClaimableHTLC {
19942018 amount_satoshis : htlc. amount_msat / 1000 ,
19952019 claimable_height : htlc. cltv_expiry ,
19962020 payment_hash : htlc. payment_hash ,
2021+ outbound_payment,
19972022 } ) ;
19982023 } else if us. payment_preimages . get ( & htlc. payment_hash ) . is_some ( ) {
19992024 claimable_inbound_htlc_value_sat += htlc. amount_msat / 1000 ;
0 commit comments