@@ -172,6 +172,9 @@ const LDK_PAYMENT_RETRY_TIMEOUT: Duration = Duration::from_secs(10);
172172// The time in-between peer reconnection attempts.
173173const PEER_RECONNECTION_INTERVAL : Duration = Duration :: from_secs ( 10 ) ;
174174
175+ // The time in-between RGS sync attempts.
176+ const RGS_SYNC_INTERVAL : Duration = Duration :: from_secs ( 60 * 60 ) ;
177+
175178// The time in-between node announcement broadcast attempts.
176179const NODE_ANN_BCAST_INTERVAL : Duration = Duration :: from_secs ( 60 * 60 ) ;
177180
@@ -550,7 +553,7 @@ impl Builder {
550553 ) ) ;
551554
552555 // Reset the RGS sync timestamp in case we somehow switch gossip sources
553- io:: utils:: write_rgs_latest_sync_timestamp (
556+ io:: utils:: write_latest_rgs_sync_timestamp (
554557 0 ,
555558 Arc :: clone ( & kv_store) ,
556559 Arc :: clone ( & logger) ,
@@ -560,7 +563,7 @@ impl Builder {
560563 }
561564 GossipSourceConfig :: RapidGossipSync ( rgs_server) => {
562565 let latest_sync_timestamp =
563- io:: utils:: read_rgs_latest_sync_timestamp ( Arc :: clone ( & kv_store) ) . unwrap_or ( 0 ) ;
566+ io:: utils:: read_latest_rgs_sync_timestamp ( Arc :: clone ( & kv_store) ) . unwrap_or ( 0 ) ;
564567 Arc :: new ( GossipSource :: new_rgs (
565568 rgs_server. clone ( ) ,
566569 latest_sync_timestamp,
@@ -756,38 +759,39 @@ impl Node {
756759 let gossip_source = Arc :: clone ( & self . gossip_source ) ;
757760 let gossip_sync_store = Arc :: clone ( & self . kv_store ) ;
758761 let gossip_sync_logger = Arc :: clone ( & self . logger ) ;
759- let stop_gossip_sync = Arc :: clone ( & stop_running ) ;
762+ let mut stop_gossip_sync = self . stop_receiver . clone ( ) ;
760763 runtime. spawn ( async move {
764+ let mut interval = tokio:: time:: interval ( RGS_SYNC_INTERVAL ) ;
761765 loop {
762- let gossip_sync_logger = Arc :: clone ( & gossip_sync_logger) ;
763- let stop_gossip_sync = Arc :: clone ( & stop_gossip_sync) ;
764- if stop_gossip_sync. load ( Ordering :: Acquire ) {
765- return ;
766- }
767-
768- let now = Instant :: now ( ) ;
769- match gossip_source. update_rgs_snapshot ( ) . await {
770- Ok ( updated_timestamp) => {
771- log_info ! (
772- gossip_sync_logger,
773- "Background sync of RGS gossip data finished in {}ms." ,
774- now. elapsed( ) . as_millis( )
775- ) ;
776- io:: utils:: write_rgs_latest_sync_timestamp (
777- updated_timestamp,
778- Arc :: clone ( & gossip_sync_store) ,
779- Arc :: clone ( & gossip_sync_logger) ,
780- )
781- . expect ( "Persistence failed" ) ;
766+ tokio:: select! {
767+ _ = stop_gossip_sync. changed( ) => {
768+ return ;
769+ }
770+ _ = interval. tick( ) => {
771+ let gossip_sync_logger = Arc :: clone( & gossip_sync_logger) ;
772+ let now = Instant :: now( ) ;
773+ match gossip_source. update_rgs_snapshot( ) . await {
774+ Ok ( updated_timestamp) => {
775+ log_info!(
776+ gossip_sync_logger,
777+ "Background sync of RGS gossip data finished in {}ms." ,
778+ now. elapsed( ) . as_millis( )
779+ ) ;
780+ io:: utils:: write_latest_rgs_sync_timestamp(
781+ updated_timestamp,
782+ Arc :: clone( & gossip_sync_store) ,
783+ Arc :: clone( & gossip_sync_logger) ,
784+ )
785+ . expect( "Persistence failed" ) ;
786+ }
787+ Err ( e) => log_error!(
788+ gossip_sync_logger,
789+ "Background sync of RGS gossip data failed: {}" ,
790+ e
791+ ) ,
792+ }
782793 }
783- Err ( e) => log_error ! (
784- gossip_sync_logger,
785- "Background sync of RGS gossip data failed: {}" ,
786- e
787- ) ,
788794 }
789-
790- tokio:: time:: sleep ( Duration :: from_secs ( 60 * 60 ) ) . await ;
791795 }
792796 } ) ;
793797 }
@@ -906,30 +910,51 @@ impl Node {
906910 let bcast_cm = Arc :: clone ( & self . channel_manager ) ;
907911 let bcast_pm = Arc :: clone ( & self . peer_manager ) ;
908912 let bcast_config = Arc :: clone ( & self . config ) ;
913+ let bcast_store = Arc :: clone ( & self . kv_store ) ;
914+ let bcast_logger = Arc :: clone ( & self . logger ) ;
909915 let mut stop_bcast = self . stop_receiver . clone ( ) ;
910916 runtime. spawn ( async move {
911- let mut interval = tokio:: time:: interval ( NODE_ANN_BCAST_INTERVAL ) ;
917+ // We check every 30 secs whether our last broadcast is NODE_ANN_BCAST_INTERVAL away.
918+ let mut interval = tokio:: time:: interval ( Duration :: from_secs ( 30 ) ) ;
912919 loop {
913920 tokio:: select! {
914921 _ = stop_bcast. changed( ) => {
915922 return ;
916923 }
917- _ = interval. tick( ) , if bcast_cm . list_channels ( ) . iter ( ) . any ( |chan| chan . is_public ) => {
918- while bcast_pm . get_peer_node_ids ( ) . is_empty ( ) {
919- // Sleep a bit and retry if we don't have any peers yet.
920- tokio :: time:: sleep ( Duration :: from_secs ( 5 ) ) . await ;
921-
922- // Check back if we need to stop.
923- match stop_bcast . has_changed ( ) {
924- Ok ( false ) => { } ,
925- Ok ( true ) => return ,
926- Err ( _ ) => return ,
924+ _ = interval. tick( ) => {
925+ let skip_broadcast = match io :: utils :: read_latest_node_ann_bcast_timestamp ( Arc :: clone ( & bcast_store ) ) {
926+ Ok ( latest_bcast_time_secs ) => {
927+ // Skip if the time hasn't elapsed yet.
928+ let next_bcast_unix_time = SystemTime :: UNIX_EPOCH + Duration :: from_secs ( latest_bcast_time_secs ) + NODE_ANN_BCAST_INTERVAL ;
929+ next_bcast_unix_time . elapsed ( ) . is_err ( )
930+ }
931+ Err ( _ ) => {
932+ // Don't skip if we haven't broadcasted before.
933+ false
927934 }
935+ } ;
936+
937+ if skip_broadcast {
938+ continue ;
939+ }
940+
941+ if bcast_cm. list_channels( ) . iter( ) . any( |chan| chan. is_public) {
942+ // Skip if we don't have any public channels.
943+ continue ;
944+ }
945+
946+ if bcast_pm. get_peer_node_ids( ) . is_empty( ) {
947+ // Skip if we don't have any connected peers to gossip to.
948+ continue ;
928949 }
929950
930951 let addresses =
931952 bcast_config. listening_address. iter( ) . cloned( ) . map( |a| a. 0 ) . collect( ) ;
932953 bcast_pm. broadcast_node_announcement( [ 0 ; 3 ] , [ 0 ; 32 ] , addresses) ;
954+
955+ let unix_time_secs = SystemTime :: now( ) . duration_since( SystemTime :: UNIX_EPOCH ) . unwrap( ) . as_secs( ) ;
956+ io:: utils:: write_latest_node_ann_bcast_timestamp( unix_time_secs, Arc :: clone( & bcast_store) , Arc :: clone( & bcast_logger) )
957+ . expect( "Persistence failed" ) ;
933958 }
934959 }
935960 }
0 commit comments