@@ -691,6 +691,8 @@ pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, M, T, F, L> = ChannelManage
691
691
// |
692
692
// |__`forward_htlcs`
693
693
// |
694
+ // |__`pending_intercepted_payments`
695
+ // |
694
696
// |__`channel_state`
695
697
// | |
696
698
// | |__`id_to_peer`
@@ -2334,8 +2336,10 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
2334
2336
let forwarding_id_opt = match id_option {
2335
2337
None => { // unknown_next_peer
2336
2338
// Note that this is likely a timing oracle for detecting whether an scid is a
2337
- // phantom.
2338
- if fake_scid:: is_valid_phantom ( & self . fake_scid_rand_bytes , * short_channel_id, & self . genesis_hash ) {
2339
+ // phantom or an intercept.
2340
+ if fake_scid:: is_valid_phantom ( & self . fake_scid_rand_bytes , * short_channel_id, & self . genesis_hash ) ||
2341
+ fake_scid:: is_valid_intercept ( & self . fake_scid_rand_bytes , * short_channel_id, & self . genesis_hash )
2342
+ {
2339
2343
None
2340
2344
} else {
2341
2345
break Some ( ( "Don't have available channel for forwarding as requested." , 0x4000 | 10 , None ) ) ;
@@ -5113,28 +5117,80 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
5113
5117
fn forward_htlcs ( & self , per_source_pending_forwards : & mut [ ( u64 , OutPoint , Vec < ( PendingHTLCInfo , u64 ) > ) ] ) {
5114
5118
for & mut ( prev_short_channel_id, prev_funding_outpoint, ref mut pending_forwards) in per_source_pending_forwards {
5115
5119
let mut forward_event = None ;
5120
+ let mut new_intercept_events = Vec :: new ( ) ;
5121
+ let mut failed_intercept_forwards = Vec :: new ( ) ;
5116
5122
if !pending_forwards. is_empty ( ) {
5117
5123
let mut forward_htlcs = self . forward_htlcs . lock ( ) . unwrap ( ) ;
5118
- if forward_htlcs. is_empty ( ) {
5119
- forward_event = Some ( Duration :: from_millis ( MIN_HTLC_RELAY_HOLDING_CELL_MILLIS ) )
5120
- }
5121
5124
for ( forward_info, prev_htlc_id) in pending_forwards. drain ( ..) {
5122
- match forward_htlcs. entry ( match forward_info. routing {
5123
- PendingHTLCRouting :: Forward { short_channel_id, .. } => short_channel_id,
5124
- PendingHTLCRouting :: Receive { .. } => 0 ,
5125
- PendingHTLCRouting :: ReceiveKeysend { .. } => 0 ,
5126
- } ) {
5125
+ let scid = match forward_info. routing {
5126
+ PendingHTLCRouting :: Forward { short_channel_id, .. } => short_channel_id,
5127
+ PendingHTLCRouting :: Receive { .. } => 0 ,
5128
+ PendingHTLCRouting :: ReceiveKeysend { .. } => 0 ,
5129
+ } ;
5130
+ let forward_htlcs_empty = forward_htlcs. is_empty ( ) ;
5131
+ match forward_htlcs. entry ( scid) {
5127
5132
hash_map:: Entry :: Occupied ( mut entry) => {
5128
5133
entry. get_mut ( ) . push ( HTLCForwardInfo :: AddHTLC ( PendingAddHTLCInfo {
5129
5134
prev_short_channel_id, prev_funding_outpoint, prev_htlc_id, forward_info } ) ) ;
5130
5135
} ,
5131
5136
hash_map:: Entry :: Vacant ( entry) => {
5132
- entry. insert ( vec ! ( HTLCForwardInfo :: AddHTLC ( PendingAddHTLCInfo {
5133
- prev_short_channel_id, prev_funding_outpoint, prev_htlc_id, forward_info } ) ) ) ;
5137
+ if !self . short_to_chan_info . read ( ) . unwrap ( ) . contains_key ( & scid) &&
5138
+ forward_info. amt_incoming . is_some ( ) &&
5139
+ fake_scid:: is_valid_intercept ( & self . fake_scid_rand_bytes , scid, & self . genesis_hash )
5140
+ {
5141
+ let intercept_id = InterceptId ( Sha256 :: hash ( & forward_info. incoming_shared_secret ) . into_inner ( ) ) ;
5142
+ let mut pending_intercepts = self . pending_intercepted_htlcs . lock ( ) . unwrap ( ) ;
5143
+ match pending_intercepts. entry ( intercept_id) {
5144
+ hash_map:: Entry :: Vacant ( entry) => {
5145
+ new_intercept_events. push ( events:: Event :: HTLCIntercepted {
5146
+ requested_next_hop_scid : scid,
5147
+ payment_hash : forward_info. payment_hash ,
5148
+ inbound_amount_msat : forward_info. amt_incoming . unwrap ( ) ,
5149
+ expected_outbound_amount_msat : forward_info. amt_to_forward ,
5150
+ intercept_id
5151
+ } ) ;
5152
+ entry. insert ( PendingAddHTLCInfo {
5153
+ prev_short_channel_id, prev_funding_outpoint, prev_htlc_id, forward_info } ) ;
5154
+ } ,
5155
+ hash_map:: Entry :: Occupied ( _) => {
5156
+ log_info ! ( self . logger, "Failed to forward incoming HTLC: detected duplicate intercepted payment over short channel id {}" , scid) ;
5157
+ let htlc_source = HTLCSource :: PreviousHopData ( HTLCPreviousHopData {
5158
+ short_channel_id : prev_short_channel_id,
5159
+ outpoint : prev_funding_outpoint,
5160
+ htlc_id : prev_htlc_id,
5161
+ incoming_packet_shared_secret : forward_info. incoming_shared_secret ,
5162
+ phantom_shared_secret : None ,
5163
+ } ) ;
5164
+
5165
+ failed_intercept_forwards. push ( ( htlc_source, forward_info. payment_hash ,
5166
+ HTLCFailReason :: Reason { failure_code : 0x4000 | 10 , data : Vec :: new ( ) } ,
5167
+ HTLCDestination :: UnknownNextHop { requested_forward_scid : scid } ,
5168
+ ) ) ;
5169
+ }
5170
+ }
5171
+ } else {
5172
+ // We don't want to generate a PendingHTLCsForwardable event if only intercepted
5173
+ // payments are being forwarded.
5174
+ if forward_htlcs_empty {
5175
+ forward_event = Some ( Duration :: from_millis ( MIN_HTLC_RELAY_HOLDING_CELL_MILLIS ) ) ;
5176
+ }
5177
+ entry. insert ( vec ! ( HTLCForwardInfo :: AddHTLC ( PendingAddHTLCInfo {
5178
+ prev_short_channel_id, prev_funding_outpoint, prev_htlc_id, forward_info } ) ) ) ;
5179
+ }
5134
5180
}
5135
5181
}
5136
5182
}
5137
5183
}
5184
+
5185
+ for ( htlc_source, payment_hash, failure_reason, destination) in failed_intercept_forwards. drain ( ..) {
5186
+ self . fail_htlc_backwards_internal ( htlc_source, & payment_hash, failure_reason, destination) ;
5187
+ }
5188
+
5189
+ if !new_intercept_events. is_empty ( ) {
5190
+ let mut events = self . pending_events . lock ( ) . unwrap ( ) ;
5191
+ events. append ( & mut new_intercept_events) ;
5192
+ }
5193
+
5138
5194
match forward_event {
5139
5195
Some ( time) => {
5140
5196
let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
0 commit comments