@@ -58,6 +58,7 @@ use crate::events::{
58
58
use crate::events::{FundingInfo, PaidBolt12Invoice};
59
59
// Since this struct is returned in `list_channels` methods, expose it here in case users want to
60
60
// construct one themselves.
61
+ use crate::io;
61
62
use crate::ln::channel::PendingV2Channel;
62
63
use crate::ln::channel::{
63
64
self, Channel, ChannelError, ChannelUpdateStatus, FundedChannel, InboundV1Channel,
@@ -78,7 +79,7 @@ use crate::ln::onion_payment::{
78
79
};
79
80
use crate::ln::onion_utils::{self};
80
81
use crate::ln::onion_utils::{HTLCFailReason, LocalHTLCFailureReason};
81
- use crate::ln::our_peer_storage::EncryptedOurPeerStorage;
82
+ use crate::ln::our_peer_storage::{ EncryptedOurPeerStorage, PeerStorageMonitorHolderList} ;
82
83
#[cfg(test)]
83
84
use crate::ln::outbound_payment;
84
85
use crate::ln::outbound_payment::{
@@ -174,7 +175,6 @@ use lightning_invoice::{
174
175
175
176
use alloc::collections::{btree_map, BTreeMap};
176
177
177
- use crate::io;
178
178
use crate::io::Read;
179
179
use crate::prelude::*;
180
180
use crate::sync::{Arc, FairRwLock, LockHeldState, LockTestExt, Mutex, RwLock, RwLockReadGuard};
@@ -3014,6 +3014,7 @@ pub(super) const MAX_UNFUNDED_CHANNEL_PEERS: usize = 50;
3014
3014
/// This constant defines the upper limit for the size of data
3015
3015
/// that can be stored for a peer. It is set to 1024 bytes (1 kilobyte)
3016
3016
/// to prevent excessive resource consumption.
3017
+ #[cfg(not(test))]
3017
3018
const MAX_PEER_STORAGE_SIZE: usize = 1024;
3018
3019
3019
3020
/// The maximum number of peers which we do not have a (funded) channel with. Once we reach this
@@ -8807,6 +8808,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
8807
8808
&self, peer_node_id: PublicKey, msg: msgs::PeerStorageRetrieval,
8808
8809
) -> Result<(), MsgHandleErrInternal> {
8809
8810
// TODO: Check if have any stale or missing ChannelMonitor.
8811
+ let per_peer_state = self.per_peer_state.read().unwrap();
8810
8812
let logger = WithContext::from(&self.logger, Some(peer_node_id), None, None);
8811
8813
let err = || {
8812
8814
MsgHandleErrInternal::from_chan_no_close(
@@ -8833,6 +8835,55 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
8833
8835
8834
8836
log_trace!(logger, "Got valid {}-byte peer backup from {}", decrypted.len(), peer_node_id);
8835
8837
8838
+ let mut cursor = io::Cursor::new(decrypted);
8839
+ match <PeerStorageMonitorHolderList as Readable>::read(&mut cursor) {
8840
+ Ok(mon_list) => {
8841
+ for mon_holder in mon_list.monitors.iter() {
8842
+ let peer_state_mutex =
8843
+ match per_peer_state.get(&mon_holder.counterparty_node_id) {
8844
+ Some(mutex) => mutex,
8845
+ None => {
8846
+ log_debug!(
8847
+ logger,
8848
+ "Not able to find peer_state for the counterparty {}, channelId {}",
8849
+ log_pubkey!(mon_holder.counterparty_node_id),
8850
+ mon_holder.channel_id
8851
+ );
8852
+ continue;
8853
+ },
8854
+ };
8855
+
8856
+ let peer_state_lock = peer_state_mutex.lock().unwrap();
8857
+ let peer_state = &*peer_state_lock;
8858
+
8859
+ match peer_state.channel_by_id.get(&mon_holder.channel_id) {
8860
+ Some(chan) => {
8861
+ if let Some(funded_chan) = chan.as_funded() {
8862
+ if funded_chan
8863
+ .get_revoked_counterparty_commitment_transaction_number()
8864
+ > mon_holder.min_seen_secret
8865
+ {
8866
+ panic!(
8867
+ "Lost channel state for channel {}.
8868
+ Received peer storage with a more recent state than what our node had.
8869
+ Use the FundRecoverer to initiate a force close and sweep the funds.",
8870
+ &mon_holder.channel_id
8871
+ );
8872
+ }
8873
+ }
8874
+ },
8875
+ None => {
8876
+ // TODO: Figure out if this channel is so old that we have forgotten about it.
8877
+ panic!("Lost a channel {}", &mon_holder.channel_id);
8878
+ },
8879
+ }
8880
+ }
8881
+ },
8882
+
8883
+ Err(e) => {
8884
+ panic!("Wrong serialisation of PeerStorageMonitorHolderList: {}", e);
8885
+ },
8886
+ }
8836
8887
Ok(())
8837
8888
}
8838
8889
@@ -8858,6 +8909,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
8858
8909
), ChannelId([0; 32])));
8859
8910
}
8860
8911
8912
+ #[cfg(not(test))]
8861
8913
if msg.data.len() > MAX_PEER_STORAGE_SIZE {
8862
8914
log_debug!(logger, "Sending warning to peer and ignoring peer storage request from {} as its over 1KiB", log_pubkey!(counterparty_node_id));
8863
8915
0 commit comments