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

Track all subnet validator sets in the validator manager #2253

Merged
merged 4 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion vms/platformvm/block/executor/proposal_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ func TestBanffProposalBlockTrackedSubnet(t *testing.T) {
require.NoError(propBlk.Accept(context.Background()))
require.NoError(commitBlk.Accept(context.Background()))
_, ok := env.config.Validators.GetValidator(subnetID, subnetValidatorNodeID)
require.Equal(tracked, ok)
require.True(ok)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/block/executor/standard_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ func TestBanffStandardBlockTrackedSubnet(t *testing.T) {
require.NoError(block.Verify(context.Background()))
require.NoError(block.Accept(context.Background()))
_, ok := env.config.Validators.GetValidator(subnetID, subnetValidatorNodeID)
require.Equal(tracked, ok)
require.True(ok)
})
}
}
Expand Down
67 changes: 22 additions & 45 deletions vms/platformvm/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,6 @@ type State interface {

GetBlockIDAtHeight(height uint64) (ids.ID, error)

// ApplyCurrentValidators adds all the current validators and delegators of
// [subnetID] into [vdrs].
ApplyCurrentValidators(subnetID ids.ID, vdrs validators.Manager) error

// ApplyValidatorWeightDiffs iterates from [startHeight] towards the genesis
// block until it has applied all of the diffs up to and including
// [endHeight]. Applying the diffs modifies [validators].
Expand Down Expand Up @@ -1139,26 +1135,6 @@ func (s *state) SetCurrentSupply(subnetID ids.ID, cs uint64) {
}
}

func (s *state) ApplyCurrentValidators(subnetID ids.ID, vdrs validators.Manager) error {
for nodeID, validator := range s.currentStakers.validators[subnetID] {
staker := validator.validator
if err := vdrs.AddStaker(subnetID, nodeID, staker.PublicKey, staker.TxID, staker.Weight); err != nil {
return err
}

delegatorIterator := NewTreeIterator(validator.delegators)
for delegatorIterator.Next() {
staker := delegatorIterator.Value()
if err := vdrs.AddWeight(subnetID, nodeID, staker.Weight); err != nil {
delegatorIterator.Release()
return err
}
}
delegatorIterator.Release()
}
return nil
}

func (s *state) ApplyValidatorWeightDiffs(
ctx context.Context,
validators map[ids.NodeID]*validators.GetValidatorOutput,
Expand Down Expand Up @@ -1689,13 +1665,28 @@ func (s *state) loadPendingValidators() error {
// Invariant: initValidatorSets requires loadCurrentValidators to have already
// been called.
func (s *state) initValidatorSets() error {
if s.cfg.Validators.Count(constants.PrimaryNetworkID) != 0 {
// Enforce the invariant that the validator set is empty here.
return errValidatorSetAlreadyPopulated
}
err := s.ApplyCurrentValidators(constants.PrimaryNetworkID, s.cfg.Validators)
if err != nil {
return err
for subnetID, validators := range s.currentStakers.validators {
if s.cfg.Validators.Count(subnetID) != 0 {
// Enforce the invariant that the validator set is empty here.
return fmt.Errorf("%w: %s", errValidatorSetAlreadyPopulated, subnetID)
}

for nodeID, validator := range validators {
staker := validator.validator
if err := s.cfg.Validators.AddStaker(subnetID, nodeID, staker.PublicKey, staker.TxID, staker.Weight); err != nil {
return err
}

delegatorIterator := NewTreeIterator(validator.delegators)
for delegatorIterator.Next() {
staker := delegatorIterator.Value()
StephenButtolph marked this conversation as resolved.
Show resolved Hide resolved
if err := s.cfg.Validators.AddWeight(subnetID, nodeID, staker.Weight); err != nil {
delegatorIterator.Release()
return err
}
}
delegatorIterator.Release()
StephenButtolph marked this conversation as resolved.
Show resolved Hide resolved
}
}

vl := validators.NewLogger(s.ctx.Log, s.bootstrapped, constants.PrimaryNetworkID, s.ctx.NodeID)
Expand All @@ -1709,15 +1700,6 @@ func (s *state) initValidatorSets() error {
s.metrics.SetTotalStake(totalWeight)

for subnetID := range s.cfg.TrackedSubnets {
if s.cfg.Validators.Count(subnetID) != 0 {
// Enforce the invariant that the validator set is empty here.
return errValidatorSetAlreadyPopulated
}
err := s.ApplyCurrentValidators(subnetID, s.cfg.Validators)
if err != nil {
return err
}

vl := validators.NewLogger(s.ctx.Log, s.bootstrapped, subnetID, s.ctx.NodeID)
s.cfg.Validators.RegisterCallbackListener(subnetID, vl)
}
Expand Down Expand Up @@ -2109,11 +2091,6 @@ func (s *state) writeCurrentStakers(updateValidators bool, height uint64) error
continue
}

// We only track the current validator set of tracked subnets.
if subnetID != constants.PrimaryNetworkID && !s.cfg.TrackedSubnets.Contains(subnetID) {
continue
}

if weightDiff.Decrease {
err = s.cfg.Validators.RemoveWeight(subnetID, nodeID, weightDiff.Amount)
} else {
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/txs/executor/advance_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ func TestTrackedSubnet(t *testing.T) {
env.state.SetHeight(dummyHeight)
require.NoError(env.state.Commit())
_, ok := env.config.Validators.GetValidator(subnetID, ids.NodeID(subnetValidatorNodeID))
require.Equal(tracked, ok)
require.True(ok)
})
}
}
Expand Down
21 changes: 1 addition & 20 deletions vms/platformvm/validators/manager.go
StephenButtolph marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ type State interface {
GetLastAccepted() ids.ID
GetStatelessBlock(blockID ids.ID) (block.Block, error)

// ApplyCurrentValidators adds all the current validators and delegators of
// [subnetID] into [vdrs].
ApplyCurrentValidators(subnetID ids.ID, vdrs validators.Manager) error

// ApplyValidatorWeightDiffs iterates from [startHeight] towards the genesis
// block until it has applied all of the diffs up to and including
// [endHeight]. Applying the diffs modifies [validators].
Expand Down Expand Up @@ -346,22 +342,7 @@ func (m *manager) getCurrentValidatorSets(
ctx context.Context,
subnetID ids.ID,
) (map[ids.NodeID]*validators.GetValidatorOutput, map[ids.NodeID]*validators.GetValidatorOutput, uint64, error) {
subnetManager := m.cfg.Validators
if subnetManager.Count(subnetID) == 0 {
// If this subnet isn't tracked, there will not be any registered
// validators. To calculate the current validators we need to first
// fetch them from state. We generate a new manager as we don't want to
// modify that long-lived reference.
//
// TODO: remove this once all subnets are included in the validator
// manager.
subnetManager = validators.NewManager()
if err := m.state.ApplyCurrentValidators(subnetID, subnetManager); err != nil {
return nil, nil, 0, err
}
}

subnetMap := subnetManager.GetMap(subnetID)
subnetMap := m.cfg.Validators.GetMap(subnetID)
primaryMap := m.cfg.Validators.GetMap(constants.PrimaryNetworkID)
currentHeight, err := m.getCurrentHeight(ctx)
return subnetMap, primaryMap, currentHeight, err
Expand Down
3 changes: 0 additions & 3 deletions vms/platformvm/vm_regression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1397,9 +1397,6 @@ func TestRemovePermissionedValidatorDuringPendingToCurrentTransitionTracked(t *t
require.NoError(createSubnetBlock.Accept(context.Background()))
require.NoError(vm.SetPreference(context.Background(), vm.manager.LastAccepted()))

vm.TrackedSubnets.Add(createSubnetTx.ID())
require.NoError(vm.state.ApplyCurrentValidators(createSubnetTx.ID(), vm.Validators))

addSubnetValidatorTx, err := vm.txBuilder.NewAddSubnetValidatorTx(
defaultMaxValidatorStake,
uint64(validatorStartTime.Unix()),
Expand Down