Skip to content

Commit 2f04e81

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 d024251 commit 2f04e81

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

lightning/src/ln/channelmanager.rs

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

717+
/// SCID/SCID Alias -> forward infos. Key of 0 means payments received.
718+
///
719+
/// Note that because we may have an SCID Alias as the key we can have two entries per channel,
720+
/// though in practice we probably won't be receiving HTLCs for a channel both via the alias
721+
/// and via the classic SCID.
722+
///
723+
/// Note that no consistency guarantees are made about the existence of a channel with the
724+
/// `short_channel_id` here, nor the `short_channel_id` in the `PendingHTLCInfo`!
725+
pub(super) forward_htlcs: Mutex<HashMap<u64, Vec<HTLCForwardInfo>>>,
726+
727727
/// The set of outbound SCID aliases across all our channels, including unconfirmed channels
728728
/// and some closed channels which reached a usable state prior to being closed. This is used
729729
/// only to avoid duplicates, and is not persisted explicitly to disk, but rebuilt from the
@@ -1601,13 +1601,13 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
16011601
channel_state: Mutex::new(ChannelHolder{
16021602
by_id: HashMap::new(),
16031603
short_to_chan_info: HashMap::new(),
1604-
forward_htlcs: HashMap::new(),
16051604
claimable_htlcs: HashMap::new(),
16061605
pending_msg_events: Vec::new(),
16071606
}),
16081607
outbound_scid_aliases: Mutex::new(HashSet::new()),
16091608
pending_inbound_payments: Mutex::new(HashMap::new()),
16101609
pending_outbound_payments: Mutex::new(HashMap::new()),
1610+
forward_htlcs: Mutex::new(HashMap::new()),
16111611
id_to_peer: Mutex::new(HashMap::new()),
16121612

16131613
our_network_key: keys_manager.get_node_secret(Recipient::Node).unwrap(),
@@ -3095,7 +3095,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
30953095
let mut channel_state_lock = self.channel_state.lock().unwrap();
30963096
let channel_state = &mut *channel_state_lock;
30973097

3098-
for (short_chan_id, mut pending_forwards) in channel_state.forward_htlcs.drain() {
3098+
for (short_chan_id, mut pending_forwards) in self.forward_htlcs.lock().unwrap().drain() {
30993099
if short_chan_id != 0 {
31003100
let forward_chan_id = match channel_state.short_to_chan_info.get(&short_chan_id) {
31013101
Some((_cp_id, chan_id)) => chan_id.clone(),
@@ -4029,17 +4029,19 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
40294029
};
40304030

40314031
let mut forward_event = None;
4032-
if channel_state_lock.forward_htlcs.is_empty() {
4032+
let mut forward_htlcs = self.forward_htlcs.lock().unwrap();
4033+
if forward_htlcs.is_empty() {
40334034
forward_event = Some(Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS));
40344035
}
4035-
match channel_state_lock.forward_htlcs.entry(short_channel_id) {
4036+
match forward_htlcs.entry(short_channel_id) {
40364037
hash_map::Entry::Occupied(mut entry) => {
40374038
entry.get_mut().push(HTLCForwardInfo::FailHTLC { htlc_id, err_packet });
40384039
},
40394040
hash_map::Entry::Vacant(entry) => {
40404041
entry.insert(vec!(HTLCForwardInfo::FailHTLC { htlc_id, err_packet }));
40414042
}
40424043
}
4044+
mem::drop(forward_htlcs);
40434045
mem::drop(channel_state_lock);
40444046
let mut pending_events = self.pending_events.lock().unwrap();
40454047
if let Some(time) = forward_event {
@@ -4987,11 +4989,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
49874989
let mut forward_event = None;
49884990
if !pending_forwards.is_empty() {
49894991
let mut channel_state = self.channel_state.lock().unwrap();
4990-
if channel_state.forward_htlcs.is_empty() {
4992+
let mut forward_htlcs = self.forward_htlcs.lock().unwrap();
4993+
if forward_htlcs.is_empty() {
49914994
forward_event = Some(Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS))
49924995
}
49934996
for (forward_info, prev_htlc_id) in pending_forwards.drain(..) {
4994-
match channel_state.forward_htlcs.entry(match forward_info.routing {
4997+
match forward_htlcs.entry(match forward_info.routing {
49954998
PendingHTLCRouting::Forward { short_channel_id, .. } => short_channel_id,
49964999
PendingHTLCRouting::Receive { .. } => 0,
49975000
PendingHTLCRouting::ReceiveKeysend { .. } => 0,
@@ -6680,8 +6683,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Writeable f
66806683
}
66816684
}
66826685

6683-
(channel_state.forward_htlcs.len() as u64).write(writer)?;
6684-
for (short_channel_id, pending_forwards) in channel_state.forward_htlcs.iter() {
6686+
let forward_htlcs = self.forward_htlcs.lock().unwrap();
6687+
(forward_htlcs.len() as u64).write(writer)?;
6688+
for (short_channel_id, pending_forwards) in forward_htlcs.iter() {
66856689
short_channel_id.write(writer)?;
66866690
(pending_forwards.len() as u64).write(writer)?;
66876691
for forward in pending_forwards {
@@ -7290,14 +7294,14 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
72907294
channel_state: Mutex::new(ChannelHolder {
72917295
by_id,
72927296
short_to_chan_info,
7293-
forward_htlcs,
72947297
claimable_htlcs,
72957298
pending_msg_events: Vec::new(),
72967299
}),
72977300
inbound_payment_key: expanded_inbound_key,
72987301
pending_inbound_payments: Mutex::new(pending_inbound_payments),
72997302
pending_outbound_payments: Mutex::new(pending_outbound_payments.unwrap()),
73007303

7304+
forward_htlcs: Mutex::new(forward_htlcs),
73017305
outbound_scid_aliases: Mutex::new(outbound_scid_aliases),
73027306
id_to_peer: Mutex::new(id_to_peer),
73037307
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, .. } =>
@@ -1019,8 +1019,8 @@ fn test_phantom_onion_hmac_failure() {
10191019

10201020
// Modify the payload so the phantom hop's HMAC is bogus.
10211021
let sha256_of_onion = {
1022-
let mut channel_state = nodes[1].node.channel_state.lock().unwrap();
1023-
let mut pending_forward = channel_state.forward_htlcs.get_mut(&phantom_scid).unwrap();
1022+
let mut forward_htlcs = nodes[1].node.forward_htlcs.lock().unwrap();
1023+
let mut pending_forward = forward_htlcs.get_mut(&phantom_scid).unwrap();
10241024
match pending_forward[0] {
10251025
HTLCForwardInfo::AddHTLC {
10261026
forward_info: PendingHTLCInfo {
@@ -1079,7 +1079,7 @@ fn test_phantom_invalid_onion_payload() {
10791079
commitment_signed_dance!(nodes[1], nodes[0], &update_0.commitment_signed, false, true);
10801080

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

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

0 commit comments

Comments
 (0)