@@ -81,7 +81,7 @@ use crate::ln::onion_utils::{
8181 decode_fulfill_attribution_data, HTLCFailReason, LocalHTLCFailureReason,
8282};
8383use crate::ln::onion_utils::{process_fulfill_attribution_data, AttributionData};
84- use crate::ln::our_peer_storage::EncryptedOurPeerStorage;
84+ use crate::ln::our_peer_storage::{ EncryptedOurPeerStorage, PeerStorageMonitorHolder} ;
8585#[cfg(test)]
8686use crate::ln::outbound_payment;
8787use crate::ln::outbound_payment::{
@@ -2959,6 +2959,7 @@ pub(super) const MAX_UNFUNDED_CHANNEL_PEERS: usize = 50;
29592959/// This constant defines the upper limit for the size of data
29602960/// that can be stored for a peer. It is set to 1024 bytes (1 kilobyte)
29612961/// to prevent excessive resource consumption.
2962+ #[cfg(not(test))]
29622963const MAX_PEER_STORAGE_SIZE: usize = 1024;
29632964
29642965/// The maximum number of peers which we do not have a (funded) channel with. Once we reach this
@@ -9332,7 +9333,53 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
93329333 };
93339334
93349335 log_trace!(logger, "Got valid {}-byte peer backup from {}", decrypted.len(), peer_node_id);
9336+ let per_peer_state = self.per_peer_state.read().unwrap();
9337+
9338+ let mut cursor = io::Cursor::new(decrypted);
9339+ let mon_list = <Vec<PeerStorageMonitorHolder> as Readable>::read(&mut cursor)
9340+ .unwrap_or_else(|e| {
9341+ // This should NEVER happen.
9342+ log_debug!(self.logger, "Unable to unpack the retrieved peer storage {:?}", e);
9343+ Vec::new()
9344+ });
9345+
9346+ for mon_holder in mon_list.iter() {
9347+ let peer_state_mutex = match per_peer_state.get(&mon_holder.counterparty_node_id) {
9348+ Some(mutex) => mutex,
9349+ None => {
9350+ log_debug!(
9351+ logger,
9352+ "Not able to find peer_state for the counterparty {}, channelId {}",
9353+ log_pubkey!(mon_holder.counterparty_node_id),
9354+ mon_holder.channel_id
9355+ );
9356+ continue;
9357+ },
9358+ };
93359359
9360+ let peer_state_lock = peer_state_mutex.lock().unwrap();
9361+ let peer_state = &*peer_state_lock;
9362+
9363+ match peer_state.channel_by_id.get(&mon_holder.channel_id) {
9364+ Some(chan) => {
9365+ if let Some(funded_chan) = chan.as_funded() {
9366+ if funded_chan.get_revoked_counterparty_commitment_transaction_number()
9367+ > mon_holder.min_seen_secret
9368+ {
9369+ panic!(
9370+ "Lost channel state for channel {}.\n\
9371+ Received peer storage with a more recent state than what our node had.\n\
9372+ Use the FundRecoverer to initiate a force close and sweep the funds.",
9373+ &mon_holder.channel_id
9374+ );
9375+ }
9376+ }
9377+ },
9378+ None => {
9379+ log_debug!(logger, "Found an unknown channel {}", &mon_holder.channel_id);
9380+ },
9381+ }
9382+ }
93369383 Ok(())
93379384 }
93389385
@@ -9358,6 +9405,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
93589405 ), ChannelId([0; 32])));
93599406 }
93609407
9408+ #[cfg(not(test))]
93619409 if msg.data.len() > MAX_PEER_STORAGE_SIZE {
93629410 log_debug!(logger, "Sending warning to peer and ignoring peer storage request from {} as its over 1KiB", log_pubkey!(counterparty_node_id));
93639411
0 commit comments