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

Score manager gas optimization #11246

Merged
merged 6 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
44 changes: 24 additions & 20 deletions packages/protocol/contracts-0.8/common/ScoreManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ contract ScoreManager is Initializable, Ownable {
event ValidatorScoreSet(address indexed validator, uint256 score);

uint256 private constant FIXED1_UINT = 1e24;
uint256 public constant ZERO_FIXED1_UINT = FIXED1_UINT + 1;

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

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

function setGroupScore(address group, uint256 score) external onlyOwner {
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;
require(
score <= ZERO_FIXED1_UINT,
"Score must be less than or equal to 1e24 or ZERO_FIXED1_UINT."
);
groupScores[group] = score;

emit GroupScoreSet(group, score);
}

function setValidatorScore(address validator, uint256 score) external onlyOwner {
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;
require(
score <= ZERO_FIXED1_UINT,
"Score must be less than or equal to 1e24 or ZERO_FIXED1_UINT."
);
validatorScores[validator] = score;

emit ValidatorScoreSet(validator, score);
}

function getGroupScore(address group) external view returns (uint256) {
Score storage groupScore = groupScores[group];
if (!groupScore.exists) {
uint256 score = groupScores[group];
if (score == 0) {
return FIXED1_UINT;
} else if (score == ZERO_FIXED1_UINT) {
return 0;
}
return groupScore.score;

return score;
pahor167 marked this conversation as resolved.
Show resolved Hide resolved
}

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

/**
Expand Down
20 changes: 16 additions & 4 deletions packages/protocol/test-sol/unit/common/ScoreManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ contract ScoreManagerTest is Test, TestConstants {
event GroupScoreSet(address indexed group, uint256 score);
event ValidatorScoreSet(address indexed validator, uint256 score);

uint256 constant ZERO_FIXED1_UINT = 1e24 + 1;

function setUp() public virtual {
owner = address(this);
nonOwner = actor("nonOwner");
Expand Down Expand Up @@ -51,14 +53,19 @@ contract ScoreManagerTest_setGroupScore is ScoreManagerTest {
}

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

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

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

function test_EmitsGroupScoreSet() public {
vm.expectEmit(false, false, false, true);
emit GroupScoreSet(owner, 42);
Expand All @@ -79,8 +86,13 @@ contract ScoreManagerTest_setValidatorScore is ScoreManagerTest {
}

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

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

function test_EmitsValidatorScoreSet() public {
Expand Down
Loading