diff --git a/README.md b/README.md index aa58824..be16045 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/ImpactEvaluator.sol b/src/ImpactEvaluator.sol index 9b260e2..574ea9f 100644 --- a/src/ImpactEvaluator.sol +++ b/src/ImpactEvaluator.sol @@ -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( @@ -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 { @@ -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 diff --git a/test/ImpactEvaluator.t.sol b/test/ImpactEvaluator.t.sol index c1806d5..449a987 100644 --- a/test/ImpactEvaluator.t.sol +++ b/test/ImpactEvaluator.t.sol @@ -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 { @@ -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)); @@ -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 { @@ -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); @@ -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"); } @@ -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 { @@ -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" + ); } } @@ -172,7 +166,7 @@ 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 { @@ -180,6 +174,6 @@ contract ImpactEvaluatorTest is Test { 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); } }