Skip to content

Commit

Permalink
setScores: remove moreScoresExpected argument (#37)
Browse files Browse the repository at this point in the history
* setScores: remove `moreScoresExpected` argument

* fmt
  • Loading branch information
juliangruber authored Oct 5, 2023
1 parent 38c89dc commit 8fb8618
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 55 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

## Evaluator functions

### `.setScores(uint roundIndex, address payable[] addresses, uint[] scores, bool moreScoresExpected)`
### `.setScores(uint roundIndex, address payable[] addresses, uint[] scores)`

## Admin functions

Expand Down
50 changes: 17 additions & 33 deletions src/ImpactEvaluator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ contract ImpactEvaluator is AccessControl {
function setScores(
uint roundIndex,
address payable[] memory addresses,
uint64[] memory scores,
bool moreScoresExpected
uint64[] memory scores
) public {
require(hasRole(EVALUATE_ROLE, msg.sender), "Not an evaluator");
require(
Expand All @@ -89,40 +88,15 @@ contract ImpactEvaluator is AccessControl {
Round storage round = openRounds[roundIndex];
require(round.exists, "Open round does not exist");

if (moreScoresExpected) {
for (uint i = 0; i < addresses.length; i++) {
round.addresses.push(addresses[i]);
round.scores.push(scores[i]);
}
} else {
(
address payable[] memory mergedAddresses,
uint64[] memory mergedScores
) = mergeScores(round, addresses, scores);
reward(mergedAddresses, mergedScores);
cleanUpRound(round, roundIndex);
for (uint i = 0; i < addresses.length; i++) {
round.addresses.push(addresses[i]);
round.scores.push(scores[i]);
}
}

function mergeScores(
Round storage round,
address payable[] memory addresses,
uint64[] memory scores
) private view returns (address payable[] memory, uint64[] memory) {
uint mergedLength = round.addresses.length + addresses.length;
address payable[] memory mergedAddresses = new address payable[](
mergedLength
);
uint64[] memory mergedScores = new uint64[](mergedLength);
for (uint i = 0; i < round.addresses.length; i++) {
mergedAddresses[i] = round.addresses[i];
mergedScores[i] = round.scores[i];
}
for (uint i = 0; i < addresses.length; i++) {
mergedAddresses[round.addresses.length + i] = addresses[i];
mergedScores[round.addresses.length + i] = scores[i];
if (allScoresSubmitted(round.scores)) {
reward(round.addresses, round.scores);
cleanUpRound(round, roundIndex);
}
return (mergedAddresses, mergedScores);
}

function cleanUpRound(Round storage round, uint roundIndex) private {
Expand All @@ -133,6 +107,16 @@ contract ImpactEvaluator is AccessControl {
delete openRounds[roundIndex];
}

function allScoresSubmitted(
uint64[] memory scores
) private pure returns (bool) {
uint totalScore = 0;
for (uint i = 0; i < scores.length; i++) {
totalScore += scores[i];
}
return totalScore >= MAX_SCORE;
}

function reward(
address payable[] memory addresses,
uint64[] memory scores
Expand Down
36 changes: 15 additions & 21 deletions test/ImpactEvaluator.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,7 @@ contract ImpactEvaluatorTest is Test {
function test_SetScoresNotEvaluator() public {
ImpactEvaluator impactEvaluator = new ImpactEvaluator(address(0x1));
vm.expectRevert("Not an evaluator");
impactEvaluator.setScores(
0,
new address payable[](0),
new uint64[](0),
false
);
impactEvaluator.setScores(0, new address payable[](0), new uint64[](0));
}

function test_SetScores() public {
Expand All @@ -74,12 +69,7 @@ contract ImpactEvaluatorTest is Test {
address(this)
);
vm.expectRevert("Addresses and scores length mismatch");
impactEvaluator.setScores(
0,
new address payable[](1),
new uint64[](0),
false
);
impactEvaluator.setScores(0, new address payable[](1), new uint64[](0));

address payable[] memory addresses = new address payable[](1);
addresses[0] = payable(vm.addr(1));
Expand All @@ -88,11 +78,11 @@ contract ImpactEvaluatorTest is Test {
vm.deal(payable(address(impactEvaluator)), 100 ether);
vm.expectEmit(false, false, false, true);
emit Transfer(addresses[0], 100 ether);
impactEvaluator.setScores(0, addresses, scores, false);
impactEvaluator.setScores(0, addresses, scores);
assertEq(addresses[0].balance, 100 ether, "correct balance");

vm.expectRevert("Open round does not exist");
impactEvaluator.setScores(0, addresses, scores, false);
impactEvaluator.setScores(0, addresses, scores);
}

function test_SetScoresMultipleParticipants() public {
Expand All @@ -109,7 +99,7 @@ contract ImpactEvaluatorTest is Test {
scores[1] = 25e13;
scores[2] = 25e13;
vm.deal(payable(address(impactEvaluator)), 100 ether);
impactEvaluator.setScores(0, addresses, scores, false);
impactEvaluator.setScores(0, addresses, scores);
assertEq(addresses[0].balance, 50 ether, "addresses[0] balance");
assertEq(addresses[1].balance, 25 ether);
assertEq(addresses[2].balance, 25 ether);
Expand All @@ -130,7 +120,7 @@ contract ImpactEvaluatorTest is Test {
emit Transfer(addresses[0], 100 ether - 1e5);
vm.expectEmit(false, false, false, true);
emit Transfer(addresses[0], 1e5);
impactEvaluator.setScores(0, addresses, scores, false);
impactEvaluator.setScores(0, addresses, scores);
assertEq(addresses[0].balance, 100 ether - 1e5, "addresses[0] balance");
assertEq(addresses[1].balance, 1e5, "addresses[1] balance");
}
Expand All @@ -142,7 +132,7 @@ contract ImpactEvaluatorTest is Test {
address payable[] memory addresses = new address payable[](0);
uint64[] memory scores = new uint64[](0);
vm.deal(payable(address(impactEvaluator)), 100 ether);
impactEvaluator.setScores(0, addresses, scores, false);
impactEvaluator.setScores(0, addresses, scores);
}

function test_SetScoresMultipleCalls() public {
Expand All @@ -155,10 +145,14 @@ contract ImpactEvaluatorTest is Test {
addresses[0] = payable(vm.addr(i + 1));
uint64[] memory scores = new uint64[](1);
scores[0] = impactEvaluator.MAX_SCORE() / iterations;
impactEvaluator.setScores(0, addresses, scores, i < iterations - 1);
impactEvaluator.setScores(0, addresses, scores);
}
for (uint i = 0; i < iterations; i++) {
assertEq(vm.addr(i + 1).balance, 100 ether / iterations, "right balance");
assertEq(
vm.addr(i + 1).balance,
100 ether / iterations,
"right balance"
);
}
}

Expand All @@ -172,14 +166,14 @@ contract ImpactEvaluatorTest is Test {
scores[0] = impactEvaluator.MAX_SCORE() + 1;
vm.deal(payable(address(impactEvaluator)), 100 ether);
vm.expectRevert("Sum of scores too big");
impactEvaluator.setScores(0, addresses, scores, false);
impactEvaluator.setScores(0, addresses, scores);
}

function test_SetScoresUnfinishedRound() public {
ImpactEvaluator impactEvaluator = new ImpactEvaluator(address(this));
address payable[] memory addresses = new address payable[](0);
uint64[] memory scores = new uint64[](0);
vm.expectRevert("Round not finished");
impactEvaluator.setScores(0, addresses, scores, false);
impactEvaluator.setScores(0, addresses, scores);
}
}

0 comments on commit 8fb8618

Please sign in to comment.