From 0655ca3299fff8169c3cf05b532e9479de68cb43 Mon Sep 17 00:00:00 2001 From: Bernhard Scholz Date: Thu, 31 Oct 2024 20:35:19 +1100 Subject: [PATCH] Reformatting code --- contracts/sfc/ConstantsManager.sol | 5 +++-- contracts/sfc/SFC.sol | 23 +++++++++++++---------- contracts/sfc/SFCState.sol | 10 +++++----- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/contracts/sfc/ConstantsManager.sol b/contracts/sfc/ConstantsManager.sol index 870dd9b..f1319c0 100644 --- a/contracts/sfc/ConstantsManager.sol +++ b/contracts/sfc/ConstantsManager.sol @@ -33,7 +33,7 @@ contract ConstantsManager is Ownable { uint256 public gasPriceBalancingCounterweight; // epoch threshold for stop counting alive epochs (avoid diminishing impact of new uptimes) and - // is also the minimum number of epochs necessary for enabling the deactivation. + // is also the minimum number of epochs necessary for enabling the deactivation. int32 public numEpochsAliveThreshold; // minimum average uptime in Q1.30 format; acceptable bounds [0,0.9] @@ -208,7 +208,8 @@ contract ConstantsManager is Ownable { if (v < 0) { revert ValueTooSmall(); } - if (v > 966367641) { // 0.9 in Q1.30 + if (v > 966367641) { + // 0.9 in Q1.30 revert ValueTooLarge(); } minAverageUptime = v; diff --git a/contracts/sfc/SFC.sol b/contracts/sfc/SFC.sol index 4f9d14c..877ad3e 100644 --- a/contracts/sfc/SFC.sol +++ b/contracts/sfc/SFC.sol @@ -382,21 +382,24 @@ contract SFC is SFCBase, Version { for (uint256 i = 0; i < validatorIDs.length; i++) { uint256 validatorID = validatorIDs[i]; // compute normalised uptime as a percentage in the Q1.30 fixed-point format - uint256 normalisedUptime = uptimes[i] * (1 << 30)/ epochDuration; + uint256 normalisedUptime = (uptimes[i] * (1 << 30)) / epochDuration; if (normalisedUptime < 0) { normalisedUptime = 0; } else if (normalisedUptime > 1 << 30) { normalisedUptime = 1 << 30; } - // update average uptime data structure + // update average uptime data structure // Assumes that if in the previous snapshot the validator // does not exist, the map returns zero. int32 n = prevSnapshot.averageData[validatorID].numEpochsAlive; int64 tmp; - if (n > 0) { - tmp = int64(n-1) * int64(snapshot.averageData[validatorID].averageUptime) + int64(uint64(normalisedUptime)); - if (n > 1) { - tmp += (int64(n) * int64(prevSnapshot.averageData[validatorID].averageUptimeError)) / int64(n-1); + if (n > 0) { + tmp = + int64(n - 1) * + int64(snapshot.averageData[validatorID].averageUptime) + + int64(uint64(normalisedUptime)); + if (n > 1) { + tmp += (int64(n) * int64(prevSnapshot.averageData[validatorID].averageUptimeError)) / int64(n - 1); } snapshot.averageData[validatorID].averageUptimeError = int32(tmp % int64(n)); tmp /= int64(n); @@ -404,9 +407,9 @@ contract SFC is SFCBase, Version { tmp = int64(uint64(normalisedUptime)); } if (tmp < 0) { - tmp = 0; - } else if (tmp > 1 << 30){ - tmp = 1 << 30; + tmp = 0; + } else if (tmp > 1 << 30) { + tmp = 1 << 30; } snapshot.averageData[validatorID].averageUptime = int32(tmp); if (n < c.numEpochsAliveThreshold()) { @@ -414,7 +417,7 @@ contract SFC is SFCBase, Version { } // remove validator if average uptime drops below min average uptime // (by setting minAverageUptime to zero, this check is ignored) - if (int32(tmp) < c.minAverageUptime() && n >= c.numEpochsAliveThreshold() ) { + if (int32(tmp) < c.minAverageUptime() && n >= c.numEpochsAliveThreshold()) { _setValidatorDeactivated(validatorIDs[i], OFFLINE_BIT); _syncValidator(validatorIDs[i], false); } diff --git a/contracts/sfc/SFCState.sol b/contracts/sfc/SFCState.sol index 405991e..e6e25b4 100644 --- a/contracts/sfc/SFCState.sol +++ b/contracts/sfc/SFCState.sol @@ -48,8 +48,8 @@ contract SFCState is Initializable, Ownable { mapping(address => mapping(uint256 => uint256)) public stashedRewardsUntilEpoch; struct WithdrawalRequest { - uint256 epoch; // epoch where undelegated - uint256 time; // when undelegated + uint256 epoch; // epoch where undelegated + uint256 time; // when undelegated uint256 amount; } @@ -73,12 +73,12 @@ contract SFCState is Initializable, Ownable { // data structure to compute average uptime for each active validator struct AverageData { - // average uptime + // average uptime int32 averageUptime; // average uptime error term int32 averageUptimeError; // number of alive epochs (counts only up to numEpochsAliveThreshold) - int32 numEpochsAlive; + int32 numEpochsAlive; } struct EpochSnapshot { @@ -88,7 +88,7 @@ contract SFCState is Initializable, Ownable { mapping(uint256 => uint256) accumulatedRewardPerToken; // validator ID => accumulated online time mapping(uint256 => uint256) accumulatedUptime; - // validator ID => average uptime as a percentage + // validator ID => average uptime as a percentage mapping(uint256 => AverageData) averageData; // validator ID => gas fees from txs originated by the validator mapping(uint256 => uint256) accumulatedOriginatedTxsFee;