Skip to content

Commit 16f7688

Browse files
committed
f - Use a more accurate approximation
1 parent 7cf67ea commit 16f7688

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

lightning/src/routing/scoring.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -1002,16 +1002,17 @@ impl<L: Deref<Target = u64>, BRT: Deref<Target = HistoricalBucketRangeTracker>,
10021002
0
10031003
} else {
10041004
// Decay the offset by the appropriate number of half lives. If half of the next half
1005-
// life has passed, approximate an additional three-quarter life by summing the results
1006-
// of taking both the *next two* half lives instead. This helps smooth out the decay.
1005+
// life has passed, approximate an additional three-quarter life to help smooth out the
1006+
// decay.
10071007
let elapsed_time = self.now.duration_since(*self.last_updated).as_secs();
10081008
let half_decays = elapsed_time / (half_life / 2);
10091009
let decays = half_decays / 2;
10101010
let decayed_offset_msat = offset_msat.checked_shr(decays as u32).unwrap_or(0);
10111011
if half_decays % 2 == 0 {
10121012
decayed_offset_msat
10131013
} else {
1014-
(decayed_offset_msat >> 1) + (decayed_offset_msat >> 2)
1014+
// 71 / 100 ~= core::f64::consts::FRAC_1_SQRT_2
1015+
(decayed_offset_msat as u128 * 71 / 100) as u64
10151016
}
10161017
}
10171018
}
@@ -2229,11 +2230,11 @@ mod tests {
22292230
// Half decay (i.e., three-quarter life)
22302231
SinceEpoch::advance(Duration::from_secs(1));
22312232
let usage = ChannelUsage { amount_msat: 128, ..usage };
2232-
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 18);
2233+
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 22);
22332234
let usage = ChannelUsage { amount_msat: 256, ..usage };
2234-
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 103);
2235+
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 106);
22352236
let usage = ChannelUsage { amount_msat: 768, ..usage };
2236-
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 957);
2237+
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 916);
22372238
let usage = ChannelUsage { amount_msat: 896, ..usage };
22382239
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), u64::max_value());
22392240

0 commit comments

Comments
 (0)