@@ -23,6 +23,7 @@ use crate::logger::{log_error, log_info, LdkLogger, LogLevel, LogWriter, Logger}
2323use crate :: message_handler:: NodeCustomMessageHandler ;
2424use crate :: payment:: store:: PaymentStore ;
2525use crate :: peer_store:: PeerStore ;
26+ use crate :: scoring:: BackgroundPathfindingScoresSyncer ;
2627use crate :: tx_broadcaster:: TransactionBroadcaster ;
2728use crate :: types:: {
2829 ChainMonitor , ChannelManager , DynStore , GossipSync , Graph , KeysManager , MessageRouter ,
@@ -41,7 +42,8 @@ use lightning::ln::peer_handler::{IgnoringMessageHandler, MessageHandler};
4142use lightning:: routing:: gossip:: NodeAlias ;
4243use lightning:: routing:: router:: DefaultRouter ;
4344use lightning:: routing:: scoring:: {
44- ProbabilisticScorer , ProbabilisticScoringDecayParameters , ProbabilisticScoringFeeParameters ,
45+ CombinedScorer , ProbabilisticScorer , ProbabilisticScoringDecayParameters ,
46+ ProbabilisticScoringFeeParameters ,
4547} ;
4648use lightning:: sign:: EntropySource ;
4749
@@ -97,6 +99,11 @@ enum GossipSourceConfig {
9799 RapidGossipSync ( String ) ,
98100}
99101
102+ #[ derive( Debug , Clone ) ]
103+ struct PathfindingScoresSyncConfig {
104+ url : String ,
105+ }
106+
100107#[ derive( Debug , Clone ) ]
101108struct LiquiditySourceConfig {
102109 // LSPS2 service's (address, node_id, token)
@@ -229,6 +236,7 @@ pub struct NodeBuilder {
229236 gossip_source_config : Option < GossipSourceConfig > ,
230237 liquidity_source_config : Option < LiquiditySourceConfig > ,
231238 log_writer_config : Option < LogWriterConfig > ,
239+ pathfinding_scores_sync_config : Option < PathfindingScoresSyncConfig > ,
232240}
233241
234242impl NodeBuilder {
@@ -245,13 +253,15 @@ impl NodeBuilder {
245253 let gossip_source_config = None ;
246254 let liquidity_source_config = None ;
247255 let log_writer_config = None ;
256+ let scoring_source_config = None ;
248257 Self {
249258 config,
250259 entropy_source_config,
251260 chain_data_source_config,
252261 gossip_source_config,
253262 liquidity_source_config,
254263 log_writer_config,
264+ pathfinding_scores_sync_config : scoring_source_config,
255265 }
256266 }
257267
@@ -322,6 +332,14 @@ impl NodeBuilder {
322332 self
323333 }
324334
335+ /// Configures the [`Node`] instance to source its external scores from the given URL.
336+ ///
337+ /// The external scores are merged into the local scoring system to improve routing.
338+ pub fn set_pathfinding_scores_source ( & mut self , url : String ) -> & mut Self {
339+ self . pathfinding_scores_sync_config = Some ( PathfindingScoresSyncConfig { url } ) ;
340+ self
341+ }
342+
325343 /// Configures the [`Node`] instance to source its inbound liquidity from the given
326344 /// [LSPS2](https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md)
327345 /// service.
@@ -540,6 +558,7 @@ impl NodeBuilder {
540558 config,
541559 self . chain_data_source_config . as_ref ( ) ,
542560 self . gossip_source_config . as_ref ( ) ,
561+ self . pathfinding_scores_sync_config . as_ref ( ) ,
543562 self . liquidity_source_config . as_ref ( ) ,
544563 seed_bytes,
545564 logger,
@@ -562,6 +581,7 @@ impl NodeBuilder {
562581 config,
563582 self . chain_data_source_config . as_ref ( ) ,
564583 self . gossip_source_config . as_ref ( ) ,
584+ self . pathfinding_scores_sync_config . as_ref ( ) ,
565585 self . liquidity_source_config . as_ref ( ) ,
566586 seed_bytes,
567587 logger,
@@ -654,6 +674,12 @@ impl ArcedNodeBuilder {
654674 self . inner . write ( ) . unwrap ( ) . set_gossip_source_rgs ( rgs_server_url) ;
655675 }
656676
677+ /// Configures the [`Node`] instance to source its external scores from the given URL. These scores are used to
678+ /// augment the internal pathfinding scoring system to improve routing.
679+ pub fn set_pathfinding_scores_source ( & self , url : String ) {
680+ self . inner . write ( ) . unwrap ( ) . set_pathfinding_scores_source ( url) ;
681+ }
682+
657683 /// Configures the [`Node`] instance to source its inbound liquidity from the given
658684 /// [LSPS2](https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md)
659685 /// service.
@@ -806,6 +832,7 @@ impl ArcedNodeBuilder {
806832fn build_with_store_internal (
807833 config : Arc < Config > , chain_data_source_config : Option < & ChainDataSourceConfig > ,
808834 gossip_source_config : Option < & GossipSourceConfig > ,
835+ pathfinding_scores_sync_config : Option < & PathfindingScoresSyncConfig > ,
809836 liquidity_source_config : Option < & LiquiditySourceConfig > , seed_bytes : [ u8 ; 64 ] ,
810837 logger : Arc < Logger > , kv_store : Arc < DynStore > ,
811838) -> Result < Node , BuildError > {
@@ -950,26 +977,24 @@ fn build_with_store_internal(
950977 } ,
951978 } ;
952979
953- let scorer = match io:: utils:: read_scorer (
980+ let local_scorer = match io:: utils:: read_scorer (
954981 Arc :: clone ( & kv_store) ,
955982 Arc :: clone ( & network_graph) ,
956983 Arc :: clone ( & logger) ,
957984 ) {
958- Ok ( scorer) => Arc :: new ( Mutex :: new ( scorer) ) ,
985+ Ok ( scorer) => scorer,
959986 Err ( e) => {
960987 if e. kind ( ) == std:: io:: ErrorKind :: NotFound {
961988 let params = ProbabilisticScoringDecayParameters :: default ( ) ;
962- Arc :: new ( Mutex :: new ( ProbabilisticScorer :: new (
963- params,
964- Arc :: clone ( & network_graph) ,
965- Arc :: clone ( & logger) ,
966- ) ) )
989+ ProbabilisticScorer :: new ( params, Arc :: clone ( & network_graph) , Arc :: clone ( & logger) )
967990 } else {
968991 return Err ( BuildError :: ReadFailed ) ;
969992 }
970993 } ,
971994 } ;
972995
996+ let scorer = Arc :: new ( Mutex :: new ( CombinedScorer :: new ( local_scorer) ) ) ;
997+
973998 let scoring_fee_params = ProbabilisticScoringFeeParameters :: default ( ) ;
974999 let router = Arc :: new ( DefaultRouter :: new (
9751000 Arc :: clone ( & network_graph) ,
@@ -1129,6 +1154,19 @@ fn build_with_store_internal(
11291154 } ,
11301155 } ;
11311156
1157+ let background_pathfinding_scores_syncer = if let Some ( config) = pathfinding_scores_sync_config
1158+ {
1159+ Some ( Arc :: new ( BackgroundPathfindingScoresSyncer :: new (
1160+ config. url . clone ( ) ,
1161+ Arc :: clone ( & scorer) ,
1162+ Arc :: clone ( & node_metrics) ,
1163+ Arc :: clone ( & kv_store) ,
1164+ Arc :: clone ( & logger) ,
1165+ ) ) )
1166+ } else {
1167+ None
1168+ } ;
1169+
11321170 let liquidity_source = liquidity_source_config. as_ref ( ) . and_then ( |lsc| {
11331171 lsc. lsps2_service . as_ref ( ) . map ( |( address, node_id, token) | {
11341172 let lsps2_client_config = Some ( LSPS2ClientConfig { } ) ;
@@ -1303,6 +1341,7 @@ fn build_with_store_internal(
13031341 keys_manager,
13041342 network_graph,
13051343 gossip_source,
1344+ background_pathfinding_scores_syncer,
13061345 liquidity_source,
13071346 kv_store,
13081347 logger,
0 commit comments