Skip to content

Commit

Permalink
batch setScores updates (#62)
Browse files Browse the repository at this point in the history
* update `balanceHeld` less often

* also batch `previousRoundRemainingReward` updates

* also cache calculation

* remove redundant `previousRoundRemainingReward`

* keep precision
  • Loading branch information
juliangruber authored Nov 13, 2023
1 parent 567680a commit d58c61d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/Balances.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ contract Balances {
return scheduledForTransfer.length;
}

// `increaseParticipantBalance` and `increaseBalanceHeld` need to be called
// in tandem.

function increaseParticipantBalance(
address payable participant,
uint amount
) internal {
uint oldBalance = balances[participant];
uint newBalance = oldBalance + amount;
balances[participant] = newBalance;
balanceHeld += amount;
if (
oldBalance <= minBalanceForTransfer &&
newBalance > minBalanceForTransfer
Expand All @@ -47,6 +49,10 @@ contract Balances {
}
}

function increaseBalanceHeld(uint amount) internal {
balanceHeld += amount;
}

function _releaseRewards() internal {
require(
scheduledForTransfer.length == 0,
Expand Down
10 changes: 6 additions & 4 deletions src/ImpactEvaluator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ contract ImpactEvaluator is AccessControl, Balances {
uint public previousRoundIndex;
uint public previousRoundTotalScores;
uint public previousRoundRoundReward;
uint public previousRoundRemainingReward;

uint public nextRoundLength = 10;
uint public roundReward = 100 ether;
Expand All @@ -37,6 +36,8 @@ contract ImpactEvaluator is AccessControl, Balances {
receive() external payable {}

function advanceRound() private {
uint previousRoundRemainingReward = (1 - (previousRoundTotalScores /
MAX_SCORE)) * previousRoundRoundReward;
uint availableInContract = availableBalance() -
previousRoundRemainingReward -
currentRoundRoundReward;
Expand All @@ -46,7 +47,6 @@ contract ImpactEvaluator is AccessControl, Balances {
previousRoundIndex = currentRoundIndex;
previousRoundTotalScores = 0;
previousRoundRoundReward = currentRoundRoundReward;
previousRoundRemainingReward = currentRoundRoundReward;
currentRoundIndex = currentRoundEndBlockNumber == 0
? 0
: currentRoundIndex + 1;
Expand Down Expand Up @@ -102,23 +102,25 @@ contract ImpactEvaluator is AccessControl, Balances {
roundIndex == previousRoundIndex,
"Can only score previous round"
);

uint sumOfScores = 0;
uint addedBalance = 0;
for (uint i = 0; i < addresses.length; i++) {
uint score = scores[i];
address payable participant = addresses[i];
sumOfScores += score;
uint amount = (score * previousRoundRoundReward) / MAX_SCORE;
if (participant != 0x000000000000000000000000000000000000dEaD) {
increaseParticipantBalance(participant, amount);
addedBalance += amount;
}
previousRoundRemainingReward -= amount;
}
require(
sumOfScores + previousRoundTotalScores <= MAX_SCORE,
"Sum of scores including historic too big"
);
previousRoundTotalScores += sumOfScores;
increaseBalanceHeld(addedBalance);
}

function releaseRewards() public onlyAdmin {
Expand Down

0 comments on commit d58c61d

Please sign in to comment.