Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions lightning/src/routing/scoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,10 @@ struct ChannelLiquidity {
/// Time when the historical liquidity bounds were last modified as an offset against the unix
/// epoch.
offset_history_last_updated: Duration,

/// The last time when the liquidity bounds were updated with new payment information (i.e.
/// ignoring decays).
last_datapoint_time: Duration,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we name this last_datapoint_duration_since_epoch to clarify the format since Duration is a ubiquitous type that doesn't tell us how time is measured?

Copy link
Contributor

@tnull tnull Apr 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL, I just realized this is exactly what @joostjager had you change the variable names from in his naming nits above.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I figured _time was pretty clear (what else could it possibly be?)

}

/// A snapshot of [`ChannelLiquidity`] in one direction assuming a certain channel capacity.
Expand All @@ -911,6 +915,7 @@ struct DirectedChannelLiquidity<L: Deref<Target = u64>, HT: Deref<Target = Histo
capacity_msat: u64,
last_updated: T,
offset_history_last_updated: T,
last_datapoint_time: T,
}

impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> ProbabilisticScorer<G, L> where L::Target: Logger {
Expand Down Expand Up @@ -1153,6 +1158,7 @@ impl ChannelLiquidity {
liquidity_history: HistoricalLiquidityTracker::new(),
last_updated,
offset_history_last_updated: last_updated,
last_datapoint_time: last_updated,
}
}

Expand Down Expand Up @@ -1185,6 +1191,7 @@ impl ChannelLiquidity {
capacity_msat,
last_updated: &self.last_updated,
offset_history_last_updated: &self.offset_history_last_updated,
last_datapoint_time: &self.last_datapoint_time,
}
}

Expand All @@ -1208,6 +1215,7 @@ impl ChannelLiquidity {
capacity_msat,
last_updated: &mut self.last_updated,
offset_history_last_updated: &mut self.offset_history_last_updated,
last_datapoint_time: &mut self.last_datapoint_time,
}
}

Expand Down Expand Up @@ -1554,6 +1562,7 @@ DirectedChannelLiquidity<L, HT, T> {
*self.max_liquidity_offset_msat = 0;
}
*self.last_updated = duration_since_epoch;
*self.last_datapoint_time = duration_since_epoch;
}

/// Adjusts the upper bound of the channel liquidity balance in this direction.
Expand All @@ -1563,6 +1572,7 @@ DirectedChannelLiquidity<L, HT, T> {
*self.min_liquidity_offset_msat = 0;
}
*self.last_updated = duration_since_epoch;
*self.last_datapoint_time = duration_since_epoch;
}
}

Expand Down Expand Up @@ -2372,6 +2382,7 @@ impl Writeable for ChannelLiquidity {
(5, self.liquidity_history.writeable_min_offset_history(), required),
(7, self.liquidity_history.writeable_max_offset_history(), required),
(9, self.offset_history_last_updated, required),
(11, self.last_datapoint_time, required),
});
Ok(())
}
Expand All @@ -2388,6 +2399,7 @@ impl Readable for ChannelLiquidity {
let mut max_liquidity_offset_history: Option<HistoricalBucketRangeTracker> = None;
let mut last_updated = Duration::from_secs(0);
let mut offset_history_last_updated = None;
let mut last_datapoint_time = None;
read_tlv_fields!(r, {
(0, min_liquidity_offset_msat, required),
(1, legacy_min_liq_offset_history, option),
Expand All @@ -2397,6 +2409,7 @@ impl Readable for ChannelLiquidity {
(5, min_liquidity_offset_history, option),
(7, max_liquidity_offset_history, option),
(9, offset_history_last_updated, option),
(11, last_datapoint_time, option),
});

if min_liquidity_offset_history.is_none() {
Expand All @@ -2421,6 +2434,7 @@ impl Readable for ChannelLiquidity {
),
last_updated,
offset_history_last_updated: offset_history_last_updated.unwrap_or(last_updated),
last_datapoint_time: last_datapoint_time.unwrap_or(last_updated),
})
}
}
Expand Down Expand Up @@ -2594,19 +2608,20 @@ mod tests {
let logger = TestLogger::new();
let last_updated = Duration::ZERO;
let offset_history_last_updated = Duration::ZERO;
let last_datapoint_time = Duration::ZERO;
let network_graph = network_graph(&logger);
let decay_params = ProbabilisticScoringDecayParameters::default();
let mut scorer = ProbabilisticScorer::new(decay_params, &network_graph, &logger)
.with_channel(42,
ChannelLiquidity {
min_liquidity_offset_msat: 700, max_liquidity_offset_msat: 100,
last_updated, offset_history_last_updated,
last_updated, offset_history_last_updated, last_datapoint_time,
liquidity_history: HistoricalLiquidityTracker::new(),
})
.with_channel(43,
ChannelLiquidity {
min_liquidity_offset_msat: 700, max_liquidity_offset_msat: 100,
last_updated, offset_history_last_updated,
last_updated, offset_history_last_updated, last_datapoint_time,
liquidity_history: HistoricalLiquidityTracker::new(),
});
let source = source_node_id();
Expand Down Expand Up @@ -2673,13 +2688,14 @@ mod tests {
let logger = TestLogger::new();
let last_updated = Duration::ZERO;
let offset_history_last_updated = Duration::ZERO;
let last_datapoint_time = Duration::ZERO;
let network_graph = network_graph(&logger);
let decay_params = ProbabilisticScoringDecayParameters::default();
let mut scorer = ProbabilisticScorer::new(decay_params, &network_graph, &logger)
.with_channel(42,
ChannelLiquidity {
min_liquidity_offset_msat: 200, max_liquidity_offset_msat: 400,
last_updated, offset_history_last_updated,
last_updated, offset_history_last_updated, last_datapoint_time,
liquidity_history: HistoricalLiquidityTracker::new(),
});
let source = source_node_id();
Expand Down Expand Up @@ -2733,13 +2749,14 @@ mod tests {
let logger = TestLogger::new();
let last_updated = Duration::ZERO;
let offset_history_last_updated = Duration::ZERO;
let last_datapoint_time = Duration::ZERO;
let network_graph = network_graph(&logger);
let decay_params = ProbabilisticScoringDecayParameters::default();
let mut scorer = ProbabilisticScorer::new(decay_params, &network_graph, &logger)
.with_channel(42,
ChannelLiquidity {
min_liquidity_offset_msat: 200, max_liquidity_offset_msat: 400,
last_updated, offset_history_last_updated,
last_updated, offset_history_last_updated, last_datapoint_time,
liquidity_history: HistoricalLiquidityTracker::new(),
});
let source = source_node_id();
Expand Down Expand Up @@ -2845,6 +2862,7 @@ mod tests {
let logger = TestLogger::new();
let last_updated = Duration::ZERO;
let offset_history_last_updated = Duration::ZERO;
let last_datapoint_time = Duration::ZERO;
let network_graph = network_graph(&logger);
let params = ProbabilisticScoringFeeParameters {
liquidity_penalty_multiplier_msat: 1_000,
Expand All @@ -2858,7 +2876,7 @@ mod tests {
.with_channel(42,
ChannelLiquidity {
min_liquidity_offset_msat: 40, max_liquidity_offset_msat: 40,
last_updated, offset_history_last_updated,
last_updated, offset_history_last_updated, last_datapoint_time,
liquidity_history: HistoricalLiquidityTracker::new(),
});
let source = source_node_id();
Expand Down