Skip to content

Commit b9cd00d

Browse files
committed
Track the last time we received new information about a channel
In the next commit we'll enable users to assign a penalty to channels which we don't have any recent information for to better enable probing diversity. In order to do so, we need to actually track the last time we received new information for a channel, which we do here.
1 parent 46bf812 commit b9cd00d

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

lightning/src/routing/scoring.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,7 @@ struct DummyLiquidity {
887887
c: HistoricalLiquidityTracker,
888888
d: Duration,
889889
e: Duration,
890+
f: Duration,
890891
}
891892

892893
/// The amount of padding required to make [`ChannelLiquidity`] (plus a u64) a full 4 cache lines.
@@ -915,6 +916,10 @@ struct ChannelLiquidity {
915916
/// epoch.
916917
offset_history_last_updated: Duration,
917918

919+
/// The last time when the liquidity bounds were updated with new payment information (i.e.
920+
/// ignoring decays).
921+
last_datapoint: Duration,
922+
918923
_padding: [u64; LIQ_PADDING_LEN],
919924
}
920925

@@ -930,7 +935,7 @@ struct ChannelLiquidity {
930935
//
931936
// The next two cache lines will have the historical points, which we only access last during
932937
// scoring, followed by the last_updated `Duration`s (which we do not need during scoring). The
933-
// extra padding brings us up to a clean four cache lines.
938+
// `last_datapoint` `Duration` and extra padding bring us up to a clean 4 cache lines.
934939
const _LIQUIDITY_MAP_SIZING_CHECK: usize = 256 - ::core::mem::size_of::<(u64, ChannelLiquidity)>();
935940
const _LIQUIDITY_MAP_SIZING_CHECK_2: usize = ::core::mem::size_of::<(u64, ChannelLiquidity)>() - 256;
936941

@@ -942,6 +947,7 @@ struct DirectedChannelLiquidity<L: Deref<Target = u64>, HT: Deref<Target = Histo
942947
capacity_msat: u64,
943948
last_updated: T,
944949
offset_history_last_updated: T,
950+
last_datapoint: T,
945951
}
946952

947953
impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> ProbabilisticScorer<G, L> where L::Target: Logger {
@@ -1184,6 +1190,7 @@ impl ChannelLiquidity {
11841190
liquidity_history: HistoricalLiquidityTracker::new(),
11851191
last_updated,
11861192
offset_history_last_updated: last_updated,
1193+
last_datapoint: last_updated,
11871194
_padding: [0; LIQ_PADDING_LEN],
11881195
}
11891196
}
@@ -1217,6 +1224,7 @@ impl ChannelLiquidity {
12171224
capacity_msat,
12181225
last_updated: &self.last_updated,
12191226
offset_history_last_updated: &self.offset_history_last_updated,
1227+
last_datapoint: &self.last_datapoint,
12201228
}
12211229
}
12221230

@@ -1240,6 +1248,7 @@ impl ChannelLiquidity {
12401248
capacity_msat,
12411249
last_updated: &mut self.last_updated,
12421250
offset_history_last_updated: &mut self.offset_history_last_updated,
1251+
last_datapoint: &mut self.last_datapoint,
12431252
}
12441253
}
12451254

@@ -1586,6 +1595,7 @@ DirectedChannelLiquidity<L, HT, T> {
15861595
*self.max_liquidity_offset_msat = 0;
15871596
}
15881597
*self.last_updated = duration_since_epoch;
1598+
*self.last_datapoint = duration_since_epoch;
15891599
}
15901600

15911601
/// Adjusts the upper bound of the channel liquidity balance in this direction.
@@ -1595,6 +1605,7 @@ DirectedChannelLiquidity<L, HT, T> {
15951605
*self.min_liquidity_offset_msat = 0;
15961606
}
15971607
*self.last_updated = duration_since_epoch;
1608+
*self.last_datapoint = duration_since_epoch;
15981609
}
15991610
}
16001611

@@ -2404,6 +2415,7 @@ impl Writeable for ChannelLiquidity {
24042415
(5, self.liquidity_history.writeable_min_offset_history(), required),
24052416
(7, self.liquidity_history.writeable_max_offset_history(), required),
24062417
(9, self.offset_history_last_updated, required),
2418+
(11, self.last_datapoint, required),
24072419
});
24082420
Ok(())
24092421
}
@@ -2420,6 +2432,7 @@ impl Readable for ChannelLiquidity {
24202432
let mut max_liquidity_offset_history: Option<HistoricalBucketRangeTracker> = None;
24212433
let mut last_updated = Duration::from_secs(0);
24222434
let mut offset_history_last_updated = None;
2435+
let mut last_datapoint = None;
24232436
read_tlv_fields!(r, {
24242437
(0, min_liquidity_offset_msat, required),
24252438
(1, legacy_min_liq_offset_history, option),
@@ -2429,6 +2442,7 @@ impl Readable for ChannelLiquidity {
24292442
(5, min_liquidity_offset_history, option),
24302443
(7, max_liquidity_offset_history, option),
24312444
(9, offset_history_last_updated, option),
2445+
(11, last_datapoint, option),
24322446
});
24332447

24342448
if min_liquidity_offset_history.is_none() {
@@ -2453,6 +2467,7 @@ impl Readable for ChannelLiquidity {
24532467
),
24542468
last_updated,
24552469
offset_history_last_updated: offset_history_last_updated.unwrap_or(last_updated),
2470+
last_datapoint: last_datapoint.unwrap_or(last_updated),
24562471
_padding: [0; LIQ_PADDING_LEN],
24572472
})
24582473
}
@@ -2627,20 +2642,21 @@ mod tests {
26272642
let logger = TestLogger::new();
26282643
let last_updated = Duration::ZERO;
26292644
let offset_history_last_updated = Duration::ZERO;
2645+
let last_datapoint = Duration::ZERO;
26302646
let network_graph = network_graph(&logger);
26312647
let decay_params = ProbabilisticScoringDecayParameters::default();
26322648
let mut scorer = ProbabilisticScorer::new(decay_params, &network_graph, &logger)
26332649
.with_channel(42,
26342650
ChannelLiquidity {
26352651
min_liquidity_offset_msat: 700, max_liquidity_offset_msat: 100,
2636-
last_updated, offset_history_last_updated,
2652+
last_updated, offset_history_last_updated, last_datapoint,
26372653
liquidity_history: HistoricalLiquidityTracker::new(),
26382654
_padding: [0; LIQ_PADDING_LEN],
26392655
})
26402656
.with_channel(43,
26412657
ChannelLiquidity {
26422658
min_liquidity_offset_msat: 700, max_liquidity_offset_msat: 100,
2643-
last_updated, offset_history_last_updated,
2659+
last_updated, offset_history_last_updated, last_datapoint,
26442660
liquidity_history: HistoricalLiquidityTracker::new(),
26452661
_padding: [0; LIQ_PADDING_LEN],
26462662
});
@@ -2708,13 +2724,14 @@ mod tests {
27082724
let logger = TestLogger::new();
27092725
let last_updated = Duration::ZERO;
27102726
let offset_history_last_updated = Duration::ZERO;
2727+
let last_datapoint = Duration::ZERO;
27112728
let network_graph = network_graph(&logger);
27122729
let decay_params = ProbabilisticScoringDecayParameters::default();
27132730
let mut scorer = ProbabilisticScorer::new(decay_params, &network_graph, &logger)
27142731
.with_channel(42,
27152732
ChannelLiquidity {
27162733
min_liquidity_offset_msat: 200, max_liquidity_offset_msat: 400,
2717-
last_updated, offset_history_last_updated,
2734+
last_updated, offset_history_last_updated, last_datapoint,
27182735
liquidity_history: HistoricalLiquidityTracker::new(),
27192736
_padding: [0; LIQ_PADDING_LEN],
27202737
});
@@ -2769,13 +2786,14 @@ mod tests {
27692786
let logger = TestLogger::new();
27702787
let last_updated = Duration::ZERO;
27712788
let offset_history_last_updated = Duration::ZERO;
2789+
let last_datapoint = Duration::ZERO;
27722790
let network_graph = network_graph(&logger);
27732791
let decay_params = ProbabilisticScoringDecayParameters::default();
27742792
let mut scorer = ProbabilisticScorer::new(decay_params, &network_graph, &logger)
27752793
.with_channel(42,
27762794
ChannelLiquidity {
27772795
min_liquidity_offset_msat: 200, max_liquidity_offset_msat: 400,
2778-
last_updated, offset_history_last_updated,
2796+
last_updated, offset_history_last_updated, last_datapoint,
27792797
liquidity_history: HistoricalLiquidityTracker::new(),
27802798
_padding: [0; LIQ_PADDING_LEN],
27812799
});
@@ -2882,6 +2900,7 @@ mod tests {
28822900
let logger = TestLogger::new();
28832901
let last_updated = Duration::ZERO;
28842902
let offset_history_last_updated = Duration::ZERO;
2903+
let last_datapoint = Duration::ZERO;
28852904
let network_graph = network_graph(&logger);
28862905
let params = ProbabilisticScoringFeeParameters {
28872906
liquidity_penalty_multiplier_msat: 1_000,
@@ -2895,7 +2914,7 @@ mod tests {
28952914
.with_channel(42,
28962915
ChannelLiquidity {
28972916
min_liquidity_offset_msat: 40, max_liquidity_offset_msat: 40,
2898-
last_updated, offset_history_last_updated,
2917+
last_updated, offset_history_last_updated, last_datapoint,
28992918
liquidity_history: HistoricalLiquidityTracker::new(),
29002919
_padding: [0; LIQ_PADDING_LEN],
29012920
});

0 commit comments

Comments
 (0)