Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix missing validation of scores #32

Merged
merged 2 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/ImpactEvaluator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ contract ImpactEvaluator is AccessControl {
Round storage round = rounds[roundIndex];
require(round.exists, "Round does not exist");
require(!round.scoresSubmitted, "Scores already submitted");
validateScores(scores);
for (uint i = 0; i < addresses.length; i++) {
round.scores[addresses[i]] = scores[i];
}
Expand All @@ -113,6 +114,14 @@ contract ImpactEvaluator is AccessControl {
reward(addresses, scores);
}

function validateScores(uint64[] memory scores) pure private {
uint64 sum = 0;
for (uint i = 0; i < scores.length; i++) {
sum += scores[i];
}
require(sum <= 1e15, "Sum of scores too big");
bajtos marked this conversation as resolved.
Show resolved Hide resolved
}

function reward(
address payable[] memory addresses,
uint64[] memory scores
Expand Down
13 changes: 13 additions & 0 deletions test/ImpactEvaluator.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,19 @@ contract ImpactEvaluatorTest is Test {
impactEvaluator.setScores(0, addresses, scores, "0 tasks performed");
}

function test_SetScoresTooBig() public {
ImpactEvaluator impactEvaluator = new ImpactEvaluator(address(this));
impactEvaluator.adminAdvanceRound();

address payable[] memory addresses = new address payable[](1);
addresses[0] = payable(vm.addr(1));
uint64[] memory scores = new uint64[](1);
scores[0] = 1e15 + 1;
vm.deal(payable(address(impactEvaluator)), 100 ether);
vm.expectRevert("Sum of scores too big");
impactEvaluator.setScores(0, addresses, scores, "1 task performed");
}

function test_SetScoresUnfinishedRound() public {
ImpactEvaluator impactEvaluator = new ImpactEvaluator(address(this));
address payable[] memory addresses = new address payable[](0);
Expand Down