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