-
Notifications
You must be signed in to change notification settings - Fork 386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Peer Storage Feature – Part 2 #3623
Open
adi2011
wants to merge
23
commits into
lightningdevkit:main
Choose a base branch
from
adi2011:peer-storage/encrypt-decrypt
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 16 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
b133e80
Add method to derive Peer Storage encryption key
3778b81
Add OurPeerStorage for serialized Peer Storage backups
adi2011 28d60ec
Enable ChainMonitor to distribute PeerStorage
ebefcd1
Distribute PeerStorage from ChainMonitor
65aee4d
Handle PeerStorageRetrieval in ChannelManager
35863ce
test: update test_peer_storage to validate latest changes
adi2011 777f6b1
fixup: Add OurPeerStorage for serialized Peer Storage backups
adi2011 c3a3859
fixup: Add method to derive Peer Storage encryption key
adi2011 ef2b8a6
fixup: Add to Enable ChainMonitor to distribute PeerStorage
adi2011 162b6c4
fixup: Add method to derive Peer Storage encryption key
adi2011 5a881a3
fixup: Add OurPeerStorage for serialized Peer Storage backups
adi2011 30d9068
fixup: Add method to derive Peer Storage encryption key
adi2011 5deb6ad
fixup: Distribute PeerStorage from ChainMonitor
adi2011 569caa4
fixup: Enable ChainMonitor to distribute PeerStorage
adi2011 58affb5
fixup: Handle PeerStorageRetrieval in ChannelManager
adi2011 4730cdc
fixup: Enable ChainMonitor to distribute PeerStorage
adi2011 5ef58e9
fixup: Add method to derive Peer Storage encryption key
adi2011 f6d6204
fixup: Add method to derive Peer Storage encryption key
adi2011 57341a1
fixup: Add method to derive Peer Storage encryption key
adi2011 42af2ab
fixup: Add OurPeerStorage for serialized Peer Storage backups
adi2011 0fd40de
fixup: Distribute PeerStorage from ChainMonitor
adi2011 0c10bd5
fixup: Handle PeerStorageRetrieval in ChannelManager
adi2011 4adeeee
fixup: Add OurPeerStorage for serialized Peer Storage backups
adi2011 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,19 +32,21 @@ use crate::chain::chaininterface::{BroadcasterInterface, FeeEstimator}; | |
use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, Balance, MonitorEvent, TransactionOutputs, WithChannelMonitor}; | ||
use crate::chain::transaction::{OutPoint, TransactionData}; | ||
use crate::ln::types::ChannelId; | ||
use crate::ln::msgs::{self, BaseMessageHandler, Init, MessageSendEvent}; | ||
use crate::ln::our_peer_storage::OurPeerStorage; | ||
use crate::sign::ecdsa::EcdsaChannelSigner; | ||
use crate::events::{self, Event, EventHandler, ReplayEvent}; | ||
use crate::util::logger::{Logger, WithContext}; | ||
use crate::util::errors::APIError; | ||
use crate::util::persist::MonitorName; | ||
use crate::util::wakers::{Future, Notifier}; | ||
use crate::ln::channel_state::ChannelDetails; | ||
|
||
use crate::prelude::*; | ||
use crate::sync::{RwLock, RwLockReadGuard, Mutex, MutexGuard}; | ||
use crate::types::features::{InitFeatures, NodeFeatures}; | ||
use core::ops::Deref; | ||
use core::sync::atomic::{AtomicUsize, Ordering}; | ||
use bitcoin::secp256k1::PublicKey; | ||
use bitcoin::secp256k1::{PublicKey, SecretKey}; | ||
|
||
/// `Persist` defines behavior for persisting channel monitors: this could mean | ||
/// writing once to disk, and/or uploading to one or more backup services. | ||
|
@@ -253,6 +255,9 @@ pub struct ChainMonitor<ChannelSigner: EcdsaChannelSigner, C: Deref, T: Deref, F | |
/// A [`Notifier`] used to wake up the background processor in case we have any [`Event`]s for | ||
/// it to give to users (or [`MonitorEvent`]s for `ChannelManager` to process). | ||
event_notifier: Notifier, | ||
pending_send_only_events: Mutex<Vec<MessageSendEvent>>, | ||
|
||
our_peerstorage_encryption_key: SecretKey, | ||
} | ||
|
||
impl<ChannelSigner: EcdsaChannelSigner, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref> ChainMonitor<ChannelSigner, C, T, F, L, P> | ||
|
@@ -386,7 +391,15 @@ where C::Target: chain::Filter, | |
/// pre-filter blocks or only fetch blocks matching a compact filter. Otherwise, clients may | ||
/// always need to fetch full blocks absent another means for determining which blocks contain | ||
/// transactions relevant to the watched channels. | ||
pub fn new(chain_source: Option<C>, broadcaster: T, logger: L, feeest: F, persister: P) -> Self { | ||
/// | ||
/// # Note | ||
/// `our_peerstorage_encryption_key` must be obtained from [`crate::sign::NodeSigner::get_peer_storage_key()`]. | ||
/// This key is used to encrypt peer storage backups. | ||
/// | ||
/// **Important**: This key should not be set arbitrarily or changed after initialization. The same key | ||
/// is obtained by the `ChannelManager` through `KeyMananger` to decrypt peer backups. | ||
/// Using an inconsistent or incorrect key will result in the inability to decrypt previously encrypted backups. | ||
pub fn new(chain_source: Option<C>, broadcaster: T, logger: L, feeest: F, persister: P, our_peerstorage_encryption_key: SecretKey) -> Self { | ||
Self { | ||
monitors: RwLock::new(new_hash_map()), | ||
chain_source, | ||
|
@@ -397,6 +410,8 @@ where C::Target: chain::Filter, | |
pending_monitor_events: Mutex::new(Vec::new()), | ||
highest_chain_height: AtomicUsize::new(0), | ||
event_notifier: Notifier::new(), | ||
pending_send_only_events: Mutex::new(Vec::new()), | ||
our_peerstorage_encryption_key | ||
} | ||
} | ||
|
||
|
@@ -665,6 +680,52 @@ where C::Target: chain::Filter, | |
}); | ||
} | ||
} | ||
|
||
/// Retrieves all node IDs associated with the monitors. | ||
/// | ||
/// This function collects the counterparty node IDs from all monitors into a `HashSet`, | ||
/// ensuring unique IDs are returned. | ||
fn get_peer_node_ids(&self) -> HashSet<PublicKey> { | ||
let mon = self.monitors.read().unwrap(); | ||
mon | ||
.values() | ||
.map(|monitor| monitor.monitor.get_counterparty_node_id()) | ||
.collect() | ||
} | ||
|
||
fn send_peer_storage(&self, their_node_id: PublicKey) { | ||
// TODO: Serialize `ChannelMonitor`s inside `our_peer_storage`. | ||
|
||
let encrypted_data = OurPeerStorage::create_from_data(self.our_peerstorage_encryption_key, Vec::new()); | ||
log_debug!(self.logger, "Sending Peer Storage from chainmonitor"); | ||
self.pending_send_only_events.lock().unwrap().push(MessageSendEvent::SendPeerStorage { node_id: their_node_id, | ||
msg: msgs::PeerStorage { data: encrypted_data } }) | ||
} | ||
} | ||
|
||
impl<ChannelSigner: EcdsaChannelSigner, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref> BaseMessageHandler for ChainMonitor<ChannelSigner, C, T, F, L, P> | ||
where C::Target: chain::Filter, | ||
T::Target: BroadcasterInterface, | ||
F::Target: FeeEstimator, | ||
L::Target: Logger, | ||
P::Target: Persist<ChannelSigner>, | ||
{ | ||
fn get_and_clear_pending_msg_events(&self) -> Vec<MessageSendEvent> { | ||
let mut pending_events = self.pending_send_only_events.lock().unwrap(); | ||
core::mem::take(&mut *pending_events) | ||
} | ||
|
||
fn peer_disconnected(&self, _their_node_id: PublicKey) {} | ||
|
||
fn provided_node_features(&self) -> NodeFeatures { | ||
NodeFeatures::empty() | ||
} | ||
|
||
fn provided_init_features(&self, _their_node_id: PublicKey) -> InitFeatures { | ||
InitFeatures::empty() | ||
} | ||
|
||
fn peer_connected(&self, _their_node_id: PublicKey, _msg: &Init, _inbound: bool) -> Result<(), ()> { Ok(()) } | ||
} | ||
|
||
impl<ChannelSigner: EcdsaChannelSigner, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref> | ||
|
@@ -682,6 +743,12 @@ where | |
monitor.block_connected( | ||
header, txdata, height, &*self.broadcaster, &*self.fee_estimator, &self.logger) | ||
}); | ||
|
||
// Send peer storage everytime a new block arrives. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How did we choose that interval? Do we only want to send if the data actually changed? |
||
for node_id in self.get_peer_node_ids() { | ||
self.send_peer_storage(node_id); | ||
} | ||
|
||
// Assume we may have some new events and wake the event processor | ||
self.event_notifier.notify(); | ||
} | ||
|
@@ -733,6 +800,12 @@ where | |
header, height, &*self.broadcaster, &*self.fee_estimator, &self.logger | ||
) | ||
}); | ||
|
||
// Send peer storage everytime a new block arrives. | ||
for node_id in self.get_peer_node_ids() { | ||
self.send_peer_storage(node_id); | ||
} | ||
|
||
// Assume we may have some new events and wake the event processor | ||
self.event_notifier.notify(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not quite sure why this code move is necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So that we can use
keys_manager.get_peer_storage_key()
insideChainMonitor::new()
.