Skip to content

Commit 72f9cf2

Browse files
Add ScoreProducer trait
This allows users to create their own structs that implement scoring methods. When these methods receive updates or called, they execute the user's customized function and update metadata in a customized way.
1 parent fe966f8 commit 72f9cf2

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

lightning/src/routing/network_graph.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use chain::chaininterface::{ChainError, ChainWatchInterface};
1313
use ln::features::{ChannelFeatures, NodeFeatures};
1414
use ln::msgs::{DecodeError,ErrorAction,LightningError,RoutingMessageHandler,NetAddress};
1515
use ln::msgs;
16-
use routing::router::RouteHop;
1716
use util::ser::{Writeable, Readable, Writer};
1817
use util::logger::Logger;
1918

@@ -428,15 +427,30 @@ impl Readable for NodeInfo {
428427
}
429428
}
430429

431-
/// Users store custom channel metadata separately. This trait is implemented by users, so that NetworkGraph
432-
/// objects can call into them to update the metadata and retrieve a minimum fee penalty when doing the
433-
/// route-finding algorithm.
430+
/// Represents custom methods used to update channel scores based on user stored metadata. By
431+
/// default, these methods do nothing. Users may customize these methods.
432+
pub trait ScoreProducer {
433+
/// Default method for tracking PaymentFailed events
434+
fn score_payment_failure(&self) {}
435+
/// Default method for tracking PaymentSent events
436+
fn score_payment_success(&self) {}
437+
}
438+
439+
/// Users store custom channel metadata separately. This trait is implemented by users, who may use
440+
/// the NetworkGraph to update whatever metadata they are storing about their view of the network.
434441
pub trait ChannelScorer {
435442
/// Holds information about a given channel that is to be updated when the user wishes to update
436443
/// the metadata they are tracking (eg metrics for reliability, uptime, floppiness, etc.)
437444
type Metadata;
438-
/// Returns penalty fee for a given channel ID based on user's channel metadata
439-
fn calculate_minimum_fee_penalty(&self, _channel_id: u64) -> u64 { 0 }
445+
/// Any struct that implements the ScoreProducer trait. By default, these methods do not do
446+
/// anything, but a user could create a list of customized ScorerProducers that implement select
447+
/// methods of a given trait.
448+
type ScorerProducer: ScoreProducer;
449+
/// Return score for a given channel by using user-defined channel_scorers
450+
fn calculate_minimum_fee_penalty(&self, channel_scorers: Vec<Self::ScorerProducer>, channel: Self::Metadata) -> u64;
451+
/// Adds a ScoreProducer struct to user metadata. These are overriden customizations of the
452+
/// default methods provided by rust-lightning.
453+
fn add_scorer_producer(&self, score_producer: Self::ScorerProducer);
440454
}
441455

442456
/// Represents the network as nodes and channels between them

lightning/src/routing/router.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use chain::chaininterface::ChainWatchInterface;
99
use ln::channelmanager;
1010
use ln::features::{ChannelFeatures, NodeFeatures};
1111
use ln::msgs::{DecodeError,ErrorAction,LightningError};
12-
use routing::network_graph::{NetGraphMsgHandler, RoutingFees, ChannelScorer};
12+
use routing::network_graph::{NetGraphMsgHandler, RoutingFees};
1313
use util::ser::{Writeable, Readable};
1414
use util::logger::Logger;
1515

@@ -104,12 +104,6 @@ impl Readable for Route {
104104
}
105105
}
106106

107-
/// Another default implementation of ChannelScorer. The use case is a user processing the
108-
/// success/failure of a given Route. They can implement these functions to fit their needs.
109-
impl ChannelScorer for Route {
110-
type Metadata = Option<u8>;
111-
}
112-
113107
/// A channel descriptor which provides a last-hop route to get_route
114108
pub struct RouteHint {
115109
/// The node_id of the non-target end of the route

0 commit comments

Comments
 (0)