@@ -95,7 +95,7 @@ enum PendingHTLCRouting {
95
95
short_channel_id : u64 , // This should be NonZero<u64> eventually when we bump MSRV
96
96
} ,
97
97
Receive {
98
- payment_data : Option < msgs:: FinalOnionHopData > ,
98
+ payment_data : msgs:: FinalOnionHopData ,
99
99
incoming_cltv_expiry : u32 , // Used to track when we should expire pending HTLCs that go unclaimed
100
100
} ,
101
101
}
@@ -159,7 +159,7 @@ struct ClaimableHTLC {
159
159
/// total_msat (which may differ from value if this is a Multi-Path Payment) and a
160
160
/// payment_secret which prevents path-probing attacks and can associate different HTLCs which
161
161
/// are part of the same payment.
162
- payment_data : Option < msgs:: FinalOnionHopData > ,
162
+ payment_data : msgs:: FinalOnionHopData ,
163
163
cltv_expiry : u32 ,
164
164
}
165
165
@@ -331,7 +331,7 @@ pub(super) struct ChannelHolder<Signer: Sign> {
331
331
/// Note that while this is held in the same mutex as the channels themselves, no consistency
332
332
/// guarantees are made about the channels given here actually existing anymore by the time you
333
333
/// go to read them!
334
- claimable_htlcs : HashMap < ( PaymentHash , Option < PaymentSecret > ) , Vec < ClaimableHTLC > > ,
334
+ claimable_htlcs : HashMap < PaymentHash , Vec < ClaimableHTLC > > ,
335
335
/// Messages to send to peers - pushed to in the same lock that they are generated in (except
336
336
/// for broadcast messages, where ordering isn't as strict).
337
337
pub ( super ) pending_msg_events : Vec < MessageSendEvent > ,
@@ -1237,14 +1237,18 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
1237
1237
msgs:: OnionHopDataFormat :: FinalNode { payment_data } => payment_data,
1238
1238
} ;
1239
1239
1240
+ if payment_data. is_none ( ) {
1241
+ return_err ! ( "We require payment_secrets" , 0x4000 |0x2000 |3 , & [ 0 ; 0 ] ) ;
1242
+ }
1243
+
1240
1244
// Note that we could obviously respond immediately with an update_fulfill_htlc
1241
1245
// message, however that would leak that we are the recipient of this payment, so
1242
1246
// instead we stay symmetric with the forwarding case, only responding (after a
1243
1247
// delay) once they've send us a commitment_signed!
1244
1248
1245
1249
PendingHTLCStatus :: Forward ( PendingHTLCInfo {
1246
1250
routing : PendingHTLCRouting :: Receive {
1247
- payment_data,
1251
+ payment_data : payment_data . unwrap ( ) ,
1248
1252
incoming_cltv_expiry : msg. cltv_expiry ,
1249
1253
} ,
1250
1254
payment_hash : msg. payment_hash . clone ( ) ,
@@ -1923,60 +1927,74 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
1923
1927
routing : PendingHTLCRouting :: Receive { payment_data, incoming_cltv_expiry } ,
1924
1928
incoming_shared_secret, payment_hash, amt_to_forward, .. } ,
1925
1929
prev_funding_outpoint } => {
1926
- let prev_hop = HTLCPreviousHopData {
1927
- short_channel_id : prev_short_channel_id,
1928
- outpoint : prev_funding_outpoint,
1929
- htlc_id : prev_htlc_id,
1930
- incoming_packet_shared_secret : incoming_shared_secret,
1931
- } ;
1932
-
1933
- let mut total_value = 0 ;
1934
- let payment_secret_opt =
1935
- if let & Some ( ref data) = & payment_data { Some ( data. payment_secret . clone ( ) ) } else { None } ;
1936
- let htlcs = channel_state. claimable_htlcs . entry ( ( payment_hash, payment_secret_opt) )
1937
- . or_insert ( Vec :: new ( ) ) ;
1938
- htlcs. push ( ClaimableHTLC {
1939
- prev_hop,
1930
+ let claimable_htlc = ClaimableHTLC {
1931
+ prev_hop : HTLCPreviousHopData {
1932
+ short_channel_id : prev_short_channel_id,
1933
+ outpoint : prev_funding_outpoint,
1934
+ htlc_id : prev_htlc_id,
1935
+ incoming_packet_shared_secret : incoming_shared_secret,
1936
+ } ,
1940
1937
value : amt_to_forward,
1941
1938
payment_data : payment_data. clone ( ) ,
1942
1939
cltv_expiry : incoming_cltv_expiry,
1943
- } ) ;
1944
- if let & Some ( ref data) = & payment_data {
1940
+ } ;
1941
+
1942
+ macro_rules! fail_htlc {
1943
+ ( $htlc: expr) => {
1944
+ let mut htlc_msat_height_data = byte_utils:: be64_to_array( $htlc. value) . to_vec( ) ;
1945
+ htlc_msat_height_data. extend_from_slice(
1946
+ & byte_utils:: be32_to_array( self . best_block. read( ) . unwrap( ) . height( ) ) ,
1947
+ ) ;
1948
+ failed_forwards. push( ( HTLCSource :: PreviousHopData ( HTLCPreviousHopData {
1949
+ short_channel_id: $htlc. prev_hop. short_channel_id,
1950
+ outpoint: prev_funding_outpoint,
1951
+ htlc_id: $htlc. prev_hop. htlc_id,
1952
+ incoming_packet_shared_secret: $htlc. prev_hop. incoming_packet_shared_secret,
1953
+ } ) , payment_hash,
1954
+ HTLCFailReason :: Reason { failure_code: 0x4000 | 15 , data: htlc_msat_height_data }
1955
+ ) ) ;
1956
+ }
1957
+ }
1958
+
1959
+ // Check that the payment hash and secret are known. Note that we
1960
+ // MUST take care to handle the "unknown payment hash" and
1961
+ // "incorrect payment secret" cases here identically or we'd expose
1962
+ // that we are the ultimately recipient of the given payment hash.
1963
+ // Further, we must not expose whether we have any other HTLCs
1964
+ // associated with the same payment_hash pending or not.
1965
+ if {
1966
+ let payment_secrets = self . pending_inbound_payments . lock ( ) . unwrap ( ) ;
1967
+ if let Some ( inbound_payment) = payment_secrets. get ( & payment_hash) {
1968
+ inbound_payment. payment_secret != payment_data. payment_secret ||
1969
+ ( inbound_payment. min_value_msat . is_some ( ) && inbound_payment. min_value_msat . unwrap ( ) > payment_data. total_msat )
1970
+ } else { true }
1971
+ } {
1972
+ log_trace ! ( self . logger, "Failing new HTLC with payment_hash {} as it didn't match our expected payment secret." , log_bytes!( payment_hash. 0 ) ) ;
1973
+ fail_htlc ! ( claimable_htlc) ;
1974
+ } else {
1975
+ let mut total_value = 0 ;
1976
+ let htlcs = channel_state. claimable_htlcs . entry ( payment_hash)
1977
+ . or_insert ( Vec :: new ( ) ) ;
1978
+ htlcs. push ( claimable_htlc) ;
1945
1979
for htlc in htlcs. iter ( ) {
1946
1980
total_value += htlc. value ;
1947
- if htlc. payment_data . as_ref ( ) . unwrap ( ) . total_msat != data . total_msat {
1981
+ if htlc. payment_data . total_msat != payment_data . total_msat {
1948
1982
total_value = msgs:: MAX_VALUE_MSAT ;
1949
1983
}
1950
1984
if total_value >= msgs:: MAX_VALUE_MSAT { break ; }
1951
1985
}
1952
- if total_value >= msgs:: MAX_VALUE_MSAT || total_value > data. total_msat {
1986
+ if total_value >= msgs:: MAX_VALUE_MSAT || total_value > payment_data. total_msat {
1987
+ log_trace ! ( self . logger, "Failing HTLCs with payment_hash {} as the total value {} ran over expected value {}" , log_bytes!( payment_hash. 0 ) , total_value, payment_data. total_msat) ;
1953
1988
for htlc in htlcs. iter ( ) {
1954
- let mut htlc_msat_height_data = byte_utils:: be64_to_array ( htlc. value ) . to_vec ( ) ;
1955
- htlc_msat_height_data. extend_from_slice (
1956
- & byte_utils:: be32_to_array ( self . best_block . read ( ) . unwrap ( ) . height ( ) ) ,
1957
- ) ;
1958
- failed_forwards. push ( ( HTLCSource :: PreviousHopData ( HTLCPreviousHopData {
1959
- short_channel_id : htlc. prev_hop . short_channel_id ,
1960
- outpoint : prev_funding_outpoint,
1961
- htlc_id : htlc. prev_hop . htlc_id ,
1962
- incoming_packet_shared_secret : htlc. prev_hop . incoming_packet_shared_secret ,
1963
- } ) , payment_hash,
1964
- HTLCFailReason :: Reason { failure_code : 0x4000 | 15 , data : htlc_msat_height_data }
1965
- ) ) ;
1989
+ fail_htlc ! ( htlc) ;
1966
1990
}
1967
- } else if total_value == data . total_msat {
1991
+ } else if total_value == payment_data . total_msat {
1968
1992
new_events. push ( events:: Event :: PaymentReceived {
1969
1993
payment_hash,
1970
- payment_secret : Some ( data . payment_secret ) ,
1994
+ payment_secret : Some ( payment_data . payment_secret ) ,
1971
1995
amt : total_value,
1972
1996
} ) ;
1973
1997
}
1974
- } else {
1975
- new_events. push ( events:: Event :: PaymentReceived {
1976
- payment_hash,
1977
- payment_secret : None ,
1978
- amt : amt_to_forward,
1979
- } ) ;
1980
1998
}
1981
1999
} ,
1982
2000
HTLCForwardInfo :: AddHTLC { .. } => {
@@ -2067,11 +2085,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2067
2085
let _persistence_guard = PersistenceNotifierGuard :: new ( & self . total_consistency_lock , & self . persistence_notifier ) ;
2068
2086
2069
2087
let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
2070
- let payment_secrets = self . payment_secrets . lock ( ) . unwrap ( ) ;
2071
- let payment_secret = if let Some ( secret) = payment_secrets. get ( & payment_hash) {
2072
- Some ( secret. payment_secret )
2073
- } else { return false ; } ;
2074
- let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( & ( * payment_hash, payment_secret) ) ;
2088
+ let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( payment_hash) ;
2075
2089
if let Some ( mut sources) = removed_source {
2076
2090
for htlc in sources. drain ( ..) {
2077
2091
if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
@@ -2253,11 +2267,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2253
2267
let _persistence_guard = PersistenceNotifierGuard :: new ( & self . total_consistency_lock , & self . persistence_notifier ) ;
2254
2268
2255
2269
let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
2256
- let payment_secrets = self . payment_secrets . lock ( ) . unwrap ( ) ;
2257
- let payment_secret = if let Some ( secret) = payment_secrets. get ( & payment_hash) {
2258
- Some ( secret. payment_secret )
2259
- } else { return false ; } ;
2260
- let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( & ( payment_hash, payment_secret) ) ;
2270
+ let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( & payment_hash) ;
2261
2271
if let Some ( mut sources) = removed_source {
2262
2272
assert ! ( !sources. is_empty( ) ) ;
2263
2273
@@ -2273,13 +2283,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2273
2283
// provide the preimage, so worrying too much about the optimal handling isn't worth
2274
2284
// it.
2275
2285
2276
- let ( is_mpp, mut valid_mpp) = if let & Some ( ref data) = & sources[ 0 ] . payment_data {
2277
- assert ! ( payment_secret. is_some( ) ) ;
2278
- ( true , data. total_msat >= expected_amount)
2279
- } else {
2280
- assert ! ( payment_secret. is_none( ) ) ;
2281
- ( false , false )
2282
- } ;
2286
+ let is_mpp = true ;
2287
+ let mut valid_mpp = sources[ 0 ] . payment_data . total_msat >= expected_amount;
2283
2288
2284
2289
for htlc in sources. iter ( ) {
2285
2290
if !is_mpp || !valid_mpp { break ; }
@@ -3564,7 +3569,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3564
3569
} ) ;
3565
3570
3566
3571
if let Some ( height) = height_opt {
3567
- channel_state. claimable_htlcs . retain ( |& ( ref payment_hash, _ ) , htlcs| {
3572
+ channel_state. claimable_htlcs . retain ( |payment_hash, htlcs| {
3568
3573
htlcs. retain ( |htlc| {
3569
3574
// If height is approaching the number of blocks we think it takes us to get
3570
3575
// our commitment transaction confirmed before the HTLC expires, plus the
@@ -4063,7 +4068,8 @@ impl Writeable for PendingHTLCInfo {
4063
4068
} ,
4064
4069
& PendingHTLCRouting :: Receive { ref payment_data, ref incoming_cltv_expiry } => {
4065
4070
1u8 . write ( writer) ?;
4066
- payment_data. write ( writer) ?;
4071
+ payment_data. payment_secret . write ( writer) ?;
4072
+ payment_data. total_msat . write ( writer) ?;
4067
4073
incoming_cltv_expiry. write ( writer) ?;
4068
4074
} ,
4069
4075
}
@@ -4084,7 +4090,10 @@ impl Readable for PendingHTLCInfo {
4084
4090
short_channel_id : Readable :: read ( reader) ?,
4085
4091
} ,
4086
4092
1u8 => PendingHTLCRouting :: Receive {
4087
- payment_data : Readable :: read ( reader) ?,
4093
+ payment_data : msgs:: FinalOnionHopData {
4094
+ payment_secret : Readable :: read ( reader) ?,
4095
+ total_msat : Readable :: read ( reader) ?,
4096
+ } ,
4088
4097
incoming_cltv_expiry : Readable :: read ( reader) ?,
4089
4098
} ,
4090
4099
_ => return Err ( DecodeError :: InvalidValue ) ,
@@ -4156,12 +4165,29 @@ impl_writeable!(HTLCPreviousHopData, 0, {
4156
4165
incoming_packet_shared_secret
4157
4166
} ) ;
4158
4167
4159
- impl_writeable ! ( ClaimableHTLC , 0 , {
4160
- prev_hop,
4161
- value,
4162
- payment_data,
4163
- cltv_expiry
4164
- } ) ;
4168
+ impl Writeable for ClaimableHTLC {
4169
+ fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , :: std:: io:: Error > {
4170
+ self . prev_hop . write ( writer) ?;
4171
+ self . value . write ( writer) ?;
4172
+ self . payment_data . payment_secret . write ( writer) ?;
4173
+ self . payment_data . total_msat . write ( writer) ?;
4174
+ self . cltv_expiry . write ( writer)
4175
+ }
4176
+ }
4177
+
4178
+ impl Readable for ClaimableHTLC {
4179
+ fn read < R : :: std:: io:: Read > ( reader : & mut R ) -> Result < Self , DecodeError > {
4180
+ Ok ( ClaimableHTLC {
4181
+ prev_hop : Readable :: read ( reader) ?,
4182
+ value : Readable :: read ( reader) ?,
4183
+ payment_data : msgs:: FinalOnionHopData {
4184
+ payment_secret : Readable :: read ( reader) ?,
4185
+ total_msat : Readable :: read ( reader) ?,
4186
+ } ,
4187
+ cltv_expiry : Readable :: read ( reader) ?,
4188
+ } )
4189
+ }
4190
+ }
4165
4191
4166
4192
impl Writeable for HTLCSource {
4167
4193
fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , :: std:: io:: Error > {
0 commit comments