From 6ce927e5978016db9508015f6a1442f02e36e4a5 Mon Sep 17 00:00:00 2001 From: Mikhail Zabaluev Date: Mon, 24 Jun 2024 15:09:56 +0300 Subject: [PATCH] vm-genesis: prevent division by zero Rewrite the computation for rewards rate to avoid division by zero when the epoch duration exceeds one year. --- aptos-move/vm-genesis/src/lib.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/aptos-move/vm-genesis/src/lib.rs b/aptos-move/vm-genesis/src/lib.rs index 1dfe710dad109..916780bb45067 100644 --- a/aptos-move/vm-genesis/src/lib.rs +++ b/aptos-move/vm-genesis/src/lib.rs @@ -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.