Skip to content

Commit a4df16f

Browse files
committed
wip
1 parent d378023 commit a4df16f

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

lightning/src/routing/scoring.rs

+49-2
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,19 @@ where L::Target: Logger {
477477
channel_liquidities: HashMap<u64, ChannelLiquidity>,
478478
}
479479

480+
pub struct ChannelLiquidities(HashMap<u64, ChannelLiquidity>);
481+
482+
impl Readable for ChannelLiquidities {
483+
#[inline]
484+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
485+
let mut channel_liquidities = new_hash_map();
486+
read_tlv_fields!(r, {
487+
(0, channel_liquidities, required),
488+
});
489+
Ok(ChannelLiquidities(channel_liquidities))
490+
}
491+
}
492+
480493
/// Parameters for configuring [`ProbabilisticScorer`].
481494
///
482495
/// Used to configure base, liquidity, and amount penalties, the sum of which comprises the channel
@@ -804,7 +817,7 @@ impl ProbabilisticScoringDecayParameters {
804817
/// first node in the ordering of the channel's counterparties. Thus, swapping the two liquidity
805818
/// offset fields gives the opposite direction.
806819
#[repr(C)] // Force the fields in memory to be in the order we specify
807-
struct ChannelLiquidity {
820+
pub struct ChannelLiquidity {
808821
/// Lower channel liquidity bound in terms of an offset from zero.
809822
min_liquidity_offset_msat: u64,
810823

@@ -814,7 +827,7 @@ struct ChannelLiquidity {
814827
liquidity_history: HistoricalLiquidityTracker,
815828

816829
/// Time when either liquidity bound was last modified as an offset since the unix epoch.
817-
last_updated: Duration,
830+
pub last_updated: Duration,
818831

819832
/// Time when the historical liquidity bounds were last modified as an offset against the unix
820833
/// epoch.
@@ -853,6 +866,21 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> ProbabilisticScorer<G, L> whe
853866
}
854867
}
855868

869+
/// Merge external channel liquidity data into the internal state.
870+
pub fn merge(&mut self, other: ChannelLiquidities) {
871+
872+
let channel_liquidities = &mut self.channel_liquidities;
873+
874+
for (id, item) in other.0 {
875+
match channel_liquidities.get_mut(&id) {
876+
None => { channel_liquidities.insert(id, item); },
877+
Some(current) => {
878+
current.merge(&item);
879+
}
880+
}
881+
}
882+
}
883+
856884
#[cfg(test)]
857885
fn with_channel(mut self, short_channel_id: u64, liquidity: ChannelLiquidity) -> Self {
858886
assert!(self.channel_liquidities.insert(short_channel_id, liquidity).is_none());
@@ -1074,6 +1102,12 @@ impl ChannelLiquidity {
10741102
}
10751103
}
10761104

1105+
fn merge(&mut self, other: &Self) {
1106+
self.liquidity_history.merge(&other.liquidity_history);
1107+
1108+
// TODO: Merge other fields.
1109+
}
1110+
10771111
/// Returns a view of the channel liquidity directed from `source` to `target` assuming
10781112
/// `capacity_msat`.
10791113
fn as_directed(
@@ -1805,6 +1839,13 @@ mod bucketed_history {
18051839
self.buckets[bucket] = self.buckets[bucket].saturating_add(BUCKET_FIXED_POINT_ONE);
18061840
}
18071841
}
1842+
1843+
/// Returns the average of the buckets between the two trackers.
1844+
pub(crate) fn merge(&mut self, other: &Self) -> () {
1845+
for (index, bucket) in self.buckets.iter_mut().enumerate() {
1846+
*bucket = (*bucket + other.buckets[index]) / 2;
1847+
}
1848+
}
18081849
}
18091850

18101851
impl_writeable_tlv_based!(HistoricalBucketRangeTracker, { (0, buckets, required) });
@@ -1901,6 +1942,12 @@ mod bucketed_history {
19011942
-> DirectedHistoricalLiquidityTracker<&'a mut HistoricalLiquidityTracker> {
19021943
DirectedHistoricalLiquidityTracker { source_less_than_target, tracker: self }
19031944
}
1945+
1946+
pub fn merge(&mut self, other: &Self) {
1947+
self.min_liquidity_offset_history.merge(&other.min_liquidity_offset_history);
1948+
self.max_liquidity_offset_history.merge(&other.max_liquidity_offset_history);
1949+
self.recalculate_valid_point_count();
1950+
}
19041951
}
19051952

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

0 commit comments

Comments
 (0)