Skip to content

Commit 02316d2

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 deprecated 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 02316d2

File tree

2 files changed

+64
-104
lines changed

2 files changed

+64
-104
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 58 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,7 @@ pub(super) const FEERATE_TRACKING_BLOCKS: usize = 144;
12381238
///
12391239
/// Note that this struct will be removed entirely soon, in favor of storing no inbound payment data
12401240
/// and instead encoding it in the payment secret.
1241+
#[derive(Debug)]
12411242
struct PendingInboundPayment {
12421243
/// The payment secret that the sender must use for us to accept this payment
12431244
payment_secret: PaymentSecret,
@@ -2166,25 +2167,23 @@ where
21662167
// |
21672168
// |__`per_peer_state`
21682169
// |
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`
2170+
// |__`claimable_payments`
2171+
// |
2172+
// |__`pending_outbound_payments` // This field's struct contains a map of pending outbounds
2173+
// |
2174+
// |__`peer_state`
2175+
// |
2176+
// |__`outpoint_to_peer`
2177+
// |
2178+
// |__`short_to_chan_info`
2179+
// |
2180+
// |__`outbound_scid_aliases`
2181+
// |
2182+
// |__`best_block`
2183+
// |
2184+
// |__`pending_events`
2185+
// |
2186+
// |__`pending_background_events`
21882187
//
21892188
pub struct ChannelManager<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, MR: Deref, L: Deref>
21902189
where
@@ -2214,14 +2213,6 @@ where
22142213
best_block: RwLock<BestBlock>,
22152214
secp_ctx: Secp256k1<secp256k1::All>,
22162215

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-
22252216
/// The session_priv bytes and retry metadata of outbound payments which are pending resolution.
22262217
/// The authoritative state of these HTLCs resides either within Channels or ChannelMonitors
22272218
/// (if the channel has been force-closed), however we track them here to prevent duplicative
@@ -3217,7 +3208,6 @@ where
32173208
best_block: RwLock::new(params.best_block),
32183209

32193210
outbound_scid_aliases: Mutex::new(new_hash_set()),
3220-
pending_inbound_payments: Mutex::new(new_hash_map()),
32213211
pending_outbound_payments: OutboundPayments::new(new_hash_map()),
32223212
forward_htlcs: Mutex::new(new_hash_map()),
32233213
decode_update_add_htlcs: Mutex::new(new_hash_map()),
@@ -5896,66 +5886,36 @@ where
58965886
// that we are the ultimate recipient of the given payment hash.
58975887
// Further, we must not expose whether we have any other HTLCs
58985888
// 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-
}
5889+
match claimable_htlc.onion_payload {
5890+
OnionPayload::Invoice { .. } => {
59385891
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();
5892+
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) {
5893+
Ok(result) => result,
5894+
Err(()) => {
5895+
log_trace!(self.logger, "Failing new HTLC with payment_hash {} as payment verification failed", &payment_hash);
5896+
fail_htlc!(claimable_htlc, payment_hash);
5897+
}
5898+
};
5899+
if let Some(min_final_cltv_expiry_delta) = min_final_cltv_expiry_delta {
5900+
let expected_min_expiry_height = (self.current_best_block().height + min_final_cltv_expiry_delta as u32) as u64;
5901+
if (cltv_expiry as u64) < expected_min_expiry_height {
5902+
log_trace!(self.logger, "Failing new HTLC with payment_hash {} as its CLTV expiry was too soon (had {}, earliest expected {})",
5903+
&payment_hash, cltv_expiry, expected_min_expiry_height);
5904+
fail_htlc!(claimable_htlc, payment_hash);
59555905
}
59565906
}
5907+
let purpose = events::PaymentPurpose::from_parts(
5908+
payment_preimage,
5909+
payment_data.payment_secret,
5910+
payment_context,
5911+
);
5912+
check_total_value!(purpose);
59575913
},
5958-
};
5914+
OnionPayload::Spontaneous(preimage) => {
5915+
let purpose = events::PaymentPurpose::SpontaneousPayment(preimage);
5916+
check_total_value!(purpose);
5917+
}
5918+
}
59595919
},
59605920
HTLCForwardInfo::FailHTLC { .. } | HTLCForwardInfo::FailMalformedHTLC { .. } => {
59615921
panic!("Got pending fail of our own HTLC");
@@ -10121,10 +10081,6 @@ where
1012110081
}
1012210082
}
1012310083
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-
});
1012810084
}
1012910085

1013010086
fn get_relevant_txids(&self) -> Vec<(Txid, u32, Option<BlockHash>)> {
@@ -11873,7 +11829,6 @@ where
1187311829
decode_update_add_htlcs_opt = Some(decode_update_add_htlcs);
1187411830
}
1187511831

11876-
let pending_inbound_payments = self.pending_inbound_payments.lock().unwrap();
1187711832
let claimable_payments = self.claimable_payments.lock().unwrap();
1187811833
let pending_outbound_payments = self.pending_outbound_payments.pending_outbound_payments.lock().unwrap();
1187911834

@@ -11945,11 +11900,10 @@ where
1194511900
(self.highest_seen_timestamp.load(Ordering::Acquire) as u32).write(writer)?;
1194611901
(self.highest_seen_timestamp.load(Ordering::Acquire) as u32).write(writer)?;
1194711902

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-
}
11903+
// LDK versions prior to 0.0.104 wrote `pending_inbound_payments` here, with deprecated support
11904+
// for stateful inbound payments maintained until 0.0.116, after which no further inbound
11905+
// payments could have been written here.
11906+
(0 as u64).write(writer)?;
1195311907

1195411908
// For backwards compat, write the session privs and their total length.
1195511909
let mut num_pending_outbounds_compat: u64 = 0;
@@ -12463,12 +12417,13 @@ where
1246312417
let _last_node_announcement_serial: u32 = Readable::read(reader)?; // Only used < 0.0.111
1246412418
let highest_seen_timestamp: u32 = Readable::read(reader)?;
1246512419

12420+
// The last version where a pending inbound payment may have been added was 0.0.116.
1246612421
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)));
1246812422
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-
}
12423+
let payment_hash: PaymentHash = Readable::read(reader)?;
12424+
let logger = WithContext::from(&args.logger, None, None, Some(payment_hash));
12425+
let inbound: PendingInboundPayment = Readable::read(reader)?;
12426+
log_warn!(logger, "Ignoring deprecated pending inbound payment with payment hash {}: {:?}", payment_hash, inbound);
1247212427
}
1247312428

1247412429
let pending_outbound_payments_count_compat: u64 = Readable::read(reader)?;
@@ -12855,16 +12810,16 @@ where
1285512810
OnionPayload::Invoice { _legacy_hop_data } => {
1285612811
if let Some(hop_data) = _legacy_hop_data {
1285712812
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) {
12813+
payment_preimage:
12814+
match inbound_payment::verify(
12815+
payment_hash, &hop_data, 0, &expanded_inbound_key, &args.logger
12816+
) {
1286112817
Ok((payment_preimage, _)) => payment_preimage,
1286212818
Err(()) => {
1286312819
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);
1286412820
return Err(DecodeError::InvalidValue);
1286512821
}
12866-
}
12867-
},
12822+
},
1286812823
payment_secret: hop_data.payment_secret,
1286912824
}
1287012825
} else { return Err(DecodeError::InvalidValue); }
@@ -13043,7 +12998,6 @@ where
1304312998
best_block: RwLock::new(BestBlock::new(best_block_hash, best_block_height)),
1304412999

1304513000
inbound_payment_key: expanded_inbound_key,
13046-
pending_inbound_payments: Mutex::new(pending_inbound_payments),
1304713001
pending_outbound_payments: pending_outbounds,
1304813002
pending_intercepted_htlcs: Mutex::new(pending_intercepted_htlcs.unwrap()),
1304913003

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Backwards Compatibility
2+
* Pending inbound payments added in versions 0.0.116 or earlier using the
3+
`create_inbound_payment{,_for_hash}_legacy` API will be ignored on `ChannelManager`
4+
deserialization and fail to be received
5+
6+

0 commit comments

Comments
 (0)