Skip to content

Commit

Permalink
Update score manager behaviour (#11224)
Browse files Browse the repository at this point in the history
* Update score manager behavior

* scoremanager events

* Remove magic number

---------

Co-authored-by: Martín Volpe <[email protected]>
  • Loading branch information
pahor167 and martinvol authored Sep 25, 2024
1 parent 580bd9d commit 74ca26b
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 8 deletions.
43 changes: 38 additions & 5 deletions packages/protocol/contracts-0.8/common/ScoreManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@ import "../../contracts/common/interfaces/ICeloVersionedContract.sol";
import "@openzeppelin/contracts8/access/Ownable.sol";

contract ScoreManager is Initializable, Ownable {
mapping(address => uint256) public scores;
struct Score {
uint256 score;
bool exists;
}

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

uint256 private constant FIXED1_UINT = 1e24;

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

/**
* @notice Sets initialized == true on implementation contracts
Expand All @@ -22,19 +33,41 @@ contract ScoreManager is Initializable, Ownable {
}

function setGroupScore(address group, uint256 score) external onlyOwner {
scores[group] = score;
require(score <= FIXED1_UINT, "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 {
scores[validator] = score;
require(score <= FIXED1_UINT, "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) {
return scores[group];
Score storage groupScore = groupScores[group];
if (!groupScore.exists) {
return FIXED1_UINT;
}
return groupScore.score;
}

function getValidatorScore(address validator) external view returns (uint256) {
return scores[validator];
Score storage validatorScore = validatorScores[validator];
if (!validatorScore.exists) {
return FIXED1_UINT;
}
return validatorScore.score;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,11 +409,9 @@ contract E2E_EpochManager_FinishNextEpochProcess is E2E_EpochManager {
);
}
for (uint256 i = 0; i < groups.length; i++) {
uint256 rewards = rewards[i];

for (uint256 j = 0; j < groupWithVotes.length; j++) {
if (groupWithVotes[j].group == groups[i]) {
groupWithVotes[j].votes += rewards;
groupWithVotes[j].votes += rewards[i];
break;
}
}
Expand Down
95 changes: 95 additions & 0 deletions packages/protocol/test-sol/unit/common/ScoreManager.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.7 <0.8.20;

import "celo-foundry-8/Test.sol";
import { TestConstants } from "@test-sol/constants.sol";

import "@celo-contracts/common/interfaces/IRegistry.sol";
import "@celo-contracts/common/interfaces/IScoreManager.sol";
import "@celo-contracts-8/common/ScoreManager.sol";

contract ScoreManagerTest is Test, TestConstants {
IRegistry registry;
IScoreManager public scoreManager;
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");

deployCodeTo("Registry.sol", abi.encode(false), REGISTRY_ADDRESS);

ScoreManager scoreManagerImpl = new ScoreManager(true);
scoreManager = IScoreManager(address(scoreManagerImpl));

registry = IRegistry(REGISTRY_ADDRESS);

registry.setAddressFor("ScoreManager", address(scoreManager));

scoreManagerImpl.initialize();
}

function _whenL2() public {
deployCodeTo("Registry.sol", abi.encode(false), PROXY_ADMIN_ADDRESS);
}
}

contract ScoreManagerTest_setGroupScore is ScoreManagerTest {
function test_setGroupScore() public {
scoreManager.setGroupScore(owner, 42);
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 {
function test_setValidatorScore() public {
scoreManager.setValidatorScore(owner, 42);
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);
}
}

0 comments on commit 74ca26b

Please sign in to comment.