Skip to content

Commit d9af88c

Browse files
Aditya Sharmaadi2011
Aditya Sharma
authored andcommitted
Handle PeerStorageRetrieval in ChannelManager
Ensure ChannelManager properly handles peer_storage_retrieval. - Write internal_peer_storage_retreival to verify if we recv correct peer storage. - Send error if we get invalid peer_storage data.
1 parent 3f73c8e commit d9af88c

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ use crate::ln::onion_payment::{
7878
};
7979
use crate::ln::onion_utils::{self};
8080
use crate::ln::onion_utils::{HTLCFailReason, LocalHTLCFailureReason};
81+
use crate::ln::our_peer_storage::EncryptedOurPeerStorage;
8182
#[cfg(test)]
8283
use crate::ln::outbound_payment;
8384
use crate::ln::outbound_payment::{
@@ -8546,15 +8547,38 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
85468547
}
85478548

85488549
#[rustfmt::skip]
8549-
fn internal_peer_storage_retrieval(&self, counterparty_node_id: PublicKey, _msg: msgs::PeerStorageRetrieval) -> Result<(), MsgHandleErrInternal> {
8550-
// TODO: Decrypt and check if have any stale or missing ChannelMonitor.
8550+
fn internal_peer_storage_retrieval(&self, counterparty_node_id: PublicKey, msg: msgs::PeerStorageRetrieval) -> Result<(), MsgHandleErrInternal> {
8551+
// TODO: Check if have any stale or missing ChannelMonitor.
85518552
let logger = WithContext::from(&self.logger, Some(counterparty_node_id), None, None);
8553+
let err = MsgHandleErrInternal::from_chan_no_close(
8554+
ChannelError::Ignore("Invalid PeerStorageRetrieval message received.".into()),
8555+
ChannelId([0; 32]),
8556+
);
8557+
let err_str = || {
8558+
format!("Invalid PeerStorage received from {}", counterparty_node_id)
8559+
};
8560+
8561+
let encrypted_ops = match EncryptedOurPeerStorage::new(msg.data) {
8562+
Ok(encrypted_ops) => encrypted_ops,
8563+
Err(_) => {
8564+
log_debug!(logger, "{}", err_str());
8565+
return Err(err);
8566+
}
8567+
};
85528568

8553-
log_debug!(logger, "Received unexpected peer_storage_retrieval from {}. This is unusual since we do not yet distribute peer storage. Sending a warning.", log_pubkey!(counterparty_node_id));
8569+
let decrypted_data = match encrypted_ops.decrypt(&self.node_signer.get_peer_storage_key()) {
8570+
Ok(decrypted_ops) => decrypted_ops.into_vec(),
8571+
Err(_) => {
8572+
log_debug!(logger, "{}", err_str());
8573+
return Err(err);
8574+
}
8575+
};
85548576

8555-
Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Warn(
8556-
"Invalid peer_storage_retrieval message received.".into(),
8557-
), ChannelId([0; 32])))
8577+
if decrypted_data.is_empty() {
8578+
log_debug!(logger, "Received a peer storage from peer {} with 0 channels.", log_pubkey!(counterparty_node_id));
8579+
}
8580+
8581+
Ok(())
85588582
}
85598583

85608584
#[rustfmt::skip]
@@ -16755,7 +16779,7 @@ pub mod bench {
1675516779
use crate::ln::msgs::{BaseMessageHandler, ChannelMessageHandler, Init, MessageSendEvent};
1675616780
use crate::routing::gossip::NetworkGraph;
1675716781
use crate::routing::router::{PaymentParameters, RouteParameters};
16758-
use crate::sign::{InMemorySigner, KeysManager};
16782+
use crate::sign::{InMemorySigner, KeysManager, NodeSigner};
1675916783
use crate::util::config::{MaxDustHTLCExposure, UserConfig};
1676016784
use crate::util::test_utils;
1676116785

0 commit comments

Comments
 (0)