Skip to content

Commit 1733f0a

Browse files
committed
Track "steady-state" channel balances in history buckets not live
The lower-bound of the scoring history buckets generally never get used - if we try to send a payment and it fails, we don't learn a new lower-bound for the liquidity of a channel, and if we successfully send a payment we only learn a lower-bound that applied *before* we sent the payment, not after it completed. If we assume channels have some "steady-state" liquidity, then tracking our liquidity estimates *after* a payment doesn't really make sense - we're not super likely to make a second payment across the same channel immediately (or, if we are, we can use our un-decayed liquidity estimates for that). By the time we do go to use the same channel again, we'd assume that its back at its "steady-state" and the impacts of our payment have been lost. To combat both of these effects, here we "subtract" the impact of any just-successful payments from our liquidity estimates prior to updating the historical buckets.
1 parent 9ed46fc commit 1733f0a

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

lightning/src/routing/scoring.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ impl<L: DerefMut<Target = u64>, BRT: DerefMut<Target = HistoricalBucketRangeTrac
10391039
log_trace!(logger, "Max liquidity of {} is {} (already less than or equal to {})",
10401040
chan_descr, existing_max_msat, amount_msat);
10411041
}
1042-
self.update_history_buckets();
1042+
self.update_history_buckets(0);
10431043
}
10441044

10451045
/// Adjusts the channel liquidity balance bounds when failing to route `amount_msat` downstream.
@@ -1052,18 +1052,22 @@ impl<L: DerefMut<Target = u64>, BRT: DerefMut<Target = HistoricalBucketRangeTrac
10521052
log_trace!(logger, "Min liquidity of {} is {} (already greater than or equal to {})",
10531053
chan_descr, existing_min_msat, amount_msat);
10541054
}
1055-
self.update_history_buckets();
1055+
self.update_history_buckets(0);
10561056
}
10571057

10581058
/// Adjusts the channel liquidity balance bounds when successfully routing `amount_msat`.
10591059
fn successful<Log: Deref>(&mut self, amount_msat: u64, chan_descr: fmt::Arguments, logger: &Log) where Log::Target: Logger {
10601060
let max_liquidity_msat = self.max_liquidity_msat().checked_sub(amount_msat).unwrap_or(0);
10611061
log_debug!(logger, "Subtracting {} from max liquidity of {} (setting it to {})", amount_msat, chan_descr, max_liquidity_msat);
10621062
self.set_max_liquidity_msat(max_liquidity_msat);
1063-
self.update_history_buckets();
1063+
self.update_history_buckets(amount_msat);
10641064
}
10651065

1066-
fn update_history_buckets(&mut self) {
1066+
/// Updates the history buckets for this channel. Because the history buckets track what we now
1067+
/// know about the channel's state *prior to our payment* (i.e. what we assume is "steady
1068+
/// state"), we allow the caller to set an offset applied to our liquidity bounds which
1069+
/// represents the amount of the successful payment we just made.
1070+
fn update_history_buckets(&mut self, bucket_offset_msat: u64) {
10671071
let half_lives = self.now.duration_since(*self.last_updated).as_secs()
10681072
.checked_div(self.params.historical_no_updates_half_life.as_secs())
10691073
.map(|v| v.try_into().unwrap_or(u32::max_value())).unwrap_or(u32::max_value());
@@ -1072,11 +1076,11 @@ impl<L: DerefMut<Target = u64>, BRT: DerefMut<Target = HistoricalBucketRangeTrac
10721076

10731077
let min_liquidity_offset_msat = self.decayed_offset_msat(*self.min_liquidity_offset_msat);
10741078
self.min_liquidity_offset_history.track_datapoint(
1075-
min_liquidity_offset_msat, self.capacity_msat
1079+
min_liquidity_offset_msat + bucket_offset_msat, self.capacity_msat
10761080
);
10771081
let max_liquidity_offset_msat = self.decayed_offset_msat(*self.max_liquidity_offset_msat);
10781082
self.max_liquidity_offset_history.track_datapoint(
1079-
max_liquidity_offset_msat, self.capacity_msat
1083+
max_liquidity_offset_msat.saturating_sub(bucket_offset_msat), self.capacity_msat
10801084
);
10811085
}
10821086

0 commit comments

Comments
 (0)