Skip to content

Commit bd50af7

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 d143906 commit bd50af7

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

lightning-background-processor/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,7 @@ mod tests {
10871087
use lightning::routing::gossip::{NetworkGraph, P2PGossipSync};
10881088
use lightning::routing::router::{CandidateRouteHop, DefaultRouter, Path, RouteHop};
10891089
use lightning::routing::scoring::{ChannelUsage, LockableScore, ScoreLookUp, ScoreUpdate};
1090-
use lightning::sign::{ChangeDestinationSource, InMemorySigner, KeysManager};
1090+
use lightning::sign::{ChangeDestinationSource, InMemorySigner, KeysManager, NodeSigner};
10911091
use lightning::types::features::{ChannelFeatures, NodeFeatures};
10921092
use lightning::types::payment::PaymentHash;
10931093
use lightning::util::config::UserConfig;

lightning-liquidity/tests/common/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#![allow(unused_macros)]
66

77
use lightning::chain::Filter;
8-
use lightning::sign::EntropySource;
8+
use lightning::sign::{EntropySource, NodeSigner};
99

1010
use bitcoin::blockdata::constants::{genesis_block, ChainHash};
1111
use bitcoin::blockdata::transaction::Transaction;
@@ -431,6 +431,7 @@ pub(crate) fn create_liquidity_node(
431431
logger.clone(),
432432
fee_estimator.clone(),
433433
kv_store.clone(),
434+
keys_manager.get_peer_storage_key(),
434435
));
435436
let best_block = BestBlock::from_network(network);
436437
let chain_params = ChainParameters { network, best_block };

lightning/src/ln/channelmanager.rs

+30-7
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ use crate::types::payment::{PaymentHash, PaymentPreimage, PaymentSecret};
5151
use crate::ln::channel::{self, Channel, ChannelError, ChannelUpdateStatus, FundedChannel, ShutdownResult, UpdateFulfillCommitFetch, OutboundV1Channel, ReconnectionMsg, InboundV1Channel, WithChannelContext};
5252
#[cfg(any(dual_funding, splicing))]
5353
use crate::ln::channel::PendingV2Channel;
54+
use crate::ln::our_peer_storage::OurPeerStorage;
5455
use crate::ln::channel_state::ChannelDetails;
5556
use crate::types::features::{Bolt12InvoiceFeatures, ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
5657
#[cfg(any(feature = "_test_utils", test))]
@@ -78,8 +79,8 @@ use crate::onion_message::async_payments::{AsyncPaymentsMessage, HeldHtlcAvailab
7879
use crate::onion_message::dns_resolution::HumanReadableName;
7980
use crate::onion_message::messenger::{Destination, MessageRouter, Responder, ResponseInstruction, MessageSendInstructions};
8081
use crate::onion_message::offers::{OffersMessage, OffersMessageHandler};
81-
use crate::sign::{EntropySource, NodeSigner, Recipient, SignerProvider};
8282
use crate::sign::ecdsa::EcdsaChannelSigner;
83+
use crate::sign::{EntropySource, NodeSigner, Recipient, SignerProvider};
8384
use crate::util::config::{ChannelConfig, ChannelConfigUpdate, ChannelConfigOverrides, UserConfig};
8485
use crate::util::wakers::{Future, Notifier};
8586
use crate::util::scid_utils::fake_scid;
@@ -8296,15 +8297,37 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
82968297
}
82978298
}
82988299

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

8303-
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));
8304+
if msg.data.len() < 16 {
8305+
log_debug!(logger, "Invalid YourPeerStorage received from {}", log_pubkey!(counterparty_node_id));
8306+
return Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Warn(
8307+
"Invalid peer_storage_retrieval message received.".into(),
8308+
), ChannelId([0; 32])));
8309+
}
8310+
8311+
let mut res = vec![0; msg.data.len() - 16];
8312+
let our_peerstorage_encryption_key = self.node_signer.get_peer_storage_key();
8313+
let mut cyphertext_with_key = Vec::with_capacity(msg.data.len() + our_peerstorage_encryption_key.len());
8314+
cyphertext_with_key.extend(msg.data.clone());
8315+
cyphertext_with_key.extend_from_slice(&our_peerstorage_encryption_key);
83048316

8305-
Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Warn(
8306-
"Invalid peer_storage_retrieval message received.".into(),
8307-
), ChannelId([0; 32])))
8317+
match OurPeerStorage::decrypt_our_peer_storage(&mut res, cyphertext_with_key.as_slice()) {
8318+
Ok(()) => {
8319+
// Decryption successful, the plaintext is now stored in `res`.
8320+
log_debug!(logger, "Received a peer storage from peer {}", log_pubkey!(counterparty_node_id));
8321+
}
8322+
Err(_) => {
8323+
log_debug!(logger, "Invalid YourPeerStorage received from {}", log_pubkey!(counterparty_node_id));
8324+
8325+
return Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Warn(
8326+
"Invalid peer_storage_retrieval message received.".into(),
8327+
), ChannelId([0; 32])));
8328+
}
8329+
}
8330+
Ok(())
83088331
}
83098332

83108333
fn internal_peer_storage(&self, counterparty_node_id: PublicKey, msg: msgs::PeerStorage) -> Result<(), MsgHandleErrInternal> {

0 commit comments

Comments
 (0)