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

refactor and test: service staking optimization #135

Merged
merged 12 commits into from
Oct 25, 2023

Conversation

kupermind
Copy link
Collaborator

  • Optimizing service staking contract logic;
  • Adding tests.

@kupermind kupermind requested review from 77ph and mariapiamo October 24, 2023 13:36
Comment on lines -355 to -364
// Get the service Ids set length
uint256 size = setServiceIds.length;
serviceIds = new uint256[](size);

// Record service Ids
for (uint256 i = 0; i < size; ++i) {
// Get current service Id
serviceIds[i] = setServiceIds[i];
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't allocate serviceIds if the rewards calculation is not going to be successful.

Comment on lines +357 to +358
lastAvailableRewards = availableRewards;
if (block.timestamp - tsCheckpointLast >= livenessPeriod && lastAvailableRewards > 0) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merged that check in one: for the liveness period and available rewards.

Comment on lines -511 to +508
(uint256[] memory serviceIds, , , , , ) = checkpoint();
(uint256[] memory serviceIds, , , , , bool success) = checkpoint();

// If the checkpoint was not successful, the serviceIds set is not returned and needs to be allocated
if (!success) {
serviceIds = getServiceIds();
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checkpoint now might not return serviceIds in memory if it was not successful. So in that case it must be obtained separately.

Comment on lines -565 to +562
if (numServices > 0) {
for (uint256 i = 0; i < numServices; ++i) {
Copy link
Collaborator Author

@kupermind kupermind Oct 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to check for the numServices, just loop with them. Also, the eligibleServiceIds.length is not correct as the length is equal to the full length of serviceIds, and it must have the maximum of numServices elements.

@codecov-commenter
Copy link

codecov-commenter commented Oct 24, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (cfa4180) 99.73% compared to head (a424c5a) 99.73%.

❗ Current head a424c5a differs from pull request most recent head 7b4af73. Consider uploading reports for the commit 7b4af73 to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #135   +/-   ##
=======================================
  Coverage   99.73%   99.73%           
=======================================
  Files          22       22           
  Lines        1530     1536    +6     
  Branches      287      286    -1     
=======================================
+ Hits         1526     1532    +6     
  Misses          4        4           
Flag Coverage Δ
unittests 99.73% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
contracts/staking/ServiceStakingBase.sol 99.42% <100.00%> (+0.02%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

}
}
}

/// @dev Gets staked service Ids.
/// @return serviceIds Staked service Ids.
function getServiceIds() public view returns (uint256[] memory serviceIds) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the function that returns current staking serviceIds. The default ABI getter would not be enough as it does not return all of the Ids and the length, but just a single element with the provided index.

Comment on lines +596 to +600
/// @dev Gets the next reward checkpoint timestamp.
/// @return tsNext Next reward checkpoint timestamp.
function getNextRewardCheckpointTimestamp() external view returns (uint256 tsNext) {
// Last checkpoint timestamp plus the liveness period
tsNext = tsCheckpoint + livenessPeriod;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getter of the next reward checkpoint timestamp - the agent can always compare with the block.timestamp and decide whether to call a checkpoint() or skip for now.

@kupermind kupermind merged commit 18ec1d8 into main Oct 25, 2023
2 checks passed
@kupermind kupermind deleted the service_staking_optimization branch October 25, 2023 09:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants