Skip to content

Commit df12df3

Browse files
Move forward_htlcs into standalone lock
As we are eventually removing the `channel_state` lock, this commit moves the `forward_htlcs` map out of the `channel_state` lock, to ease that process.
1 parent 8886d1d commit df12df3

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -401,16 +401,6 @@ pub(super) struct ChannelHolder<Signer: Sign> {
401401
/// SCIDs being added once the funding transaction is confirmed at the channel's required
402402
/// confirmation depth.
403403
pub(super) short_to_chan_info: HashMap<u64, (PublicKey, [u8; 32])>,
404-
/// SCID/SCID Alias -> forward infos. Key of 0 means payments received.
405-
///
406-
/// Note that because we may have an SCID Alias as the key we can have two entries per channel,
407-
/// though in practice we probably won't be receiving HTLCs for a channel both via the alias
408-
/// and via the classic SCID.
409-
///
410-
/// Note that while this is held in the same mutex as the channels themselves, no consistency
411-
/// guarantees are made about the existence of a channel with the short id here, nor the short
412-
/// ids in the PendingHTLCInfo!
413-
pub(super) forward_htlcs: HashMap<u64, Vec<HTLCForwardInfo>>,
414404
/// Map from payment hash to the payment data and any HTLCs which are to us and can be
415405
/// failed/claimed by the user.
416406
///
@@ -722,6 +712,19 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
722712
/// Locked *after* channel_state.
723713
pending_outbound_payments: Mutex<HashMap<PaymentId, PendingOutboundPayment>>,
724714

715+
/// SCID/SCID Alias -> forward infos. Key of 0 means payments received.
716+
///
717+
/// Note that because we may have an SCID Alias as the key we can have two entries per channel,
718+
/// though in practice we probably won't be receiving HTLCs for a channel both via the alias
719+
/// and via the classic SCID.
720+
///
721+
/// Note that no consistency guarantees are made about the existence of a channel with the
722+
/// `short_channel_id` here, nor the `short_channel_id` in the `PendingHTLCInfo`!
723+
#[cfg(test)]
724+
pub(super) forward_htlcs: Mutex<HashMap<u64, Vec<HTLCForwardInfo>>>,
725+
#[cfg(not(test))]
726+
forward_htlcs: Mutex<HashMap<u64, Vec<HTLCForwardInfo>>>,
727+
725728
/// The set of outbound SCID aliases across all our channels, including unconfirmed channels
726729
/// and some closed channels which reached a usable state prior to being closed. This is used
727730
/// only to avoid duplicates, and is not persisted explicitly to disk, but rebuilt from the
@@ -1595,13 +1598,13 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
15951598
channel_state: Mutex::new(ChannelHolder{
15961599
by_id: HashMap::new(),
15971600
short_to_chan_info: HashMap::new(),
1598-
forward_htlcs: HashMap::new(),
15991601
claimable_htlcs: HashMap::new(),
16001602
pending_msg_events: Vec::new(),
16011603
}),
16021604
outbound_scid_aliases: Mutex::new(HashSet::new()),
16031605
pending_inbound_payments: Mutex::new(HashMap::new()),
16041606
pending_outbound_payments: Mutex::new(HashMap::new()),
1607+
forward_htlcs: Mutex::new(HashMap::new()),
16051608
id_to_peer: Mutex::new(HashMap::new()),
16061609

16071610
our_network_key: keys_manager.get_node_secret(Recipient::Node).unwrap(),
@@ -3005,7 +3008,10 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
30053008
let mut channel_state_lock = self.channel_state.lock().unwrap();
30063009
let channel_state = &mut *channel_state_lock;
30073010

3008-
for (short_chan_id, mut pending_forwards) in channel_state.forward_htlcs.drain() {
3011+
let mut forward_htlcs = HashMap::new();
3012+
mem::swap(&mut forward_htlcs, &mut self.forward_htlcs.lock().unwrap());
3013+
3014+
for (short_chan_id, mut pending_forwards) in forward_htlcs {
30093015
if short_chan_id != 0 {
30103016
let forward_chan_id = match channel_state.short_to_chan_info.get(&short_chan_id) {
30113017
Some((_cp_id, chan_id)) => chan_id.clone(),
@@ -3904,17 +3910,19 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
39043910
};
39053911

39063912
let mut forward_event = None;
3907-
if channel_state_lock.forward_htlcs.is_empty() {
3913+
let mut forward_htlcs = self.forward_htlcs.lock().unwrap();
3914+
if forward_htlcs.is_empty() {
39083915
forward_event = Some(Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS));
39093916
}
3910-
match channel_state_lock.forward_htlcs.entry(short_channel_id) {
3917+
match forward_htlcs.entry(short_channel_id) {
39113918
hash_map::Entry::Occupied(mut entry) => {
39123919
entry.get_mut().push(HTLCForwardInfo::FailHTLC { htlc_id, err_packet });
39133920
},
39143921
hash_map::Entry::Vacant(entry) => {
39153922
entry.insert(vec!(HTLCForwardInfo::FailHTLC { htlc_id, err_packet }));
39163923
}
39173924
}
3925+
mem::drop(forward_htlcs);
39183926
mem::drop(channel_state_lock);
39193927
let mut pending_events = self.pending_events.lock().unwrap();
39203928
if let Some(time) = forward_event {
@@ -4862,11 +4870,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
48624870
let mut forward_event = None;
48634871
if !pending_forwards.is_empty() {
48644872
let mut channel_state = self.channel_state.lock().unwrap();
4865-
if channel_state.forward_htlcs.is_empty() {
4873+
let mut forward_htlcs = self.forward_htlcs.lock().unwrap();
4874+
if forward_htlcs.is_empty() {
48664875
forward_event = Some(Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS))
48674876
}
48684877
for (forward_info, prev_htlc_id) in pending_forwards.drain(..) {
4869-
match channel_state.forward_htlcs.entry(match forward_info.routing {
4878+
match forward_htlcs.entry(match forward_info.routing {
48704879
PendingHTLCRouting::Forward { short_channel_id, .. } => short_channel_id,
48714880
PendingHTLCRouting::Receive { .. } => 0,
48724881
PendingHTLCRouting::ReceiveKeysend { .. } => 0,
@@ -6552,8 +6561,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Writeable f
65526561
}
65536562
}
65546563

6555-
(channel_state.forward_htlcs.len() as u64).write(writer)?;
6556-
for (short_channel_id, pending_forwards) in channel_state.forward_htlcs.iter() {
6564+
let forward_htlcs = self.forward_htlcs.lock().unwrap();
6565+
(forward_htlcs.len() as u64).write(writer)?;
6566+
for (short_channel_id, pending_forwards) in forward_htlcs.iter() {
65576567
short_channel_id.write(writer)?;
65586568
(pending_forwards.len() as u64).write(writer)?;
65596569
for forward in pending_forwards {
@@ -7165,14 +7175,14 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
71657175
channel_state: Mutex::new(ChannelHolder {
71667176
by_id,
71677177
short_to_chan_info,
7168-
forward_htlcs,
71697178
claimable_htlcs,
71707179
pending_msg_events: Vec::new(),
71717180
}),
71727181
inbound_payment_key: expanded_inbound_key,
71737182
pending_inbound_payments: Mutex::new(pending_inbound_payments),
71747183
pending_outbound_payments: Mutex::new(pending_outbound_payments.unwrap()),
71757184

7185+
forward_htlcs: Mutex::new(forward_htlcs),
71767186
outbound_scid_aliases: Mutex::new(outbound_scid_aliases),
71777187
id_to_peer: Mutex::new(id_to_peer),
71787188
fake_scid_rand_bytes: fake_scid_rand_bytes.unwrap(),

lightning/src/ln/onion_route_tests.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ fn test_onion_failure() {
540540
}, || {}, true, Some(17), None, None);
541541

542542
run_onion_failure_test("final_incorrect_cltv_expiry", 1, &nodes, &route, &payment_hash, &payment_secret, |_| {}, || {
543-
for (_, pending_forwards) in nodes[1].node.channel_state.lock().unwrap().forward_htlcs.iter_mut() {
543+
for (_, pending_forwards) in nodes[1].node.forward_htlcs.lock().unwrap().iter_mut() {
544544
for f in pending_forwards.iter_mut() {
545545
match f {
546546
&mut HTLCForwardInfo::AddHTLC { ref mut forward_info, .. } =>
@@ -553,7 +553,7 @@ fn test_onion_failure() {
553553

554554
run_onion_failure_test("final_incorrect_htlc_amount", 1, &nodes, &route, &payment_hash, &payment_secret, |_| {}, || {
555555
// violate amt_to_forward > msg.amount_msat
556-
for (_, pending_forwards) in nodes[1].node.channel_state.lock().unwrap().forward_htlcs.iter_mut() {
556+
for (_, pending_forwards) in nodes[1].node.forward_htlcs.lock().unwrap().iter_mut() {
557557
for f in pending_forwards.iter_mut() {
558558
match f {
559559
&mut HTLCForwardInfo::AddHTLC { ref mut forward_info, .. } =>
@@ -1021,8 +1021,8 @@ fn test_phantom_onion_hmac_failure() {
10211021

10221022
// Modify the payload so the phantom hop's HMAC is bogus.
10231023
let sha256_of_onion = {
1024-
let mut channel_state = nodes[1].node.channel_state.lock().unwrap();
1025-
let mut pending_forward = channel_state.forward_htlcs.get_mut(&phantom_scid).unwrap();
1024+
let mut forward_htlcs = nodes[1].node.forward_htlcs.lock().unwrap();
1025+
let mut pending_forward = forward_htlcs.get_mut(&phantom_scid).unwrap();
10261026
match pending_forward[0] {
10271027
HTLCForwardInfo::AddHTLC {
10281028
forward_info: PendingHTLCInfo {
@@ -1081,7 +1081,7 @@ fn test_phantom_invalid_onion_payload() {
10811081
commitment_signed_dance!(nodes[1], nodes[0], &update_0.commitment_signed, false, true);
10821082

10831083
// Modify the onion packet to have an invalid payment amount.
1084-
for (_, pending_forwards) in nodes[1].node.channel_state.lock().unwrap().forward_htlcs.iter_mut() {
1084+
for (_, pending_forwards) in nodes[1].node.forward_htlcs.lock().unwrap().iter_mut() {
10851085
for f in pending_forwards.iter_mut() {
10861086
match f {
10871087
&mut HTLCForwardInfo::AddHTLC {
@@ -1152,7 +1152,7 @@ fn test_phantom_final_incorrect_cltv_expiry() {
11521152
commitment_signed_dance!(nodes[1], nodes[0], &update_0.commitment_signed, false, true);
11531153

11541154
// Modify the payload so the phantom hop's HMAC is bogus.
1155-
for (_, pending_forwards) in nodes[1].node.channel_state.lock().unwrap().forward_htlcs.iter_mut() {
1155+
for (_, pending_forwards) in nodes[1].node.forward_htlcs.lock().unwrap().iter_mut() {
11561156
for f in pending_forwards.iter_mut() {
11571157
match f {
11581158
&mut HTLCForwardInfo::AddHTLC {

0 commit comments

Comments
 (0)