Skip to content

Commit

Permalink
Split writeCurrentStakers into multiple functions (#3500)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Oct 29, 2024
1 parent f500dee commit 801762c
Show file tree
Hide file tree
Showing 3 changed files with 324 additions and 230 deletions.
29 changes: 29 additions & 0 deletions vms/platformvm/state/stakers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package state

import (
"errors"
"fmt"

"github.com/google/btree"

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

func (d *diffValidator) WeightDiff() (ValidatorWeightDiff, error) {
weightDiff := ValidatorWeightDiff{
Decrease: d.validatorStatus == deleted,
}
if d.validatorStatus != unmodified {
weightDiff.Amount = d.validator.Weight
}

for _, staker := range d.deletedDelegators {
if err := weightDiff.Add(true, staker.Weight); err != nil {
return ValidatorWeightDiff{}, fmt.Errorf("failed to decrease node weight diff: %w", err)
}
}

addedDelegatorIterator := iterator.FromTree(d.addedDelegators)
defer addedDelegatorIterator.Release()

for addedDelegatorIterator.Next() {
staker := addedDelegatorIterator.Value()

if err := weightDiff.Add(false, staker.Weight); err != nil {
return ValidatorWeightDiff{}, fmt.Errorf("failed to increase node weight diff: %w", err)
}
}

return weightDiff, nil
}

// GetValidator attempts to fetch the validator with the given subnetID and
// nodeID.
// Invariant: Assumes that the validator will never be removed and then added.
Expand Down
Loading

0 comments on commit 801762c

Please sign in to comment.