Skip to content

Commit 813c5ff

Browse files
committed
split Score into ScoreUpdate and ScoreLookUp
1 parent 63c334f commit 813c5ff

File tree

4 files changed

+173
-180
lines changed

4 files changed

+173
-180
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use lightning::ln::peer_handler::APeerManager;
3434
use lightning::routing::gossip::{NetworkGraph, P2PGossipSync};
3535
use lightning::routing::utxo::UtxoLookup;
3636
use lightning::routing::router::Router;
37-
use lightning::routing::scoring::{Score, WriteableScore};
37+
use lightning::routing::scoring::{ScoreUpdate, WriteableScore};
3838
use lightning::util::logger::Logger;
3939
use lightning::util::persist::Persister;
4040
#[cfg(feature = "std")]
@@ -858,7 +858,7 @@ mod tests {
858858
use lightning::ln::peer_handler::{PeerManager, MessageHandler, SocketDescriptor, IgnoringMessageHandler};
859859
use lightning::routing::gossip::{NetworkGraph, NodeId, P2PGossipSync};
860860
use lightning::routing::router::{DefaultRouter, Path, RouteHop};
861-
use lightning::routing::scoring::{ChannelUsage, Score};
861+
use lightning::routing::scoring::{ChannelUsage, ScoreUpdate, ScoreLookUp};
862862
use lightning::util::config::UserConfig;
863863
use lightning::util::ser::Writeable;
864864
use lightning::util::test_utils;
@@ -1033,12 +1033,14 @@ mod tests {
10331033
fn write<W: lightning::util::ser::Writer>(&self, _: &mut W) -> Result<(), lightning::io::Error> { Ok(()) }
10341034
}
10351035

1036-
impl Score for TestScorer {
1036+
impl ScoreLookUp for TestScorer {
10371037
type ScoreParams = ();
10381038
fn channel_penalty_msat(
10391039
&self, _short_channel_id: u64, _source: &NodeId, _target: &NodeId, _usage: ChannelUsage, _score_params: &Self::ScoreParams
10401040
) -> u64 { unimplemented!(); }
1041+
}
10411042

1043+
impl ScoreUpdate for TestScorer {
10421044
fn payment_path_failed(&mut self, actual_path: &Path, actual_short_channel_id: u64) {
10431045
if let Some(expectations) = &mut self.event_expectations {
10441046
match expectations.pop_front().unwrap() {
@@ -1804,4 +1806,5 @@ mod tests {
18041806
r1.unwrap().unwrap();
18051807
r2.unwrap()
18061808
}
1809+
18071810
}

lightning/src/routing/router.rs

Lines changed: 24 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::ln::features::{Bolt11InvoiceFeatures, Bolt12InvoiceFeatures, ChannelF
2020
use crate::ln::msgs::{DecodeError, ErrorAction, LightningError, MAX_VALUE_MSAT};
2121
use crate::offers::invoice::{BlindedPayInfo, Bolt12Invoice};
2222
use crate::routing::gossip::{DirectedChannelInfo, EffectiveCapacity, ReadOnlyNetworkGraph, NetworkGraph, NodeId, RoutingFees};
23-
use crate::routing::scoring::{ChannelUsage, LockableScore, Score};
23+
use crate::routing::scoring::{ChannelUsage, LockableScore, ScoreLookUp};
2424
use crate::util::ser::{Writeable, Readable, ReadableArgs, Writer};
2525
use crate::util::logger::{Level, Logger};
2626
use crate::util::chacha20::ChaCha20;
@@ -33,9 +33,9 @@ use core::{cmp, fmt};
3333
use core::ops::Deref;
3434

3535
/// A [`Router`] implemented using [`find_route`].
36-
pub struct DefaultRouter<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref, SP: Sized, Sc: Score<ScoreParams = SP>> where
36+
pub struct DefaultRouter<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>> where
3737
L::Target: Logger,
38-
S::Target: for <'a> LockableScore<'a, Score = Sc>,
38+
S::Target: for <'a> LockableScore<'a, ScoreLookUp = Sc>,
3939
{
4040
network_graph: G,
4141
logger: L,
@@ -44,9 +44,9 @@ pub struct DefaultRouter<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref,
4444
score_params: SP
4545
}
4646

47-
impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref, SP: Sized, Sc: Score<ScoreParams = SP>> DefaultRouter<G, L, S, SP, Sc> where
47+
impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>> DefaultRouter<G, L, S, SP, Sc> where
4848
L::Target: Logger,
49-
S::Target: for <'a> LockableScore<'a, Score = Sc>,
49+
S::Target: for <'a> LockableScore<'a, ScoreLookUp = Sc>,
5050
{
5151
/// Creates a new router.
5252
pub fn new(network_graph: G, logger: L, random_seed_bytes: [u8; 32], scorer: S, score_params: SP) -> Self {
@@ -55,9 +55,9 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref, SP: Sized, Sc: Scor
5555
}
5656
}
5757

58-
impl< G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref, SP: Sized, Sc: Score<ScoreParams = SP>> Router for DefaultRouter<G, L, S, SP, Sc> where
58+
impl< G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>> Router for DefaultRouter<G, L, S, SP, Sc> where
5959
L::Target: Logger,
60-
S::Target: for <'a> LockableScore<'a, Score = Sc>,
60+
S::Target: for <'a> LockableScore<'a, ScoreLookUp = Sc>,
6161
{
6262
fn find_route(
6363
&self,
@@ -106,18 +106,18 @@ pub trait Router {
106106
}
107107
}
108108

109-
/// [`Score`] implementation that factors in in-flight HTLC liquidity.
109+
/// [`ScoreLookUp`] implementation that factors in in-flight HTLC liquidity.
110110
///
111-
/// Useful for custom [`Router`] implementations to wrap their [`Score`] on-the-fly when calling
111+
/// Useful for custom [`Router`] implementations to wrap their [`ScoreLookUp`] on-the-fly when calling
112112
/// [`find_route`].
113113
///
114-
/// [`Score`]: crate::routing::scoring::Score
115-
pub struct ScorerAccountingForInFlightHtlcs<'a, SP: Sized, Sc: 'a + Score<ScoreParams = SP>, S: LockableScore<'a, Score = Sc> + ?Sized> {
114+
/// [`ScoreLookUp`]: crate::routing::scoring::ScoreLookUp
115+
pub struct ScorerAccountingForInFlightHtlcs<'a, SP: Sized, Sc: 'a + ScoreLookUp<ScoreParams = SP>, S: LockableScore<'a, ScoreLookUp = Sc> + ?Sized> {
116116
scorer: &'a S,
117117
// Maps a channel's short channel id and its direction to the liquidity used up.
118118
inflight_htlcs: &'a InFlightHtlcs,
119119
}
120-
impl<'a, SP: Sized, Sc: Score<ScoreParams = SP>, S: LockableScore<'a, Score = Sc> + ?Sized> ScorerAccountingForInFlightHtlcs<'a, SP, Sc, S> {
120+
impl<'a, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>, S: LockableScore<'a, ScoreLookUp = Sc> + ?Sized> ScorerAccountingForInFlightHtlcs<'a, SP, Sc, S> {
121121
/// Initialize a new `ScorerAccountingForInFlightHtlcs`.
122122
pub fn new(scorer: &'a S, inflight_htlcs: &'a InFlightHtlcs) -> Self {
123123
ScorerAccountingForInFlightHtlcs {
@@ -128,11 +128,11 @@ impl<'a, SP: Sized, Sc: Score<ScoreParams = SP>, S: LockableScore<'a, Score = Sc
128128
}
129129

130130
#[cfg(c_bindings)]
131-
impl<'a, SP: Sized, Sc: Score<ScoreParams = SP>, S: LockableScore<'a, Score = Sc> + ?Sized> Writeable for ScorerAccountingForInFlightHtlcs<'a, SP, Sc, S> {
131+
impl<'a, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>, S: LockableScore<'a, ScoreLookUp = Sc> + ?Sized> Writeable for ScorerAccountingForInFlightHtlcs<'a, SP, Sc, S> {
132132
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> { self.scorer.write_lock().write(writer) }
133133
}
134134

135-
impl<'a, SP: Sized, Sc: 'a + Score<ScoreParams = SP>, S: LockableScore<'a, Score = Sc> + ?Sized> Score for ScorerAccountingForInFlightHtlcs<'a, SP, Sc, S> {
135+
impl<'a, SP: Sized, Sc: 'a + ScoreLookUp<ScoreParams = SP>, S: LockableScore<'a, ScoreLookUp = Sc> + ?Sized> ScoreLookUp for ScorerAccountingForInFlightHtlcs<'a, SP, Sc, S> {
136136
type ScoreParams = Sc::ScoreParams;
137137
fn channel_penalty_msat(&self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage, score_params: &Self::ScoreParams) -> u64 {
138138
if let Some(used_liquidity) = self.inflight_htlcs.used_liquidity_msat(
@@ -148,22 +148,6 @@ impl<'a, SP: Sized, Sc: 'a + Score<ScoreParams = SP>, S: LockableScore<'a, Score
148148
self.scorer.read_lock().channel_penalty_msat(short_channel_id, source, target, usage, score_params)
149149
}
150150
}
151-
152-
fn payment_path_failed(&mut self, path: &Path, short_channel_id: u64) {
153-
self.scorer.write_lock().payment_path_failed(path, short_channel_id)
154-
}
155-
156-
fn payment_path_successful(&mut self, path: &Path) {
157-
self.scorer.write_lock().payment_path_successful(path)
158-
}
159-
160-
fn probe_failed(&mut self, path: &Path, short_channel_id: u64) {
161-
self.scorer.write_lock().probe_failed(path, short_channel_id)
162-
}
163-
164-
fn probe_successful(&mut self, path: &Path) {
165-
self.scorer.write_lock().probe_successful(path)
166-
}
167151
}
168152

169153
/// A data structure for tracking in-flight HTLCs. May be used during pathfinding to account for
@@ -1409,7 +1393,7 @@ fn sort_first_hop_channels(
14091393
/// [`ChannelManager::list_usable_channels`]: crate::ln::channelmanager::ChannelManager::list_usable_channels
14101394
/// [`Event::PaymentPathFailed`]: crate::events::Event::PaymentPathFailed
14111395
/// [`NetworkGraph`]: crate::routing::gossip::NetworkGraph
1412-
pub fn find_route<L: Deref, GL: Deref, S: Score>(
1396+
pub fn find_route<L: Deref, GL: Deref, S: ScoreLookUp>(
14131397
our_node_pubkey: &PublicKey, route_params: &RouteParameters,
14141398
network_graph: &NetworkGraph<GL>, first_hops: Option<&[&ChannelDetails]>, logger: L,
14151399
scorer: &S, score_params: &S::ScoreParams, random_seed_bytes: &[u8; 32]
@@ -1423,7 +1407,7 @@ where L::Target: Logger, GL::Target: Logger {
14231407
Ok(route)
14241408
}
14251409

1426-
pub(crate) fn get_route<L: Deref, S: Score>(
1410+
pub(crate) fn get_route<L: Deref, S: ScoreLookUp>(
14271411
our_node_pubkey: &PublicKey, payment_params: &PaymentParameters, network_graph: &ReadOnlyNetworkGraph,
14281412
first_hops: Option<&[&ChannelDetails]>, final_value_msat: u64, logger: L, scorer: &S, score_params: &S::ScoreParams,
14291413
_random_seed_bytes: &[u8; 32]
@@ -2613,7 +2597,7 @@ fn build_route_from_hops_internal<L: Deref>(
26132597
hop_ids: [Option<NodeId>; MAX_PATH_LENGTH_ESTIMATE as usize],
26142598
}
26152599

2616-
impl Score for HopScorer {
2600+
impl ScoreLookUp for HopScorer {
26172601
type ScoreParams = ();
26182602
fn channel_penalty_msat(&self, _short_channel_id: u64, source: &NodeId, target: &NodeId,
26192603
_usage: ChannelUsage, _score_params: &Self::ScoreParams) -> u64
@@ -2631,14 +2615,6 @@ fn build_route_from_hops_internal<L: Deref>(
26312615
}
26322616
u64::max_value()
26332617
}
2634-
2635-
fn payment_path_failed(&mut self, _path: &Path, _short_channel_id: u64) {}
2636-
2637-
fn payment_path_successful(&mut self, _path: &Path) {}
2638-
2639-
fn probe_failed(&mut self, _path: &Path, _short_channel_id: u64) {}
2640-
2641-
fn probe_successful(&mut self, _path: &Path) {}
26422618
}
26432619

26442620
impl<'a> Writeable for HopScorer {
@@ -2672,7 +2648,7 @@ mod tests {
26722648
use crate::routing::router::{get_route, build_route_from_hops_internal, add_random_cltv_offset, default_node_features,
26732649
BlindedTail, InFlightHtlcs, Path, PaymentParameters, Route, RouteHint, RouteHintHop, RouteHop, RoutingFees,
26742650
DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA, MAX_PATH_LENGTH_ESTIMATE};
2675-
use crate::routing::scoring::{ChannelUsage, FixedPenaltyScorer, Score, ProbabilisticScorer, ProbabilisticScoringFeeParameters, ProbabilisticScoringDecayParameters};
2651+
use crate::routing::scoring::{ChannelUsage, FixedPenaltyScorer, ScoreLookUp, ProbabilisticScorer, ProbabilisticScoringFeeParameters, ProbabilisticScoringDecayParameters};
26762652
use crate::routing::test_utils::{add_channel, add_or_update_node, build_graph, build_line_graph, id_to_feature_flags, get_nodes, update_channel};
26772653
use crate::chain::transaction::OutPoint;
26782654
use crate::sign::EntropySource;
@@ -5720,16 +5696,11 @@ mod tests {
57205696
impl Writeable for BadChannelScorer {
57215697
fn write<W: Writer>(&self, _w: &mut W) -> Result<(), crate::io::Error> { unimplemented!() }
57225698
}
5723-
impl Score for BadChannelScorer {
5699+
impl ScoreLookUp for BadChannelScorer {
57245700
type ScoreParams = ();
57255701
fn channel_penalty_msat(&self, short_channel_id: u64, _: &NodeId, _: &NodeId, _: ChannelUsage, _score_params:&Self::ScoreParams) -> u64 {
57265702
if short_channel_id == self.short_channel_id { u64::max_value() } else { 0 }
57275703
}
5728-
5729-
fn payment_path_failed(&mut self, _path: &Path, _short_channel_id: u64) {}
5730-
fn payment_path_successful(&mut self, _path: &Path) {}
5731-
fn probe_failed(&mut self, _path: &Path, _short_channel_id: u64) {}
5732-
fn probe_successful(&mut self, _path: &Path) {}
57335704
}
57345705

57355706
struct BadNodeScorer {
@@ -5741,16 +5712,11 @@ mod tests {
57415712
fn write<W: Writer>(&self, _w: &mut W) -> Result<(), crate::io::Error> { unimplemented!() }
57425713
}
57435714

5744-
impl Score for BadNodeScorer {
5715+
impl ScoreLookUp for BadNodeScorer {
57455716
type ScoreParams = ();
57465717
fn channel_penalty_msat(&self, _: u64, _: &NodeId, target: &NodeId, _: ChannelUsage, _score_params:&Self::ScoreParams) -> u64 {
57475718
if *target == self.node_id { u64::max_value() } else { 0 }
57485719
}
5749-
5750-
fn payment_path_failed(&mut self, _path: &Path, _short_channel_id: u64) {}
5751-
fn payment_path_successful(&mut self, _path: &Path) {}
5752-
fn probe_failed(&mut self, _path: &Path, _short_channel_id: u64) {}
5753-
fn probe_successful(&mut self, _path: &Path) {}
57545720
}
57555721

57565722
#[test]
@@ -6721,6 +6687,7 @@ pub(crate) mod bench_utils {
67216687
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
67226688

67236689
use crate::chain::transaction::OutPoint;
6690+
use crate::routing::scoring::ScoreUpdate;
67246691
use crate::sign::{EntropySource, KeysManager};
67256692
use crate::ln::channelmanager::{self, ChannelCounterparty, ChannelDetails};
67266693
use crate::ln::features::Bolt11InvoiceFeatures;
@@ -6814,7 +6781,7 @@ pub(crate) mod bench_utils {
68146781
}
68156782
}
68166783

6817-
pub(crate) fn generate_test_routes<S: Score>(graph: &NetworkGraph<&TestLogger>, scorer: &mut S,
6784+
pub(crate) fn generate_test_routes<S: ScoreLookUp + ScoreUpdate>(graph: &NetworkGraph<&TestLogger>, scorer: &mut S,
68186785
score_params: &S::ScoreParams, features: Bolt11InvoiceFeatures, mut seed: u64,
68196786
starting_amount: u64, route_count: usize,
68206787
) -> Vec<(ChannelDetails, PaymentParameters, u64)> {
@@ -6894,6 +6861,7 @@ pub(crate) mod bench_utils {
68946861
#[cfg(ldk_bench)]
68956862
pub mod benches {
68966863
use super::*;
6864+
use crate::routing::scoring::{ScoreUpdate, ScoreLookUp};
68976865
use crate::sign::{EntropySource, KeysManager};
68986866
use crate::ln::channelmanager;
68996867
use crate::ln::features::Bolt11InvoiceFeatures;
@@ -6956,7 +6924,7 @@ pub mod benches {
69566924
"generate_large_mpp_routes_with_probabilistic_scorer");
69576925
}
69586926

6959-
fn generate_routes<S: Score>(
6927+
fn generate_routes<S: ScoreLookUp + ScoreUpdate>(
69606928
bench: &mut Criterion, graph: &NetworkGraph<&TestLogger>, mut scorer: S,
69616929
score_params: &S::ScoreParams, features: Bolt11InvoiceFeatures, starting_amount: u64,
69626930
bench_name: &'static str,

0 commit comments

Comments
 (0)