Skip to content

Commit

Permalink
Reformatting code
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard Scholz committed Oct 31, 2024
1 parent dffbd41 commit 0655ca3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
5 changes: 3 additions & 2 deletions contracts/sfc/ConstantsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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;
Expand Down
23 changes: 13 additions & 10 deletions contracts/sfc/SFC.sol
Original file line number Diff line number Diff line change
Expand Up @@ -382,39 +382,42 @@ 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);
} else {
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()) {
snapshot.averageData[validatorID].numEpochsAlive = n + 1;
}
// 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);
}
Expand Down
10 changes: 5 additions & 5 deletions contracts/sfc/SFCState.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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 {
Expand All @@ -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;
Expand Down

0 comments on commit 0655ca3

Please sign in to comment.