Skip to content

Commit

Permalink
platformvm.VM -- replace Config field with validators.Manager (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Laine authored Nov 17, 2023
1 parent a803f38 commit 40934bb
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 40 deletions.
6 changes: 3 additions & 3 deletions vms/platformvm/block/builder/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func newEnvironment(t *testing.T) *environment {
res.fx = defaultFx(t, res.clk, res.ctx.Log, res.isBootstrapped.Get())

rewardsCalc := reward.NewCalculator(res.config.RewardConfig)
res.state = defaultState(t, res.config, res.ctx, res.baseDB, rewardsCalc)
res.state = defaultState(t, res.config.Validators, res.ctx, res.baseDB, rewardsCalc)

res.atomicUTXOs = avax.NewAtomicUTXOManager(res.ctx.SharedMemory, txs.Codec)
res.uptimes = uptime.NewManager(res.state, res.clk)
Expand Down Expand Up @@ -237,7 +237,7 @@ func addSubnet(t *testing.T, env *environment) {

func defaultState(
t *testing.T,
cfg *config.Config,
validators validators.Manager,
ctx *snow.Context,
db database.Database,
rewards reward.Calculator,
Expand All @@ -250,7 +250,7 @@ func defaultState(
db,
genesisBytes,
prometheus.NewRegistry(),
cfg,
validators,
execCfg,
ctx,
metrics.Noop,
Expand Down
6 changes: 3 additions & 3 deletions vms/platformvm/block/executor/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func newEnvironment(t *testing.T, ctrl *gomock.Controller) *environment {
res.atomicUTXOs = avax.NewAtomicUTXOManager(res.ctx.SharedMemory, txs.Codec)

if ctrl == nil {
res.state = defaultState(res.config, res.ctx, res.baseDB, rewardsCalc)
res.state = defaultState(res.config.Validators, res.ctx, res.baseDB, rewardsCalc)
res.uptimes = uptime.NewManager(res.state, res.clk)
res.utxosHandler = utxo.NewHandler(res.ctx, res.clk, res.fx)
res.txBuilder = p_tx_builder.New(
Expand Down Expand Up @@ -269,7 +269,7 @@ func addSubnet(env *environment) {
}

func defaultState(
cfg *config.Config,
validators validators.Manager,
ctx *snow.Context,
db database.Database,
rewards reward.Calculator,
Expand All @@ -280,7 +280,7 @@ func defaultState(
db,
genesisBytes,
prometheus.NewRegistry(),
cfg,
validators,
execCfg,
ctx,
metrics.Noop,
Expand Down
44 changes: 22 additions & 22 deletions vms/platformvm/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,10 @@ type stateBlk struct {
type state struct {
validatorState

cfg *config.Config
ctx *snow.Context
metrics metrics.Metrics
rewards reward.Calculator
validators validators.Manager
ctx *snow.Context
metrics metrics.Metrics
rewards reward.Calculator

baseDB *versiondb.Database

Expand Down Expand Up @@ -451,7 +451,7 @@ func New(
db database.Database,
genesisBytes []byte,
metricsReg prometheus.Registerer,
cfg *config.Config,
validators validators.Manager,
execCfg *config.ExecutionConfig,
ctx *snow.Context,
metrics metrics.Metrics,
Expand All @@ -460,7 +460,7 @@ func New(
s, err := newState(
db,
metrics,
cfg,
validators,
execCfg,
ctx,
metricsReg,
Expand Down Expand Up @@ -504,7 +504,7 @@ func New(
func newState(
db database.Database,
metrics metrics.Metrics,
cfg *config.Config,
validators validators.Manager,
execCfg *config.ExecutionConfig,
ctx *snow.Context,
metricsReg prometheus.Registerer,
Expand Down Expand Up @@ -627,11 +627,11 @@ func newState(
return &state{
validatorState: newValidatorState(),

cfg: cfg,
ctx: ctx,
metrics: metrics,
rewards: rewards,
baseDB: baseDB,
validators: validators,
ctx: ctx,
metrics: metrics,
rewards: rewards,
baseDB: baseDB,

addedBlockIDs: make(map[uint64]ids.ID),
blockIDCache: blockIDCache,
Expand Down Expand Up @@ -1661,21 +1661,21 @@ func (s *state) loadPendingValidators() error {
// been called.
func (s *state) initValidatorSets() error {
for subnetID, validators := range s.currentStakers.validators {
if s.cfg.Validators.Count(subnetID) != 0 {
if s.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 {
validatorStaker := validator.validator
if err := s.cfg.Validators.AddStaker(subnetID, nodeID, validatorStaker.PublicKey, validatorStaker.TxID, validatorStaker.Weight); err != nil {
if err := s.validators.AddStaker(subnetID, nodeID, validatorStaker.PublicKey, validatorStaker.TxID, validatorStaker.Weight); err != nil {
return err
}

delegatorIterator := NewTreeIterator(validator.delegators)
for delegatorIterator.Next() {
delegatorStaker := delegatorIterator.Value()
if err := s.cfg.Validators.AddWeight(subnetID, nodeID, delegatorStaker.Weight); err != nil {
if err := s.validators.AddWeight(subnetID, nodeID, delegatorStaker.Weight); err != nil {
delegatorIterator.Release()
return err
}
Expand All @@ -1684,8 +1684,8 @@ func (s *state) initValidatorSets() error {
}
}

s.metrics.SetLocalStake(s.cfg.Validators.GetWeight(constants.PrimaryNetworkID, s.ctx.NodeID))
totalWeight, err := s.cfg.Validators.TotalWeight(constants.PrimaryNetworkID)
s.metrics.SetLocalStake(s.validators.GetWeight(constants.PrimaryNetworkID, s.ctx.NodeID))
totalWeight, err := s.validators.TotalWeight(constants.PrimaryNetworkID)
if err != nil {
return fmt.Errorf("failed to get total weight of primary network validators: %w", err)
}
Expand Down Expand Up @@ -2079,19 +2079,19 @@ func (s *state) writeCurrentStakers(updateValidators bool, height uint64) error
}

if weightDiff.Decrease {
err = s.cfg.Validators.RemoveWeight(subnetID, nodeID, weightDiff.Amount)
err = s.validators.RemoveWeight(subnetID, nodeID, weightDiff.Amount)
} else {
if validatorDiff.validatorStatus == added {
staker := validatorDiff.validator
err = s.cfg.Validators.AddStaker(
err = s.validators.AddStaker(
subnetID,
nodeID,
staker.PublicKey,
staker.TxID,
weightDiff.Amount,
)
} else {
err = s.cfg.Validators.AddWeight(subnetID, nodeID, weightDiff.Amount)
err = s.validators.AddWeight(subnetID, nodeID, weightDiff.Amount)
}
}
if err != nil {
Expand All @@ -2107,12 +2107,12 @@ func (s *state) writeCurrentStakers(updateValidators bool, height uint64) error
return nil
}

totalWeight, err := s.cfg.Validators.TotalWeight(constants.PrimaryNetworkID)
totalWeight, err := s.validators.TotalWeight(constants.PrimaryNetworkID)
if err != nil {
return fmt.Errorf("failed to get total weight of primary network: %w", err)
}

s.metrics.SetLocalStake(s.cfg.Validators.GetWeight(constants.PrimaryNetworkID, s.ctx.NodeID))
s.metrics.SetLocalStake(s.validators.GetWeight(constants.PrimaryNetworkID, s.ctx.NodeID))
s.metrics.SetTotalStake(totalWeight)
return nil
}
Expand Down
4 changes: 1 addition & 3 deletions vms/platformvm/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,7 @@ func newStateFromDB(require *require.Assertions, db database.Database) State {
state, err := newState(
db,
metrics.Noop,
&config.Config{
Validators: validators.NewManager(),
},
validators.NewManager(),
execCfg,
&snow.Context{},
prometheus.NewRegistry(),
Expand Down
6 changes: 3 additions & 3 deletions vms/platformvm/txs/executor/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func newEnvironment(t *testing.T, postBanff, postCortina bool) *environment {
fx := defaultFx(clk, ctx.Log, isBootstrapped.Get())

rewards := reward.NewCalculator(config.RewardConfig)
baseState := defaultState(&config, ctx, baseDB, rewards)
baseState := defaultState(config.Validators, ctx, baseDB, rewards)

atomicUTXOs := avax.NewAtomicUTXOManager(ctx.SharedMemory, txs.Codec)
uptimes := uptime.NewManager(baseState, clk)
Expand Down Expand Up @@ -218,7 +218,7 @@ func addSubnet(
}

func defaultState(
cfg *config.Config,
validators validators.Manager,
ctx *snow.Context,
db database.Database,
rewards reward.Calculator,
Expand All @@ -229,7 +229,7 @@ func defaultState(
db,
genesisBytes,
prometheus.NewRegistry(),
cfg,
validators,
execCfg,
ctx,
metrics.Noop,
Expand Down
4 changes: 1 addition & 3 deletions vms/platformvm/validators/manager_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ func BenchmarkGetValidatorSet(b *testing.B) {
db,
genesisBytes,
prometheus.NewRegistry(),
&config.Config{
Validators: vdrs,
},
vdrs,
execConfig,
&snow.Context{
NetworkID: constants.UnitTestID,
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (vm *VM) Initialize(
vm.db,
genesisBytes,
registerer,
&vm.Config,
vm.Config.Validators,
execConfig,
vm.ctx,
vm.metrics,
Expand Down
4 changes: 2 additions & 2 deletions vms/platformvm/vm_regression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ func TestRejectedStateRegressionInvalidValidatorTimestamp(t *testing.T) {
vm.db,
nil,
prometheus.NewRegistry(),
&vm.Config,
vm.Config.Validators,
execCfg,
vm.ctx,
metrics.Noop,
Expand Down Expand Up @@ -955,7 +955,7 @@ func TestRejectedStateRegressionInvalidValidatorReward(t *testing.T) {
vm.db,
nil,
prometheus.NewRegistry(),
&vm.Config,
vm.Config.Validators,
execCfg,
vm.ctx,
metrics.Noop,
Expand Down

0 comments on commit 40934bb

Please sign in to comment.