Skip to content

Commit 54f2266

Browse files
committed
Pipe Duration-based time information through scoring pipeline
In the coming commits, the `T: Time` bound on `ProbabilisticScorer` will be removed. In order to enable that, we need to pass the current time (as a `Duration` since the unix epoch) through the score updating pipeline, allowing us to keep the `*last_updated_time` fields up-to-date as we go.
1 parent ca1dcb6 commit 54f2266

File tree

1 file changed

+41
-27
lines changed

1 file changed

+41
-27
lines changed

lightning/src/routing/scoring.rs

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,9 @@ impl<T: Time> ChannelLiquidity<T> {
10651065
}
10661066
}
10671067

1068-
fn decayed_offset(&self, offset: u64, decay_params: ProbabilisticScoringDecayParameters) -> u64 {
1068+
fn decayed_offset(&self, offset: u64, duration_since_epoch: Duration,
1069+
decay_params: ProbabilisticScoringDecayParameters
1070+
) -> u64 {
10691071
let half_life = decay_params.liquidity_offset_half_life.as_secs();
10701072
if half_life != 0 {
10711073
let elapsed_time = T::now().duration_since(self.last_updated).as_secs() as f64;
@@ -1267,44 +1269,50 @@ impl<L: Deref<Target = u64>, BRT: Deref<Target = HistoricalBucketRangeTracker>,
12671269

12681270
impl<L: DerefMut<Target = u64>, BRT: DerefMut<Target = HistoricalBucketRangeTracker>, T: Time, U: DerefMut<Target = T>> DirectedChannelLiquidity<L, BRT, T, U> {
12691271
/// Adjusts the channel liquidity balance bounds when failing to route `amount_msat`.
1270-
fn failed_at_channel<Log: Deref>(&mut self, amount_msat: u64, chan_descr: fmt::Arguments, logger: &Log) where Log::Target: Logger {
1272+
fn failed_at_channel<Log: Deref>(
1273+
&mut self, amount_msat: u64, duration_since_epoch: Duration, chan_descr: fmt::Arguments, logger: &Log
1274+
) where Log::Target: Logger {
12711275
let existing_max_msat = self.max_liquidity_msat();
12721276
if amount_msat < existing_max_msat {
12731277
log_debug!(logger, "Setting max liquidity of {} from {} to {}", chan_descr, existing_max_msat, amount_msat);
1274-
self.set_max_liquidity_msat(amount_msat);
1278+
self.set_max_liquidity_msat(amount_msat, duration_since_epoch);
12751279
} else {
12761280
log_trace!(logger, "Max liquidity of {} is {} (already less than or equal to {})",
12771281
chan_descr, existing_max_msat, amount_msat);
12781282
}
1279-
self.update_history_buckets(0);
1283+
self.update_history_buckets(0, duration_since_epoch);
12801284
}
12811285

12821286
/// Adjusts the channel liquidity balance bounds when failing to route `amount_msat` downstream.
1283-
fn failed_downstream<Log: Deref>(&mut self, amount_msat: u64, chan_descr: fmt::Arguments, logger: &Log) where Log::Target: Logger {
1287+
fn failed_downstream<Log: Deref>(
1288+
&mut self, amount_msat: u64, duration_since_epoch: Duration, chan_descr: fmt::Arguments, logger: &Log
1289+
) where Log::Target: Logger {
12841290
let existing_min_msat = self.min_liquidity_msat();
12851291
if amount_msat > existing_min_msat {
12861292
log_debug!(logger, "Setting min liquidity of {} from {} to {}", existing_min_msat, chan_descr, amount_msat);
1287-
self.set_min_liquidity_msat(amount_msat);
1293+
self.set_min_liquidity_msat(amount_msat, duration_since_epoch);
12881294
} else {
12891295
log_trace!(logger, "Min liquidity of {} is {} (already greater than or equal to {})",
12901296
chan_descr, existing_min_msat, amount_msat);
12911297
}
1292-
self.update_history_buckets(0);
1298+
self.update_history_buckets(0, duration_since_epoch);
12931299
}
12941300

12951301
/// Adjusts the channel liquidity balance bounds when successfully routing `amount_msat`.
1296-
fn successful<Log: Deref>(&mut self, amount_msat: u64, chan_descr: fmt::Arguments, logger: &Log) where Log::Target: Logger {
1302+
fn successful<Log: Deref>(&mut self,
1303+
amount_msat: u64, duration_since_epoch: Duration, chan_descr: fmt::Arguments, logger: &Log
1304+
) where Log::Target: Logger {
12971305
let max_liquidity_msat = self.max_liquidity_msat().checked_sub(amount_msat).unwrap_or(0);
12981306
log_debug!(logger, "Subtracting {} from max liquidity of {} (setting it to {})", amount_msat, chan_descr, max_liquidity_msat);
1299-
self.set_max_liquidity_msat(max_liquidity_msat);
1300-
self.update_history_buckets(amount_msat);
1307+
self.set_max_liquidity_msat(max_liquidity_msat, duration_since_epoch);
1308+
self.update_history_buckets(amount_msat, duration_since_epoch);
13011309
}
13021310

13031311
/// Updates the history buckets for this channel. Because the history buckets track what we now
13041312
/// know about the channel's state *prior to our payment* (i.e. what we assume is "steady
13051313
/// state"), we allow the caller to set an offset applied to our liquidity bounds which
13061314
/// represents the amount of the successful payment we just made.
1307-
fn update_history_buckets(&mut self, bucket_offset_msat: u64) {
1315+
fn update_history_buckets(&mut self, bucket_offset_msat: u64, duration_since_epoch: Duration) {
13081316
let half_lives = self.now.duration_since(*self.offset_history_last_updated).as_secs()
13091317
.checked_div(self.decay_params.historical_no_updates_half_life.as_secs())
13101318
.map(|v| v.try_into().unwrap_or(u32::max_value())).unwrap_or(u32::max_value());
@@ -1323,7 +1331,7 @@ impl<L: DerefMut<Target = u64>, BRT: DerefMut<Target = HistoricalBucketRangeTrac
13231331
}
13241332

13251333
/// Adjusts the lower bound of the channel liquidity balance in this direction.
1326-
fn set_min_liquidity_msat(&mut self, amount_msat: u64) {
1334+
fn set_min_liquidity_msat(&mut self, amount_msat: u64, duration_since_epoch: Duration) {
13271335
*self.min_liquidity_offset_msat = amount_msat;
13281336
*self.max_liquidity_offset_msat = if amount_msat > self.max_liquidity_msat() {
13291337
0
@@ -1334,7 +1342,7 @@ impl<L: DerefMut<Target = u64>, BRT: DerefMut<Target = HistoricalBucketRangeTrac
13341342
}
13351343

13361344
/// Adjusts the upper bound of the channel liquidity balance in this direction.
1337-
fn set_max_liquidity_msat(&mut self, amount_msat: u64) {
1345+
fn set_max_liquidity_msat(&mut self, amount_msat: u64, duration_since_epoch: Duration) {
13381346
*self.max_liquidity_offset_msat = self.capacity_msat.checked_sub(amount_msat).unwrap_or(0);
13391347
*self.min_liquidity_offset_msat = if amount_msat < self.min_liquidity_msat() {
13401348
0
@@ -1390,7 +1398,7 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreLookUp for Prob
13901398
}
13911399

13921400
impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for ProbabilisticScorerUsingTime<G, L, T> where L::Target: Logger {
1393-
fn payment_path_failed(&mut self, path: &Path, short_channel_id: u64, _duration_since_epoch: Duration) {
1401+
fn payment_path_failed(&mut self, path: &Path, short_channel_id: u64, duration_since_epoch: Duration) {
13941402
let amount_msat = path.final_value_msat();
13951403
log_trace!(self.logger, "Scoring path through to SCID {} as having failed at {} msat", short_channel_id, amount_msat);
13961404
let network_graph = self.network_graph.read_only();
@@ -1413,13 +1421,15 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for Prob
14131421
.entry(hop.short_channel_id)
14141422
.or_insert_with(ChannelLiquidity::new)
14151423
.as_directed_mut(source, &target, capacity_msat, self.decay_params)
1416-
.failed_at_channel(amount_msat, format_args!("SCID {}, towards {:?}", hop.short_channel_id, target), &self.logger);
1424+
.failed_at_channel(amount_msat, duration_since_epoch,
1425+
format_args!("SCID {}, towards {:?}", hop.short_channel_id, target), &self.logger);
14171426
} else {
14181427
self.channel_liquidities
14191428
.entry(hop.short_channel_id)
14201429
.or_insert_with(ChannelLiquidity::new)
14211430
.as_directed_mut(source, &target, capacity_msat, self.decay_params)
1422-
.failed_downstream(amount_msat, format_args!("SCID {}, towards {:?}", hop.short_channel_id, target), &self.logger);
1431+
.failed_downstream(amount_msat, duration_since_epoch,
1432+
format_args!("SCID {}, towards {:?}", hop.short_channel_id, target), &self.logger);
14231433
}
14241434
} else {
14251435
log_debug!(self.logger, "Not able to penalize channel with SCID {} as we do not have graph info for it (likely a route-hint last-hop).",
@@ -1429,7 +1439,7 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for Prob
14291439
}
14301440
}
14311441

1432-
fn payment_path_successful(&mut self, path: &Path, _duration_since_epoch: Duration) {
1442+
fn payment_path_successful(&mut self, path: &Path, duration_since_epoch: Duration) {
14331443
let amount_msat = path.final_value_msat();
14341444
log_trace!(self.logger, "Scoring path through SCID {} as having succeeded at {} msat.",
14351445
path.hops.split_last().map(|(hop, _)| hop.short_channel_id).unwrap_or(0), amount_msat);
@@ -1447,7 +1457,8 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for Prob
14471457
.entry(hop.short_channel_id)
14481458
.or_insert_with(ChannelLiquidity::new)
14491459
.as_directed_mut(source, &target, capacity_msat, self.decay_params)
1450-
.successful(amount_msat, format_args!("SCID {}, towards {:?}", hop.short_channel_id, target), &self.logger);
1460+
.successful(amount_msat, duration_since_epoch,
1461+
format_args!("SCID {}, towards {:?}", hop.short_channel_id, target), &self.logger);
14511462
} else {
14521463
log_debug!(self.logger, "Not able to learn for channel with SCID {} as we do not have graph info for it (likely a route-hint last-hop).",
14531464
hop.short_channel_id);
@@ -1463,12 +1474,15 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for Prob
14631474
self.payment_path_failed(path, u64::max_value(), duration_since_epoch)
14641475
}
14651476

1466-
fn decay_liquidity_certainty(&mut self, _duration_since_epoch: Duration) {
1477+
fn decay_liquidity_certainty(&mut self, duration_since_epoch: Duration) {
14671478
let decay_params = self.decay_params;
14681479
self.channel_liquidities.retain(|_scid, liquidity| {
1469-
liquidity.min_liquidity_offset_msat = liquidity.decayed_offset(liquidity.min_liquidity_offset_msat, decay_params);
1470-
liquidity.max_liquidity_offset_msat = liquidity.decayed_offset(liquidity.max_liquidity_offset_msat, decay_params);
1480+
liquidity.min_liquidity_offset_msat =
1481+
liquidity.decayed_offset(liquidity.min_liquidity_offset_msat, duration_since_epoch, decay_params);
1482+
liquidity.max_liquidity_offset_msat =
1483+
liquidity.decayed_offset(liquidity.max_liquidity_offset_msat, duration_since_epoch, decay_params);
14711484
liquidity.last_updated = T::now();
1485+
14721486
let elapsed_time =
14731487
T::now().duration_since(liquidity.offset_history_last_updated);
14741488
if elapsed_time > decay_params.historical_no_updates_half_life {
@@ -2404,7 +2418,7 @@ mod tests {
24042418

24052419
scorer.channel_liquidities.get_mut(&42).unwrap()
24062420
.as_directed_mut(&source, &target, 1_000, decay_params)
2407-
.set_min_liquidity_msat(200);
2421+
.set_min_liquidity_msat(200, Duration::ZERO);
24082422

24092423
let liquidity = scorer.channel_liquidities.get(&42).unwrap()
24102424
.as_directed(&source, &target, 1_000, decay_params);
@@ -2430,7 +2444,7 @@ mod tests {
24302444

24312445
scorer.channel_liquidities.get_mut(&43).unwrap()
24322446
.as_directed_mut(&target, &recipient, 1_000, decay_params)
2433-
.set_max_liquidity_msat(200);
2447+
.set_max_liquidity_msat(200, Duration::ZERO);
24342448

24352449
let liquidity = scorer.channel_liquidities.get(&43).unwrap()
24362450
.as_directed(&target, &recipient, 1_000, decay_params);
@@ -2476,7 +2490,7 @@ mod tests {
24762490
// Reset from source to target.
24772491
scorer.channel_liquidities.get_mut(&42).unwrap()
24782492
.as_directed_mut(&source, &target, 1_000, decay_params)
2479-
.set_min_liquidity_msat(900);
2493+
.set_min_liquidity_msat(900, Duration::ZERO);
24802494

24812495
let liquidity = scorer.channel_liquidities.get(&42).unwrap()
24822496
.as_directed(&source, &target, 1_000, decay_params);
@@ -2491,7 +2505,7 @@ mod tests {
24912505
// Reset from target to source.
24922506
scorer.channel_liquidities.get_mut(&42).unwrap()
24932507
.as_directed_mut(&target, &source, 1_000, decay_params)
2494-
.set_min_liquidity_msat(400);
2508+
.set_min_liquidity_msat(400, Duration::ZERO);
24952509

24962510
let liquidity = scorer.channel_liquidities.get(&42).unwrap()
24972511
.as_directed(&source, &target, 1_000, decay_params);
@@ -2537,7 +2551,7 @@ mod tests {
25372551
// Reset from source to target.
25382552
scorer.channel_liquidities.get_mut(&42).unwrap()
25392553
.as_directed_mut(&source, &target, 1_000, decay_params)
2540-
.set_max_liquidity_msat(300);
2554+
.set_max_liquidity_msat(300, Duration::ZERO);
25412555

25422556
let liquidity = scorer.channel_liquidities.get(&42).unwrap()
25432557
.as_directed(&source, &target, 1_000, decay_params);
@@ -2552,7 +2566,7 @@ mod tests {
25522566
// Reset from target to source.
25532567
scorer.channel_liquidities.get_mut(&42).unwrap()
25542568
.as_directed_mut(&target, &source, 1_000, decay_params)
2555-
.set_max_liquidity_msat(600);
2569+
.set_max_liquidity_msat(600, Duration::ZERO);
25562570

25572571
let liquidity = scorer.channel_liquidities.get(&42).unwrap()
25582572
.as_directed(&source, &target, 1_000, decay_params);

0 commit comments

Comments
 (0)