Skip to content

Commit fce28ab

Browse files
Aditya Sharmaadi2011
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 37aa11a commit fce28ab

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 34 additions & 8 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::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))]
@@ -77,8 +78,8 @@ use crate::onion_message::async_payments::{AsyncPaymentsMessage, HeldHtlcAvailab
7778
use crate::onion_message::dns_resolution::HumanReadableName;
7879
use crate::onion_message::messenger::{Destination, MessageRouter, Responder, ResponseInstruction, MessageSendInstructions};
7980
use crate::onion_message::offers::{OffersMessage, OffersMessageHandler};
80-
use crate::sign::{EntropySource, NodeSigner, Recipient, SignerProvider};
8181
use crate::sign::ecdsa::EcdsaChannelSigner;
82+
use crate::sign::{EntropySource, NodeSigner, Recipient, SignerProvider};
8283
use crate::util::config::{ChannelConfig, ChannelConfigUpdate, ChannelConfigOverrides, UserConfig};
8384
use crate::util::wakers::{Future, Notifier};
8485
use crate::util::scid_utils::fake_scid;
@@ -8343,15 +8344,40 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
83438344
}
83448345
}
83458346

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

8350-
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));
8351+
// `MIN_CYPHERTEXT_LEN` is 16 bytes because the mandatory authentication tag length is 16 bytes.
8352+
const MIN_CYPHERTEXT_LEN: usize = 16;
8353+
8354+
if msg.data.len() < MIN_CYPHERTEXT_LEN {
8355+
log_debug!(logger, "Invalid YourPeerStorage received from {}", log_pubkey!(counterparty_node_id));
8356+
return Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Warn(
8357+
"Invalid peer_storage_retrieval message received.".into(),
8358+
), ChannelId([0; 32])));
8359+
}
8360+
8361+
let our_peerstorage_encryption_key = self.node_signer.get_peer_storage_key();
8362+
let our_peer_storage = OurPeerStorage::new(msg.data);
83518363

8352-
Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Warn(
8353-
"Invalid peer_storage_retrieval message received.".into(),
8354-
), ChannelId([0; 32])))
8364+
match our_peer_storage.decrypt_our_peer_storage(our_peerstorage_encryption_key) {
8365+
Ok(decrypted_data) => {
8366+
// Decryption successful.
8367+
if decrypted_data.len() == 0 {
8368+
log_trace!(logger, "Received a peer storage from peer {} with 0 channels.", log_pubkey!(counterparty_node_id));
8369+
}
8370+
}
8371+
Err(_) => {
8372+
log_debug!(logger, "Invalid YourPeerStorage received from {}", log_pubkey!(counterparty_node_id));
8373+
8374+
return Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(
8375+
"Invalid peer_storage_retrieval message received.".into(),
8376+
), ChannelId([0; 32])));
8377+
}
8378+
}
8379+
8380+
Ok(())
83558381
}
83568382

83578383
fn internal_peer_storage(&self, counterparty_node_id: PublicKey, msg: msgs::PeerStorage) -> Result<(), MsgHandleErrInternal> {
@@ -16326,7 +16352,7 @@ mod tests {
1632616352
pub mod bench {
1632716353
use crate::chain::Listen;
1632816354
use crate::chain::chainmonitor::{ChainMonitor, Persist};
16329-
use crate::sign::{KeysManager, InMemorySigner};
16355+
use crate::sign::{KeysManager, InMemorySigner, NodeSigner};
1633016356
use crate::events::Event;
1633116357
use crate::ln::channelmanager::{BestBlock, ChainParameters, ChannelManager, PaymentHash, PaymentPreimage, PaymentId, RecipientOnionFields, Retry};
1633216358
use crate::ln::functional_test_utils::*;

0 commit comments

Comments
 (0)