Skip to content

Commit 45b9d83

Browse files
committed
Determine if we have lost data
Deserialise the ChannelMonitors and compare the data to determine if we have lost some states.
1 parent a05b011 commit 45b9d83

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ use crate::ln::onion_utils::{
8181
decode_fulfill_attribution_data, HTLCFailReason, LocalHTLCFailureReason,
8282
};
8383
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};
8585
#[cfg(test)]
8686
use crate::ln::outbound_payment;
8787
use crate::ln::outbound_payment::{
@@ -2959,6 +2959,7 @@ pub(super) const MAX_UNFUNDED_CHANNEL_PEERS: usize = 50;
29592959
/// This constant defines the upper limit for the size of data
29602960
/// that can be stored for a peer. It is set to 1024 bytes (1 kilobyte)
29612961
/// to prevent excessive resource consumption.
2962+
#[cfg(not(test))]
29622963
const MAX_PEER_STORAGE_SIZE: usize = 1024;
29632964

29642965
/// 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/
93329333
};
93339334

93349335
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+
};
93359359

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+
}
93369384
Ok(())
93379385
}
93389386

@@ -9358,6 +9406,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
93589406
), ChannelId([0; 32])));
93599407
}
93609408

9409+
#[cfg(not(test))]
93619410
if msg.data.len() > MAX_PEER_STORAGE_SIZE {
93629411
log_debug!(logger, "Sending warning to peer and ignoring peer storage request from {} as its over 1KiB", log_pubkey!(counterparty_node_id));
93639412

0 commit comments

Comments
 (0)