Skip to content

Commit 2ed21b8

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 da127d3 commit 2ed21b8

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
@@ -1070,7 +1070,7 @@ impl<L: DerefMut<Target = u64>, BRT: DerefMut<Target = HistoricalBucketRangeTrac
10701070
log_trace!(logger, "Max liquidity of {} is {} (already less than or equal to {})",
10711071
chan_descr, existing_max_msat, amount_msat);
10721072
}
1073-
self.update_history_buckets();
1073+
self.update_history_buckets(0);
10741074
}
10751075

10761076
/// Adjusts the channel liquidity balance bounds when failing to route `amount_msat` downstream.
@@ -1083,18 +1083,22 @@ impl<L: DerefMut<Target = u64>, BRT: DerefMut<Target = HistoricalBucketRangeTrac
10831083
log_trace!(logger, "Min liquidity of {} is {} (already greater than or equal to {})",
10841084
chan_descr, existing_min_msat, amount_msat);
10851085
}
1086-
self.update_history_buckets();
1086+
self.update_history_buckets(0);
10871087
}
10881088

10891089
/// Adjusts the channel liquidity balance bounds when successfully routing `amount_msat`.
10901090
fn successful<Log: Deref>(&mut self, amount_msat: u64, chan_descr: fmt::Arguments, logger: &Log) where Log::Target: Logger {
10911091
let max_liquidity_msat = self.max_liquidity_msat().checked_sub(amount_msat).unwrap_or(0);
10921092
log_debug!(logger, "Subtracting {} from max liquidity of {} (setting it to {})", amount_msat, chan_descr, max_liquidity_msat);
10931093
self.set_max_liquidity_msat(max_liquidity_msat);
1094-
self.update_history_buckets();
1094+
self.update_history_buckets(amount_msat);
10951095
}
10961096

1097-
fn update_history_buckets(&mut self) {
1097+
/// Updates the history buckets for this channel. Because the history buckets track what we now
1098+
/// know about the channel's state *prior to our payment* (i.e. what we assume is "steady
1099+
/// state"), we allow the caller to set an offset applied to our liquidity bounds which
1100+
/// represents the amount of the successful payment we just made.
1101+
fn update_history_buckets(&mut self, bucket_offset_msat: u64) {
10981102
let half_lives = self.now.duration_since(*self.last_updated).as_secs()
10991103
.checked_div(self.decay_params.historical_no_updates_half_life.as_secs())
11001104
.map(|v| v.try_into().unwrap_or(u32::max_value())).unwrap_or(u32::max_value());
@@ -1103,11 +1107,11 @@ impl<L: DerefMut<Target = u64>, BRT: DerefMut<Target = HistoricalBucketRangeTrac
11031107

11041108
let min_liquidity_offset_msat = self.decayed_offset_msat(*self.min_liquidity_offset_msat);
11051109
self.liquidity_history.min_liquidity_offset_history.track_datapoint(
1106-
min_liquidity_offset_msat, self.capacity_msat
1110+
min_liquidity_offset_msat + bucket_offset_msat, self.capacity_msat
11071111
);
11081112
let max_liquidity_offset_msat = self.decayed_offset_msat(*self.max_liquidity_offset_msat);
11091113
self.liquidity_history.max_liquidity_offset_history.track_datapoint(
1110-
max_liquidity_offset_msat, self.capacity_msat
1114+
max_liquidity_offset_msat.saturating_sub(bucket_offset_msat), self.capacity_msat
11111115
);
11121116
}
11131117

0 commit comments

Comments
 (0)