Skip to content

Commit 5baa1f8

Browse files
authored
Allow setting custom scoring parameters (#23)
The ProbabilisticScorer and DefaultRouter were always initialized with hardcoded defaults. If you wanted different fee penalties or decay behavior, you had to edit the source. This adds a ProbabilisticScoringParameters struct and exposes it through the builder API so both fee and decay parameters are configurable. Ported from cequals#5; the prober module from the original does not exist on this branch so that part was dropped.
1 parent e935695 commit 5baa1f8

4 files changed

Lines changed: 41 additions & 4 deletions

File tree

src/builder.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ use crate::payment::asynchronous::om_mailbox::OnionMessageMailbox;
7373
use crate::peer_store::PeerStore;
7474
use crate::router::{LSPS4BlindedPathConfig, LSPS4Router};
7575
use crate::runtime::Runtime;
76+
use crate::scoring::ProbabilisticScoringParameters;
7677
use crate::tx_broadcaster::TransactionBroadcaster;
7778
use crate::types::{
7879
ChainMonitor, ChannelManager, DynStore, GossipSync, Graph, KeysManager, MessageRouter,
@@ -620,6 +621,14 @@ impl NodeBuilder {
620621
Ok(self)
621622
}
622623

624+
/// Sets the parameters for the scoring algorithm.
625+
pub fn set_scoring_params(
626+
&mut self, scoring_params: ProbabilisticScoringParameters,
627+
) -> &mut Self {
628+
self.config.scoring_parameters = scoring_params;
629+
self
630+
}
631+
623632
/// Builds a [`Node`] instance with a [`SqliteStore`] backend and according to the options
624633
/// previously configured.
625634
pub fn build(&self) -> Result<Node, BuildError> {
@@ -1133,6 +1142,11 @@ impl ArcedNodeBuilder {
11331142
self.inner.write().unwrap().set_async_payments_role(role).map(|_| ())
11341143
}
11351144

1145+
/// Sets the parameters for the scoring algorithm.
1146+
pub fn set_scoring_params(&self, scoring_params: ProbabilisticScoringParameters) {
1147+
self.inner.write().unwrap().set_scoring_params(scoring_params);
1148+
}
1149+
11361150
/// Builds a [`Node`] instance with a [`SqliteStore`] backend and according to the options
11371151
/// previously configured.
11381152
pub fn build(&self) -> Result<Arc<Node>, BuildError> {
@@ -1577,8 +1591,12 @@ fn build_with_store_internal(
15771591
Ok(scorer) => scorer,
15781592
Err(e) => {
15791593
if e.kind() == std::io::ErrorKind::NotFound {
1580-
let params = ProbabilisticScoringDecayParameters::default();
1581-
ProbabilisticScorer::new(params, Arc::clone(&network_graph), Arc::clone(&logger))
1594+
let decay_params = config.scoring_parameters.decay_params;
1595+
ProbabilisticScorer::new(
1596+
decay_params,
1597+
Arc::clone(&network_graph),
1598+
Arc::clone(&logger),
1599+
)
15821600
} else {
15831601
log_error!(logger, "Failed to read scoring data from store: {}", e);
15841602
return Err(BuildError::ReadFailed);
@@ -1606,7 +1624,7 @@ fn build_with_store_internal(
16061624
},
16071625
}
16081626

1609-
let scoring_fee_params = ProbabilisticScoringFeeParameters::default();
1627+
let scoring_fee_params = config.scoring_parameters.fee_params.clone();
16101628
let inner_router = DefaultRouter::new(
16111629
Arc::clone(&network_graph),
16121630
Arc::clone(&logger),

src/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use lightning::util::config::{
2020
};
2121

2222
use crate::logger::LogLevel;
23+
use crate::scoring::ProbabilisticScoringParameters;
2324

2425
// Config defaults
2526
const DEFAULT_NETWORK: Network = Network::Bitcoin;
@@ -184,6 +185,9 @@ pub struct Config {
184185
/// **Note:** If unset, default parameters will be used, and you will be able to override the
185186
/// parameters on a per-payment basis in the corresponding method calls.
186187
pub route_parameters: Option<RouteParametersConfig>,
188+
/// The parameters used to configure the [`lightning::routing::scoring::ProbabilisticScorer`]
189+
/// used by the node.
190+
pub scoring_parameters: ProbabilisticScoringParameters,
187191
}
188192

189193
impl Default for Config {
@@ -198,6 +202,7 @@ impl Default for Config {
198202
anchor_channels_config: Some(AnchorChannelsConfig::default()),
199203
route_parameters: None,
200204
node_alias: None,
205+
scoring_parameters: ProbabilisticScoringParameters::default(),
201206
}
202207
}
203208
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ mod tx_broadcaster;
103103
mod types;
104104
mod wallet;
105105

106+
pub use scoring::ProbabilisticScoringParameters;
107+
106108
use std::default::Default;
107109
use std::net::ToSocketAddrs;
108110
use std::sync::{Arc, Mutex, RwLock};

src/scoring.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,22 @@ use std::io::Cursor;
22
use std::sync::{Arc, Mutex, RwLock};
33
use std::time::{Duration, SystemTime};
44

5-
use lightning::routing::scoring::ChannelLiquidities;
5+
use lightning::routing::scoring::{
6+
ChannelLiquidities, ProbabilisticScoringDecayParameters, ProbabilisticScoringFeeParameters,
7+
};
68
use lightning::util::ser::Readable;
79
use lightning::{log_error, log_info, log_trace};
810

11+
/// The parameters used to configure the [`lightning::routing::scoring::ProbabilisticScorer`]
12+
/// used by the node.
13+
#[derive(Debug, Clone, Default)]
14+
pub struct ProbabilisticScoringParameters {
15+
/// The fee parameters used by the router to compute path penalties.
16+
pub fee_params: ProbabilisticScoringFeeParameters,
17+
/// The decay parameters used by the scorer to reduce certainty of liquidity information.
18+
pub decay_params: ProbabilisticScoringDecayParameters,
19+
}
20+
921
use crate::config::{
1022
EXTERNAL_PATHFINDING_SCORES_SYNC_INTERVAL, EXTERNAL_PATHFINDING_SCORES_SYNC_TIMEOUT_SECS,
1123
};

0 commit comments

Comments
 (0)