Skip to content

Commit cda6adc

Browse files
committed
Hard-code scorer parameters to ProbabilisticScoringFeeParameters
The scorer currently relies on an associated type for the fee parameters. This isn't supportable at all in bindings, and for lack of a better option we simply hard-code the parameter for all scorers to `ProbabilisticScoringFeeParameters`.
1 parent 763a362 commit cda6adc

File tree

6 files changed

+39
-34
lines changed

6 files changed

+39
-34
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,8 +1141,6 @@ mod tests {
11411141
Arc<test_utils::TestLogger>,
11421142
Arc<KeysManager>,
11431143
Arc<LockingWrapper<TestScorer>>,
1144-
(),
1145-
TestScorer,
11461144
>,
11471145
>,
11481146
Arc<
@@ -1412,10 +1410,11 @@ mod tests {
14121410
}
14131411

14141412
impl ScoreLookUp for TestScorer {
1413+
#[cfg(not(c_bindings))]
14151414
type ScoreParams = ();
14161415
fn channel_penalty_msat(
14171416
&self, _candidate: &CandidateRouteHop, _usage: ChannelUsage,
1418-
_score_params: &Self::ScoreParams,
1417+
_score_params: &lightning::routing::scoring::ProbabilisticScoringFeeParameters,
14191418
) -> u64 {
14201419
unimplemented!();
14211420
}

lightning-liquidity/tests/common/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ type ChannelManager = channelmanager::ChannelManager<
8080
Arc<test_utils::TestLogger>,
8181
Arc<KeysManager>,
8282
Arc<LockingWrapper<TestScorer>>,
83-
(),
84-
TestScorer,
8583
>,
8684
>,
8785
Arc<
@@ -286,10 +284,11 @@ impl lightning::util::ser::Writeable for TestScorer {
286284
}
287285

288286
impl ScoreLookUp for TestScorer {
287+
#[cfg(not(c_bindings))]
289288
type ScoreParams = ();
290289
fn channel_penalty_msat(
291290
&self, _candidate: &CandidateRouteHop, _usage: ChannelUsage,
292-
_score_params: &Self::ScoreParams,
291+
_score_params: &lightning::routing::scoring::ProbabilisticScoringFeeParameters,
293292
) -> u64 {
294293
unimplemented!();
295294
}

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,8 +1466,6 @@ pub type SimpleArcChannelManager<M, T, F, L> = ChannelManager<
14661466
Arc<L>,
14671467
Arc<KeysManager>,
14681468
Arc<RwLock<ProbabilisticScorer<Arc<NetworkGraph<Arc<L>>>, Arc<L>>>>,
1469-
ProbabilisticScoringFeeParameters,
1470-
ProbabilisticScorer<Arc<NetworkGraph<Arc<L>>>, Arc<L>>,
14711469
>>,
14721470
Arc<DefaultMessageRouter<
14731471
Arc<NetworkGraph<Arc<L>>>,
@@ -1502,8 +1500,6 @@ pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, M, T, F, L>
15021500
&'g L,
15031501
&'c KeysManager,
15041502
&'h RwLock<ProbabilisticScorer<&'f NetworkGraph<&'g L>, &'g L>>,
1505-
ProbabilisticScoringFeeParameters,
1506-
ProbabilisticScorer<&'f NetworkGraph<&'g L>, &'g L>
15071503
>,
15081504
&'i DefaultMessageRouter<
15091505
&'f NetworkGraph<&'g L>,
@@ -1669,7 +1665,7 @@ where
16691665
/// # fee_estimator: &dyn lightning::chain::chaininterface::FeeEstimator,
16701666
/// # chain_monitor: &dyn lightning::chain::Watch<lightning::sign::InMemorySigner>,
16711667
/// # tx_broadcaster: &dyn lightning::chain::chaininterface::BroadcasterInterface,
1672-
/// # router: &lightning::routing::router::DefaultRouter<&NetworkGraph<&'a L>, &'a L, &ES, &S, SP, SL>,
1668+
/// # router: &lightning::routing::router::DefaultRouter<&NetworkGraph<&'a L>, &'a L, &ES, &S>,
16731669
/// # message_router: &lightning::onion_message::messenger::DefaultMessageRouter<&NetworkGraph<&'a L>, &'a L, &ES>,
16741670
/// # logger: &L,
16751671
/// # entropy_source: &ES,

lightning/src/routing/router.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,32 @@ pub use lightning_types::routing::{RouteHint, RouteHintHop};
4747
/// it will create a one-hop path using the recipient as the introduction node if it is a announced
4848
/// node. Otherwise, there is no way to find a path to the introduction node in order to send a
4949
/// payment, and thus an `Err` is returned.
50-
pub struct DefaultRouter<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>> where
50+
pub struct DefaultRouter<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref, S: Deref> where
5151
L::Target: Logger,
52-
S::Target: for <'a> LockableScore<'a, ScoreLookUp = Sc>,
52+
S::Target: for <'a> LockableScore<'a>,
5353
ES::Target: EntropySource,
5454
{
5555
network_graph: G,
5656
logger: L,
5757
entropy_source: ES,
5858
scorer: S,
59-
score_params: SP,
59+
score_params: crate::routing::scoring::ProbabilisticScoringFeeParameters,
6060
}
6161

62-
impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>> DefaultRouter<G, L, ES, S, SP, Sc> where
62+
impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref, S: Deref> DefaultRouter<G, L, ES, S> where
6363
L::Target: Logger,
64-
S::Target: for <'a> LockableScore<'a, ScoreLookUp = Sc>,
64+
S::Target: for <'a> LockableScore<'a>,
6565
ES::Target: EntropySource,
6666
{
6767
/// Creates a new router.
68-
pub fn new(network_graph: G, logger: L, entropy_source: ES, scorer: S, score_params: SP) -> Self {
68+
pub fn new(network_graph: G, logger: L, entropy_source: ES, scorer: S, score_params: crate::routing::scoring::ProbabilisticScoringFeeParameters) -> Self {
6969
Self { network_graph, logger, entropy_source, scorer, score_params }
7070
}
7171
}
7272

73-
impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>> Router for DefaultRouter<G, L, ES, S, SP, Sc> where
73+
impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref, S: Deref> Router for DefaultRouter<G, L, ES, S> where
7474
L::Target: Logger,
75-
S::Target: for <'a> LockableScore<'a, ScoreLookUp = Sc>,
75+
S::Target: for <'a> LockableScore<'a>,
7676
ES::Target: EntropySource,
7777
{
7878
fn find_route(
@@ -244,8 +244,9 @@ impl<'a, S: Deref> ScorerAccountingForInFlightHtlcs<'a, S> where S::Target: Scor
244244
}
245245

246246
impl<'a, S: Deref> ScoreLookUp for ScorerAccountingForInFlightHtlcs<'a, S> where S::Target: ScoreLookUp {
247+
#[cfg(not(c_bindings))]
247248
type ScoreParams = <S::Target as ScoreLookUp>::ScoreParams;
248-
fn channel_penalty_msat(&self, candidate: &CandidateRouteHop, usage: ChannelUsage, score_params: &Self::ScoreParams) -> u64 {
249+
fn channel_penalty_msat(&self, candidate: &CandidateRouteHop, usage: ChannelUsage, score_params: &crate::routing::scoring::ProbabilisticScoringFeeParameters) -> u64 {
249250
let target = match candidate.target() {
250251
Some(target) => target,
251252
None => return self.scorer.channel_penalty_msat(candidate, usage, score_params),
@@ -2064,7 +2065,7 @@ fn sort_first_hop_channels(
20642065
pub fn find_route<L: Deref, GL: Deref, S: ScoreLookUp>(
20652066
our_node_pubkey: &PublicKey, route_params: &RouteParameters,
20662067
network_graph: &NetworkGraph<GL>, first_hops: Option<&[&ChannelDetails]>, logger: L,
2067-
scorer: &S, score_params: &S::ScoreParams, random_seed_bytes: &[u8; 32]
2068+
scorer: &S, score_params: &crate::routing::scoring::ProbabilisticScoringFeeParameters, random_seed_bytes: &[u8; 32]
20682069
) -> Result<Route, LightningError>
20692070
where L::Target: Logger, GL::Target: Logger {
20702071
let graph_lock = network_graph.read_only();
@@ -2076,7 +2077,7 @@ where L::Target: Logger, GL::Target: Logger {
20762077

20772078
pub(crate) fn get_route<L: Deref, S: ScoreLookUp>(
20782079
our_node_pubkey: &PublicKey, route_params: &RouteParameters, network_graph: &ReadOnlyNetworkGraph,
2079-
first_hops: Option<&[&ChannelDetails]>, logger: L, scorer: &S, score_params: &S::ScoreParams,
2080+
first_hops: Option<&[&ChannelDetails]>, logger: L, scorer: &S, score_params: &crate::routing::scoring::ProbabilisticScoringFeeParameters,
20802081
_random_seed_bytes: &[u8; 32]
20812082
) -> Result<Route, LightningError>
20822083
where L::Target: Logger {
@@ -3494,9 +3495,10 @@ fn build_route_from_hops_internal<L: Deref>(
34943495
}
34953496

34963497
impl ScoreLookUp for HopScorer {
3498+
#[cfg(not(c_bindings))]
34973499
type ScoreParams = ();
34983500
fn channel_penalty_msat(&self, candidate: &CandidateRouteHop,
3499-
_usage: ChannelUsage, _score_params: &Self::ScoreParams) -> u64
3501+
_usage: ChannelUsage, _score_params: &crate::routing::scoring::ProbabilisticScoringFeeParameters) -> u64
35003502
{
35013503
let mut cur_id = self.our_node_id;
35023504
for i in 0..self.hop_ids.len() {
@@ -6994,8 +6996,9 @@ mod tests {
69946996
fn write<W: Writer>(&self, _w: &mut W) -> Result<(), crate::io::Error> { unimplemented!() }
69956997
}
69966998
impl ScoreLookUp for BadChannelScorer {
6999+
#[cfg(not(c_bindings))]
69977000
type ScoreParams = ();
6998-
fn channel_penalty_msat(&self, candidate: &CandidateRouteHop, _: ChannelUsage, _score_params:&Self::ScoreParams) -> u64 {
7001+
fn channel_penalty_msat(&self, candidate: &CandidateRouteHop, _: ChannelUsage, _score_params: &crate::routing::scoring::ProbabilisticScoringFeeParameters) -> u64 {
69997002
if candidate.short_channel_id() == Some(self.short_channel_id) { u64::max_value() } else { 0 }
70007003
}
70017004
}
@@ -7010,8 +7013,9 @@ mod tests {
70107013
}
70117014

70127015
impl ScoreLookUp for BadNodeScorer {
7016+
#[cfg(not(c_bindings))]
70137017
type ScoreParams = ();
7014-
fn channel_penalty_msat(&self, candidate: &CandidateRouteHop, _: ChannelUsage, _score_params:&Self::ScoreParams) -> u64 {
7018+
fn channel_penalty_msat(&self, candidate: &CandidateRouteHop, _: ChannelUsage, _score_params: &crate::routing::scoring::ProbabilisticScoringFeeParameters) -> u64 {
70157019
if candidate.target() == Some(self.node_id) { u64::max_value() } else { 0 }
70167020
}
70177021
}
@@ -8803,7 +8807,7 @@ pub(crate) mod bench_utils {
88038807
}
88048808

88058809
pub(crate) fn generate_test_routes<S: ScoreLookUp + ScoreUpdate>(graph: &NetworkGraph<&TestLogger>, scorer: &mut S,
8806-
score_params: &S::ScoreParams, features: Bolt11InvoiceFeatures, mut seed: u64,
8810+
score_params: &crate::routing::scoring::ProbabilisticScoringFeeParameters, features: Bolt11InvoiceFeatures, mut seed: u64,
88078811
starting_amount: u64, route_count: usize,
88088812
) -> Vec<(ChannelDetails, PaymentParameters, u64)> {
88098813
let payer = payer_pubkey();
@@ -8933,7 +8937,7 @@ pub mod benches {
89338937

89348938
fn generate_routes<S: ScoreLookUp + ScoreUpdate>(
89358939
bench: &mut Criterion, graph: &NetworkGraph<&TestLogger>, mut scorer: S,
8936-
score_params: &S::ScoreParams, features: Bolt11InvoiceFeatures, starting_amount: u64,
8940+
score_params: &crate::routing::scoring::ProbabilisticScoringFeeParameters, features: Bolt11InvoiceFeatures, starting_amount: u64,
89378941
bench_name: &'static str,
89388942
) {
89398943
// First, get 100 (source, destination) pairs for which route-getting actually succeeds...

lightning/src/routing/scoring.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,13 @@ macro_rules! define_score { ($($supertrait: path)*) => {
8888
///
8989
/// Scoring is in terms of fees willing to be paid in order to avoid routing through a channel.
9090
pub trait ScoreLookUp {
91+
#[cfg(not(c_bindings))]
9192
/// A configurable type which should contain various passed-in parameters for configuring the scorer,
9293
/// on a per-routefinding-call basis through to the scorer methods,
9394
/// which are used to determine the parameters for the suitability of channels for use.
95+
///
96+
/// Note that due to limitations in many other languages' generics features, language bindings
97+
/// use [`ProbabilisticScoringFeeParameters`] for the parameters on all scorers.
9498
type ScoreParams;
9599
/// Returns the fee in msats willing to be paid to avoid routing `send_amt_msat` through the
96100
/// given channel in the direction from `source` to `target`.
@@ -101,7 +105,7 @@ pub trait ScoreLookUp {
101105
/// [`u64::max_value`] is given to indicate sufficient capacity for the invoice's full amount.
102106
/// Thus, implementations should be overflow-safe.
103107
fn channel_penalty_msat(
104-
&self, candidate: &CandidateRouteHop, usage: ChannelUsage, score_params: &Self::ScoreParams
108+
&self, candidate: &CandidateRouteHop, usage: ChannelUsage, score_params: &ProbabilisticScoringFeeParameters
105109
) -> u64;
106110
}
107111

@@ -139,9 +143,10 @@ impl<T: ScoreLookUp + ScoreUpdate $(+ $supertrait)*> Score for T {}
139143

140144
#[cfg(not(c_bindings))]
141145
impl<S: ScoreLookUp, T: Deref<Target=S>> ScoreLookUp for T {
146+
#[cfg(not(c_bindings))]
142147
type ScoreParams = S::ScoreParams;
143148
fn channel_penalty_msat(
144-
&self, candidate: &CandidateRouteHop, usage: ChannelUsage, score_params: &Self::ScoreParams
149+
&self, candidate: &CandidateRouteHop, usage: ChannelUsage, score_params: &ProbabilisticScoringFeeParameters
145150
) -> u64 {
146151
self.deref().channel_penalty_msat(candidate, usage, score_params)
147152
}
@@ -322,8 +327,9 @@ impl<'a, T: 'a + Score> Deref for MultiThreadedScoreLockRead<'a, T> {
322327

323328
#[cfg(c_bindings)]
324329
impl<'a, T: Score> ScoreLookUp for MultiThreadedScoreLockRead<'a, T> {
330+
#[cfg(not(c_bindings))]
325331
type ScoreParams = T::ScoreParams;
326-
fn channel_penalty_msat(&self, candidate:&CandidateRouteHop, usage: ChannelUsage, score_params: &Self::ScoreParams
332+
fn channel_penalty_msat(&self, candidate:&CandidateRouteHop, usage: ChannelUsage, score_params: &ProbabilisticScoringFeeParameters
327333
) -> u64 {
328334
self.0.channel_penalty_msat(candidate, usage, score_params)
329335
}
@@ -404,8 +410,9 @@ impl FixedPenaltyScorer {
404410
}
405411

406412
impl ScoreLookUp for FixedPenaltyScorer {
413+
#[cfg(not(c_bindings))]
407414
type ScoreParams = ();
408-
fn channel_penalty_msat(&self, _: &CandidateRouteHop, _: ChannelUsage, _score_params: &Self::ScoreParams) -> u64 {
415+
fn channel_penalty_msat(&self, _: &CandidateRouteHop, _: ChannelUsage, _score_params: &ProbabilisticScoringFeeParameters) -> u64 {
409416
self.penalty_msat
410417
}
411418
}
@@ -1476,6 +1483,7 @@ DirectedChannelLiquidity<L, HT, T> {
14761483
}
14771484

14781485
impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> ScoreLookUp for ProbabilisticScorer<G, L> where L::Target: Logger {
1486+
#[cfg(not(c_bindings))]
14791487
type ScoreParams = ProbabilisticScoringFeeParameters;
14801488
fn channel_penalty_msat(
14811489
&self, candidate: &CandidateRouteHop, usage: ChannelUsage, score_params: &ProbabilisticScoringFeeParameters

lightning/src/util/test_utils.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ pub struct TestRouter<'a> {
121121
&'a TestLogger,
122122
Arc<RandomBytes>,
123123
&'a RwLock<TestScorer>,
124-
(),
125-
TestScorer,
126124
>,
127125
pub network_graph: Arc<NetworkGraph<&'a TestLogger>>,
128126
pub next_routes: Mutex<VecDeque<(RouteParameters, Option<Result<Route, LightningError>>)>>,
@@ -1469,9 +1467,10 @@ impl crate::util::ser::Writeable for TestScorer {
14691467
}
14701468

14711469
impl ScoreLookUp for TestScorer {
1470+
#[cfg(not(c_bindings))]
14721471
type ScoreParams = ();
14731472
fn channel_penalty_msat(
1474-
&self, candidate: &CandidateRouteHop, usage: ChannelUsage, _score_params: &Self::ScoreParams
1473+
&self, candidate: &CandidateRouteHop, usage: ChannelUsage, _score_params: &crate::routing::scoring::ProbabilisticScoringFeeParameters
14751474
) -> u64 {
14761475
let short_channel_id = match candidate.globally_unique_short_channel_id() {
14771476
Some(scid) => scid,

0 commit comments

Comments
 (0)