Skip to content

Commit be04a28

Browse files
committed
f - use floating-point operations for liquidity decay
1 parent feaed9b commit be04a28

File tree

1 file changed

+10
-41
lines changed

1 file changed

+10
-41
lines changed

lightning/src/routing/scoring.rs

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -997,30 +997,10 @@ impl<L: Deref<Target = u64>, BRT: Deref<Target = HistoricalBucketRangeTracker>,
997997
}
998998

999999
fn decayed_offset_msat(&self, offset_msat: u64) -> u64 {
1000-
let elapsed_time = self.now.duration_since(*self.last_updated).as_secs();
1001-
let half_life = self.params.liquidity_offset_half_life.as_secs();
1002-
1003-
match elapsed_time.checked_div(half_life) {
1004-
None => 0,
1005-
Some(decays) => match elapsed_time.checked_div(half_life / 2) {
1006-
None => 0,
1007-
Some(half_decays) => {
1008-
// Decay the offset by the appropriate number of half lives. If half of the next
1009-
// half life has passed, approximate an additional three-quarter life by summing
1010-
// the results of taking both the *next two* half lives instead. This helps
1011-
// smooth out the decay.
1012-
if half_decays % 2 == 0 {
1013-
offset_msat.checked_shr(decays as u32).unwrap_or(0)
1014-
} else {
1015-
offset_msat
1016-
.checked_shr((decays + 1) as u32)
1017-
.map(|decayed_offset_msat| decayed_offset_msat
1018-
+ offset_msat.checked_shr((decays + 2) as u32).unwrap_or(0))
1019-
.unwrap_or(0)
1020-
}
1021-
}
1022-
},
1023-
}
1000+
let elapsed_time = self.now.duration_since(*self.last_updated).as_secs() as f64;
1001+
let half_life = self.params.liquidity_offset_half_life.as_secs() as f64;
1002+
let decays = elapsed_time / half_life;
1003+
(0.5f64.powf(decays) * offset_msat as f64) as u64
10241004
}
10251005
}
10261006

@@ -2222,29 +2202,18 @@ mod tests {
22222202
let usage = ChannelUsage { amount_msat: 896, ..usage };
22232203
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), u64::max_value());
22242204

2225-
// No decay
2226-
SinceEpoch::advance(Duration::from_secs(4));
2227-
let usage = ChannelUsage { amount_msat: 128, ..usage };
2228-
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 0);
2229-
let usage = ChannelUsage { amount_msat: 256, ..usage };
2230-
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 93);
2231-
let usage = ChannelUsage { amount_msat: 768, ..usage };
2232-
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 1_479);
2233-
let usage = ChannelUsage { amount_msat: 896, ..usage };
2234-
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), u64::max_value());
2235-
2236-
// Half decay (i.e., three-quarter life)
2237-
SinceEpoch::advance(Duration::from_secs(1));
2205+
// Partial decay
2206+
SinceEpoch::advance(Duration::from_secs(5));
22382207
let usage = ChannelUsage { amount_msat: 128, ..usage };
2239-
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 18);
2208+
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 22);
22402209
let usage = ChannelUsage { amount_msat: 256, ..usage };
2241-
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 103);
2210+
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 106);
22422211
let usage = ChannelUsage { amount_msat: 768, ..usage };
2243-
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 957);
2212+
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 916);
22442213
let usage = ChannelUsage { amount_msat: 896, ..usage };
22452214
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), u64::max_value());
22462215

2247-
// One decay (i.e., half life)
2216+
// Full decay
22482217
SinceEpoch::advance(Duration::from_secs(5));
22492218
let usage = ChannelUsage { amount_msat: 64, ..usage };
22502219
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 0);

0 commit comments

Comments
 (0)