@@ -81,7 +81,7 @@ use crate::ln::onion_utils::{
81
81
decode_fulfill_attribution_data, HTLCFailReason, LocalHTLCFailureReason,
82
82
};
83
83
use 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} ;
85
85
#[cfg(test)]
86
86
use crate::ln::outbound_payment;
87
87
use crate::ln::outbound_payment::{
@@ -2959,6 +2959,7 @@ pub(super) const MAX_UNFUNDED_CHANNEL_PEERS: usize = 50;
2959
2959
/// This constant defines the upper limit for the size of data
2960
2960
/// that can be stored for a peer. It is set to 1024 bytes (1 kilobyte)
2961
2961
/// to prevent excessive resource consumption.
2962
+ #[cfg(not(test))]
2962
2963
const MAX_PEER_STORAGE_SIZE: usize = 1024;
2963
2964
2964
2965
/// The maximum number of peers which we do not have a (funded) channel with. Once we reach this
@@ -9332,7 +9333,54 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
9332
9333
};
9333
9334
9334
9335
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).unwrap_or_else(|e| {
9340
+ // This should NEVER happen.
9341
+ log_debug!(self.logger, "Unable to unpack the retrieved peer storage {:?}", e);
9342
+ Vec::new()
9343
+ });
9344
+
9345
+ for mon_holder in mon_list.iter() {
9346
+ let peer_state_mutex =
9347
+ 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
+ };
9335
9359
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
9367
+ .get_revoked_counterparty_commitment_transaction_number()
9368
+ > mon_holder.min_seen_secret
9369
+ {
9370
+ panic!(
9371
+ "Lost channel state for channel {}.\n\
9372
+ Received peer storage with a more recent state than what our node had.\n\
9373
+ Use the FundRecoverer to initiate a force close and sweep the funds.",
9374
+ &mon_holder.channel_id
9375
+ );
9376
+ }
9377
+ }
9378
+ },
9379
+ None => {
9380
+ log_debug!(logger, "Found an unknown channel {}", &mon_holder.channel_id);
9381
+ },
9382
+ }
9383
+ }
9336
9384
Ok(())
9337
9385
}
9338
9386
@@ -9358,6 +9406,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
9358
9406
), ChannelId([0; 32])));
9359
9407
}
9360
9408
9409
+ #[cfg(not(test))]
9361
9410
if msg.data.len() > MAX_PEER_STORAGE_SIZE {
9362
9411
log_debug!(logger, "Sending warning to peer and ignoring peer storage request from {} as its over 1KiB", log_pubkey!(counterparty_node_id));
9363
9412
0 commit comments