Skip to content

Commit c2572a1

Browse files
committed
f - Use a more accurate approximation
1 parent f68238b commit c2572a1

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

lightning/src/routing/scoring.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,16 +1156,17 @@ impl<L: Deref<Target = u64>, BRT: Deref<Target = HistoricalBucketRangeTracker>,
11561156
0
11571157
} else {
11581158
// Decay the offset by the appropriate number of half lives. If half of the next half
1159-
// life has passed, approximate an additional three-quarter life by summing the results
1160-
// of taking both the *next two* half lives instead. This helps smooth out the decay.
1159+
// life has passed, approximate an additional three-quarter life to help smooth out the
1160+
// decay.
11611161
let elapsed_time = self.now.duration_since(*self.last_updated).as_secs();
11621162
let half_decays = elapsed_time / (half_life / 2);
11631163
let decays = half_decays / 2;
11641164
let decayed_offset_msat = offset_msat.checked_shr(decays as u32).unwrap_or(0);
11651165
if half_decays % 2 == 0 {
11661166
decayed_offset_msat
11671167
} else {
1168-
(decayed_offset_msat >> 1) + (decayed_offset_msat >> 2)
1168+
// 71 / 100 ~= core::f64::consts::FRAC_1_SQRT_2
1169+
(decayed_offset_msat as u128 * 71 / 100) as u64
11691170
}
11701171
}
11711172
}
@@ -2442,11 +2443,11 @@ mod tests {
24422443
// Half decay (i.e., three-quarter life)
24432444
SinceEpoch::advance(Duration::from_secs(1));
24442445
let usage = ChannelUsage { amount_msat: 128, ..usage };
2445-
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage, &params), 18);
2446+
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage, &params), 22);
24462447
let usage = ChannelUsage { amount_msat: 256, ..usage };
2447-
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage, &params), 103);
2448+
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage, &params), 106);
24482449
let usage = ChannelUsage { amount_msat: 768, ..usage };
2449-
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage, &params), 957);
2450+
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage, &params), 916);
24502451
let usage = ChannelUsage { amount_msat: 896, ..usage };
24512452
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage, &params), u64::max_value());
24522453

0 commit comments

Comments
 (0)