@@ -113,6 +113,7 @@ pub(super) struct PendingHTLCInfo {
113
113
pub ( super ) incoming_shared_secret : [ u8 ; 32 ] ,
114
114
payment_hash : PaymentHash ,
115
115
pub ( super ) amt_to_forward : u64 ,
116
+ pub ( super ) amt_incoming : Option < u64 > , // Added in 0.0.113
116
117
pub ( super ) outgoing_cltv_value : u32 ,
117
118
}
118
119
@@ -129,20 +130,22 @@ pub(super) enum PendingHTLCStatus {
129
130
Fail ( HTLCFailureMsg ) ,
130
131
}
131
132
132
- pub ( super ) enum HTLCForwardInfo {
133
- AddHTLC {
134
- forward_info : PendingHTLCInfo ,
133
+ pub ( super ) struct PendingAddHTLCInfo {
134
+ pub ( super ) forward_info : PendingHTLCInfo ,
135
135
136
- // These fields are produced in `forward_htlcs()` and consumed in
137
- // `process_pending_htlc_forwards()` for constructing the
138
- // `HTLCSource::PreviousHopData` for failed and forwarded
139
- // HTLCs.
140
- //
141
- // Note that this may be an outbound SCID alias for the associated channel.
142
- prev_short_channel_id : u64 ,
143
- prev_htlc_id : u64 ,
144
- prev_funding_outpoint : OutPoint ,
145
- } ,
136
+ // These fields are produced in `forward_htlcs()` and consumed in
137
+ // `process_pending_htlc_forwards()` for constructing the
138
+ // `HTLCSource::PreviousHopData` for failed and forwarded
139
+ // HTLCs.
140
+ //
141
+ // Note that this may be an outbound SCID alias for the associated channel.
142
+ prev_short_channel_id : u64 ,
143
+ prev_htlc_id : u64 ,
144
+ prev_funding_outpoint : OutPoint ,
145
+ }
146
+
147
+ pub ( super ) enum HTLCForwardInfo {
148
+ AddHTLC ( PendingAddHTLCInfo ) ,
146
149
FailHTLC {
147
150
htlc_id : u64 ,
148
151
err_packet : msgs:: OnionErrorPacket ,
@@ -2194,6 +2197,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
2194
2197
routing,
2195
2198
payment_hash,
2196
2199
incoming_shared_secret : shared_secret,
2200
+ amt_incoming : Some ( amt_msat) ,
2197
2201
amt_to_forward : amt_msat,
2198
2202
outgoing_cltv_value : hop_data. outgoing_cltv_value ,
2199
2203
} )
@@ -2290,6 +2294,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
2290
2294
} ,
2291
2295
payment_hash : msg. payment_hash . clone ( ) ,
2292
2296
incoming_shared_secret : shared_secret,
2297
+ amt_incoming : Some ( msg. amount_msat ) ,
2293
2298
amt_to_forward : next_hop_data. amt_to_forward ,
2294
2299
outgoing_cltv_value : next_hop_data. outgoing_cltv_value ,
2295
2300
} )
@@ -2308,7 +2313,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
2308
2313
None => { // unknown_next_peer
2309
2314
// Note that this is likely a timing oracle for detecting whether an scid is a
2310
2315
// phantom.
2311
- if fake_scid:: is_valid_phantom ( & self . fake_scid_rand_bytes , * short_channel_id) {
2316
+ if fake_scid:: is_valid_phantom ( & self . fake_scid_rand_bytes , * short_channel_id, & self . genesis_hash ) {
2312
2317
None
2313
2318
} else {
2314
2319
break Some ( ( "Don't have available channel for forwarding as requested." , 0x4000 | 10 , None ) ) ;
@@ -3149,82 +3154,86 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
3149
3154
( ) => {
3150
3155
for forward_info in pending_forwards. drain( ..) {
3151
3156
match forward_info {
3152
- HTLCForwardInfo :: AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
3153
- routing, incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value } ,
3154
- prev_funding_outpoint } => {
3155
- macro_rules! failure_handler {
3156
- ( $msg: expr, $err_code: expr, $err_data: expr, $phantom_ss: expr, $next_hop_unknown: expr) => {
3157
- log_info!( self . logger, "Failed to accept/forward incoming HTLC: {}" , $msg) ;
3158
-
3159
- let htlc_source = HTLCSource :: PreviousHopData ( HTLCPreviousHopData {
3160
- short_channel_id: prev_short_channel_id,
3161
- outpoint: prev_funding_outpoint,
3162
- htlc_id: prev_htlc_id,
3163
- incoming_packet_shared_secret: incoming_shared_secret,
3164
- phantom_shared_secret: $phantom_ss,
3165
- } ) ;
3166
-
3167
- let reason = if $next_hop_unknown {
3168
- HTLCDestination :: UnknownNextHop { requested_forward_scid: short_chan_id }
3169
- } else {
3170
- HTLCDestination :: FailedPayment { payment_hash }
3171
- } ;
3172
-
3173
- failed_forwards. push( ( htlc_source, payment_hash,
3174
- HTLCFailReason :: Reason { failure_code: $err_code, data: $err_data } ,
3175
- reason
3176
- ) ) ;
3177
- continue ;
3178
- }
3157
+ HTLCForwardInfo :: AddHTLC ( PendingAddHTLCInfo {
3158
+ prev_short_channel_id, prev_htlc_id, prev_funding_outpoint,
3159
+ forward_info: PendingHTLCInfo {
3160
+ routing, incoming_shared_secret, payment_hash, amt_to_forward,
3161
+ outgoing_cltv_value, amt_incoming: _
3162
+ }
3163
+ } ) => {
3164
+ macro_rules! failure_handler {
3165
+ ( $msg: expr, $err_code: expr, $err_data: expr, $phantom_ss: expr, $next_hop_unknown: expr) => {
3166
+ log_info!( self . logger, "Failed to accept/forward incoming HTLC: {}" , $msg) ;
3167
+
3168
+ let htlc_source = HTLCSource :: PreviousHopData ( HTLCPreviousHopData {
3169
+ short_channel_id: prev_short_channel_id,
3170
+ outpoint: prev_funding_outpoint,
3171
+ htlc_id: prev_htlc_id,
3172
+ incoming_packet_shared_secret: incoming_shared_secret,
3173
+ phantom_shared_secret: $phantom_ss,
3174
+ } ) ;
3175
+
3176
+ let reason = if $next_hop_unknown {
3177
+ HTLCDestination :: UnknownNextHop { requested_forward_scid: short_chan_id }
3178
+ } else {
3179
+ HTLCDestination :: FailedPayment { payment_hash }
3180
+ } ;
3181
+
3182
+ failed_forwards. push( ( htlc_source, payment_hash,
3183
+ HTLCFailReason :: Reason { failure_code: $err_code, data: $err_data } ,
3184
+ reason
3185
+ ) ) ;
3186
+ continue ;
3179
3187
}
3180
- macro_rules! fail_forward {
3181
- ( $msg : expr , $err_code : expr , $err_data : expr , $phantom_ss : expr ) => {
3182
- {
3183
- failure_handler! ( $msg , $err_code , $err_data , $phantom_ss , true ) ;
3184
- }
3188
+ }
3189
+ macro_rules! fail_forward {
3190
+ ( $msg : expr , $err_code : expr , $err_data : expr , $phantom_ss : expr ) => {
3191
+ {
3192
+ failure_handler! ( $msg , $err_code , $err_data , $phantom_ss , true ) ;
3185
3193
}
3186
3194
}
3187
- macro_rules! failed_payment {
3188
- ( $msg : expr , $err_code : expr , $err_data : expr , $phantom_ss : expr ) => {
3189
- {
3190
- failure_handler! ( $msg , $err_code , $err_data , $phantom_ss , false ) ;
3191
- }
3195
+ }
3196
+ macro_rules! failed_payment {
3197
+ ( $msg : expr , $err_code : expr , $err_data : expr , $phantom_ss : expr ) => {
3198
+ {
3199
+ failure_handler! ( $msg , $err_code , $err_data , $phantom_ss , false ) ;
3192
3200
}
3193
3201
}
3194
- if let PendingHTLCRouting :: Forward { onion_packet, .. } = routing {
3195
- let phantom_secret_res = self . keys_manager. get_node_secret( Recipient :: PhantomNode ) ;
3196
- if phantom_secret_res. is_ok( ) && fake_scid:: is_valid_phantom( & self . fake_scid_rand_bytes, short_chan_id) {
3197
- let phantom_shared_secret = SharedSecret :: new( & onion_packet. public_key. unwrap( ) , & phantom_secret_res. unwrap( ) ) . secret_bytes( ) ;
3198
- let next_hop = match onion_utils:: decode_next_payment_hop( phantom_shared_secret, & onion_packet. hop_data, onion_packet. hmac, payment_hash) {
3199
- Ok ( res) => res,
3200
- Err ( onion_utils:: OnionDecodeErr :: Malformed { err_msg, err_code } ) => {
3201
- let sha256_of_onion = Sha256 :: hash( & onion_packet. hop_data) . into_inner( ) ;
3202
- // In this scenario, the phantom would have sent us an
3203
- // `update_fail_malformed_htlc`, meaning here we encrypt the error as
3204
- // if it came from us (the second-to-last hop) but contains the sha256
3205
- // of the onion.
3206
- failed_payment!( err_msg, err_code, sha256_of_onion. to_vec( ) , None ) ;
3207
- } ,
3208
- Err ( onion_utils:: OnionDecodeErr :: Relay { err_msg, err_code } ) => {
3209
- failed_payment!( err_msg, err_code, Vec :: new( ) , Some ( phantom_shared_secret) ) ;
3210
- } ,
3211
- } ;
3212
- match next_hop {
3213
- onion_utils:: Hop :: Receive ( hop_data) => {
3214
- match self . construct_recv_pending_htlc_info( hop_data, incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value, Some ( phantom_shared_secret) ) {
3215
- Ok ( info) => phantom_receives. push( ( prev_short_channel_id, prev_funding_outpoint, vec![ ( info, prev_htlc_id) ] ) ) ,
3216
- Err ( ReceiveError { err_code, err_data, msg } ) => failed_payment!( msg, err_code, err_data, Some ( phantom_shared_secret) )
3217
- }
3218
- } ,
3219
- _ => panic!( ) ,
3220
- }
3221
- } else {
3222
- fail_forward!( format!( "Unknown short channel id {} for forward HTLC" , short_chan_id) , 0x4000 | 10 , Vec :: new( ) , None ) ;
3202
+ }
3203
+ if let PendingHTLCRouting :: Forward { onion_packet, .. } = routing {
3204
+ let phantom_secret_res = self . keys_manager. get_node_secret( Recipient :: PhantomNode ) ;
3205
+ if phantom_secret_res. is_ok( ) && fake_scid:: is_valid_phantom( & self . fake_scid_rand_bytes, short_chan_id, & self . genesis_hash) {
3206
+ let phantom_shared_secret = SharedSecret :: new( & onion_packet. public_key. unwrap( ) , & phantom_secret_res. unwrap( ) ) . secret_bytes( ) ;
3207
+ let next_hop = match onion_utils:: decode_next_payment_hop( phantom_shared_secret, & onion_packet. hop_data, onion_packet. hmac, payment_hash) {
3208
+ Ok ( res) => res,
3209
+ Err ( onion_utils:: OnionDecodeErr :: Malformed { err_msg, err_code } ) => {
3210
+ let sha256_of_onion = Sha256 :: hash( & onion_packet. hop_data) . into_inner( ) ;
3211
+ // In this scenario, the phantom would have sent us an
3212
+ // `update_fail_malformed_htlc`, meaning here we encrypt the error as
3213
+ // if it came from us (the second-to-last hop) but contains the sha256
3214
+ // of the onion.
3215
+ failed_payment!( err_msg, err_code, sha256_of_onion. to_vec( ) , None ) ;
3216
+ } ,
3217
+ Err ( onion_utils:: OnionDecodeErr :: Relay { err_msg, err_code } ) => {
3218
+ failed_payment!( err_msg, err_code, Vec :: new( ) , Some ( phantom_shared_secret) ) ;
3219
+ } ,
3220
+ } ;
3221
+ match next_hop {
3222
+ onion_utils:: Hop :: Receive ( hop_data) => {
3223
+ match self . construct_recv_pending_htlc_info( hop_data, incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value, Some ( phantom_shared_secret) ) {
3224
+ Ok ( info) => phantom_receives. push( ( prev_short_channel_id, prev_funding_outpoint, vec![ ( info, prev_htlc_id) ] ) ) ,
3225
+ Err ( ReceiveError { err_code, err_data, msg } ) => failed_payment!( msg, err_code, err_data, Some ( phantom_shared_secret) )
3226
+ }
3227
+ } ,
3228
+ _ => panic!( ) ,
3223
3229
}
3224
3230
} else {
3225
3231
fail_forward!( format!( "Unknown short channel id {} for forward HTLC" , short_chan_id) , 0x4000 | 10 , Vec :: new( ) , None ) ;
3226
3232
}
3227
- } ,
3233
+ } else {
3234
+ fail_forward!( format!( "Unknown short channel id {} for forward HTLC" , short_chan_id) , 0x4000 | 10 , Vec :: new( ) , None ) ;
3235
+ }
3236
+ } ,
3228
3237
HTLCForwardInfo :: FailHTLC { .. } => {
3229
3238
// Channel went away before we could fail it. This implies
3230
3239
// the channel is now on chain and our counterparty is
@@ -3252,11 +3261,13 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
3252
3261
let mut fail_htlc_msgs = Vec :: new ( ) ;
3253
3262
for forward_info in pending_forwards. drain ( ..) {
3254
3263
match forward_info {
3255
- HTLCForwardInfo :: AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info : PendingHTLCInfo {
3256
- routing : PendingHTLCRouting :: Forward {
3257
- onion_packet, ..
3258
- } , incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value } ,
3259
- prev_funding_outpoint } => {
3264
+ HTLCForwardInfo :: AddHTLC ( PendingAddHTLCInfo {
3265
+ prev_short_channel_id, prev_htlc_id, prev_funding_outpoint ,
3266
+ forward_info : PendingHTLCInfo {
3267
+ incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value,
3268
+ routing : PendingHTLCRouting :: Forward { onion_packet, .. } , amt_incoming : _,
3269
+ } ,
3270
+ } ) => {
3260
3271
log_trace ! ( self . logger, "Adding HTLC from short id {} with payment_hash {} to channel with short id {} after delay" , prev_short_channel_id, log_bytes!( payment_hash. 0 ) , short_chan_id) ;
3261
3272
let htlc_source = HTLCSource :: PreviousHopData ( HTLCPreviousHopData {
3262
3273
short_channel_id : prev_short_channel_id,
@@ -3377,9 +3388,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
3377
3388
} else {
3378
3389
for forward_info in pending_forwards. drain ( ..) {
3379
3390
match forward_info {
3380
- HTLCForwardInfo :: AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info : PendingHTLCInfo {
3381
- routing, incoming_shared_secret, payment_hash, amt_to_forward, .. } ,
3382
- prev_funding_outpoint } => {
3391
+ HTLCForwardInfo :: AddHTLC ( PendingAddHTLCInfo {
3392
+ prev_short_channel_id, prev_htlc_id, prev_funding_outpoint,
3393
+ forward_info : PendingHTLCInfo {
3394
+ routing, incoming_shared_secret, payment_hash, amt_to_forward, ..
3395
+ }
3396
+ } ) => {
3383
3397
let ( cltv_expiry, onion_payload, payment_data, phantom_shared_secret) = match routing {
3384
3398
PendingHTLCRouting :: Receive { payment_data, incoming_cltv_expiry, phantom_shared_secret } => {
3385
3399
let _legacy_hop_data = Some ( payment_data. clone ( ) ) ;
@@ -5089,12 +5103,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
5089
5103
PendingHTLCRouting :: ReceiveKeysend { .. } => 0 ,
5090
5104
} ) {
5091
5105
hash_map:: Entry :: Occupied ( mut entry) => {
5092
- entry. get_mut ( ) . push ( HTLCForwardInfo :: AddHTLC { prev_short_channel_id , prev_funding_outpoint ,
5093
- prev_htlc_id, forward_info } ) ;
5106
+ entry. get_mut ( ) . push ( HTLCForwardInfo :: AddHTLC ( PendingAddHTLCInfo {
5107
+ prev_short_channel_id , prev_funding_outpoint , prev_htlc_id, forward_info } ) ) ;
5094
5108
} ,
5095
5109
hash_map:: Entry :: Vacant ( entry) => {
5096
- entry. insert ( vec ! ( HTLCForwardInfo :: AddHTLC { prev_short_channel_id , prev_funding_outpoint ,
5097
- prev_htlc_id, forward_info } ) ) ;
5110
+ entry. insert ( vec ! ( HTLCForwardInfo :: AddHTLC ( PendingAddHTLCInfo {
5111
+ prev_short_channel_id , prev_funding_outpoint , prev_htlc_id, forward_info } ) ) ) ;
5098
5112
}
5099
5113
}
5100
5114
}
@@ -6459,7 +6473,8 @@ impl_writeable_tlv_based!(PendingHTLCInfo, {
6459
6473
( 2 , incoming_shared_secret, required) ,
6460
6474
( 4 , payment_hash, required) ,
6461
6475
( 6 , amt_to_forward, required) ,
6462
- ( 8 , outgoing_cltv_value, required)
6476
+ ( 8 , outgoing_cltv_value, required) ,
6477
+ ( 9 , amt_incoming, option) ,
6463
6478
} ) ;
6464
6479
6465
6480
@@ -6681,18 +6696,20 @@ impl_writeable_tlv_based_enum!(HTLCFailReason,
6681
6696
} ,
6682
6697
; ) ;
6683
6698
6699
+ impl_writeable_tlv_based ! ( PendingAddHTLCInfo , {
6700
+ ( 0 , forward_info, required) ,
6701
+ ( 2 , prev_short_channel_id, required) ,
6702
+ ( 4 , prev_htlc_id, required) ,
6703
+ ( 6 , prev_funding_outpoint, required) ,
6704
+ } ) ;
6705
+
6684
6706
impl_writeable_tlv_based_enum ! ( HTLCForwardInfo ,
6685
- ( 0 , AddHTLC ) => {
6686
- ( 0 , forward_info, required) ,
6687
- ( 2 , prev_short_channel_id, required) ,
6688
- ( 4 , prev_htlc_id, required) ,
6689
- ( 6 , prev_funding_outpoint, required) ,
6690
- } ,
6691
6707
( 1 , FailHTLC ) => {
6692
6708
( 0 , htlc_id, required) ,
6693
6709
( 2 , err_packet, required) ,
6694
- } ,
6695
- ; ) ;
6710
+ } ;
6711
+ ( 0 , AddHTLC )
6712
+ ) ;
6696
6713
6697
6714
impl_writeable_tlv_based ! ( PendingInboundPayment , {
6698
6715
( 0 , payment_secret, required) ,
0 commit comments