Skip to content

Commit 9a59ac1

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 9acecdc commit 9a59ac1

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ use crate::events::{
5858
use crate::events::{FundingInfo, PaidBolt12Invoice};
5959
// Since this struct is returned in `list_channels` methods, expose it here in case users want to
6060
// construct one themselves.
61+
use crate::io;
6162
use crate::ln::channel::PendingV2Channel;
6263
use crate::ln::channel::{
6364
self, Channel, ChannelError, ChannelUpdateStatus, FundedChannel, InboundV1Channel,
@@ -78,7 +79,7 @@ use crate::ln::onion_payment::{
7879
};
7980
use crate::ln::onion_utils::{self};
8081
use crate::ln::onion_utils::{HTLCFailReason, LocalHTLCFailureReason};
81-
use crate::ln::our_peer_storage::EncryptedOurPeerStorage;
82+
use crate::ln::our_peer_storage::{EncryptedOurPeerStorage, PeerStorageMonitorHolderList};
8283
#[cfg(test)]
8384
use crate::ln::outbound_payment;
8485
use crate::ln::outbound_payment::{
@@ -174,7 +175,6 @@ use lightning_invoice::{
174175

175176
use alloc::collections::{btree_map, BTreeMap};
176177

177-
use crate::io;
178178
use crate::io::Read;
179179
use crate::prelude::*;
180180
use crate::sync::{Arc, FairRwLock, LockHeldState, LockTestExt, Mutex, RwLock, RwLockReadGuard};
@@ -3014,6 +3014,7 @@ pub(super) const MAX_UNFUNDED_CHANNEL_PEERS: usize = 50;
30143014
/// This constant defines the upper limit for the size of data
30153015
/// that can be stored for a peer. It is set to 1024 bytes (1 kilobyte)
30163016
/// to prevent excessive resource consumption.
3017+
#[cfg(not(test))]
30173018
const MAX_PEER_STORAGE_SIZE: usize = 1024;
30183019

30193020
/// 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/
88078808
&self, peer_node_id: PublicKey, msg: msgs::PeerStorageRetrieval,
88088809
) -> Result<(), MsgHandleErrInternal> {
88098810
// TODO: Check if have any stale or missing ChannelMonitor.
8811+
let per_peer_state = self.per_peer_state.read().unwrap();
88108812
let logger = WithContext::from(&self.logger, Some(peer_node_id), None, None);
88118813
let err = || {
88128814
MsgHandleErrInternal::from_chan_no_close(
@@ -8833,6 +8835,55 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
88338835

88348836
log_trace!(logger, "Got valid {}-byte peer backup from {}", decrypted.len(), peer_node_id);
88358837

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+
}
88368887
Ok(())
88378888
}
88388889

@@ -8858,6 +8909,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
88588909
), ChannelId([0; 32])));
88598910
}
88608911

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

0 commit comments

Comments
 (0)