Skip to content

Commit 129e1f6

Browse files
Persist pending intercepted htlcs in ChannelManager
No htlcs are intercepted yet, that will be added in upcoming commit(s) Co-authored-by: John Cantrell <[email protected]> Co-authored-by: Valentine Wallace <[email protected]>
1 parent 440c3ee commit 129e1f6

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,24 @@ impl Readable for PaymentId {
207207
Ok(PaymentId(buf))
208208
}
209209
}
210+
211+
/// An identifier used to uniquely identify an intercepted HTLC to LDK.
212+
/// (C-not exported) as we just use [u8; 32] directly
213+
#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
214+
pub struct InterceptId(pub [u8; 32]);
215+
216+
impl Writeable for InterceptId {
217+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
218+
self.0.write(w)
219+
}
220+
}
221+
222+
impl Readable for InterceptId {
223+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
224+
let buf: [u8; 32] = Readable::read(r)?;
225+
Ok(InterceptId(buf))
226+
}
227+
}
210228
/// Tracks the inbound corresponding to an outbound HTLC
211229
#[allow(clippy::derive_hash_xor_eq)] // Our Hash is faithful to the data, we just don't have SecretKey::hash
212230
#[derive(Clone, PartialEq, Eq)]
@@ -751,6 +769,11 @@ pub struct ChannelManager<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
751769
pub(super) forward_htlcs: Mutex<HashMap<u64, Vec<HTLCForwardInfo>>>,
752770
#[cfg(not(test))]
753771
forward_htlcs: Mutex<HashMap<u64, Vec<HTLCForwardInfo>>>,
772+
/// Storage for HTLCs that have been intercepted and bubbled up to the user. We hold them here
773+
/// until the user tells us what we should do with them.
774+
///
775+
/// See `ChannelManager` struct-level documentation for lock order requirements.
776+
pending_intercepted_htlcs: Mutex<HashMap<InterceptId, PendingAddHTLCInfo>>,
754777

755778
/// Map from payment hash to the payment data and any HTLCs which are to us and can be
756779
/// failed/claimed by the user.
@@ -1566,6 +1589,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
15661589
pending_outbound_payments: Mutex::new(HashMap::new()),
15671590
forward_htlcs: Mutex::new(HashMap::new()),
15681591
claimable_htlcs: Mutex::new(HashMap::new()),
1592+
pending_intercepted_htlcs: Mutex::new(HashMap::new()),
15691593
id_to_peer: Mutex::new(HashMap::new()),
15701594
short_to_chan_info: FairRwLock::new(HashMap::new()),
15711595

@@ -6991,8 +7015,15 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Writeable for ChannelMana
69917015
_ => {},
69927016
}
69937017
}
7018+
7019+
let mut pending_intercepted_htlcs = None;
7020+
let our_pending_intercepts = self.pending_intercepted_htlcs.lock().unwrap();
7021+
if our_pending_intercepts.len() != 0 {
7022+
pending_intercepted_htlcs = Some(our_pending_intercepts);
7023+
}
69947024
write_tlv_fields!(writer, {
69957025
(1, pending_outbound_payments_no_retry, required),
7026+
(2, pending_intercepted_htlcs, option),
69967027
(3, pending_outbound_payments, required),
69977028
(5, self.our_network_pubkey, required),
69987029
(7, self.fake_scid_rand_bytes, required),
@@ -7306,12 +7337,14 @@ impl<'a, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
73067337
// pending_outbound_payments_no_retry is for compatibility with 0.0.101 clients.
73077338
let mut pending_outbound_payments_no_retry: Option<HashMap<PaymentId, HashSet<[u8; 32]>>> = None;
73087339
let mut pending_outbound_payments = None;
7340+
let mut pending_intercepted_htlcs: Option<HashMap<InterceptId, PendingAddHTLCInfo>> = Some(HashMap::new());
73097341
let mut received_network_pubkey: Option<PublicKey> = None;
73107342
let mut fake_scid_rand_bytes: Option<[u8; 32]> = None;
73117343
let mut probing_cookie_secret: Option<[u8; 32]> = None;
73127344
let mut claimable_htlc_purposes = None;
73137345
read_tlv_fields!(reader, {
73147346
(1, pending_outbound_payments_no_retry, option),
7347+
(2, pending_intercepted_htlcs, option),
73157348
(3, pending_outbound_payments, option),
73167349
(5, received_network_pubkey, option),
73177350
(7, fake_scid_rand_bytes, option),
@@ -7534,6 +7567,7 @@ impl<'a, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
75347567
inbound_payment_key: expanded_inbound_key,
75357568
pending_inbound_payments: Mutex::new(pending_inbound_payments),
75367569
pending_outbound_payments: Mutex::new(pending_outbound_payments.unwrap()),
7570+
pending_intercepted_htlcs: Mutex::new(pending_intercepted_htlcs.unwrap()),
75377571

75387572
forward_htlcs: Mutex::new(forward_htlcs),
75397573
claimable_htlcs: Mutex::new(claimable_htlcs),

0 commit comments

Comments
 (0)