Skip to content

Commit 9606fa1

Browse files
committed
Allow combining of Prob Scorers
1 parent 6f58072 commit 9606fa1

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

lightning/src/routing/scoring.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,20 @@ struct HistoricalBucketRangeTracker {
650650

651651
impl HistoricalBucketRangeTracker {
652652
fn new() -> Self { Self { buckets: [0; 8] } }
653+
654+
/// Returns the average of the buckets between the two trackers.
655+
pub(crate) fn combine(mut self, other: Self) -> Self {
656+
let mut buckets: [u16; 8] = [0; 8];
657+
for i in 0..8 {
658+
let current = self.buckets[i];
659+
let other = other.buckets[i];
660+
buckets[i] = (current + other) / 2;
661+
}
662+
663+
self.buckets = buckets;
664+
self
665+
}
666+
653667
fn track_datapoint(&mut self, liquidity_offset_msat: u64, capacity_msat: u64) {
654668
// We have 8 leaky buckets for min and max liquidity. Each bucket tracks the amount of time
655669
// we spend in each bucket as a 16-bit fixed-point number with a 5 bit fractional part.
@@ -787,6 +801,7 @@ impl HistoricalMinMaxBuckets<'_> {
787801
/// Direction is defined in terms of [`NodeId`] partial ordering, where the source node is the
788802
/// first node in the ordering of the channel's counterparties. Thus, swapping the two liquidity
789803
/// offset fields gives the opposite direction.
804+
#[derive(Clone)]
790805
struct ChannelLiquidity<T: Time> {
791806
/// Lower channel liquidity bound in terms of an offset from zero.
792807
min_liquidity_offset_msat: u64,
@@ -801,6 +816,21 @@ struct ChannelLiquidity<T: Time> {
801816
max_liquidity_offset_history: HistoricalBucketRangeTracker,
802817
}
803818

819+
impl<T: Time> ChannelLiquidity<T> {
820+
pub(crate) fn combine(mut self, other: Self) -> Self {
821+
self.min_liquidity_offset_msat = self.min_liquidity_offset_msat.max(other.min_liquidity_offset_msat);
822+
self.max_liquidity_offset_msat = self.max_liquidity_offset_msat.min(other.max_liquidity_offset_msat);
823+
self.min_liquidity_offset_history.combine(other.min_liquidity_offset_history);
824+
self.max_liquidity_offset_history.combine(other.max_liquidity_offset_history);
825+
826+
if self.last_updated.duration_since(other.last_updated) < Duration::from_secs(0) {
827+
self.last_updated = other.last_updated;
828+
}
829+
830+
self
831+
}
832+
}
833+
804834
/// A snapshot of [`ChannelLiquidity`] in one direction assuming a certain channel capacity and
805835
/// decayed with a given half life.
806836
struct DirectedChannelLiquidity<L: Deref<Target = u64>, BRT: Deref<Target = HistoricalBucketRangeTracker>, T: Time, U: Deref<Target = T>> {
@@ -942,6 +972,27 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ProbabilisticScorerU
942972
}
943973
None
944974
}
975+
976+
/// Combines two `ProbabilisticScorerUsingTime` into one.
977+
///
978+
/// This merges the channel liquidity information of both scorers.
979+
pub fn combine(mut self, other: Self) -> Self {
980+
let mut channel_liquidities = self.channel_liquidities;
981+
982+
for (id, item) in other.channel_liquidities {
983+
match channel_liquidities.get(&id) {
984+
None => { channel_liquidities.insert(id, item); },
985+
Some(current) => {
986+
let liquidity = current.clone();
987+
channel_liquidities.insert(id, liquidity.combine(item));
988+
}
989+
}
990+
}
991+
992+
self.channel_liquidities = channel_liquidities;
993+
994+
self
995+
}
945996
}
946997

947998
impl<T: Time> ChannelLiquidity<T> {

0 commit comments

Comments
 (0)