@@ -32,16 +32,17 @@ use crate::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
3232use crate :: chain:: channelmonitor:: { ChannelMonitor , ChannelMonitorUpdate , Balance , MonitorEvent , TransactionOutputs , WithChannelMonitor } ;
3333use crate :: chain:: transaction:: { OutPoint , TransactionData } ;
3434use crate :: ln:: types:: ChannelId ;
35+ use crate :: ln:: msgs:: { self , BaseMessageHandler , Init , MessageSendEvent } ;
3536use crate :: sign:: ecdsa:: EcdsaChannelSigner ;
3637use crate :: events:: { self , Event , EventHandler , ReplayEvent } ;
3738use crate :: util:: logger:: { Logger , WithContext } ;
3839use crate :: util:: errors:: APIError ;
3940use crate :: util:: persist:: MonitorName ;
4041use crate :: util:: wakers:: { Future , Notifier } ;
4142use crate :: ln:: channel_state:: ChannelDetails ;
42-
4343use crate :: prelude:: * ;
4444use crate :: sync:: { RwLock , RwLockReadGuard , Mutex , MutexGuard } ;
45+ use crate :: types:: features:: { InitFeatures , NodeFeatures } ;
4546use core:: ops:: Deref ;
4647use core:: sync:: atomic:: { AtomicUsize , Ordering } ;
4748use bitcoin:: secp256k1:: PublicKey ;
@@ -215,6 +216,9 @@ impl<ChannelSigner: EcdsaChannelSigner> Deref for LockedChannelMonitor<'_, Chann
215216 }
216217}
217218
219+ /// Represents Secret Key used for encrypting Peer Storage.
220+ type PeerStorageKey = [ u8 ; 32 ] ;
221+
218222/// An implementation of [`chain::Watch`] for monitoring channels.
219223///
220224/// Connected and disconnected blocks must be provided to `ChainMonitor` as documented by
@@ -253,6 +257,7 @@ pub struct ChainMonitor<ChannelSigner: EcdsaChannelSigner, C: Deref, T: Deref, F
253257 /// A [`Notifier`] used to wake up the background processor in case we have any [`Event`]s for
254258 /// it to give to users (or [`MonitorEvent`]s for `ChannelManager` to process).
255259 event_notifier : Notifier ,
260+ pending_send_only_events : Mutex < Vec < MessageSendEvent > > ,
256261}
257262
258263impl < 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,
386391 /// pre-filter blocks or only fetch blocks matching a compact filter. Otherwise, clients may
387392 /// always need to fetch full blocks absent another means for determining which blocks contain
388393 /// transactions relevant to the watched channels.
389- pub fn new ( chain_source : Option < C > , broadcaster : T , logger : L , feeest : F , persister : P ) -> Self {
394+ ///
395+ /// # Note
396+ /// `our_peerstorage_encryption_key` must be obtained from [`crate::sign::NodeSigner::get_peer_storage_key()`].
397+ /// This key is used to encrypt peer storage backups.
398+ ///
399+ /// **Important**: This key should not be set arbitrarily or changed after initialization. The same key
400+ /// is obtained by the `ChannelManager` through `KeyMananger` to decrypt peer backups.
401+ /// Using an inconsistent or incorrect key will result in the inability to decrypt previously encrypted backups.
402+ pub fn new ( chain_source : Option < C > , broadcaster : T , logger : L , feeest : F , persister : P , our_peerstorage_encryption_key : PeerStorageKey ) -> Self {
390403 Self {
391404 monitors : RwLock :: new ( new_hash_map ( ) ) ,
392405 chain_source,
@@ -397,6 +410,7 @@ where C::Target: chain::Filter,
397410 pending_monitor_events : Mutex :: new ( Vec :: new ( ) ) ,
398411 highest_chain_height : AtomicUsize :: new ( 0 ) ,
399412 event_notifier : Notifier :: new ( ) ,
413+ pending_send_only_events : Mutex :: new ( Vec :: new ( ) ) ,
400414 }
401415 }
402416
@@ -665,6 +679,47 @@ where C::Target: chain::Filter,
665679 } ) ;
666680 }
667681 }
682+
683+ /// Retrieves all node IDs associated with the monitors.
684+ ///
685+ /// This function collects the counterparty node IDs from all monitors into a `HashSet`,
686+ /// ensuring unique IDs are returned.
687+ fn get_peer_node_ids ( & self ) -> HashSet < PublicKey > {
688+ let mon = self . monitors . read ( ) . unwrap ( ) ;
689+ mon
690+ . values ( )
691+ . map ( |monitor| monitor. monitor . get_counterparty_node_id ( ) )
692+ . collect ( )
693+ }
694+
695+ fn send_peer_storage ( & self , their_node_id : PublicKey ) {
696+ // TODO: Serialize `ChannelMonitor`s inside `our_peer_storage` and update [`OurPeerStorage::block_height`] accordingly.
697+ }
698+ }
699+
700+ impl < ChannelSigner : EcdsaChannelSigner , C : Deref , T : Deref , F : Deref , L : Deref , P : Deref > BaseMessageHandler for ChainMonitor < ChannelSigner , C , T , F , L , P >
701+ where C :: Target : chain:: Filter ,
702+ T :: Target : BroadcasterInterface ,
703+ F :: Target : FeeEstimator ,
704+ L :: Target : Logger ,
705+ P :: Target : Persist < ChannelSigner > ,
706+ {
707+ fn get_and_clear_pending_msg_events ( & self ) -> Vec < MessageSendEvent > {
708+ let mut pending_events = self . pending_send_only_events . lock ( ) . unwrap ( ) ;
709+ core:: mem:: take ( & mut * pending_events)
710+ }
711+
712+ fn peer_disconnected ( & self , _their_node_id : PublicKey ) { }
713+
714+ fn provided_node_features ( & self ) -> NodeFeatures {
715+ NodeFeatures :: empty ( )
716+ }
717+
718+ fn provided_init_features ( & self , _their_node_id : PublicKey ) -> InitFeatures {
719+ InitFeatures :: empty ( )
720+ }
721+
722+ fn peer_connected ( & self , _their_node_id : PublicKey , _msg : & Init , _inbound : bool ) -> Result < ( ) , ( ) > { Ok ( ( ) ) }
668723}
669724
670725impl < ChannelSigner : EcdsaChannelSigner , C : Deref , T : Deref , F : Deref , L : Deref , P : Deref >
0 commit comments