Skip to content

Commit

Permalink
ScoreManager update
Browse files Browse the repository at this point in the history
  • Loading branch information
pahor167 committed Oct 16, 2024
1 parent b2329a3 commit 53dec22
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
19 changes: 11 additions & 8 deletions packages/protocol/contracts-0.8/common/ScoreManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ contract ScoreManager is
* @dev Set value to `ZERO_SCORE` to set score to zero.
*/
function setGroupScore(address group, uint256 score) external onlyAuthorizedToUpdateScore {
require(score > 0, "Score must be greater than ZERO.");
require(score <= ZERO_SCORE, "Score must be less than or equal to 1e24 or ZERO_SCORE.");
groupScores[group] = score;

groupScores[group] = checkScore(score);
emit GroupScoreSet(group, score);
}

Expand All @@ -85,10 +82,7 @@ contract ScoreManager is
address validator,
uint256 score
) external onlyAuthorizedToUpdateScore {
require(score > 0, "Score must be greater than ZERO.");
require(score <= ZERO_SCORE, "Score must be less than or equal to 1e24 or ZERO_SCORE.");
validatorScores[validator] = score;

validatorScores[validator] = checkScore(score);
emit ValidatorScoreSet(validator, score);
}

Expand Down Expand Up @@ -150,4 +144,13 @@ contract ScoreManager is
}
return score;
}

function checkScore(uint256 score) internal pure returns (uint256) {
if (score == 0) {
return ZERO_SCORE;
}

require(score <= FIXED1_UINT, "Score must be less than or equal to 1e24.");
return score;
}
}
22 changes: 6 additions & 16 deletions packages/protocol/test-sol/unit/common/ScoreManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,17 @@ contract ScoreManagerTest_setGroupScore is ScoreManagerTest {
}

function test_Reverts_WhenSetToMoreThan1e24Plus1() public {
vm.expectRevert("Score must be less than or equal to 1e24 or ZERO_SCORE.");
scoreManager.setGroupScore(owner, 1e24 + 2);
vm.expectRevert("Score must be less than or equal to 1e24.");
scoreManager.setGroupScore(owner, 1e24 + 1);
}

function test_Returns1FixidityWhenGroupScoreDoesNotExist() public {
assertEq(scoreManager.getGroupScore(owner), 1e24);
}

function test_Returns0WhenGroupScoreIsZERO_SCORE() public {
scoreManager.setGroupScore(owner, ZERO_SCORE);
assert(scoreManager.getGroupScore(owner) == 0);
}

function test_Reverts_WhenSettingScoreToZero() public {
vm.expectRevert("Score must be greater than ZERO.");
scoreManager.setGroupScore(owner, 0);
assert(scoreManager.getGroupScore(owner) == 0);
}

function test_EmitsGroupScoreSet() public {
Expand Down Expand Up @@ -107,20 +102,15 @@ contract ScoreManagerTest_setValidatorScore is ScoreManagerTest {
}

function test_Reverts_WhenSetToMoreThan1e24() public {
vm.expectRevert("Score must be less than or equal to 1e24 or ZERO_SCORE.");
scoreManager.setValidatorScore(owner, 1e24 + 2);
vm.expectRevert("Score must be less than or equal to 1e24.");
scoreManager.setValidatorScore(owner, 1e24 + 1);
}

function test_Returns0WhenValidatorScoreIsZero() public {
scoreManager.setValidatorScore(owner, ZERO_SCORE);
scoreManager.setValidatorScore(owner, 0);
assert(scoreManager.getValidatorScore(owner) == 0);
}

function test_Reverts_WhenSettingScoreToZero() public {
vm.expectRevert("Score must be greater than ZERO.");
scoreManager.setGroupScore(owner, 0);
}

function test_EmitsValidatorScoreSet() public {
vm.expectEmit(false, false, false, true);
emit ValidatorScoreSet(owner, 42);
Expand Down

0 comments on commit 53dec22

Please sign in to comment.