Skip to content

Commit

Permalink
Reintroduce remainder
Browse files Browse the repository at this point in the history
  • Loading branch information
thaarok committed Nov 8, 2024
1 parent 637f7f7 commit e05967e
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion contracts/sfc/SFC.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ contract SFC is Initializable, Ownable, Version {
struct AverageUptime {
// average uptime ratio as a value between 0 and 1e18
uint64 averageUptime;
// remainder from the division in the average calculation
uint32 remainder;
// number of epochs in the average (at most averageUptimeEpochsWindow)
uint32 epochs;
}
Expand Down Expand Up @@ -960,7 +962,10 @@ contract SFC is Initializable, Ownable, Version {
// the number of elements the average is calculated from
uint128 n = prev.epochs + 1;
// use lemma to add new value into the average
cur.averageUptime = uint64(((n - 1) * uint128(prev.averageUptime) + uint128(newValue)) / n);
uint128 tmp = (n - 1) * uint128(prev.averageUptime) + uint128(newValue) + prev.remainder;

cur.averageUptime = uint64(tmp / n);
cur.remainder = uint32(tmp % n);

if (cur.averageUptime > Decimal.unit()) {
cur.averageUptime = uint64(Decimal.unit());
Expand Down

0 comments on commit e05967e

Please sign in to comment.