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

Fix edge case empty waiting list #5061

Merged

Conversation

mariusmihaic
Copy link
Contributor

@mariusmihaic mariusmihaic commented Mar 7, 2023

Reasoning behind the pull request

  • With stakingV4, shuffled out nodes from eligible are sent to auction, instead of waiting list. This causes nodes to spend one epoch in auction before they are distributed (if selected based on topUP) to waiting. However, in case we have: nodes in waiting < max waiting list size, after shuffling out nodes, there is no need to send nodes to auction, since they will be selected anyway. They can be directly sent to waiting list after shuffling, because next epoch they will be selected anyway. This also fixes some edge cases when waiting lists are almost empty and nodes would be forced to spend extra epochs(auction+waiting) before being sent to eligible.

Proposed changes

  • In case number of active nodes <= max number of nodes, distribute all shuffled out nodes to waiting instead of auction

Testing procedure

  • Integration test for edge cases.

Pre-requisites

Based on the Contributing Guidelines the PR author and the reviewers must check the following requirements are met:

  • was the PR targeted to the correct branch?
  • if this is a larger feature that probably needs more than one PR, is there a feat branch created?
  • if this is a feat branch merging, do all satellite projects have a proper tag inside go.mod?

@codecov-commenter
Copy link

codecov-commenter commented Mar 7, 2023

Codecov Report

❗ No coverage uploaded for pull request base (feat/staking-v4@8237658). Click here to learn what that means.
Patch has no changes to coverable lines.

❗ Current head a9f8608 differs from pull request most recent head 09be726. Consider uploading reports for the commit 09be726 to get more accurate results

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

Additional details and impacted files
@@                Coverage Diff                 @@
##             feat/staking-v4    #5061   +/-   ##
==================================================
  Coverage                   ?   70.76%           
==================================================
  Files                      ?      681           
  Lines                      ?    87805           
  Branches                   ?        0           
==================================================
  Hits                       ?    62136           
  Misses                     ?    21065           
  Partials                   ?     4604           

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@mariusmihaic mariusmihaic self-assigned this Mar 8, 2023
@mariusmihaic mariusmihaic marked this pull request as ready for review March 9, 2023 08:43
@mariusmihaic mariusmihaic changed the title Mx 13933 fix edge case empty waiting list Fix edge case empty waiting list Mar 9, 2023
@sasurobert sasurobert self-requested a review March 9, 2023 09:07

currNodesConfig = node.NodesConfig
require.Len(t, getAllPubKeys(currNodesConfig.eligible), 8)
require.Len(t, getAllPubKeys(currNodesConfig.waiting), 4)
Copy link
Contributor

Choose a reason for hiding this comment

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

duplicated code for checking a lot of requires. Make a separate funciton.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, should've done this before.
Refactored to reuse functionality with new functions

@@ -2883,22 +2883,6 @@ func (d *delegation) executeStakeAndUpdateStatus(
return vmcommon.Ok
}

func (d *delegation) getConfigStatusAndGlobalFund() (*DelegationConfig, *DelegationContractStatus, *GlobalFundData, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

not used ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not used, remained here from a rc->feat merge

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not used, I think it was some merging "leftovers" from an rc->feat

// Distribute validators from SHUFFLED OUT -> WAITING
err = arg.distributor.DistributeValidators(newWaiting, shuffledOutMap, arg.randomness, arg.flagBalanceWaitingLists)
if err != nil {
log.Warn("distributeValidators shuffledOut failed", "error", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

should return error. why only log.Warn in case of critical errors ?

Copy link
Contributor

Choose a reason for hiding this comment

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

same for all cases.

Copy link
Contributor Author

@mariusmihaic mariusmihaic Mar 13, 2023

Choose a reason for hiding this comment

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

Did some digging + tests regarding this and it seems like we could safely return error here and it should also be backwards compatible.
Refactored to return error

@@ -283,6 +285,30 @@ func shuffleNodes(arg shuffleNodesArg) (*ResUpdateNodes, error) {

shuffledOutMap, newEligible := shuffleOutNodes(newEligible, numToRemove, arg.randomness)

numShuffled := getNumPubKeys(shuffledOutMap)
Copy link
Contributor

Choose a reason for hiding this comment

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

make a separate function for the new code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added separate function with little refactor

Copy link
Contributor

@sasurobert sasurobert left a comment

Choose a reason for hiding this comment

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

We should really continue refactoring shuffleNodes function into a full fedged component.

@raduchis raduchis self-requested a review March 15, 2023 10:50
@@ -285,26 +297,42 @@ func shuffleNodes(arg shuffleNodesArg) (*ResUpdateNodes, error) {

err = moveMaxNumNodesToMap(newEligible, newWaiting, arg.nodesMeta, arg.nodesPerShard)
if err != nil {
log.Warn("moveNodesToMap failed", "error", err)
return nil, fmt.Errorf("moveNodesToMap failed, error: %w", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

this could be backwards incompatible and is not protected by a flag (before it would just log.warn and now it returns error)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is related to my comment from here.
An error here could never happend, that's why it was only treated as a warn;
This case never manifested on mainnet anyway, it's safe to refactor it

// Distribute validators from SHUFFLED OUT -> WAITING
err = arg.distributor.DistributeValidators(newWaiting, shuffledOutMap, arg.randomness, arg.flagBalanceWaitingLists)
if err != nil {
log.Warn("distributeValidators shuffledOut failed", "error", err)
return nil, fmt.Errorf("distributeValidators shuffled out failed, error: %w", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

also backwards incompatibility here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is related to my comment from here.
An error here could never happend, that's why it was only treated as a warn;
This case never manifested on mainnet anyway, it's safe to refactor it

@mariusmihaic mariusmihaic merged commit c27357d into feat/staking-v4 Mar 20, 2023
@mariusmihaic mariusmihaic deleted the MX-13933-fix-edge-case-empty-waiting-list branch March 20, 2023 13:15
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