Skip to content

Commit 74aae2e

Browse files
Remove pending_inbound_payments map from ChannelManager
LDK versions prior to 0.0.104 had stateful inbound payments written in this map. In 0.0.104, we added support for stateless inbound payments with deterministically generated payment secrets, and maintained support for stateful inbound payments until 0.0.116. After 0.0.116, no further inbound payments could have been written into this map.
1 parent 206ab82 commit 74aae2e

File tree

1 file changed

+54
-106
lines changed

1 file changed

+54
-106
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 54 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,25 +2166,23 @@ where
21662166
// |
21672167
// |__`per_peer_state`
21682168
// |
2169-
// |__`pending_inbound_payments`
2170-
// |
2171-
// |__`claimable_payments`
2172-
// |
2173-
// |__`pending_outbound_payments` // This field's struct contains a map of pending outbounds
2174-
// |
2175-
// |__`peer_state`
2176-
// |
2177-
// |__`outpoint_to_peer`
2178-
// |
2179-
// |__`short_to_chan_info`
2180-
// |
2181-
// |__`outbound_scid_aliases`
2182-
// |
2183-
// |__`best_block`
2184-
// |
2185-
// |__`pending_events`
2186-
// |
2187-
// |__`pending_background_events`
2169+
// |__`claimable_payments`
2170+
// |
2171+
// |__`pending_outbound_payments` // This field's struct contains a map of pending outbounds
2172+
// |
2173+
// |__`peer_state`
2174+
// |
2175+
// |__`outpoint_to_peer`
2176+
// |
2177+
// |__`short_to_chan_info`
2178+
// |
2179+
// |__`outbound_scid_aliases`
2180+
// |
2181+
// |__`best_block`
2182+
// |
2183+
// |__`pending_events`
2184+
// |
2185+
// |__`pending_background_events`
21882186
//
21892187
pub struct ChannelManager<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, MR: Deref, L: Deref>
21902188
where
@@ -2214,14 +2212,6 @@ where
22142212
best_block: RwLock<BestBlock>,
22152213
secp_ctx: Secp256k1<secp256k1::All>,
22162214

2217-
/// Storage for PaymentSecrets and any requirements on future inbound payments before we will
2218-
/// expose them to users via a PaymentClaimable event. HTLCs which do not meet the requirements
2219-
/// here are failed when we process them as pending-forwardable-HTLCs, and entries are removed
2220-
/// after we generate a PaymentClaimable upon receipt of all MPP parts or when they time out.
2221-
///
2222-
/// See `ChannelManager` struct-level documentation for lock order requirements.
2223-
pending_inbound_payments: Mutex<HashMap<PaymentHash, PendingInboundPayment>>,
2224-
22252215
/// The session_priv bytes and retry metadata of outbound payments which are pending resolution.
22262216
/// The authoritative state of these HTLCs resides either within Channels or ChannelMonitors
22272217
/// (if the channel has been force-closed), however we track them here to prevent duplicative
@@ -3217,7 +3207,6 @@ where
32173207
best_block: RwLock::new(params.best_block),
32183208

32193209
outbound_scid_aliases: Mutex::new(new_hash_set()),
3220-
pending_inbound_payments: Mutex::new(new_hash_map()),
32213210
pending_outbound_payments: OutboundPayments::new(new_hash_map()),
32223211
forward_htlcs: Mutex::new(new_hash_map()),
32233212
decode_update_add_htlcs: Mutex::new(new_hash_map()),
@@ -5896,66 +5885,36 @@ where
58965885
// that we are the ultimate recipient of the given payment hash.
58975886
// Further, we must not expose whether we have any other HTLCs
58985887
// associated with the same payment_hash pending or not.
5899-
let mut payment_secrets = self.pending_inbound_payments.lock().unwrap();
5900-
match payment_secrets.entry(payment_hash) {
5901-
hash_map::Entry::Vacant(_) => {
5902-
match claimable_htlc.onion_payload {
5903-
OnionPayload::Invoice { .. } => {
5904-
let payment_data = payment_data.unwrap();
5905-
let (payment_preimage, min_final_cltv_expiry_delta) = match inbound_payment::verify(payment_hash, &payment_data, self.highest_seen_timestamp.load(Ordering::Acquire) as u64, &self.inbound_payment_key, &self.logger) {
5906-
Ok(result) => result,
5907-
Err(()) => {
5908-
log_trace!(self.logger, "Failing new HTLC with payment_hash {} as payment verification failed", &payment_hash);
5909-
fail_htlc!(claimable_htlc, payment_hash);
5910-
}
5911-
};
5912-
if let Some(min_final_cltv_expiry_delta) = min_final_cltv_expiry_delta {
5913-
let expected_min_expiry_height = (self.current_best_block().height + min_final_cltv_expiry_delta as u32) as u64;
5914-
if (cltv_expiry as u64) < expected_min_expiry_height {
5915-
log_trace!(self.logger, "Failing new HTLC with payment_hash {} as its CLTV expiry was too soon (had {}, earliest expected {})",
5916-
&payment_hash, cltv_expiry, expected_min_expiry_height);
5917-
fail_htlc!(claimable_htlc, payment_hash);
5918-
}
5919-
}
5920-
let purpose = events::PaymentPurpose::from_parts(
5921-
payment_preimage,
5922-
payment_data.payment_secret,
5923-
payment_context,
5924-
);
5925-
check_total_value!(purpose);
5926-
},
5927-
OnionPayload::Spontaneous(preimage) => {
5928-
let purpose = events::PaymentPurpose::SpontaneousPayment(preimage);
5929-
check_total_value!(purpose);
5930-
}
5931-
}
5932-
},
5933-
hash_map::Entry::Occupied(inbound_payment) => {
5934-
if let OnionPayload::Spontaneous(_) = claimable_htlc.onion_payload {
5935-
log_trace!(self.logger, "Failing new keysend HTLC with payment_hash {} because we already have an inbound payment with the same payment hash", &payment_hash);
5936-
fail_htlc!(claimable_htlc, payment_hash);
5937-
}
5888+
match claimable_htlc.onion_payload {
5889+
OnionPayload::Invoice { .. } => {
59385890
let payment_data = payment_data.unwrap();
5939-
if inbound_payment.get().payment_secret != payment_data.payment_secret {
5940-
log_trace!(self.logger, "Failing new HTLC with payment_hash {} as it didn't match our expected payment secret.", &payment_hash);
5941-
fail_htlc!(claimable_htlc, payment_hash);
5942-
} else if inbound_payment.get().min_value_msat.is_some() && payment_data.total_msat < inbound_payment.get().min_value_msat.unwrap() {
5943-
log_trace!(self.logger, "Failing new HTLC with payment_hash {} as it didn't match our minimum value (had {}, needed {}).",
5944-
&payment_hash, payment_data.total_msat, inbound_payment.get().min_value_msat.unwrap());
5945-
fail_htlc!(claimable_htlc, payment_hash);
5946-
} else {
5947-
let purpose = events::PaymentPurpose::from_parts(
5948-
inbound_payment.get().payment_preimage,
5949-
payment_data.payment_secret,
5950-
payment_context,
5951-
);
5952-
let payment_claimable_generated = check_total_value!(purpose);
5953-
if payment_claimable_generated {
5954-
inbound_payment.remove_entry();
5891+
let (payment_preimage, min_final_cltv_expiry_delta) = match inbound_payment::verify(payment_hash, &payment_data, self.highest_seen_timestamp.load(Ordering::Acquire) as u64, &self.inbound_payment_key, &self.logger) {
5892+
Ok(result) => result,
5893+
Err(()) => {
5894+
log_trace!(self.logger, "Failing new HTLC with payment_hash {} as payment verification failed", &payment_hash);
5895+
fail_htlc!(claimable_htlc, payment_hash);
5896+
}
5897+
};
5898+
if let Some(min_final_cltv_expiry_delta) = min_final_cltv_expiry_delta {
5899+
let expected_min_expiry_height = (self.current_best_block().height + min_final_cltv_expiry_delta as u32) as u64;
5900+
if (cltv_expiry as u64) < expected_min_expiry_height {
5901+
log_trace!(self.logger, "Failing new HTLC with payment_hash {} as its CLTV expiry was too soon (had {}, earliest expected {})",
5902+
&payment_hash, cltv_expiry, expected_min_expiry_height);
5903+
fail_htlc!(claimable_htlc, payment_hash);
59555904
}
59565905
}
5906+
let purpose = events::PaymentPurpose::from_parts(
5907+
payment_preimage,
5908+
payment_data.payment_secret,
5909+
payment_context,
5910+
);
5911+
check_total_value!(purpose);
59575912
},
5958-
};
5913+
OnionPayload::Spontaneous(preimage) => {
5914+
let purpose = events::PaymentPurpose::SpontaneousPayment(preimage);
5915+
check_total_value!(purpose);
5916+
}
5917+
}
59595918
},
59605919
HTLCForwardInfo::FailHTLC { .. } | HTLCForwardInfo::FailMalformedHTLC { .. } => {
59615920
panic!("Got pending fail of our own HTLC");
@@ -10121,10 +10080,6 @@ where
1012110080
}
1012210081
}
1012310082
max_time!(self.highest_seen_timestamp);
10124-
let mut payment_secrets = self.pending_inbound_payments.lock().unwrap();
10125-
payment_secrets.retain(|_, inbound_payment| {
10126-
inbound_payment.expiry_time > header.time as u64
10127-
});
1012810083
}
1012910084

1013010085
fn get_relevant_txids(&self) -> Vec<(Txid, u32, Option<BlockHash>)> {
@@ -11873,7 +11828,6 @@ where
1187311828
decode_update_add_htlcs_opt = Some(decode_update_add_htlcs);
1187411829
}
1187511830

11876-
let pending_inbound_payments = self.pending_inbound_payments.lock().unwrap();
1187711831
let claimable_payments = self.claimable_payments.lock().unwrap();
1187811832
let pending_outbound_payments = self.pending_outbound_payments.pending_outbound_payments.lock().unwrap();
1187911833

@@ -11945,11 +11899,10 @@ where
1194511899
(self.highest_seen_timestamp.load(Ordering::Acquire) as u32).write(writer)?;
1194611900
(self.highest_seen_timestamp.load(Ordering::Acquire) as u32).write(writer)?;
1194711901

11948-
(pending_inbound_payments.len() as u64).write(writer)?;
11949-
for (hash, pending_payment) in pending_inbound_payments.iter() {
11950-
hash.write(writer)?;
11951-
pending_payment.write(writer)?;
11952-
}
11902+
// LDK versions prior to 0.0.104 wrote `pending_inbound_payments` here, with deprecated support
11903+
// for stateful inbound payments maintained until 0.0.116, after which no further inbound
11904+
// payments could have been written here.
11905+
(0 as u64).write(writer)?;
1195311906

1195411907
// For backwards compat, write the session privs and their total length.
1195511908
let mut num_pending_outbounds_compat: u64 = 0;
@@ -12463,13 +12416,9 @@ where
1246312416
let _last_node_announcement_serial: u32 = Readable::read(reader)?; // Only used < 0.0.111
1246412417
let highest_seen_timestamp: u32 = Readable::read(reader)?;
1246512418

12419+
// The last version where a pending inbound payment may have been written was 0.0.116.
1246612420
let pending_inbound_payment_count: u64 = Readable::read(reader)?;
12467-
let mut pending_inbound_payments: HashMap<PaymentHash, PendingInboundPayment> = hash_map_with_capacity(cmp::min(pending_inbound_payment_count as usize, MAX_ALLOC_SIZE/(3*32)));
12468-
for _ in 0..pending_inbound_payment_count {
12469-
if pending_inbound_payments.insert(Readable::read(reader)?, Readable::read(reader)?).is_some() {
12470-
return Err(DecodeError::InvalidValue);
12471-
}
12472-
}
12421+
if pending_inbound_payment_count != 0 { return Err(DecodeError::InvalidValue) }
1247312422

1247412423
let pending_outbound_payments_count_compat: u64 = Readable::read(reader)?;
1247512424
let mut pending_outbound_payments_compat: HashMap<PaymentId, PendingOutboundPayment> =
@@ -12855,16 +12804,16 @@ where
1285512804
OnionPayload::Invoice { _legacy_hop_data } => {
1285612805
if let Some(hop_data) = _legacy_hop_data {
1285712806
events::PaymentPurpose::Bolt11InvoicePayment {
12858-
payment_preimage: match pending_inbound_payments.get(&payment_hash) {
12859-
Some(inbound_payment) => inbound_payment.payment_preimage,
12860-
None => match inbound_payment::verify(payment_hash, &hop_data, 0, &expanded_inbound_key, &args.logger) {
12807+
payment_preimage:
12808+
match inbound_payment::verify(
12809+
payment_hash, &hop_data, 0, &expanded_inbound_key, &args.logger
12810+
) {
1286112811
Ok((payment_preimage, _)) => payment_preimage,
1286212812
Err(()) => {
1286312813
log_error!(args.logger, "Failed to read claimable payment data for HTLC with payment hash {} - was not a pending inbound payment and didn't match our payment key", &payment_hash);
1286412814
return Err(DecodeError::InvalidValue);
1286512815
}
12866-
}
12867-
},
12816+
},
1286812817
payment_secret: hop_data.payment_secret,
1286912818
}
1287012819
} else { return Err(DecodeError::InvalidValue); }
@@ -13043,7 +12992,6 @@ where
1304312992
best_block: RwLock::new(BestBlock::new(best_block_hash, best_block_height)),
1304412993

1304512994
inbound_payment_key: expanded_inbound_key,
13046-
pending_inbound_payments: Mutex::new(pending_inbound_payments),
1304712995
pending_outbound_payments: pending_outbounds,
1304812996
pending_intercepted_htlcs: Mutex::new(pending_intercepted_htlcs.unwrap()),
1304912997

0 commit comments

Comments
 (0)