Skip to content

Commit 1c3f57a

Browse files
committed
add merge functions
1 parent bb4c43b commit 1c3f57a

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

lightning/src/routing/scoring.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,15 @@ impl ChannelLiquidity {
11301130
}
11311131
}
11321132

1133+
fn merge(&mut self, other: &Self) {
1134+
// Take average for min/max liquidity offsets.
1135+
self.min_liquidity_offset_msat = (self.min_liquidity_offset_msat + other.min_liquidity_offset_msat) / 2;
1136+
self.max_liquidity_offset_msat = (self.max_liquidity_offset_msat + other.max_liquidity_offset_msat) / 2;
1137+
1138+
// Merge historical liquidity data.
1139+
self.liquidity_history.merge(&other.liquidity_history);
1140+
}
1141+
11331142
/// Returns a view of the channel liquidity directed from `source` to `target` assuming
11341143
/// `capacity_msat`.
11351144
fn as_directed(
@@ -1672,13 +1681,11 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> CombinedScorer<G, L> where L:
16721681
pub fn merge(&mut self, external_scores: HashMap<u64, ChannelLiquidity>) {
16731682
let local_scores = &self.local_only_scorer.channel_liquidities;
16741683

1675-
for (scid, liquidity) in external_scores {
1684+
for (scid, mut liquidity) in external_scores {
16761685
if let Some(local_liquidity) = local_scores.get(&scid) {
1677-
let merged_liquidity = liquidity.merge(local_liquidity);
1678-
self.scorer.channel_liquidities.insert(scid, merged_liquidity);
1679-
} else {
1680-
self.scorer.channel_liquidities.insert(scid, liquidity);
1686+
liquidity.merge(local_liquidity);
16811687
}
1688+
self.scorer.channel_liquidities.insert(scid, liquidity);
16821689
}
16831690
}
16841691
}
@@ -1905,6 +1912,13 @@ mod bucketed_history {
19051912
self.buckets[bucket] = self.buckets[bucket].saturating_add(BUCKET_FIXED_POINT_ONE);
19061913
}
19071914
}
1915+
1916+
/// Returns the average of the buckets between the two trackers.
1917+
pub(crate) fn merge(&mut self, other: &Self) -> () {
1918+
for (index, bucket) in self.buckets.iter_mut().enumerate() {
1919+
*bucket = (*bucket + other.buckets[index]) / 2;
1920+
}
1921+
}
19081922
}
19091923

19101924
impl_writeable_tlv_based!(HistoricalBucketRangeTracker, { (0, buckets, required) });
@@ -2001,6 +2015,12 @@ mod bucketed_history {
20012015
-> DirectedHistoricalLiquidityTracker<&'a mut HistoricalLiquidityTracker> {
20022016
DirectedHistoricalLiquidityTracker { source_less_than_target, tracker: self }
20032017
}
2018+
2019+
pub fn merge(&mut self, other: &Self) {
2020+
self.min_liquidity_offset_history.merge(&other.min_liquidity_offset_history);
2021+
self.max_liquidity_offset_history.merge(&other.max_liquidity_offset_history);
2022+
self.recalculate_valid_point_count();
2023+
}
20042024
}
20052025

20062026
/// A set of buckets representing the history of where we've seen the minimum- and maximum-

0 commit comments

Comments
 (0)