From ecbf5296a5ebc8e3019b0e2165f3460e7ee09a81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20H=C3=A4ggblad?= Date: Mon, 19 Sep 2022 14:37:09 +0200 Subject: [PATCH] validator-api: guard against funny numbers in apy calc (#1636) --- .../validator-client/src/validator_api/mod.rs | 1 + .../src/contract_cache/reward_estimate.rs | 23 ++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/common/client-libs/validator-client/src/validator_api/mod.rs b/common/client-libs/validator-client/src/validator_api/mod.rs index 1fa3a50862..a36a2cde06 100644 --- a/common/client-libs/validator-client/src/validator_api/mod.rs +++ b/common/client-libs/validator-client/src/validator_api/mod.rs @@ -57,6 +57,7 @@ impl Client { V: AsRef, { let url = create_api_url(&self.url, path, params); + log::trace!("url: {:?}", url.as_str()); Ok(self.reqwest_client.get(url).send().await?.json().await?) } diff --git a/validator-api/src/contract_cache/reward_estimate.rs b/validator-api/src/contract_cache/reward_estimate.rs index d1aeb320d8..51ad38a99b 100644 --- a/validator-api/src/contract_cache/reward_estimate.rs +++ b/validator-api/src/contract_cache/reward_estimate.rs @@ -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(