@@ -1713,67 +1713,84 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1713
1713
incoming_packet_shared_secret : incoming_shared_secret,
1714
1714
} ;
1715
1715
1716
- let mut total_value = 0 ;
1717
- let payment_secret_opt =
1718
- if let & Some ( ref data) = & payment_data { Some ( data. payment_secret . clone ( ) ) } else { None } ;
1719
- let htlcs = channel_state. claimable_htlcs . entry ( ( payment_hash, payment_secret_opt) )
1720
- . or_insert ( Vec :: new ( ) ) ;
1721
- htlcs. push ( ClaimableHTLC {
1722
- prev_hop,
1723
- value : amt_to_forward,
1724
- payment_data : payment_data. clone ( ) ,
1725
- } ) ;
1726
- if let & Some ( ref data) = & payment_data {
1727
- for htlc in htlcs. iter ( ) {
1728
- total_value += htlc. value ;
1729
- if htlc. payment_data . as_ref ( ) . unwrap ( ) . total_msat != data. total_msat {
1730
- total_value = msgs:: MAX_VALUE_MSAT ;
1731
- }
1716
+ let mut total_value = 0 ;
1717
+ let payment_secret_opt = if let & Some ( ref data) = & payment_data {
1718
+ Some ( data. payment_secret . clone ( ) )
1719
+ } else {
1720
+ None
1721
+ } ;
1722
+ let htlcs = channel_state
1723
+ . claimable_htlcs
1724
+ . entry ( ( payment_hash, payment_secret_opt) )
1725
+ . or_insert ( Vec :: new ( ) ) ;
1726
+ htlcs. push ( ClaimableHTLC {
1727
+ prev_hop,
1728
+ value : amt_to_forward,
1729
+ payment_data : payment_data. clone ( ) ,
1730
+ } ) ;
1731
+ if let & Some ( ref data) = & payment_data {
1732
+ for htlc in htlcs. iter ( ) {
1733
+ total_value += htlc. value ;
1734
+ if htlc. payment_data . as_ref ( ) . unwrap ( ) . total_msat
1735
+ != data. total_msat
1736
+ {
1737
+ total_value = msgs:: MAX_VALUE_MSAT ;
1738
+ }
1732
1739
if total_value >= msgs:: MAX_VALUE_MSAT { break ; }
1733
- }
1740
+ }
1734
1741
if total_value >= msgs:: MAX_VALUE_MSAT || total_value > data. total_msat {
1735
- for htlc in htlcs. iter ( ) {
1736
- let mut htlc_msat_height_data = byte_utils:: be64_to_array ( htlc. value ) . to_vec ( ) ;
1737
- htlc_msat_height_data. extend_from_slice (
1738
- & byte_utils:: be32_to_array (
1739
- self . latest_block_height . load ( Ordering :: Acquire )
1740
- as u32 ,
1741
- ) ,
1742
- ) ;
1743
- failed_forwards. push ( ( HTLCSource :: PreviousHopData ( HTLCPreviousHopData {
1744
- short_channel_id : htlc. prev_hop . short_channel_id ,
1745
- htlc_id : htlc. prev_hop . htlc_id ,
1746
- incoming_packet_shared_secret : htlc. prev_hop . incoming_packet_shared_secret ,
1747
- } ) , payment_hash,
1748
- HTLCFailReason :: Reason { failure_code : 0x4000 | 15 , data : htlc_msat_height_data }
1749
- ) ) ;
1750
- }
1751
- } else if total_value == data. total_msat {
1752
- new_events. push ( events:: Event :: PaymentReceived {
1753
- payment_hash : payment_hash,
1754
- payment_secret : Some ( data. payment_secret ) ,
1755
- amt : total_value,
1756
- } ) ;
1757
- }
1758
- } else {
1759
- new_events. push ( events:: Event :: PaymentReceived {
1760
- payment_hash : payment_hash,
1761
- payment_secret : None ,
1762
- amt : amt_to_forward,
1763
- } ) ;
1764
- }
1742
+ for htlc in htlcs. iter ( ) {
1743
+ let mut htlc_msat_height_data =
1744
+ byte_utils:: be64_to_array ( htlc. value ) . to_vec ( ) ;
1745
+ htlc_msat_height_data. extend_from_slice (
1746
+ & byte_utils:: be32_to_array (
1747
+ self . latest_block_height . load ( Ordering :: Acquire )
1748
+ as u32 ,
1749
+ ) ,
1750
+ ) ;
1751
+ failed_forwards. push ( (
1752
+ HTLCSource :: PreviousHopData ( HTLCPreviousHopData {
1753
+ short_channel_id : htlc
1754
+ . prev_hop
1755
+ . short_channel_id ,
1756
+ htlc_id : htlc. prev_hop . htlc_id ,
1757
+ incoming_packet_shared_secret : htlc
1758
+ . prev_hop
1759
+ . incoming_packet_shared_secret ,
1760
+ } ) ,
1761
+ payment_hash,
1762
+ HTLCFailReason :: Reason {
1763
+ failure_code : 0x4000 | 15 ,
1764
+ data : htlc_msat_height_data,
1765
+ } ,
1766
+ ) ) ;
1767
+ }
1768
+ } else if total_value == data. total_msat {
1769
+ new_events. push ( events:: Event :: PaymentReceived {
1770
+ payment_hash : payment_hash,
1771
+ payment_secret : Some ( data. payment_secret ) ,
1772
+ amt : total_value,
1773
+ } ) ;
1774
+ }
1775
+ } else {
1776
+ new_events. push ( events:: Event :: PaymentReceived {
1777
+ payment_hash : payment_hash,
1778
+ payment_secret : None ,
1779
+ amt : amt_to_forward,
1780
+ } ) ;
1781
+ }
1765
1782
} ,
1766
- HTLCForwardInfo :: AddHTLC { .. } => {
1767
- panic ! ( "short_channel_id == 0 should imply any pending_forward entries are of type Receive" ) ;
1783
+ HTLCForwardInfo :: AddHTLC { .. } => {
1784
+ panic ! ( "short_channel_id == 0 should imply any pending_forward entries are of type Receive" ) ;
1768
1785
} ,
1769
- HTLCForwardInfo :: FailHTLC { .. } => {
1770
- panic ! ( "Got pending fail of our own HTLC" ) ;
1771
- }
1772
- }
1773
- }
1774
- }
1775
- }
1776
- }
1786
+ HTLCForwardInfo :: FailHTLC { .. } => {
1787
+ panic ! ( "Got pending fail of our own HTLC" ) ;
1788
+ }
1789
+ }
1790
+ }
1791
+ }
1792
+ }
1793
+ }
1777
1794
1778
1795
for ( htlc_source, payment_hash, failure_reason) in failed_forwards. drain ( ..) {
1779
1796
self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , htlc_source, & payment_hash, failure_reason) ;
@@ -1821,22 +1838,36 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1821
1838
pub fn fail_htlc_backwards ( & self , payment_hash : & PaymentHash , payment_secret : & Option < PaymentSecret > ) -> bool {
1822
1839
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1823
1840
1824
- let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
1825
- let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( & ( * payment_hash, * payment_secret) ) ;
1826
- if let Some ( mut sources) = removed_source {
1827
- for htlc in sources. drain ( ..) {
1828
- if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
1829
- let mut htlc_msat_height_data = byte_utils:: be64_to_array ( htlc. value ) . to_vec ( ) ;
1830
- htlc_msat_height_data. extend_from_slice ( & byte_utils:: be32_to_array (
1831
- self . latest_block_height . load ( Ordering :: Acquire ) as u32 ,
1832
- ) ) ;
1833
- self . fail_htlc_backwards_internal ( channel_state. take ( ) . unwrap ( ) ,
1834
- HTLCSource :: PreviousHopData ( htlc. prev_hop ) , payment_hash,
1835
- HTLCFailReason :: Reason { failure_code : 0x4000 | 15 , data : htlc_msat_height_data } ) ;
1836
- }
1837
- true
1838
- } else { false }
1839
- }
1841
+ let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
1842
+ let removed_source = channel_state
1843
+ . as_mut ( )
1844
+ . unwrap ( )
1845
+ . claimable_htlcs
1846
+ . remove ( & ( * payment_hash, * payment_secret) ) ;
1847
+ if let Some ( mut sources) = removed_source {
1848
+ for htlc in sources. drain ( ..) {
1849
+ if channel_state. is_none ( ) {
1850
+ channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
1851
+ }
1852
+ let mut htlc_msat_height_data = byte_utils:: be64_to_array ( htlc. value ) . to_vec ( ) ;
1853
+ htlc_msat_height_data. extend_from_slice ( & byte_utils:: be32_to_array (
1854
+ self . latest_block_height . load ( Ordering :: Acquire ) as u32 ,
1855
+ ) ) ;
1856
+ self . fail_htlc_backwards_internal (
1857
+ channel_state. take ( ) . unwrap ( ) ,
1858
+ HTLCSource :: PreviousHopData ( htlc. prev_hop ) ,
1859
+ payment_hash,
1860
+ HTLCFailReason :: Reason {
1861
+ failure_code : 0x4000 | 15 ,
1862
+ data : htlc_msat_height_data,
1863
+ } ,
1864
+ ) ;
1865
+ }
1866
+ true
1867
+ } else {
1868
+ false
1869
+ }
1870
+ }
1840
1871
1841
1872
/// Fails an HTLC backwards to the sender of it to us.
1842
1873
/// Note that while we take a channel_state lock as input, we do *not* assume consistency here.
@@ -1994,36 +2025,53 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1994
2025
}
1995
2026
}
1996
2027
1997
- let mut errs = Vec :: new ( ) ;
1998
- let mut claimed_any_htlcs = false ;
1999
- for htlc in sources. drain ( ..) {
2000
- if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
2001
- if ( is_mpp && !valid_mpp) || ( !is_mpp && ( htlc. value < expected_amount || htlc. value > expected_amount * 2 ) ) {
2002
- let mut htlc_msat_height_data = byte_utils:: be64_to_array ( htlc. value ) . to_vec ( ) ;
2003
- htlc_msat_height_data. extend_from_slice ( & byte_utils:: be32_to_array (
2004
- self . latest_block_height . load ( Ordering :: Acquire ) as u32 ,
2005
- ) ) ;
2006
- self . fail_htlc_backwards_internal ( channel_state. take ( ) . unwrap ( ) ,
2007
- HTLCSource :: PreviousHopData ( htlc. prev_hop ) , & payment_hash,
2008
- HTLCFailReason :: Reason { failure_code : 0x4000 |15 , data : htlc_msat_height_data } ) ;
2009
- } else {
2028
+ let mut errs = Vec :: new ( ) ;
2029
+ let mut claimed_any_htlcs = false ;
2030
+ for htlc in sources. drain ( ..) {
2031
+ if channel_state. is_none ( ) {
2032
+ channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
2033
+ }
2034
+ if ( is_mpp && !valid_mpp)
2035
+ || ( !is_mpp
2036
+ && ( htlc. value < expected_amount || htlc. value > expected_amount * 2 ) )
2037
+ {
2038
+ let mut htlc_msat_height_data = byte_utils:: be64_to_array ( htlc. value ) . to_vec ( ) ;
2039
+ htlc_msat_height_data. extend_from_slice ( & byte_utils:: be32_to_array (
2040
+ self . latest_block_height . load ( Ordering :: Acquire ) as u32 ,
2041
+ ) ) ;
2042
+ self . fail_htlc_backwards_internal (
2043
+ channel_state. take ( ) . unwrap ( ) ,
2044
+ HTLCSource :: PreviousHopData ( htlc. prev_hop ) ,
2045
+ & payment_hash,
2046
+ HTLCFailReason :: Reason {
2047
+ failure_code : 0x4000 | 15 ,
2048
+ data : htlc_msat_height_data,
2049
+ } ,
2050
+ ) ;
2051
+ } else {
2010
2052
match self . claim_funds_from_hop ( channel_state. as_mut ( ) . unwrap ( ) , htlc. prev_hop , payment_preimage) {
2011
- Err ( Some ( e) ) => {
2012
- if let msgs:: ErrorAction :: IgnoreError = e. 1 . err . action {
2013
- // We got a temporary failure updating monitor, but will claim the
2014
- // HTLC when the monitor updating is restored (or on chain).
2015
- log_error ! ( self , "Temporary failure claiming HTLC, treating as success: {}" , e. 1 . err. err) ;
2016
- claimed_any_htlcs = true ;
2053
+ Err ( Some ( e) ) => {
2054
+ if let msgs:: ErrorAction :: IgnoreError = e. 1 . err . action {
2055
+ // We got a temporary failure updating monitor, but will claim the
2056
+ // HTLC when the monitor updating is restored (or on chain).
2057
+ log_error ! (
2058
+ self ,
2059
+ "Temporary failure claiming HTLC, treating as success: {}" ,
2060
+ e. 1 . err. err
2061
+ ) ;
2062
+ claimed_any_htlcs = true ;
2017
2063
} else { errs. push ( e) ; }
2018
2064
} ,
2019
- Err ( None ) if is_mpp => unreachable ! ( "We already checked for channel existence, we can't fail here!" ) ,
2020
- Err ( None ) => {
2065
+ Err ( None ) if is_mpp => unreachable ! (
2066
+ "We already checked for channel existence, we can't fail here!"
2067
+ ) ,
2068
+ Err ( None ) => {
2021
2069
log_warn ! ( self , "Channel we expected to claim an HTLC from was closed." ) ;
2022
2070
} ,
2023
- Ok ( ( ) ) => claimed_any_htlcs = true ,
2024
- }
2025
- }
2026
- }
2071
+ Ok ( ( ) ) => claimed_any_htlcs = true ,
2072
+ }
2073
+ }
2074
+ }
2027
2075
2028
2076
// Now that we've done the entire above loop in one lock, we can handle any errors
2029
2077
// which were generated.
0 commit comments