@@ -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};
@@ -3013,6 +3013,7 @@ pub(super) const MAX_UNFUNDED_CHANNEL_PEERS: usize = 50;
3013
3013
/// This constant defines the upper limit for the size of data
3014
3014
/// that can be stored for a peer. It is set to 1024 bytes (1 kilobyte)
3015
3015
/// to prevent excessive resource consumption.
3016
+ #[cfg(not(test))]
3016
3017
const MAX_PEER_STORAGE_SIZE: usize = 1024;
3017
3018
3018
3019
/// The maximum number of peers which we do not have a (funded) channel with. Once we reach this
@@ -9076,6 +9077,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
9076
9077
&self, peer_node_id: PublicKey, msg: msgs::PeerStorageRetrieval,
9077
9078
) -> Result<(), MsgHandleErrInternal> {
9078
9079
// TODO: Check if have any stale or missing ChannelMonitor.
9080
+ let per_peer_state = self.per_peer_state.read().unwrap();
9079
9081
let logger = WithContext::from(&self.logger, Some(peer_node_id), None, None);
9080
9082
let err = || {
9081
9083
MsgHandleErrInternal::from_chan_no_close(
@@ -9102,6 +9104,55 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
9102
9104
9103
9105
log_trace!(logger, "Got valid {}-byte peer backup from {}", decrypted.len(), peer_node_id);
9104
9106
9107
+ let mut cursor = io::Cursor::new(decrypted);
9108
+ match <PeerStorageMonitorHolderList as Readable>::read(&mut cursor) {
9109
+ Ok(mon_list) => {
9110
+ for mon_holder in mon_list.monitors.iter() {
9111
+ let peer_state_mutex =
9112
+ match per_peer_state.get(&mon_holder.counterparty_node_id) {
9113
+ Some(mutex) => mutex,
9114
+ None => {
9115
+ log_debug!(
9116
+ logger,
9117
+ "Not able to find peer_state for the counterparty {}, channelId {}",
9118
+ log_pubkey!(mon_holder.counterparty_node_id),
9119
+ mon_holder.channel_id
9120
+ );
9121
+ continue;
9122
+ },
9123
+ };
9124
+
9125
+ let peer_state_lock = peer_state_mutex.lock().unwrap();
9126
+ let peer_state = &*peer_state_lock;
9127
+
9128
+ match peer_state.channel_by_id.get(&mon_holder.channel_id) {
9129
+ Some(chan) => {
9130
+ if let Some(funded_chan) = chan.as_funded() {
9131
+ if funded_chan
9132
+ .get_revoked_counterparty_commitment_transaction_number()
9133
+ > mon_holder.min_seen_secret
9134
+ {
9135
+ panic!(
9136
+ "Lost channel state for channel {}.
9137
+ Received peer storage with a more recent state than what our node had.
9138
+ Use the FundRecoverer to initiate a force close and sweep the funds.",
9139
+ &mon_holder.channel_id
9140
+ );
9141
+ }
9142
+ }
9143
+ },
9144
+ None => {
9145
+ // TODO: Figure out if this channel is so old that we have forgotten about it.
9146
+ panic!("Lost a channel {}", &mon_holder.channel_id);
9147
+ },
9148
+ }
9149
+ }
9150
+ },
9151
+
9152
+ Err(e) => {
9153
+ panic!("Wrong serialisation of PeerStorageMonitorHolderList: {}", e);
9154
+ },
9155
+ }
9105
9156
Ok(())
9106
9157
}
9107
9158
@@ -9127,6 +9178,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
9127
9178
), ChannelId([0; 32])));
9128
9179
}
9129
9180
9181
+ #[cfg(not(test))]
9130
9182
if msg.data.len() > MAX_PEER_STORAGE_SIZE {
9131
9183
log_debug!(logger, "Sending warning to peer and ignoring peer storage request from {} as its over 1KiB", log_pubkey!(counterparty_node_id));
9132
9184
0 commit comments