validator-api: guard against funny numbers in apy calc (#1636)

This commit is contained in:
Jon Häggblad
2022-09-19 14:37:09 +02:00
committed by GitHub
parent f3454409f8
commit ecbf5296a5
2 changed files with 23 additions and 1 deletions
@@ -7,7 +7,28 @@ use mixnet_contract_common::reward_params::{EpochRewardParams, NodeRewardParams,
use mixnet_contract_common::MixNodeBond;
pub fn compute_apy(epochs_per_hour: f64, reward: f64, pledge_amount: f64) -> f64 {
epochs_per_hour * 24.0 * 365.0 * 100.0 * reward / pledge_amount
if epochs_per_hour < 0.0 {
log::warn!("epochs_per_hour is negative");
}
if reward < 0.0 {
log::warn!("reward is negative");
}
if pledge_amount < 0.0 {
log::warn!("pledge_amount is negative");
}
if pledge_amount == 0.0 {
return 0.0;
}
let apy = epochs_per_hour * 24.0 * 365.0 * 100.0 * reward / pledge_amount;
// Guard against unexpected results
if apy.is_finite() {
apy
} else {
log::warn!("computed apy is not finite!");
0.0
}
}
pub fn compute_reward_estimate(