Skip to content

Commit

Permalink
scoremanager events
Browse files Browse the repository at this point in the history
  • Loading branch information
pahor167 committed Sep 24, 2024
1 parent a316fc5 commit 0d887c3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
20 changes: 15 additions & 5 deletions packages/protocol/contracts-0.8/common/ScoreManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ contract ScoreManager is Initializable, Ownable {
bool exists;
}

mapping(address => Score) public scores;
event GroupScoreSet(address indexed group, uint256 score);
event ValidatorScoreSet(address indexed validator, uint256 score);

mapping(address => Score) public groupScores;
mapping(address => Score) public validatorScores;

/**
* @notice Sets initialized == true on implementation contracts
Expand All @@ -27,31 +31,37 @@ contract ScoreManager is Initializable, Ownable {
}

function setGroupScore(address group, uint256 score) external onlyOwner {
Score storage groupScore = scores[group];
require(score <= 1e24, "Score must be less than or equal to 1e24.");
Score storage groupScore = groupScores[group];
if (!groupScore.exists) {
groupScore.exists = true;
}
groupScore.score = score;

emit GroupScoreSet(group, score);
}

function setValidatorScore(address validator, uint256 score) external onlyOwner {
Score storage validatorScore = scores[validator];
require(score <= 1e24, "Score must be less than or equal to 1e24.");
Score storage validatorScore = validatorScores[validator];
if (!validatorScore.exists) {
validatorScore.exists = true;
}
validatorScore.score = score;

emit ValidatorScoreSet(validator, score);
}

function getGroupScore(address group) external view returns (uint256) {
Score storage groupScore = scores[group];
Score storage groupScore = groupScores[group];
if (!groupScore.exists) {
return 1e24;
}
return groupScore.score;
}

function getValidatorScore(address validator) external view returns (uint256) {
Score storage validatorScore = scores[validator];
Score storage validatorScore = validatorScores[validator];
if (!validatorScore.exists) {
return 1e24;
}
Expand Down
37 changes: 37 additions & 0 deletions packages/protocol/test-sol/unit/common/ScoreManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ contract ScoreManagerTest is Test, TestConstants {
address owner;
address nonOwner;

event GroupScoreSet(address indexed group, uint256 score);
event ValidatorScoreSet(address indexed validator, uint256 score);

function setUp() public virtual {
owner = address(this);
nonOwner = actor("nonOwner");
Expand Down Expand Up @@ -41,9 +44,26 @@ contract ScoreManagerTest_setGroupScore is ScoreManagerTest {
assert(scoreManager.getGroupScore(owner) == 42);
}

function test_Reverts_WhenNotCalledByOwner() public {
vm.prank(nonOwner);
vm.expectRevert("Ownable: caller is not the owner");
scoreManager.setGroupScore(owner, 42);
}

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

function test_Returns1FixidityWhenGroupScoreDoesNotExist() public {
assert(scoreManager.getGroupScore(owner) == 1e24);
}

function test_EmitsGroupScoreSet() public {
vm.expectEmit(false, false, false, true);
emit GroupScoreSet(owner, 42);
scoreManager.setGroupScore(owner, 42);
}
}

contract ScoreManagerTest_setValidatorScore is ScoreManagerTest {
Expand All @@ -52,6 +72,23 @@ contract ScoreManagerTest_setValidatorScore is ScoreManagerTest {
assert(scoreManager.getValidatorScore(owner) == 42);
}

function test_Reverts_WhenNotCalledByOwner() public {
vm.prank(nonOwner);
vm.expectRevert("Ownable: caller is not the owner");
scoreManager.setValidatorScore(owner, 42);
}

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

function test_EmitsValidatorScoreSet() public {
vm.expectEmit(false, false, false, true);
emit ValidatorScoreSet(owner, 42);
scoreManager.setValidatorScore(owner, 42);
}

function test_Returns1FixidityWhenValidatorScoreDoesNotExist() public {
assert(scoreManager.getValidatorScore(owner) == 1e24);
}
Expand Down

0 comments on commit 0d887c3

Please sign in to comment.