Skip to content

Commit bcccef4

Browse files
Move claimable_htlcs to separate lock
1 parent 505102d commit bcccef4

File tree

1 file changed

+54
-48
lines changed

1 file changed

+54
-48
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -395,13 +395,6 @@ pub(super) enum RAACommitmentOrder {
395395
// Note this is only exposed in cfg(test):
396396
pub(super) struct ChannelHolder<Signer: Sign> {
397397
pub(super) by_id: HashMap<[u8; 32], Channel<Signer>>,
398-
/// Map from payment hash to the payment data and any HTLCs which are to us and can be
399-
/// failed/claimed by the user.
400-
///
401-
/// Note that while this is held in the same mutex as the channels themselves, no consistency
402-
/// guarantees are made about the channels given here actually existing anymore by the time you
403-
/// go to read them!
404-
claimable_htlcs: HashMap<PaymentHash, (events::PaymentPurpose, Vec<ClaimableHTLC>)>,
405398
/// Messages to send to peers - pushed to in the same lock that they are generated in (except
406399
/// for broadcast messages, where ordering isn't as strict).
407400
pub(super) pending_msg_events: Vec<MessageSendEvent>,
@@ -682,6 +675,8 @@ pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, M, T, F, L> = ChannelManage
682675
// | |
683676
// | |__`pending_inbound_payments`
684677
// | |
678+
// | |__`claimable_htlcs`
679+
// | |
685680
// | |__`pending_outbound_payments`
686681
// | |
687682
// | |__`best_block`
@@ -753,6 +748,15 @@ pub struct ChannelManager<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
753748
#[cfg(not(test))]
754749
forward_htlcs: Mutex<HashMap<u64, Vec<HTLCForwardInfo>>>,
755750

751+
/// Map from payment hash to the payment data and any HTLCs which are to us and can be
752+
/// failed/claimed by the user.
753+
///
754+
/// Note that, no consistency guarantees are made about the channels given here actually
755+
/// existing anymore by the time you go to read them!
756+
///
757+
/// See `ChannelManager` struct-level documentation for lock order requirements.
758+
claimable_htlcs: Mutex<HashMap<PaymentHash, (events::PaymentPurpose, Vec<ClaimableHTLC>)>>,
759+
756760
/// The set of outbound SCID aliases across all our channels, including unconfirmed channels
757761
/// and some closed channels which reached a usable state prior to being closed. This is used
758762
/// only to avoid duplicates, and is not persisted explicitly to disk, but rebuilt from the
@@ -1657,13 +1661,13 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
16571661

16581662
channel_state: Mutex::new(ChannelHolder{
16591663
by_id: HashMap::new(),
1660-
claimable_htlcs: HashMap::new(),
16611664
pending_msg_events: Vec::new(),
16621665
}),
16631666
outbound_scid_aliases: Mutex::new(HashSet::new()),
16641667
pending_inbound_payments: Mutex::new(HashMap::new()),
16651668
pending_outbound_payments: Mutex::new(HashMap::new()),
16661669
forward_htlcs: Mutex::new(HashMap::new()),
1670+
claimable_htlcs: Mutex::new(HashMap::new()),
16671671
id_to_peer: Mutex::new(HashMap::new()),
16681672
short_to_chan_info: FairRwLock::new(HashMap::new()),
16691673

@@ -3142,8 +3146,6 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
31423146
mem::swap(&mut forward_htlcs, &mut self.forward_htlcs.lock().unwrap());
31433147

31443148
for (short_chan_id, mut pending_forwards) in forward_htlcs {
3145-
let mut channel_state_lock = self.channel_state.lock().unwrap();
3146-
let channel_state = &mut *channel_state_lock;
31473149
if short_chan_id != 0 {
31483150
macro_rules! forwarding_channel_not_found {
31493151
() => {
@@ -3242,6 +3244,8 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
32423244
continue;
32433245
}
32443246
};
3247+
let mut channel_state_lock = self.channel_state.lock().unwrap();
3248+
let channel_state = &mut *channel_state_lock;
32453249
match channel_state.by_id.entry(forward_chan_id) {
32463250
hash_map::Entry::Vacant(_) => {
32473251
forwarding_channel_not_found!();
@@ -3434,7 +3438,8 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
34343438
payment_secret: $payment_data.payment_secret,
34353439
}
34363440
};
3437-
let (_, htlcs) = channel_state.claimable_htlcs.entry(payment_hash)
3441+
let mut claimable_htlcs = self.claimable_htlcs.lock().unwrap();
3442+
let (_, htlcs) = claimable_htlcs.entry(payment_hash)
34383443
.or_insert_with(|| (purpose(), Vec::new()));
34393444
if htlcs.len() == 1 {
34403445
if let OnionPayload::Spontaneous(_) = htlcs[0].onion_payload {
@@ -3502,7 +3507,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
35023507
check_total_value!(payment_data, payment_preimage);
35033508
},
35043509
OnionPayload::Spontaneous(preimage) => {
3505-
match channel_state.claimable_htlcs.entry(payment_hash) {
3510+
match self.claimable_htlcs.lock().unwrap().entry(payment_hash) {
35063511
hash_map::Entry::Vacant(e) => {
35073512
let purpose = events::PaymentPurpose::SpontaneousPayment(preimage);
35083513
e.insert((purpose.clone(), vec![claimable_htlc]));
@@ -3792,7 +3797,9 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
37923797
true
37933798
});
37943799

3795-
channel_state.claimable_htlcs.retain(|payment_hash, (_, htlcs)| {
3800+
mem::drop(channel_state_lock);
3801+
3802+
self.claimable_htlcs.lock().unwrap().retain(|payment_hash, (_, htlcs)| {
37963803
if htlcs.is_empty() {
37973804
// This should be unreachable
37983805
debug_assert!(false);
@@ -3846,10 +3853,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
38463853
pub fn fail_htlc_backwards(&self, payment_hash: &PaymentHash) {
38473854
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
38483855

3849-
let removed_source = {
3850-
let mut channel_state = self.channel_state.lock().unwrap();
3851-
channel_state.claimable_htlcs.remove(payment_hash)
3852-
};
3856+
let removed_source = self.claimable_htlcs.lock().unwrap().remove(payment_hash);
38533857
if let Some((_, mut sources)) = removed_source {
38543858
for htlc in sources.drain(..) {
38553859
let mut htlc_msat_height_data = byte_utils::be64_to_array(htlc.value).to_vec();
@@ -4151,7 +4155,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
41514155

41524156
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
41534157

4154-
let removed_source = self.channel_state.lock().unwrap().claimable_htlcs.remove(&payment_hash);
4158+
let removed_source = self.claimable_htlcs.lock().unwrap().remove(&payment_hash);
41554159
if let Some((payment_purpose, mut sources)) = removed_source {
41564160
assert!(!sources.is_empty());
41574161

@@ -6019,28 +6023,28 @@ where
60196023
}
60206024
true
60216025
});
6026+
}
60226027

6023-
if let Some(height) = height_opt {
6024-
channel_state.claimable_htlcs.retain(|payment_hash, (_, htlcs)| {
6025-
htlcs.retain(|htlc| {
6026-
// If height is approaching the number of blocks we think it takes us to get
6027-
// our commitment transaction confirmed before the HTLC expires, plus the
6028-
// number of blocks we generally consider it to take to do a commitment update,
6029-
// just give up on it and fail the HTLC.
6030-
if height >= htlc.cltv_expiry - HTLC_FAIL_BACK_BUFFER {
6031-
let mut htlc_msat_height_data = byte_utils::be64_to_array(htlc.value).to_vec();
6032-
htlc_msat_height_data.extend_from_slice(&byte_utils::be32_to_array(height));
6033-
6034-
timed_out_htlcs.push((HTLCSource::PreviousHopData(htlc.prev_hop.clone()), payment_hash.clone(), HTLCFailReason::Reason {
6035-
failure_code: 0x4000 | 15,
6036-
data: htlc_msat_height_data
6037-
}, HTLCDestination::FailedPayment { payment_hash: payment_hash.clone() }));
6038-
false
6039-
} else { true }
6040-
});
6041-
!htlcs.is_empty() // Only retain this entry if htlcs has at least one entry.
6028+
if let Some(height) = height_opt {
6029+
self.claimable_htlcs.lock().unwrap().retain(|payment_hash, (_, htlcs)| {
6030+
htlcs.retain(|htlc| {
6031+
// If height is approaching the number of blocks we think it takes us to get
6032+
// our commitment transaction confirmed before the HTLC expires, plus the
6033+
// number of blocks we generally consider it to take to do a commitment update,
6034+
// just give up on it and fail the HTLC.
6035+
if height >= htlc.cltv_expiry - HTLC_FAIL_BACK_BUFFER {
6036+
let mut htlc_msat_height_data = byte_utils::be64_to_array(htlc.value).to_vec();
6037+
htlc_msat_height_data.extend_from_slice(&byte_utils::be32_to_array(height));
6038+
6039+
timed_out_htlcs.push((HTLCSource::PreviousHopData(htlc.prev_hop.clone()), payment_hash.clone(), HTLCFailReason::Reason {
6040+
failure_code: 0x4000 | 15,
6041+
data: htlc_msat_height_data
6042+
}, HTLCDestination::FailedPayment { payment_hash: payment_hash.clone() }));
6043+
false
6044+
} else { true }
60426045
});
6043-
}
6046+
!htlcs.is_empty() // Only retain this entry if htlcs has at least one entry.
6047+
});
60446048
}
60456049

60466050
self.handle_init_event_channel_failures(failed_channels);
@@ -6775,16 +6779,18 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Writeable for ChannelMana
67756779
}
67766780
}
67776781

6778-
let channel_state = self.channel_state.lock().unwrap();
6779-
let mut htlc_purposes: Vec<&events::PaymentPurpose> = Vec::new();
6780-
(channel_state.claimable_htlcs.len() as u64).write(writer)?;
6781-
for (payment_hash, (purpose, previous_hops)) in channel_state.claimable_htlcs.iter() {
6782-
payment_hash.write(writer)?;
6783-
(previous_hops.len() as u64).write(writer)?;
6784-
for htlc in previous_hops.iter() {
6785-
htlc.write(writer)?;
6782+
let mut htlc_purposes: Vec<events::PaymentPurpose> = Vec::new();
6783+
{
6784+
let claimable_htlcs = self.claimable_htlcs.lock().unwrap();
6785+
(claimable_htlcs.len() as u64).write(writer)?;
6786+
for (payment_hash, (purpose, previous_hops)) in claimable_htlcs.iter() {
6787+
payment_hash.write(writer)?;
6788+
(previous_hops.len() as u64).write(writer)?;
6789+
for htlc in previous_hops.iter() {
6790+
htlc.write(writer)?;
6791+
}
6792+
htlc_purposes.push(purpose.clone());
67866793
}
6787-
htlc_purposes.push(purpose);
67886794
}
67896795

67906796
let per_peer_state = self.per_peer_state.write().unwrap();
@@ -7389,14 +7395,14 @@ impl<'a, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
73897395

73907396
channel_state: Mutex::new(ChannelHolder {
73917397
by_id,
7392-
claimable_htlcs,
73937398
pending_msg_events: Vec::new(),
73947399
}),
73957400
inbound_payment_key: expanded_inbound_key,
73967401
pending_inbound_payments: Mutex::new(pending_inbound_payments),
73977402
pending_outbound_payments: Mutex::new(pending_outbound_payments.unwrap()),
73987403

73997404
forward_htlcs: Mutex::new(forward_htlcs),
7405+
claimable_htlcs: Mutex::new(claimable_htlcs),
74007406
outbound_scid_aliases: Mutex::new(outbound_scid_aliases),
74017407
id_to_peer: Mutex::new(id_to_peer),
74027408
short_to_chan_info: FairRwLock::new(short_to_chan_info),

0 commit comments

Comments
 (0)