Skip to content

Commit 95f53e5

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 535e8d0 commit 95f53e5

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

lightning/src/ln/channelmanager.rs

+24-7
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::OurPeerStorage;
5556
use crate::ln::channel_state::ChannelDetails;
5657
use crate::types::features::{Bolt12InvoiceFeatures, ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
5758
#[cfg(any(feature = "_test_utils", test))]
@@ -8317,15 +8318,31 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
83178318
}
83188319
}
83198320

8320-
fn internal_peer_storage_retrieval(&self, counterparty_node_id: PublicKey, _msg: msgs::PeerStorageRetrieval) -> Result<(), MsgHandleErrInternal> {
8321-
// TODO: Decrypt and check if have any stale or missing ChannelMonitor.
8321+
fn internal_peer_storage_retrieval(&self, counterparty_node_id: PublicKey, msg: msgs::PeerStorageRetrieval) -> Result<(), MsgHandleErrInternal> {
8322+
// TODO: Check if have any stale or missing ChannelMonitor.
83228323
let logger = WithContext::from(&self.logger, Some(counterparty_node_id), None, None);
83238324

8324-
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));
8325+
let encrypted_ops = OurPeerStorage::EncryptedPeerStorage { cipher: msg.data };
8326+
let decrypted_data = match encrypted_ops.decrypt_peer_storage(&self.node_signer.get_peer_storage_key()) {
8327+
Ok(decrypted_ops) => decrypted_ops.into_vec(),
8328+
Err(_) => {
8329+
log_debug!(
8330+
logger,
8331+
"Invalid PeerStorage received from {}",
8332+
log_pubkey!(counterparty_node_id)
8333+
);
8334+
return Err(MsgHandleErrInternal::from_chan_no_close(
8335+
ChannelError::Ignore("Invalid PeerStorageRetrieval message received.".into()),
8336+
ChannelId([0; 32]),
8337+
));
8338+
}
8339+
};
8340+
8341+
if decrypted_data.is_empty() {
8342+
log_debug!(logger, "Received a peer storage from peer {} with 0 channels.", log_pubkey!(counterparty_node_id));
8343+
}
83258344

8326-
Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Warn(
8327-
"Invalid peer_storage_retrieval message received.".into(),
8328-
), ChannelId([0; 32])))
8345+
Ok(())
83298346
}
83308347

83318348
fn internal_peer_storage(&self, counterparty_node_id: PublicKey, msg: msgs::PeerStorage) -> Result<(), MsgHandleErrInternal> {
@@ -16337,7 +16354,7 @@ mod tests {
1633716354
pub mod bench {
1633816355
use crate::chain::Listen;
1633916356
use crate::chain::chainmonitor::{ChainMonitor, Persist};
16340-
use crate::sign::{KeysManager, InMemorySigner};
16357+
use crate::sign::{KeysManager, InMemorySigner, NodeSigner};
1634116358
use crate::events::Event;
1634216359
use crate::ln::channelmanager::{BestBlock, ChainParameters, ChannelManager, PaymentHash, PaymentPreimage, PaymentId, RecipientOnionFields, Retry};
1634316360
use crate::ln::functional_test_utils::*;

0 commit comments

Comments
 (0)