From 40934bb50220c5cec73e95f038b8a3bdce2253f5 Mon Sep 17 00:00:00 2001 From: Dan Laine Date: Fri, 17 Nov 2023 15:26:22 -0500 Subject: [PATCH] `platformvm.VM` -- replace `Config` field with `validators.Manager` (#2319) --- vms/platformvm/block/builder/helpers_test.go | 6 +-- vms/platformvm/block/executor/helpers_test.go | 6 +-- vms/platformvm/state/state.go | 44 +++++++++---------- vms/platformvm/state/state_test.go | 4 +- vms/platformvm/txs/executor/helpers_test.go | 6 +-- .../validators/manager_benchmark_test.go | 4 +- vms/platformvm/vm.go | 2 +- vms/platformvm/vm_regression_test.go | 4 +- 8 files changed, 36 insertions(+), 40 deletions(-) diff --git a/vms/platformvm/block/builder/helpers_test.go b/vms/platformvm/block/builder/helpers_test.go index f419c38c4ac0..84778add2864 100644 --- a/vms/platformvm/block/builder/helpers_test.go +++ b/vms/platformvm/block/builder/helpers_test.go @@ -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) @@ -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, @@ -250,7 +250,7 @@ func defaultState( db, genesisBytes, prometheus.NewRegistry(), - cfg, + validators, execCfg, ctx, metrics.Noop, diff --git a/vms/platformvm/block/executor/helpers_test.go b/vms/platformvm/block/executor/helpers_test.go index 9c9135fe6f9f..ff0aa13a2ea1 100644 --- a/vms/platformvm/block/executor/helpers_test.go +++ b/vms/platformvm/block/executor/helpers_test.go @@ -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( @@ -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, @@ -280,7 +280,7 @@ func defaultState( db, genesisBytes, prometheus.NewRegistry(), - cfg, + validators, execCfg, ctx, metrics.Noop, diff --git a/vms/platformvm/state/state.go b/vms/platformvm/state/state.go index fd842f684eae..199b245008f7 100644 --- a/vms/platformvm/state/state.go +++ b/vms/platformvm/state/state.go @@ -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 @@ -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, @@ -460,7 +460,7 @@ func New( s, err := newState( db, metrics, - cfg, + validators, execCfg, ctx, metricsReg, @@ -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, @@ -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, @@ -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 } @@ -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) } @@ -2079,11 +2079,11 @@ 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, @@ -2091,7 +2091,7 @@ func (s *state) writeCurrentStakers(updateValidators bool, height uint64) error weightDiff.Amount, ) } else { - err = s.cfg.Validators.AddWeight(subnetID, nodeID, weightDiff.Amount) + err = s.validators.AddWeight(subnetID, nodeID, weightDiff.Amount) } } if err != nil { @@ -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 } diff --git a/vms/platformvm/state/state_test.go b/vms/platformvm/state/state_test.go index ae79415f4bbf..3c36310c0576 100644 --- a/vms/platformvm/state/state_test.go +++ b/vms/platformvm/state/state_test.go @@ -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(), diff --git a/vms/platformvm/txs/executor/helpers_test.go b/vms/platformvm/txs/executor/helpers_test.go index c26a865bdc6f..df3150e04bdd 100644 --- a/vms/platformvm/txs/executor/helpers_test.go +++ b/vms/platformvm/txs/executor/helpers_test.go @@ -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) @@ -218,7 +218,7 @@ func addSubnet( } func defaultState( - cfg *config.Config, + validators validators.Manager, ctx *snow.Context, db database.Database, rewards reward.Calculator, @@ -229,7 +229,7 @@ func defaultState( db, genesisBytes, prometheus.NewRegistry(), - cfg, + validators, execCfg, ctx, metrics.Noop, diff --git a/vms/platformvm/validators/manager_benchmark_test.go b/vms/platformvm/validators/manager_benchmark_test.go index 0664c085c942..155811d988ad 100644 --- a/vms/platformvm/validators/manager_benchmark_test.go +++ b/vms/platformvm/validators/manager_benchmark_test.go @@ -112,9 +112,7 @@ func BenchmarkGetValidatorSet(b *testing.B) { db, genesisBytes, prometheus.NewRegistry(), - &config.Config{ - Validators: vdrs, - }, + vdrs, execConfig, &snow.Context{ NetworkID: constants.UnitTestID, diff --git a/vms/platformvm/vm.go b/vms/platformvm/vm.go index 7f7568deb813..c312e4044e8b 100644 --- a/vms/platformvm/vm.go +++ b/vms/platformvm/vm.go @@ -139,7 +139,7 @@ func (vm *VM) Initialize( vm.db, genesisBytes, registerer, - &vm.Config, + vm.Config.Validators, execConfig, vm.ctx, vm.metrics, diff --git a/vms/platformvm/vm_regression_test.go b/vms/platformvm/vm_regression_test.go index 5b50c9895622..80b38a06c234 100644 --- a/vms/platformvm/vm_regression_test.go +++ b/vms/platformvm/vm_regression_test.go @@ -648,7 +648,7 @@ func TestRejectedStateRegressionInvalidValidatorTimestamp(t *testing.T) { vm.db, nil, prometheus.NewRegistry(), - &vm.Config, + vm.Config.Validators, execCfg, vm.ctx, metrics.Noop, @@ -955,7 +955,7 @@ func TestRejectedStateRegressionInvalidValidatorReward(t *testing.T) { vm.db, nil, prometheus.NewRegistry(), - &vm.Config, + vm.Config.Validators, execCfg, vm.ctx, metrics.Noop,