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 subnet validator primary network requirements #2014

Merged
merged 5 commits into from
Sep 15, 2023
Merged
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
81 changes: 38 additions & 43 deletions vms/platformvm/txs/executor/staker_tx_verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,40 @@ var (
ErrWrongStakedAssetID = errors.New("incorrect staked assetID")
)

// verifySubnetValidatorPrimaryNetworkRequirements verifies any primary
// network requirements for [subnetValidator]. An error is returned they
// are not fulfilled.
dhrubabasu marked this conversation as resolved.
Show resolved Hide resolved
func verifySubnetValidatorPrimaryNetworkRequirements(chainState state.Chain, subnetValidator txs.Validator) error {
primaryNetworkValidator, err := GetValidator(chainState, constants.PrimaryNetworkID, subnetValidator.NodeID)
if err == database.ErrNotFound {
return fmt.Errorf(
"%s %w of the primary network",
subnetValidator.NodeID,
ErrNotValidator,
)
}
if err != nil {
return fmt.Errorf(
"failed to fetch the primary network validator for %s: %w",
subnetValidator.NodeID,
err,
)
}

// Ensure that the period this validator validates the specified subnet
// is a subset of the time they validate the primary network.
if !txs.BoundedBy(
subnetValidator.StartTime(),
subnetValidator.EndTime(),
primaryNetworkValidator.StartTime,
primaryNetworkValidator.EndTime,
) {
return ErrPeriodMismatch
}

return nil
}

// verifyAddValidatorTx carries out the validation for an AddValidatorTx.
// It returns the tx outputs that should be returned if this validator is not
// added to the staking set.
Expand Down Expand Up @@ -198,31 +232,8 @@ func verifyAddSubnetValidatorTx(
)
}

primaryNetworkValidator, err := GetValidator(chainState, constants.PrimaryNetworkID, tx.Validator.NodeID)
if err == database.ErrNotFound {
return fmt.Errorf(
"%s %w of the primary network",
tx.Validator.NodeID,
ErrNotValidator,
)
}
if err != nil {
return fmt.Errorf(
"failed to fetch the primary network validator for %s: %w",
tx.Validator.NodeID,
err,
)
}

// Ensure that the period this validator validates the specified subnet
// is a subset of the time they validate the primary network.
if !txs.BoundedBy(
tx.Validator.StartTime(),
tx.Validator.EndTime(),
primaryNetworkValidator.StartTime,
primaryNetworkValidator.EndTime,
) {
return ErrPeriodMismatch
if err := verifySubnetValidatorPrimaryNetworkRequirements(chainState, tx.Validator); err != nil {
return err
}

baseTxCreds, err := verifyPoASubnetAuthorization(backend, chainState, sTx, tx.SubnetValidator.Subnet, tx.SubnetAuth)
Expand Down Expand Up @@ -524,24 +535,8 @@ func verifyAddPermissionlessValidatorTx(

var txFee uint64
if tx.Subnet != constants.PrimaryNetworkID {
primaryNetworkValidator, err := GetValidator(chainState, constants.PrimaryNetworkID, tx.Validator.NodeID)
if err != nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The functionality change in this PR is adding the following code here:

if err == database.ErrNotFound {
	return fmt.Errorf(
		"%s %w of the primary network",
		validator.NodeID,
		ErrNotValidator,
	)
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

to clarify, this should not change validity because the functional change is to provide a more specific error message and both errors are treated the same

return fmt.Errorf(
"failed to fetch the primary network validator for %s: %w",
tx.Validator.NodeID,
err,
)
}

// Ensure that the period this validator validates the specified subnet
// is a subset of the time they validate the primary network.
if !txs.BoundedBy(
tx.Validator.StartTime(),
tx.Validator.EndTime(),
primaryNetworkValidator.StartTime,
primaryNetworkValidator.EndTime,
) {
return ErrPeriodMismatch
if err := verifySubnetValidatorPrimaryNetworkRequirements(chainState, tx.Validator); err != nil {
return err
}

txFee = backend.Config.AddSubnetValidatorFee
Expand Down