Skip to content

Commit aaed14b

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 93d29be commit aaed14b

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ use crate::ln::types::ChannelId;
5252
use crate::types::payment::{PaymentHash, PaymentPreimage, PaymentSecret};
5353
use crate::ln::channel::{self, Channel, ChannelError, ChannelUpdateStatus, FundedChannel, ShutdownResult, UpdateFulfillCommitFetch, OutboundV1Channel, ReconnectionMsg, InboundV1Channel, WithChannelContext};
5454
use crate::ln::channel::PendingV2Channel;
55+
use crate::ln::our_peer_storage::EncryptedOurPeerStorage;
5556
use crate::ln::channel_state::ChannelDetails;
5657
use crate::types::features::{Bolt12InvoiceFeatures, ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
5758
#[cfg(any(feature = "_test_utils", test))]
@@ -8346,15 +8347,36 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
83468347
}
83478348
}
83488349

8349-
fn internal_peer_storage_retrieval(&self, counterparty_node_id: PublicKey, _msg: msgs::PeerStorageRetrieval) -> Result<(), MsgHandleErrInternal> {
8350-
// TODO: Decrypt and check if have any stale or missing ChannelMonitor.
8350+
fn internal_peer_storage_retrieval(&self, counterparty_node_id: PublicKey, msg: msgs::PeerStorageRetrieval) -> Result<(), MsgHandleErrInternal> {
8351+
// TODO: Check if have any stale or missing ChannelMonitor.
83518352
let logger = WithContext::from(&self.logger, Some(counterparty_node_id), None, None);
8353+
let err = MsgHandleErrInternal::from_chan_no_close(
8354+
ChannelError::Ignore("Invalid PeerStorageRetrieval message received.".into()),
8355+
ChannelId([0; 32]),
8356+
);
8357+
let err_str = format!("Invalid PeerStorage received from {}", counterparty_node_id);
83528358

8353-
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));
8359+
let encrypted_ops = match EncryptedOurPeerStorage::new(msg.data) {
8360+
Ok(encrypted_ops) => encrypted_ops,
8361+
Err(_) => {
8362+
log_debug!(logger, "{}", err_str);
8363+
return Err(err);
8364+
}
8365+
};
83548366

8355-
Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Warn(
8356-
"Invalid peer_storage_retrieval message received.".into(),
8357-
), ChannelId([0; 32])))
8367+
let decrypted_data = match encrypted_ops.decrypt(&self.node_signer.get_peer_storage_key()) {
8368+
Ok(decrypted_ops) => decrypted_ops.into_vec(),
8369+
Err(_) => {
8370+
log_debug!(logger, "{}", err_str);
8371+
return Err(err);
8372+
}
8373+
};
8374+
8375+
if decrypted_data.is_empty() {
8376+
log_debug!(logger, "Received a peer storage from peer {} with 0 channels.", log_pubkey!(counterparty_node_id));
8377+
}
8378+
8379+
Ok(())
83588380
}
83598381

83608382
fn internal_peer_storage(&self, counterparty_node_id: PublicKey, msg: msgs::PeerStorage) -> Result<(), MsgHandleErrInternal> {
@@ -16405,7 +16427,7 @@ mod tests {
1640516427
pub mod bench {
1640616428
use crate::chain::Listen;
1640716429
use crate::chain::chainmonitor::{ChainMonitor, Persist};
16408-
use crate::sign::{KeysManager, InMemorySigner};
16430+
use crate::sign::{KeysManager, InMemorySigner, NodeSigner};
1640916431
use crate::events::Event;
1641016432
use crate::ln::channelmanager::{BestBlock, ChainParameters, ChannelManager, PaymentHash, PaymentPreimage, PaymentId, RecipientOnionFields, Retry};
1641116433
use crate::ln::functional_test_utils::*;

0 commit comments

Comments
 (0)