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

Split writeCurrentStakers into multiple functions #3500

Merged
merged 7 commits into from
Oct 29, 2024

Conversation

StephenButtolph
Copy link
Contributor

@StephenButtolph StephenButtolph commented Oct 26, 2024

Why this should be merged

writeCurrentStakers is extremely complex as it is doing 3 things:

  1. Writing weight and public key diffs
  2. Updating the in-memory validator manager
  3. Updating the on-disk representation of the validator sets.

As we implement ACP-77:

  1. Writing weight and public key diffs

and

  1. Updating the in-memory validator manager

become more complex, as it is possible that the same subnetID + nodeID pair are removed from the pre-ACP-77 validator sets and added to the post-ACP-77 validator sets.

To avoid writeCurrentStakers from including ACP-77 validators as well (making one of the most complex functions even more complex), this PR moves the functionality into separate functions.

How this works

Moves code around and implements helper functions where possible.

How this was tested

Existing tests.

Need to be documented in RELEASES.md?

There are no user facing changes.

Copy link
Contributor Author

@StephenButtolph StephenButtolph left a comment

Choose a reason for hiding this comment

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

There are no testing changes because this is a pure refactor.

@@ -273,6 +274,34 @@ type diffValidator struct {
deletedDelegators map[ids.ID]*Staker
}

func (d *diffValidator) WeightDiff() (ValidatorWeightDiff, 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 was previously implemented in writeCurrentStakersSubnetDiff.

//
// Note: This function may return a nil public key and no error if the primary
// network validator does not have a public key.
func (s *state) getInheritedPublicKey(nodeID ids.NodeID) (*bls.PublicKey, 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 logic previously existed as part of writeCurrentStakersSubnetDiff.

Comment on lines +1797 to +1799
s.updateValidatorManager(updateValidators),
s.writeValidatorDiffs(height),
s.writeCurrentStakers(codecVersion),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

While splitting these out is technically less efficient (and contains similar code). The functions were just getting too complex to understand. This is explicitly trading off some (imo insignificant) level of performance for clarity.

@StephenButtolph StephenButtolph self-assigned this Oct 27, 2024
@StephenButtolph StephenButtolph added this to the v1.11.13 milestone Oct 27, 2024
@StephenButtolph StephenButtolph marked this pull request as ready for review October 27, 2024 18:43
Base automatically changed from implement-acp-77-add-subnetid-nodeid to master October 28, 2024 18:43
func (s *state) writeCurrentStakers(updateValidators bool, height uint64, codecVersion uint16) error {
// getInheritedPublicKey returns the primary network validator's public key.
//
// Note: This function may return a nil public key and no error if the primary
Copy link
Contributor

Choose a reason for hiding this comment

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

(No action required) Under what circumstances would a primary network validator not have a public key? Doc pointers welcome.

Copy link
Contributor

Choose a reason for hiding this comment

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

Prior to Durango, the AddValidatorTx was still supported and allowed validators to be registered without a BLS key (as opposed to AddPermisionlessValidatorTx which requires the public key).

So any validators previously added using AddValidtorTx that still haven't reached their end time will not have public keys. After the 1 year maximum staking duration from the activation of Durango, all primary network validators will have public keys

// validator set changes to disk.
//
// This function must be called prior to writeCurrentStakers.
func (s *state) writeValidatorDiffs(height uint64) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

(No action required) I was going to suggest breaking this into 2 functions to simplify testing since it's so nicely divided between determining changes and then writing them, but then I saw there wasn't actually a test... 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TestState_writeStakers is the test for this behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ended up splitting up the function and expanded TestState_writeStakers

// of the primary network validator times.
return fmt.Errorf("%w: %s", errMissingPrimaryNetworkValidator, nodeID)
}
// writeValidatorDiffs writes the validator set diff contained by the pending
Copy link
Contributor

Choose a reason for hiding this comment

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

Just for my own understanding, is writeValidatorDiffs meant to only handle changes to the validators set of subnets before ConvertSubnetToL1Txs? I'm not clear on its usage vs updateValidatorManager

Copy link
Contributor Author

Choose a reason for hiding this comment

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

writeValidatorDiffs will include the logic for L1 diffs in a later PR.

@StephenButtolph StephenButtolph added this pull request to the merge queue Oct 29, 2024
Merged via the queue into master with commit 801762c Oct 29, 2024
23 checks passed
@StephenButtolph StephenButtolph deleted the refactor-write-current-stakers branch October 29, 2024 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants