@@ -83,7 +83,7 @@ use crate::ln::onion_utils::{
83
83
decode_fulfill_attribution_data, HTLCFailReason, LocalHTLCFailureReason,
84
84
};
85
85
use 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} ;
87
87
#[cfg(test)]
88
88
use crate::ln::outbound_payment;
89
89
use crate::ln::outbound_payment::{
@@ -2974,6 +2974,7 @@ pub(super) const MAX_UNFUNDED_CHANNEL_PEERS: usize = 50;
2974
2974
/// This constant defines the upper limit for the size of data
2975
2975
/// that can be stored for a peer. It is set to 1024 bytes (1 kilobyte)
2976
2976
/// to prevent excessive resource consumption.
2977
+ #[cfg(not(test))]
2977
2978
const MAX_PEER_STORAGE_SIZE: usize = 1024;
2978
2979
2979
2980
/// 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/
9706
9707
};
9707
9708
9708
9709
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
+ };
9709
9734
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
+ }
9710
9758
Ok(())
9711
9759
}
9712
9760
@@ -9732,6 +9780,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
9732
9780
), ChannelId([0; 32])));
9733
9781
}
9734
9782
9783
+ #[cfg(not(test))]
9735
9784
if msg.data.len() > MAX_PEER_STORAGE_SIZE {
9736
9785
log_debug!(logger, "Sending warning to peer and ignoring peer storage request from {} as its over 1KiB", log_pubkey!(counterparty_node_id));
9737
9786
0 commit comments