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: evict inactive services in the checkpoint #141

Merged
merged 16 commits into from
Nov 24, 2023
Merged

Conversation

kupermind
Copy link
Collaborator

  • Evict inactive services in the checkpoint.

@kupermind kupermind requested a review from mariapiamo November 21, 2023 20:50
@codecov-commenter
Copy link

codecov-commenter commented Nov 22, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (438eb3d) 98.69% compared to head (de4b5d9) 99.80%.

Additional details and impacted files
@@                Coverage Diff                 @@
##           downtime_check     #141      +/-   ##
==================================================
+ Coverage           98.69%   99.80%   +1.11%     
==================================================
  Files                  19       19              
  Lines                1535     1577      +42     
  Branches              290      292       +2     
==================================================
+ Hits                 1515     1574      +59     
+ Misses                 20        3      -17     
Flag Coverage Δ
unittests 99.80% <100.00%> (+1.11%) ⬆️

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

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

Comment on lines +108 to +112
enum ServiceStakingState {
Unstaked,
Staked,
Evicted
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Giving a bit more information about service staking statuses

Comment on lines 449 to 454
// Get the number of evicted services
for (uint256 i = 0; i < totalNumServices; ++i) {
if (evictServiceIds[i] > 0) {
numEvictServices++;
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The input array is always of a size of all the staked services, and thus for the event exact number of evicted services must be calculated.

Comment on lines 469 to 476
serviceId = evictServiceIds[i];
serviceIds[sCounter] = serviceId;

ServiceInfo storage sInfo = mapServiceInfo[serviceId];
owners[sCounter] = sInfo.owner;
multisigs[sCounter] = sInfo.multisig;
serviceInactivity[sCounter] = sInfo.inactivity;
serviceIndexes[sCounter] = 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.

Get all the data of each evicted service.


// Evict services from the global set of staked services
uint256 idx;
for (uint256 i = numEvictServices - 1; i > 0; --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.

When evicting, we go backwards, as if we go forward all the indexes will be changed with every deletion.

Comment on lines +527 to +530
uint256[] memory finalEligibleServiceIds;
uint256[] memory finalEligibleServiceRewards;
evictServiceIds = new uint256[](serviceIds.length);
uint256 curServiceId;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

For the event, we need a precise number of eligible services.

Also, evict services are allocated to the size of the all services in the set of staked services.

Comment on lines 596 to 603
// Get the overall continuous service inactivity
uint256 inactivity = mapServiceInfo[curServiceId].inactivity + serviceInactivity[i];
mapServiceInfo[curServiceId].inactivity = inactivity;
// Check for the maximum allowed inactivity time
if (inactivity > maxAllowedInactivity) {
// Evict a service if it has been inactive for more than a maximum allowed inactivity time
evictServiceIds[i] = curServiceId;
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If inactivity is bigger than the allowed one, the service assigned to evicted services list.

Copy link
Collaborator

@mariapiamo mariapiamo left a comment

Choose a reason for hiding this comment

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

Can we have

  1. a test showing that if maxNumStaker is evicted, after the checkpoint call, a new service can stake?
  2. Even if not specifically related to this pr, a test showing the revert of UnauthorizedMultisig(service.multisig) ?

contracts/staking/ServiceStakingBase.sol Show resolved Hide resolved
@kupermind
Copy link
Collaborator Author

@mariapiamo I've added requested tests

Copy link
Collaborator

@mariapiamo mariapiamo left a comment

Choose a reason for hiding this comment

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

@kupermind thanks! Just a minor suggestion for a comment

// Approve services
await serviceRegistry.approve(serviceStaking.address, serviceId + i);

// Stake the first service
Copy link
Collaborator

Choose a reason for hiding this comment

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

// stake the service

event ServiceUnstaked(uint256 indexed serviceId, address indexed owner, address indexed multisig, uint256[] nonces,
uint256 reward, uint256 tsStart);
event ServiceEvicted(uint256 indexed serviceId, address indexed owner, address indexed multisig, uint256 inactivity);
event ServiceStaked(uint256 indexed epoch, uint256 indexed serviceId, address indexed multisig, address owner,
Copy link
Contributor

Choose a reason for hiding this comment

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

why is owner not indexed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's max of 3 indexed fields. I've changed that now, not indexing the epoch.

event ServiceStaked(uint256 indexed epoch, uint256 indexed serviceId, address indexed multisig, address owner,
uint256[] nonces);
event Checkpoint(uint256 indexed epoch, uint256 availableRewards, uint256[] serviceIds, uint256[] rewards);
event ServiceUnstaked(uint256 indexed epoch, uint256 indexed serviceId, address indexed multisig, address owner,
Copy link
Contributor

Choose a reason for hiding this comment

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

see above

why tsStart? What's the point of emitting this here?

event Checkpoint(uint256 indexed epoch, uint256 availableRewards, uint256[] serviceIds, uint256[] rewards);
event ServiceUnstaked(uint256 indexed epoch, uint256 indexed serviceId, address indexed multisig, address owner,
uint256[] nonces, uint256 reward, uint256 tsStart);
event ServicesEvicted(uint256 indexed epoch, uint256[] serviceIds, address[] owners, address[] multisigs,
Copy link
Contributor

Choose a reason for hiding this comment

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

order of multisigs and owners need to be reverted to be symmetric with the other events

/// @return Eligible service rewards.
/// @return success True, if the checkpoint was successful.
/// @return All staking service Ids (including evicted ones during within a current epoch).
/// @return All staking updated nonces (including evicted ones during within a current epoch).
Copy link
Contributor

Choose a reason for hiding this comment

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

during within?

/// @return success True, if the checkpoint was successful.
/// @return All staking service Ids (including evicted ones during within a current epoch).
/// @return All staking updated nonces (including evicted ones during within a current epoch).
/// @return Set of eligible service Ids.
Copy link
Contributor

Choose a reason for hiding this comment

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

reward-eligible

Copy link
Contributor

Choose a reason for hiding this comment

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

why do we no longer return the nonces?

Copy link
Collaborator Author

@kupermind kupermind Nov 24, 2023

Choose a reason for hiding this comment

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

We return nonces, we don't return the success flag. No need to return that, just can check for the serviceIds length.

@kupermind kupermind merged commit 2996ce6 into downtime_check Nov 24, 2023
2 checks passed
@kupermind kupermind deleted the evict branch November 24, 2023 18:26
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.

4 participants