@@ -2459,9 +2459,10 @@ where
2459
2459
// `total_consistency_lock`
2460
2460
// |
2461
2461
// |__`forward_htlcs`
2462
- // | |
2463
2462
// | |__`pending_intercepted_htlcs`
2464
2463
// |
2464
+ // |__`receive_htlcs`
2465
+ // |
2465
2466
// |__`decode_update_add_htlcs`
2466
2467
// |
2467
2468
// |__`per_peer_state`
@@ -2537,7 +2538,7 @@ pub struct ChannelManager<
2537
2538
/// See `ChannelManager` struct-level documentation for lock order requirements.
2538
2539
pending_outbound_payments: OutboundPayments,
2539
2540
2540
- /// SCID/SCID Alias -> forward infos. Key of 0 means payments received.
2541
+ /// SCID/SCID Alias -> forward infos.
2541
2542
///
2542
2543
/// Note that because we may have an SCID Alias as the key we can have two entries per channel,
2543
2544
/// though in practice we probably won't be receiving HTLCs for a channel both via the alias
@@ -2556,7 +2557,13 @@ pub struct ChannelManager<
2556
2557
///
2557
2558
/// See `ChannelManager` struct-level documentation for lock order requirements.
2558
2559
pending_intercepted_htlcs: Mutex<HashMap<InterceptId, PendingAddHTLCInfo>>,
2559
-
2560
+ /// Storage for HTLCs that are meant for us.
2561
+ ///
2562
+ /// See `ChannelManager` struct-level documentation for lock order requirements.
2563
+ #[cfg(test)]
2564
+ pub(super) receive_htlcs: Mutex<Vec<HTLCForwardInfo>>,
2565
+ #[cfg(not(test))]
2566
+ receive_htlcs: Mutex<Vec<HTLCForwardInfo>>,
2560
2567
/// SCID/SCID Alias -> pending `update_add_htlc`s to decode.
2561
2568
///
2562
2569
/// Note that because we may have an SCID Alias as the key we can have two entries per channel,
@@ -3738,6 +3745,7 @@ where
3738
3745
outbound_scid_aliases: Mutex::new(new_hash_set()),
3739
3746
pending_outbound_payments: OutboundPayments::new(new_hash_map()),
3740
3747
forward_htlcs: Mutex::new(new_hash_map()),
3748
+ receive_htlcs: Mutex::new(Vec::new()),
3741
3749
decode_update_add_htlcs: Mutex::new(new_hash_map()),
3742
3750
claimable_payments: Mutex::new(ClaimablePayments { claimable_payments: new_hash_map(), pending_claiming_payments: new_hash_map() }),
3743
3751
pending_intercepted_htlcs: Mutex::new(new_hash_map()),
@@ -6355,6 +6363,9 @@ where
6355
6363
if !self.forward_htlcs.lock().unwrap().is_empty() {
6356
6364
return true;
6357
6365
}
6366
+ if !self.receive_htlcs.lock().unwrap().is_empty() {
6367
+ return true;
6368
+ }
6358
6369
if !self.decode_update_add_htlcs.lock().unwrap().is_empty() {
6359
6370
return true;
6360
6371
}
@@ -6402,22 +6413,18 @@ where
6402
6413
6403
6414
for (short_chan_id, mut pending_forwards) in forward_htlcs {
6404
6415
should_persist = NotifyOption::DoPersist;
6405
- if short_chan_id != 0 {
6406
- self.process_forward_htlcs(
6407
- short_chan_id,
6408
- &mut pending_forwards,
6409
- &mut failed_forwards,
6410
- &mut phantom_receives,
6411
- );
6412
- } else {
6413
- self.process_receive_htlcs(
6414
- &mut pending_forwards,
6415
- &mut new_events,
6416
- &mut failed_forwards,
6417
- );
6418
- }
6416
+ self.process_forward_htlcs(
6417
+ short_chan_id,
6418
+ &mut pending_forwards,
6419
+ &mut failed_forwards,
6420
+ &mut phantom_receives,
6421
+ );
6419
6422
}
6420
6423
6424
+ let mut receive_htlcs = Vec::new();
6425
+ mem::swap(&mut receive_htlcs, &mut self.receive_htlcs.lock().unwrap());
6426
+ self.process_receive_htlcs(receive_htlcs, &mut new_events, &mut failed_forwards);
6427
+
6421
6428
let best_block_height = self.best_block.read().unwrap().height;
6422
6429
let needs_persist = self.pending_outbound_payments.check_retry_payments(
6423
6430
&self.router,
@@ -6929,11 +6936,11 @@ where
6929
6936
}
6930
6937
6931
6938
fn process_receive_htlcs(
6932
- &self, pending_forwards: &mut Vec<HTLCForwardInfo>,
6939
+ &self, receive_htlcs: Vec<HTLCForwardInfo>,
6933
6940
new_events: &mut VecDeque<(Event, Option<EventCompletionAction>)>,
6934
6941
failed_forwards: &mut Vec<FailedHTLCForward>,
6935
6942
) {
6936
- 'next_forwardable_htlc: for forward_info in pending_forwards.drain(.. ) {
6943
+ 'next_forwardable_htlc: for forward_info in receive_htlcs.into_iter( ) {
6937
6944
match forward_info {
6938
6945
HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
6939
6946
prev_short_channel_id,
@@ -10346,8 +10353,21 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
10346
10353
let scid = match forward_info.routing {
10347
10354
PendingHTLCRouting::Forward { short_channel_id, .. } => short_channel_id,
10348
10355
PendingHTLCRouting::TrampolineForward { .. } => 0,
10349
- PendingHTLCRouting::Receive { .. } => 0,
10350
- PendingHTLCRouting::ReceiveKeysend { .. } => 0,
10356
+ PendingHTLCRouting::Receive { .. }
10357
+ | PendingHTLCRouting::ReceiveKeysend { .. } => {
10358
+ self.receive_htlcs.lock().unwrap().push(HTLCForwardInfo::AddHTLC(
10359
+ PendingAddHTLCInfo {
10360
+ prev_short_channel_id,
10361
+ prev_counterparty_node_id,
10362
+ prev_funding_outpoint,
10363
+ prev_channel_id,
10364
+ prev_htlc_id,
10365
+ prev_user_channel_id,
10366
+ forward_info,
10367
+ },
10368
+ ));
10369
+ continue;
10370
+ },
10351
10371
};
10352
10372
// Pull this now to avoid introducing a lock order with `forward_htlcs`.
10353
10373
let is_our_scid = self.short_to_chan_info.read().unwrap().contains_key(&scid);
@@ -15091,6 +15111,8 @@ where
15091
15111
}
15092
15112
}
15093
15113
15114
+ let receive_htlcs = self.receive_htlcs.lock().unwrap();
15115
+
15094
15116
let mut decode_update_add_htlcs_opt = None;
15095
15117
let decode_update_add_htlcs = self.decode_update_add_htlcs.lock().unwrap();
15096
15118
if !decode_update_add_htlcs.is_empty() {
@@ -15258,6 +15280,7 @@ where
15258
15280
(17, in_flight_monitor_updates, option),
15259
15281
(19, peer_storage_dir, optional_vec),
15260
15282
(21, self.flow.writeable_async_receive_offer_cache(), required),
15283
+ (23, *receive_htlcs, required_vec),
15261
15284
});
15262
15285
15263
15286
Ok(())
@@ -15818,6 +15841,7 @@ where
15818
15841
const MAX_ALLOC_SIZE: usize = 1024 * 64;
15819
15842
let forward_htlcs_count: u64 = Readable::read(reader)?;
15820
15843
let mut forward_htlcs = hash_map_with_capacity(cmp::min(forward_htlcs_count as usize, 128));
15844
+ let mut legacy_receive_htlcs: Vec<HTLCForwardInfo> = Vec::new();
15821
15845
for _ in 0..forward_htlcs_count {
15822
15846
let short_channel_id = Readable::read(reader)?;
15823
15847
let pending_forwards_count: u64 = Readable::read(reader)?;
@@ -15826,7 +15850,26 @@ where
15826
15850
MAX_ALLOC_SIZE / mem::size_of::<HTLCForwardInfo>(),
15827
15851
));
15828
15852
for _ in 0..pending_forwards_count {
15829
- pending_forwards.push(Readable::read(reader)?);
15853
+ let pending_htlc = Readable::read(reader)?;
15854
+ // Prior to LDK 0.2, Receive HTLCs used to be stored in `forward_htlcs` under SCID == 0. Here we migrate
15855
+ // the old data if necessary.
15856
+ if short_channel_id == 0 {
15857
+ match pending_htlc {
15858
+ HTLCForwardInfo::AddHTLC(ref htlc_info) => {
15859
+ if matches!(
15860
+ htlc_info.forward_info.routing,
15861
+ PendingHTLCRouting::Receive { .. }
15862
+ | PendingHTLCRouting::ReceiveKeysend { .. }
15863
+ ) {
15864
+ legacy_receive_htlcs.push(pending_htlc);
15865
+ continue;
15866
+ }
15867
+ },
15868
+ _ => {},
15869
+ }
15870
+ }
15871
+
15872
+ pending_forwards.push(pending_htlc);
15830
15873
}
15831
15874
forward_htlcs.insert(short_channel_id, pending_forwards);
15832
15875
}
@@ -15943,6 +15986,7 @@ where
15943
15986
let mut inbound_payment_id_secret = None;
15944
15987
let mut peer_storage_dir: Option<Vec<(PublicKey, Vec<u8>)>> = None;
15945
15988
let mut async_receive_offer_cache: AsyncReceiveOfferCache = AsyncReceiveOfferCache::new();
15989
+ let mut receive_htlcs = None;
15946
15990
read_tlv_fields!(reader, {
15947
15991
(1, pending_outbound_payments_no_retry, option),
15948
15992
(2, pending_intercepted_htlcs, option),
@@ -15961,8 +16005,10 @@ where
15961
16005
(17, in_flight_monitor_updates, option),
15962
16006
(19, peer_storage_dir, optional_vec),
15963
16007
(21, async_receive_offer_cache, (default_value, async_receive_offer_cache)),
16008
+ (23, receive_htlcs, optional_vec),
15964
16009
});
15965
16010
let mut decode_update_add_htlcs = decode_update_add_htlcs.unwrap_or_else(|| new_hash_map());
16011
+ let receive_htlcs = receive_htlcs.unwrap_or_else(|| legacy_receive_htlcs);
15966
16012
let peer_storage_dir: Vec<(PublicKey, Vec<u8>)> = peer_storage_dir.unwrap_or_else(Vec::new);
15967
16013
if fake_scid_rand_bytes.is_none() {
15968
16014
fake_scid_rand_bytes = Some(args.entropy_source.get_secure_random_bytes());
@@ -16791,6 +16837,7 @@ where
16791
16837
pending_intercepted_htlcs: Mutex::new(pending_intercepted_htlcs.unwrap()),
16792
16838
16793
16839
forward_htlcs: Mutex::new(forward_htlcs),
16840
+ receive_htlcs: Mutex::new(receive_htlcs),
16794
16841
decode_update_add_htlcs: Mutex::new(decode_update_add_htlcs),
16795
16842
claimable_payments: Mutex::new(ClaimablePayments {
16796
16843
claimable_payments,
0 commit comments