@@ -83,7 +83,7 @@ use crate::ln::onion_utils::{
8383 decode_fulfill_attribution_data, HTLCFailReason, LocalHTLCFailureReason,
8484};
8585use crate::ln::onion_utils::{process_fulfill_attribution_data, AttributionData};
86- use crate::ln::our_peer_storage::EncryptedOurPeerStorage;
86+ use crate::ln::our_peer_storage::{ EncryptedOurPeerStorage, PeerStorageMonitorHolder} ;
8787#[cfg(test)]
8888use crate::ln::outbound_payment;
8989use crate::ln::outbound_payment::{
@@ -2974,6 +2974,7 @@ pub(super) const MAX_UNFUNDED_CHANNEL_PEERS: usize = 50;
29742974/// This constant defines the upper limit for the size of data
29752975/// that can be stored for a peer. It is set to 1024 bytes (1 kilobyte)
29762976/// to prevent excessive resource consumption.
2977+ #[cfg(not(test))]
29772978const MAX_PEER_STORAGE_SIZE: usize = 1024;
29782979
29792980/// The maximum number of peers which we do not have a (funded) channel with. Once we reach this
@@ -9706,7 +9707,54 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
97069707 };
97079708
97089709 log_trace!(logger, "Got valid {}-byte peer backup from {}", decrypted.len(), peer_node_id);
9710+ let per_peer_state = self.per_peer_state.read().unwrap();
9711+
9712+ let mut cursor = io::Cursor::new(decrypted);
9713+ let mon_list = <Vec<PeerStorageMonitorHolder> as Readable>::read(&mut cursor)
9714+ .unwrap_or_else(|e| {
9715+ // This should NEVER happen.
9716+ debug_assert!(false);
9717+ log_debug!(self.logger, "Unable to unpack the retrieved peer storage {:?}", e);
9718+ Vec::new()
9719+ });
9720+
9721+ for mon_holder in mon_list.iter() {
9722+ let peer_state_mutex = match per_peer_state.get(&mon_holder.counterparty_node_id) {
9723+ Some(mutex) => mutex,
9724+ None => {
9725+ log_debug!(
9726+ logger,
9727+ "Not able to find peer_state for the counterparty {}, channel_id {}",
9728+ log_pubkey!(mon_holder.counterparty_node_id),
9729+ mon_holder.channel_id
9730+ );
9731+ continue;
9732+ },
9733+ };
97099734
9735+ let peer_state_lock = peer_state_mutex.lock().unwrap();
9736+ let peer_state = &*peer_state_lock;
9737+
9738+ match peer_state.channel_by_id.get(&mon_holder.channel_id) {
9739+ Some(chan) => {
9740+ if let Some(funded_chan) = chan.as_funded() {
9741+ if funded_chan.get_revoked_counterparty_commitment_transaction_number()
9742+ > mon_holder.min_seen_secret
9743+ {
9744+ panic!(
9745+ "Lost channel state for channel {}.\n\
9746+ Received peer storage with a more recent state than what our node had.\n\
9747+ Use the FundRecoverer to initiate a force close and sweep the funds.",
9748+ &mon_holder.channel_id
9749+ );
9750+ }
9751+ }
9752+ },
9753+ None => {
9754+ log_debug!(logger, "Found an unknown channel {}", &mon_holder.channel_id);
9755+ },
9756+ }
9757+ }
97109758 Ok(())
97119759 }
97129760
@@ -9732,6 +9780,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
97329780 ), ChannelId([0; 32])));
97339781 }
97349782
9783+ #[cfg(not(test))]
97359784 if msg.data.len() > MAX_PEER_STORAGE_SIZE {
97369785 log_debug!(logger, "Sending warning to peer and ignoring peer storage request from {} as its over 1KiB", log_pubkey!(counterparty_node_id));
97379786
0 commit comments