Skip to content

Commit 6791c5b

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 b2cb387 commit 6791c5b

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
@@ -1075,7 +1075,7 @@ impl<L: DerefMut<Target = u64>, BRT: DerefMut<Target = HistoricalBucketRangeTrac
10751075
log_trace!(logger, "Max liquidity of {} is {} (already less than or equal to {})",
10761076
chan_descr, existing_max_msat, amount_msat);
10771077
}
1078-
self.update_history_buckets();
1078+
self.update_history_buckets(0);
10791079
}
10801080

10811081
/// Adjusts the channel liquidity balance bounds when failing to route `amount_msat` downstream.
@@ -1088,18 +1088,22 @@ impl<L: DerefMut<Target = u64>, BRT: DerefMut<Target = HistoricalBucketRangeTrac
10881088
log_trace!(logger, "Min liquidity of {} is {} (already greater than or equal to {})",
10891089
chan_descr, existing_min_msat, amount_msat);
10901090
}
1091-
self.update_history_buckets();
1091+
self.update_history_buckets(0);
10921092
}
10931093

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

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

11091113
let min_liquidity_offset_msat = self.decayed_offset_msat(*self.min_liquidity_offset_msat);
11101114
self.min_liquidity_offset_history.track_datapoint(
1111-
min_liquidity_offset_msat, self.capacity_msat
1115+
min_liquidity_offset_msat + bucket_offset_msat, self.capacity_msat
11121116
);
11131117
let max_liquidity_offset_msat = self.decayed_offset_msat(*self.max_liquidity_offset_msat);
11141118
self.max_liquidity_offset_history.track_datapoint(
1115-
max_liquidity_offset_msat, self.capacity_msat
1119+
max_liquidity_offset_msat.saturating_sub(bucket_offset_msat), self.capacity_msat
11161120
);
11171121
}
11181122

0 commit comments

Comments
 (0)