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

Pchain validators repackaging #1284

Merged
merged 35 commits into from
May 18, 2023
Merged
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
795a10a
repack pchain validators
abi87 Apr 3, 2023
629ffd8
nit
abi87 Apr 3, 2023
61fd676
Merge branch 'dev' into pchain_proposers_repack
StephenButtolph Apr 4, 2023
e725784
nits
abi87 Apr 5, 2023
0c8bc40
nits
abi87 Apr 5, 2023
7bd3093
Merge branch 'dev' into pchain_proposers_repack
abi87 Apr 10, 2023
d91c656
Merge branch 'dev' into pchain_proposers_repack
abi87 Apr 11, 2023
97bafa4
minor renaming
abi87 Apr 11, 2023
d9b9452
clock cleanup
abi87 Apr 11, 2023
2248f3c
Merge branch 'dev' into pchain_proposers_repack
abi87 Apr 13, 2023
bea7c15
Merge branch 'dev' into pchain_proposers_repack
abi87 Apr 17, 2023
816dd86
Merge branch 'dev' into pchain_proposers_repack
abi87 Apr 20, 2023
da0d822
Merge branch 'dev' into pchain_proposers_repack
abi87 Apr 21, 2023
18e9bb8
Merge branch 'dev' into pchain_proposers_repack
abi87 Apr 24, 2023
9a978cc
Merge branch 'dev' into pchain_proposers_repack
abi87 Apr 26, 2023
db2e956
Merge branch 'dev' into pchain_proposers_repack
abi87 Apr 26, 2023
bcbe002
Merge branch 'dev' into pchain_proposers_repack
abi87 May 1, 2023
d53f5e9
Merge branch 'dev' into pchain_proposers_repack
abi87 May 2, 2023
fa3ee2b
Merge branch 'dev' into pchain_proposers_repack
abi87 May 3, 2023
4f24a50
Merge branch 'dev' into pchain_proposers_repack
abi87 May 4, 2023
ee25500
nits
abi87 May 4, 2023
42b1dd5
cleanup
abi87 May 4, 2023
a736dfc
fixed cleanup
abi87 May 4, 2023
40e6092
Merge branch 'dev' into pchain_proposers_repack
abi87 May 10, 2023
cb1eafb
nits
abi87 May 11, 2023
1e3d889
Merge branch 'dev' into pchain_proposers_repack
abi87 May 16, 2023
c2ccff7
Merge branch 'dev' into pchain_proposers_repack
abi87 May 17, 2023
4855ec9
Merge branch 'dev' into pchain_proposers_repack
abi87 May 18, 2023
330e83a
fix merge
StephenButtolph May 18, 2023
79262a5
nit rename
StephenButtolph May 18, 2023
d55469b
nit rename
StephenButtolph May 18, 2023
cfbd56f
remove duplicate comments
StephenButtolph May 18, 2023
dcc6aa7
minimize interface size
StephenButtolph May 18, 2023
bb3be27
license
StephenButtolph May 18, 2023
12590b2
Merge branch 'dev' into pchain_proposers_repack
StephenButtolph May 18, 2023
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
Prev Previous commit
Next Next commit
cleanup
  • Loading branch information
abi87 committed May 4, 2023
commit 42b1dd55390c1367aa60f1d619d319d370dcef82
109 changes: 59 additions & 50 deletions vms/platformvm/validators/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
var (
_ validators.State = (*manager)(nil)

ErrMissingValidator = errors.New("missing validator")
ErrMissingValidatorSet = errors.New("missing validator set")
)

Expand Down Expand Up @@ -178,7 +179,7 @@ func (m *manager) GetValidatorSet(ctx context.Context, height uint64, subnetID i
currentPrimaryNetworkValidators, ok := m.cfg.Validators.Get(constants.PrimaryNetworkID)
if !ok {
// This should never happen
return nil, ErrMissingValidatorSet
return nil, ErrMissingValidator
}

currentSubnetValidatorList := currentSubnetValidators.List()
Expand All @@ -187,7 +188,7 @@ func (m *manager) GetValidatorSet(ctx context.Context, height uint64, subnetID i
primaryVdr, ok := currentPrimaryNetworkValidators.Get(vdr.NodeID)
if !ok {
// This should never happen
return nil, fmt.Errorf("%w: %s", ErrMissingValidatorSet, vdr.NodeID)
return nil, fmt.Errorf("%w: %s", ErrMissingValidator, vdr.NodeID)
}
vdrSet[vdr.NodeID] = &validators.GetValidatorOutput{
NodeID: vdr.NodeID,
Expand All @@ -197,71 +198,79 @@ func (m *manager) GetValidatorSet(ctx context.Context, height uint64, subnetID i
}

for i := lastAcceptedHeight; i > height; i-- {
weightDiffs, err := m.state.GetValidatorWeightDiffs(i, subnetID)
err := m.applyValidatorDiffs(vdrSet, subnetID, height)
if err != nil {
return nil, err
}
}

for nodeID, weightDiff := range weightDiffs {
vdr, ok := vdrSet[nodeID]
if !ok {
// This node isn't in the current validator set.
vdr = &validators.GetValidatorOutput{
NodeID: nodeID,
}
vdrSet[nodeID] = vdr
}
// cache the validator set
validatorSetsCache.Put(height, vdrSet)

// The weight of this node changed at this block.
var op func(uint64, uint64) (uint64, error)
if weightDiff.Decrease {
// The validator's weight was decreased at this block, so in the
// prior block it was higher.
op = math.Add64
} else {
// The validator's weight was increased at this block, so in the
// prior block it was lower.
op = math.Sub[uint64]
}
endTime := m.clk.Time()
m.metrics.IncValidatorSetsCreated()
m.metrics.AddValidatorSetsDuration(endTime.Sub(startTime))
m.metrics.AddValidatorSetsHeightDiff(lastAcceptedHeight - height)
return vdrSet, nil
}

// Apply the weight change.
vdr.Weight, err = op(vdr.Weight, weightDiff.Amount)
if err != nil {
return nil, err
}
func (m *manager) applyValidatorDiffs(
vdrSet map[ids.NodeID]*validators.GetValidatorOutput,
subnetID ids.ID,
height uint64,
) error {
weightDiffs, err := m.state.GetValidatorWeightDiffs(height, subnetID)
if err != nil {
return err
}

if vdr.Weight == 0 {
// The validator's weight was 0 before this block so
// they weren't in the validator set.
delete(vdrSet, nodeID)
for nodeID, weightDiff := range weightDiffs {
vdr, ok := vdrSet[nodeID]
if !ok {
// This node isn't in the current validator set.
vdr = &validators.GetValidatorOutput{
NodeID: nodeID,
}
vdrSet[nodeID] = vdr
}

pkDiffs, err := m.state.GetValidatorPublicKeyDiffs(i)
// The weight of this node changed at this block.
if weightDiff.Decrease {
// The validator's weight was decreased at this block, so in the
// prior block it was higher.
vdr.Weight, err = math.Add64(vdr.Weight, weightDiff.Amount)
} else {
// The validator's weight was increased at this block, so in the
// prior block it was lower.
vdr.Weight, err = math.Sub(vdr.Weight, weightDiff.Amount)
}
if err != nil {
return nil, err
return err
}

for nodeID, pk := range pkDiffs {
// pkDiffs includes all primary network key diffs, if we are
// fetching a subnet's validator set, we should ignore non-subnet
// validators.
if vdr, ok := vdrSet[nodeID]; ok {
// The validator's public key was removed at this block, so it
// was in the validator set before.
vdr.PublicKey = pk
}
if vdr.Weight == 0 {
// The validator's weight was 0 before this block so
// they weren't in the validator set.
delete(vdrSet, nodeID)
}
}

// cache the validator set
validatorSetsCache.Put(height, vdrSet)
pkDiffs, err := m.state.GetValidatorPublicKeyDiffs(height)
if err != nil {
return err
}

endTime := m.clk.Time()
m.metrics.IncValidatorSetsCreated()
m.metrics.AddValidatorSetsDuration(endTime.Sub(startTime))
m.metrics.AddValidatorSetsHeightDiff(lastAcceptedHeight - height)
return vdrSet, nil
for nodeID, pk := range pkDiffs {
// pkDiffs includes all primary network key diffs, if we are
// fetching a subnet's validator set, we should ignore non-subnet
// validators.
if vdr, ok := vdrSet[nodeID]; ok {
// The validator's public key was removed at this block, so it
// was in the validator set before.
vdr.PublicKey = pk
}
}
return nil
}

// GetSubnetID returns subnetID of the specified chainID
Expand Down