@@ -32,6 +32,7 @@ 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;
3536use crate :: sign:: ecdsa:: EcdsaChannelSigner ;
3637use crate :: events:: { self , Event , EventHandler , ReplayEvent } ;
3738use crate :: util:: logger:: { Logger , WithContext } ;
@@ -46,6 +47,7 @@ use crate::sync::{RwLock, RwLockReadGuard, Mutex, MutexGuard};
4647use core:: ops:: Deref ;
4748use core:: sync:: atomic:: { AtomicUsize , Ordering } ;
4849use bitcoin:: secp256k1:: PublicKey ;
50+ use crate :: ln:: our_peer_storage:: OurPeerStorage ;
4951
5052/// `Persist` defines behavior for persisting channel monitors: this could mean
5153/// writing once to disk, and/or uploading to one or more backup services.
@@ -255,6 +257,8 @@ pub struct ChainMonitor<ChannelSigner: EcdsaChannelSigner, C: Deref, T: Deref, F
255257 /// it to give to users (or [`MonitorEvent`]s for `ChannelManager` to process).
256258 event_notifier : Notifier ,
257259 pending_send_only_events : Mutex < Vec < MessageSendEvent > > ,
260+ our_peer_storage : Mutex < OurPeerStorage > ,
261+ our_peerstorage_encryption_key : [ u8 ; 32 ] ,
258262}
259263
260264impl < ChannelSigner : EcdsaChannelSigner , C : Deref , T : Deref , F : Deref , L : Deref , P : Deref > ChainMonitor < ChannelSigner , C , T , F , L , P >
@@ -388,7 +392,7 @@ where C::Target: chain::Filter,
388392 /// pre-filter blocks or only fetch blocks matching a compact filter. Otherwise, clients may
389393 /// always need to fetch full blocks absent another means for determining which blocks contain
390394 /// transactions relevant to the watched channels.
391- pub fn new ( chain_source : Option < C > , broadcaster : T , logger : L , feeest : F , persister : P ) -> Self {
395+ pub fn new ( chain_source : Option < C > , broadcaster : T , logger : L , feeest : F , persister : P , our_peerstorage_encryption_key : [ u8 ; 32 ] ) -> Self {
392396 Self {
393397 monitors : RwLock :: new ( new_hash_map ( ) ) ,
394398 chain_source,
@@ -400,6 +404,8 @@ where C::Target: chain::Filter,
400404 highest_chain_height : AtomicUsize :: new ( 0 ) ,
401405 event_notifier : Notifier :: new ( ) ,
402406 pending_send_only_events : Mutex :: new ( Vec :: new ( ) ) ,
407+ our_peer_storage : Mutex :: new ( OurPeerStorage :: new ( ) ) ,
408+ our_peerstorage_encryption_key
403409 }
404410 }
405411
@@ -668,8 +674,19 @@ where C::Target: chain::Filter,
668674 } ) ;
669675 }
670676 }
671- }
672677
678+ /// Retrieves all node IDs associated with the monitors.
679+ ///
680+ /// This function collects the counterparty node IDs from all monitors into a `HashSet`,
681+ /// ensuring unique IDs are returned.
682+ fn get_peer_node_ids ( & self ) -> HashSet < PublicKey > {
683+ let mon = self . monitors . read ( ) . unwrap ( ) ;
684+ mon
685+ . values ( )
686+ . map ( |monitor| monitor. monitor . get_counterparty_node_id ( ) . unwrap ( ) . clone ( ) )
687+ . collect ( )
688+ }
689+ }
673690
674691impl < ChannelSigner : EcdsaChannelSigner , C : Deref , T : Deref , F : Deref , L : Deref , P : Deref > MessageSendEventsProvider for ChainMonitor < ChannelSigner , C , T , F , L , P >
675692where C :: Target : chain:: Filter ,
@@ -682,7 +699,8 @@ where C::Target: chain::Filter,
682699 let mut pending_events = self . pending_send_only_events . lock ( ) . unwrap ( ) ;
683700 let mut ret = Vec :: new ( ) ;
684701 core:: mem:: swap ( & mut ret, & mut * pending_events) ;
685- ret }
702+ ret
703+ }
686704}
687705
688706impl < ChannelSigner : EcdsaChannelSigner , C : Deref , T : Deref , F : Deref , L : Deref , P : Deref > SendingOnlyMessageHandler for ChainMonitor < ChannelSigner , C , T , F , L , P >
@@ -692,7 +710,11 @@ where C::Target: chain::Filter,
692710 L :: Target : Logger ,
693711 P :: Target : Persist < ChannelSigner > ,
694712{
695- fn send_peer_storage ( & self , _their_node_id : PublicKey ) {
713+ fn send_peer_storage ( & self , their_node_id : PublicKey ) {
714+ log_debug ! ( self . logger, "Sending Peer Storage from chainmonitor" ) ;
715+
716+ self . pending_send_only_events . lock ( ) . unwrap ( ) . push ( events:: MessageSendEvent :: SendPeerStorage { node_id : their_node_id
717+ , msg : msgs:: PeerStorage { data : self . our_peer_storage . lock ( ) . unwrap ( ) . encrypt_our_peer_storage ( self . our_peerstorage_encryption_key ) } } )
696718 }
697719}
698720
@@ -762,6 +784,12 @@ where
762784 header, height, & * self . broadcaster , & * self . fee_estimator , & self . logger
763785 )
764786 } ) ;
787+
788+ // Send peer storage everytime a new block arrives.
789+ for node_id in self . get_peer_node_ids ( ) {
790+ self . send_peer_storage ( node_id) ;
791+ }
792+
765793 // Assume we may have some new events and wake the event processor
766794 self . event_notifier . notify ( ) ;
767795 }
0 commit comments