From 6ecf1b82fb664a436aef09c66384009f9e2972d2 Mon Sep 17 00:00:00 2001 From: Aditya Sharma Date: Fri, 5 Apr 2024 17:01:06 +0530 Subject: [PATCH] lightning: Handle your_peer_storage from our peers. --- lightning/src/ln/channelmanager.rs | 105 ++++++++++++++++++++++++++--- 1 file changed, 97 insertions(+), 8 deletions(-) diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 1ae205c5d17..f0a6d4dfa3c 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -51,6 +51,7 @@ use crate::ln::features::Bolt11InvoiceFeatures; use crate::routing::router::{BlindedTail, InFlightHtlcs, Path, Payee, PaymentParameters, Route, RouteParameters, Router}; use crate::ln::onion_payment::{check_incoming_htlc_cltv, create_recv_pending_htlc_info, create_fwd_pending_htlc_info, decode_incoming_update_add_htlc_onion, InboundHTLCErr, NextPacketDetails}; use crate::ln::msgs; +use crate::ln::chan_utils::CounterpartyCommitmentSecrets; use crate::ln::onion_utils; use crate::ln::onion_utils::{HTLCFailReason, INVALID_ONION_BLINDING}; use crate::ln::msgs::{ChannelMessageHandler, DecodeError, LightningError}; @@ -2601,6 +2602,14 @@ where &self.default_configuration } + pub fn get_encrypted_our_peer_storage(&self) -> Vec { + let mut peer_storage = VecWriter(Vec::new()); + self.our_peer_storage.read().unwrap().write(&mut peer_storage).unwrap(); + let mut encrypted_blob = vec![0;peer_storage.0.len() + 16]; + self.inbound_payment_key.encrypt_our_peer_storage(&mut encrypted_blob, 0u64, b"", &peer_storage.0[..]); + encrypted_blob + } + fn create_and_insert_outbound_scid_alias(&self) -> u64 { let height = self.best_block.read().unwrap().height; let mut outbound_scid_alias = 0; @@ -6797,6 +6806,73 @@ where handle_new_monitor_update!(self, min_funded_chan.context.get_funding_txo().unwrap(), peer_storage_update, peer_state_lock, peer_state, per_peer_state, min_funded_chan); } + fn internal_your_peer_storage(&self, counterparty_node_id: &PublicKey, msg: &msgs::YourPeerStorageMessage) { + let per_peer_state = self.per_peer_state.read().unwrap(); + let peer_state_mutex = match per_peer_state.get(counterparty_node_id) { + Some(peer_state_mutex) => peer_state_mutex, + None => return, + }; + let mut peer_state_lock = peer_state_mutex.lock().unwrap(); + let peer_state = &mut *peer_state_lock; + let warn_msg = events::MessageSendEvent::HandleError { + node_id: *counterparty_node_id, + action: msgs::ErrorAction::SendWarningMessage { + msg: msgs::WarningMessage { + channel_id: ChannelId::from_bytes([0; 32]), + data: "Invalid peer storage sent!".to_owned(), + }, + log_level: Level::Trace, + } + }; + + if msg.data.len() < 16 { + peer_state.pending_msg_events.push(warn_msg); + return; + } + let mut res = vec![0; msg.data.len() - 16]; + + match self.inbound_payment_key.decrypt_our_peer_storage(&mut res, 0u64, b"", msg.data.as_slice()) { + Ok(()) => { + // Decryption successful, the plaintext is now stored in `res` + println!("Decryption successful: {:?}", res); + let our_peer_storage = ::read(&mut ::std::io::Cursor::new(res)).unwrap(); + let stored_channels = self.list_channels(); + + for ps_channel in &our_peer_storage.channels { + if let Some(_channel) = stored_channels.iter().find(|chan| chan.channel_id == ps_channel.channel_id) { + continue; + } else { + let keys = self.signer_provider.derive_channel_signer(ps_channel.channel_value_stoshis, ps_channel.channel_keys_id); + let monitor = ChannelMonitor::new_stub(self.secp_ctx.clone(), ps_channel, *self.best_block.read().unwrap(), keys); + let monitor_res = self.chain_monitor.watch_channel(ps_channel.funding_outpoint, monitor); + if let Ok(_persist_state) = monitor_res { + let force_close_update = ChannelMonitorUpdate { + update_id: CLOSED_CHANNEL_UPDATE_ID, + counterparty_node_id: Some(ps_channel.counterparty_node_id), + updates: vec![ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast: false }], + channel_id: Some(ps_channel.channel_id), + }; + let update_res = self.chain_monitor.update_channel(ps_channel.funding_outpoint, &force_close_update); + if update_res != ChannelMonitorUpdateStatus::Completed { + log_error!(WithContext::from(&self.logger, None, Some(ps_channel.channel_id)), + "Critical error: failed to update channel monitor from peer storage to force close.",); + } + } + else { + unreachable!("We already checked this outpoint was not recognised by the node."); + } + } + } + } + Err(_) => { + // Decryption failed + peer_state.pending_msg_events.push(warn_msg); + return; + } + } + + } + fn internal_funding_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingSigned) -> Result<(), MsgHandleErrInternal> { let best_block = *self.best_block.read().unwrap(); let per_peer_state = self.per_peer_state.read().unwrap(); @@ -9186,7 +9262,10 @@ where } fn handle_your_peer_storage(&self, counterparty_node_id: &PublicKey, msg: &msgs::YourPeerStorageMessage) { - //TODO + let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || { + self.internal_your_peer_storage(counterparty_node_id, msg); + NotifyOption::SkipPersistHandleEvents + }); } fn handle_channel_ready(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelReady) { @@ -9573,13 +9652,23 @@ where } let peer_storage = peer_state.peer_storage.clone(); - - pending_msg_events.push(events::MessageSendEvent::SendYourPeerStorageMessage { - node_id: counterparty_node_id.clone(), - msg: msgs::YourPeerStorageMessage { - data: peer_storage - }, - }); + if peer_storage.len() > 0 { + pending_msg_events.push(events::MessageSendEvent::SendYourPeerStorageMessage { + node_id: counterparty_node_id.clone(), + msg: msgs::YourPeerStorageMessage { + data: peer_storage + }, + }); + } + if peer_state.latest_features.supports_provide_peer_storage() { + let our_peer_storage = self.get_encrypted_our_peer_storage(); + pending_msg_events.push(events::MessageSendEvent::SendPeerStorageMessage { + node_id: counterparty_node_id.clone(), + msg: msgs::PeerStorageMessage { + data: our_peer_storage + }, + }); + } } return NotifyOption::SkipPersistHandleEvents;