@@ -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 > ,
@@ -1232,14 +1232,18 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
1232
1232
msgs:: OnionHopDataFormat :: FinalNode { payment_data } => payment_data,
1233
1233
} ;
1234
1234
1235
+ if payment_data. is_none ( ) {
1236
+ return_err ! ( "We require payment_secrets" , 0x4000 |0x2000 |3 , & [ 0 ; 0 ] ) ;
1237
+ }
1238
+
1235
1239
// Note that we could obviously respond immediately with an update_fulfill_htlc
1236
1240
// message, however that would leak that we are the recipient of this payment, so
1237
1241
// instead we stay symmetric with the forwarding case, only responding (after a
1238
1242
// delay) once they've send us a commitment_signed!
1239
1243
1240
1244
PendingHTLCStatus :: Forward ( PendingHTLCInfo {
1241
1245
routing : PendingHTLCRouting :: Receive {
1242
- payment_data,
1246
+ payment_data : payment_data . unwrap ( ) ,
1243
1247
incoming_cltv_expiry : msg. cltv_expiry ,
1244
1248
} ,
1245
1249
payment_hash : msg. payment_hash . clone ( ) ,
@@ -1918,60 +1922,74 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
1918
1922
routing : PendingHTLCRouting :: Receive { payment_data, incoming_cltv_expiry } ,
1919
1923
incoming_shared_secret, payment_hash, amt_to_forward, .. } ,
1920
1924
prev_funding_outpoint } => {
1921
- let prev_hop = HTLCPreviousHopData {
1922
- short_channel_id : prev_short_channel_id,
1923
- outpoint : prev_funding_outpoint,
1924
- htlc_id : prev_htlc_id,
1925
- incoming_packet_shared_secret : incoming_shared_secret,
1926
- } ;
1927
-
1928
- let mut total_value = 0 ;
1929
- let payment_secret_opt =
1930
- if let & Some ( ref data) = & payment_data { Some ( data. payment_secret . clone ( ) ) } else { None } ;
1931
- let htlcs = channel_state. claimable_htlcs . entry ( ( payment_hash, payment_secret_opt) )
1932
- . or_insert ( Vec :: new ( ) ) ;
1933
- htlcs. push ( ClaimableHTLC {
1934
- prev_hop,
1925
+ let claimable_htlc = ClaimableHTLC {
1926
+ 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
+ } ,
1935
1932
value : amt_to_forward,
1936
1933
payment_data : payment_data. clone ( ) ,
1937
1934
cltv_expiry : incoming_cltv_expiry,
1938
- } ) ;
1939
- if let & Some ( ref data) = & payment_data {
1935
+ } ;
1936
+
1937
+ macro_rules! fail_htlc {
1938
+ ( $htlc: expr) => {
1939
+ let mut htlc_msat_height_data = byte_utils:: be64_to_array( $htlc. value) . to_vec( ) ;
1940
+ htlc_msat_height_data. extend_from_slice(
1941
+ & byte_utils:: be32_to_array( self . best_block. read( ) . unwrap( ) . height( ) ) ,
1942
+ ) ;
1943
+ failed_forwards. push( ( HTLCSource :: PreviousHopData ( HTLCPreviousHopData {
1944
+ short_channel_id: $htlc. prev_hop. short_channel_id,
1945
+ outpoint: prev_funding_outpoint,
1946
+ htlc_id: $htlc. prev_hop. htlc_id,
1947
+ incoming_packet_shared_secret: $htlc. prev_hop. incoming_packet_shared_secret,
1948
+ } ) , payment_hash,
1949
+ HTLCFailReason :: Reason { failure_code: 0x4000 | 15 , data: htlc_msat_height_data }
1950
+ ) ) ;
1951
+ }
1952
+ }
1953
+
1954
+ // Check that the payment hash and secret are known. Note that we
1955
+ // MUST take care to handle the "unknown payment hash" and
1956
+ // "incorrect payment secret" cases here identically or we'd expose
1957
+ // that we are the ultimately recipient of the given payment hash.
1958
+ // Further, we must not expose whether we have any other HTLCs
1959
+ // associated with the same payment_hash pending or not.
1960
+ if {
1961
+ let payment_secrets = self . payment_secrets . lock ( ) . unwrap ( ) ;
1962
+ if let Some ( inbound_payment) = payment_secrets. get ( & payment_hash) {
1963
+ inbound_payment. payment_secret != payment_data. payment_secret ||
1964
+ ( inbound_payment. value_msat . is_some ( ) && inbound_payment. value_msat . unwrap ( ) > payment_data. total_msat )
1965
+ } else { true }
1966
+ } {
1967
+ log_trace ! ( self . logger, "Failing new HTLC with payment_hash {} as it didn't match our expected payment secret." , log_bytes!( payment_hash. 0 ) ) ;
1968
+ fail_htlc ! ( claimable_htlc) ;
1969
+ } else {
1970
+ let mut total_value = 0 ;
1971
+ let htlcs = channel_state. claimable_htlcs . entry ( payment_hash)
1972
+ . or_insert ( Vec :: new ( ) ) ;
1973
+ htlcs. push ( claimable_htlc) ;
1940
1974
for htlc in htlcs. iter ( ) {
1941
1975
total_value += htlc. value ;
1942
- if htlc. payment_data . as_ref ( ) . unwrap ( ) . total_msat != data . total_msat {
1976
+ if htlc. payment_data . total_msat != payment_data . total_msat {
1943
1977
total_value = msgs:: MAX_VALUE_MSAT ;
1944
1978
}
1945
1979
if total_value >= msgs:: MAX_VALUE_MSAT { break ; }
1946
1980
}
1947
- if total_value >= msgs:: MAX_VALUE_MSAT || total_value > data. total_msat {
1981
+ if total_value >= msgs:: MAX_VALUE_MSAT || total_value > payment_data. total_msat {
1982
+ 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) ;
1948
1983
for htlc in htlcs. iter ( ) {
1949
- let mut htlc_msat_height_data = byte_utils:: be64_to_array ( htlc. value ) . to_vec ( ) ;
1950
- htlc_msat_height_data. extend_from_slice (
1951
- & byte_utils:: be32_to_array ( self . best_block . read ( ) . unwrap ( ) . height ( ) ) ,
1952
- ) ;
1953
- failed_forwards. push ( ( HTLCSource :: PreviousHopData ( HTLCPreviousHopData {
1954
- short_channel_id : htlc. prev_hop . short_channel_id ,
1955
- outpoint : prev_funding_outpoint,
1956
- htlc_id : htlc. prev_hop . htlc_id ,
1957
- incoming_packet_shared_secret : htlc. prev_hop . incoming_packet_shared_secret ,
1958
- } ) , payment_hash,
1959
- HTLCFailReason :: Reason { failure_code : 0x4000 | 15 , data : htlc_msat_height_data }
1960
- ) ) ;
1984
+ fail_htlc ! ( htlc) ;
1961
1985
}
1962
- } else if total_value == data . total_msat {
1986
+ } else if total_value == payment_data . total_msat {
1963
1987
new_events. push ( events:: Event :: PaymentReceived {
1964
1988
payment_hash,
1965
- payment_secret : Some ( data . payment_secret ) ,
1989
+ payment_secret : Some ( payment_data . payment_secret ) ,
1966
1990
amt : total_value,
1967
1991
} ) ;
1968
1992
}
1969
- } else {
1970
- new_events. push ( events:: Event :: PaymentReceived {
1971
- payment_hash,
1972
- payment_secret : None ,
1973
- amt : amt_to_forward,
1974
- } ) ;
1975
1993
}
1976
1994
} ,
1977
1995
HTLCForwardInfo :: AddHTLC { .. } => {
@@ -2062,11 +2080,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2062
2080
let _persistence_guard = PersistenceNotifierGuard :: new ( & self . total_consistency_lock , & self . persistence_notifier ) ;
2063
2081
2064
2082
let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
2065
- let payment_secrets = self . payment_secrets . lock ( ) . unwrap ( ) ;
2066
- let payment_secret = if let Some ( secret) = payment_secrets. get ( & payment_hash) {
2067
- Some ( secret. payment_secret )
2068
- } else { return false ; } ;
2069
- let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( & ( * payment_hash, payment_secret) ) ;
2083
+ let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( payment_hash) ;
2070
2084
if let Some ( mut sources) = removed_source {
2071
2085
for htlc in sources. drain ( ..) {
2072
2086
if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
@@ -2248,11 +2262,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2248
2262
let _persistence_guard = PersistenceNotifierGuard :: new ( & self . total_consistency_lock , & self . persistence_notifier ) ;
2249
2263
2250
2264
let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
2251
- let payment_secrets = self . payment_secrets . lock ( ) . unwrap ( ) ;
2252
- let payment_secret = if let Some ( secret) = payment_secrets. get ( & payment_hash) {
2253
- Some ( secret. payment_secret )
2254
- } else { return false ; } ;
2255
- let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( & ( payment_hash, payment_secret) ) ;
2265
+ let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( & payment_hash) ;
2256
2266
if let Some ( mut sources) = removed_source {
2257
2267
assert ! ( !sources. is_empty( ) ) ;
2258
2268
@@ -2268,13 +2278,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2268
2278
// provide the preimage, so worrying too much about the optimal handling isn't worth
2269
2279
// it.
2270
2280
2271
- let ( is_mpp, mut valid_mpp) = if let & Some ( ref data) = & sources[ 0 ] . payment_data {
2272
- assert ! ( payment_secret. is_some( ) ) ;
2273
- ( true , data. total_msat >= expected_amount)
2274
- } else {
2275
- assert ! ( payment_secret. is_none( ) ) ;
2276
- ( false , false )
2277
- } ;
2281
+ let is_mpp = true ;
2282
+ let mut valid_mpp = sources[ 0 ] . payment_data . total_msat >= expected_amount;
2278
2283
2279
2284
for htlc in sources. iter ( ) {
2280
2285
if !is_mpp || !valid_mpp { break ; }
@@ -3541,7 +3546,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3541
3546
} ) ;
3542
3547
3543
3548
if let Some ( height) = height_opt {
3544
- channel_state. claimable_htlcs . retain ( |& ( ref payment_hash, _ ) , htlcs| {
3549
+ channel_state. claimable_htlcs . retain ( |payment_hash, htlcs| {
3545
3550
htlcs. retain ( |htlc| {
3546
3551
// If height is approaching the number of blocks we think it takes us to get
3547
3552
// our commitment transaction confirmed before the HTLC expires, plus the
@@ -4040,7 +4045,8 @@ impl Writeable for PendingHTLCInfo {
4040
4045
} ,
4041
4046
& PendingHTLCRouting :: Receive { ref payment_data, ref incoming_cltv_expiry } => {
4042
4047
1u8 . write ( writer) ?;
4043
- payment_data. write ( writer) ?;
4048
+ payment_data. payment_secret . write ( writer) ?;
4049
+ payment_data. total_msat . write ( writer) ?;
4044
4050
incoming_cltv_expiry. write ( writer) ?;
4045
4051
} ,
4046
4052
}
@@ -4061,7 +4067,10 @@ impl Readable for PendingHTLCInfo {
4061
4067
short_channel_id : Readable :: read ( reader) ?,
4062
4068
} ,
4063
4069
1u8 => PendingHTLCRouting :: Receive {
4064
- payment_data : Readable :: read ( reader) ?,
4070
+ payment_data : msgs:: FinalOnionHopData {
4071
+ payment_secret : Readable :: read ( reader) ?,
4072
+ total_msat : Readable :: read ( reader) ?,
4073
+ } ,
4065
4074
incoming_cltv_expiry : Readable :: read ( reader) ?,
4066
4075
} ,
4067
4076
_ => return Err ( DecodeError :: InvalidValue ) ,
@@ -4133,12 +4142,29 @@ impl_writeable!(HTLCPreviousHopData, 0, {
4133
4142
incoming_packet_shared_secret
4134
4143
} ) ;
4135
4144
4136
- impl_writeable ! ( ClaimableHTLC , 0 , {
4137
- prev_hop,
4138
- value,
4139
- payment_data,
4140
- cltv_expiry
4141
- } ) ;
4145
+ impl Writeable for ClaimableHTLC {
4146
+ fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , :: std:: io:: Error > {
4147
+ self . prev_hop . write ( writer) ?;
4148
+ self . value . write ( writer) ?;
4149
+ self . payment_data . payment_secret . write ( writer) ?;
4150
+ self . payment_data . total_msat . write ( writer) ?;
4151
+ self . cltv_expiry . write ( writer)
4152
+ }
4153
+ }
4154
+
4155
+ impl Readable for ClaimableHTLC {
4156
+ fn read < R : :: std:: io:: Read > ( reader : & mut R ) -> Result < Self , DecodeError > {
4157
+ Ok ( ClaimableHTLC {
4158
+ prev_hop : Readable :: read ( reader) ?,
4159
+ value : Readable :: read ( reader) ?,
4160
+ payment_data : msgs:: FinalOnionHopData {
4161
+ payment_secret : Readable :: read ( reader) ?,
4162
+ total_msat : Readable :: read ( reader) ?,
4163
+ } ,
4164
+ cltv_expiry : Readable :: read ( reader) ?,
4165
+ } )
4166
+ }
4167
+ }
4142
4168
4143
4169
impl Writeable for HTLCSource {
4144
4170
fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , :: std:: io:: Error > {
0 commit comments