Skip to content

Commit 8cd2f30

Browse files
author
Bernhard Scholz
committed
Convert the three maps for averaging to a single map with a struct
1 parent b044cf5 commit 8cd2f30

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

contracts/sfc/SFC.sol

+7-7
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ contract SFC is SFCBase, Version {
7171
}
7272

7373
function getEpochAverageUptime(uint256 epoch, uint256 validatorID) public view returns (int32) {
74-
return getEpochSnapshot[epoch].averageUptime[validatorID];
74+
return getEpochSnapshot[epoch].averageData[validatorID].averageUptime;
7575
}
7676

7777
function getEpochAccumulatedOriginatedTxsFee(uint256 epoch, uint256 validatorID) public view returns (uint256) {
@@ -389,14 +389,14 @@ contract SFC is SFCBase, Version {
389389
}
390390
// Assumes that if in the previous snapshot the validator
391391
// does not exist, the map returns zero.
392-
int32 n = prevSnapshot.numEpochsAlive[validatorID];
392+
int32 n = prevSnapshot.averageData[validatorID].numEpochsAlive;
393393
int64 tmp;
394394
if (n > 0) {
395-
tmp = int64(n-1) * int64(snapshot.averageUptime[validatorID]) + int64(uint64(normalisedUptime));
395+
tmp = int64(n-1) * int64(snapshot.averageData[validatorID].averageUptime) + int64(uint64(normalisedUptime));
396396
if (n > 1) {
397-
tmp += (int64(n) * int64(prevSnapshot.averageUptimeError[validatorID])) / int64(n-1);
397+
tmp += (int64(n) * int64(prevSnapshot.averageData[validatorID].averageUptimeError)) / int64(n-1);
398398
}
399-
snapshot.averageUptimeError[validatorID] = int32(tmp % int64(n));
399+
snapshot.averageData[validatorID].averageUptimeError = int32(tmp % int64(n));
400400
tmp /= int64(n);
401401
} else {
402402
tmp = int64(uint64(normalisedUptime));
@@ -406,9 +406,9 @@ contract SFC is SFCBase, Version {
406406
} else if (tmp > 1 << 30){
407407
tmp = 1 << 30;
408408
}
409-
snapshot.averageUptime[validatorID] = int32(tmp);
409+
snapshot.averageData[validatorID].averageUptime = int32(tmp);
410410
if (n < c.numEpochsAliveThreshold()) {
411-
snapshot.numEpochsAlive[validatorID] = n + 1;
411+
snapshot.averageData[validatorID].numEpochsAlive = n + 1;
412412
}
413413
}
414414
}

contracts/sfc/SFCState.sol

+13-7
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ contract SFCState is Initializable, Ownable {
4848
mapping(address => mapping(uint256 => uint256)) public stashedRewardsUntilEpoch;
4949

5050
struct WithdrawalRequest {
51-
uint256 epoch; // epoch where undelegated
52-
uint256 time; // when undelegated
51+
uint256 epoch; // epoch where undelegated
52+
uint256 time; // when undelegated
5353
uint256 amount;
5454
}
5555

@@ -71,6 +71,16 @@ contract SFCState is Initializable, Ownable {
7171

7272
mapping(address => mapping(uint256 => Rewards)) public getStashedLockupRewards;
7373

74+
// data structure to compute average uptime for each active validator
75+
struct AverageData {
76+
// average uptime
77+
int32 averageUptime;
78+
// average uptime error term
79+
int32 averageUptimeError;
80+
// number of alive epochs (counts only up to numEpochsAliveThreshold)
81+
int32 numEpochsAlive;
82+
}
83+
7484
struct EpochSnapshot {
7585
// validator ID => validator weight in the epoch
7686
mapping(uint256 => uint256) receivedStake;
@@ -79,11 +89,7 @@ contract SFCState is Initializable, Ownable {
7989
// validator ID => accumulated online time
8090
mapping(uint256 => uint256) accumulatedUptime;
8191
// validator ID => average uptime as a percentage
82-
mapping(uint256 => int32) averageUptime;
83-
// validator ID => error term of average uptime
84-
mapping(uint256 => int32) averageUptimeError;
85-
// validator ID => number of epochs alive for average uptime calculation
86-
mapping(uint256 => int32) numEpochsAlive;
92+
mapping(uint256 => AverageData) averageData;
8793
// validator ID => gas fees from txs originated by the validator
8894
mapping(uint256 => uint256) accumulatedOriginatedTxsFee;
8995
mapping(uint256 => uint256) offlineTime;

0 commit comments

Comments
 (0)