Skip to content

Commit

Permalink
Merge pull request #23 from movementlabsxyz/mikhail/fix-zero-divide-o…
Browse files Browse the repository at this point in the history
…n-long-epochs

fix: prevent zero divide on long epoch duration
  • Loading branch information
mzabaluev authored Jun 24, 2024
2 parents 9c86dde + 6ce927e commit 4925eb1
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions aptos-move/vm-genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,12 @@ fn initialize(
// Calculate the per-epoch rewards rate, represented as 2 separate ints (numerator and
// denominator).
let rewards_rate_denominator = 1_000_000_000;
let num_epochs_in_a_year = NUM_SECONDS_PER_YEAR / genesis_config.epoch_duration_secs;
// Multiplication before division to minimize rounding errors due to integer division.
let rewards_rate_numerator = (genesis_config.rewards_apy_percentage * rewards_rate_denominator
/ 100)
/ num_epochs_in_a_year;
// Fold division of the percentage by 100 into multiplication with the denominator
// to minimize rounding errors due to integer division.
let rewards_rate_numerator = genesis_config.rewards_apy_percentage
* (rewards_rate_denominator / 100)
* genesis_config.epoch_duration_secs
/ NUM_SECONDS_PER_YEAR;

// Block timestamps are in microseconds and epoch_interval is used to check if a block timestamp
// has crossed into a new epoch. So epoch_interval also needs to be in micro seconds.
Expand Down

0 comments on commit 4925eb1

Please sign in to comment.