Skip to content

Commit

Permalink
vm-genesis: prevent division by zero
Browse files Browse the repository at this point in the history
Rewrite the computation for rewards rate to avoid
division by zero when the epoch duration exceeds one year.
  • Loading branch information
mzabaluev committed Jun 24, 2024
1 parent 9c86dde commit 6ce927e
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 6ce927e

Please sign in to comment.