From 75c6cfdd0c7be33c788d2119eb40430233bf63a2 Mon Sep 17 00:00:00 2001 From: aaronbuchwald Date: Thu, 19 Sep 2024 07:58:39 -0400 Subject: [PATCH] Separate proposer monitor from vm into internal package (#1574) --- chain/dependencies.go | 2 -- .../validators}/proposer_monitor.go | 33 +++++++++++-------- vm/resolutions.go | 12 ++++--- vm/vm.go | 5 +-- 4 files changed, 31 insertions(+), 21 deletions(-) rename {vm => internal/validators}/proposer_monitor.go (82%) diff --git a/chain/dependencies.go b/chain/dependencies.go index 8c656de94c..e66946061a 100644 --- a/chain/dependencies.go +++ b/chain/dependencies.go @@ -9,7 +9,6 @@ import ( "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/snow/engine/snowman/block" - "github.com/ava-labs/avalanchego/snow/validators" "github.com/ava-labs/avalanchego/trace" "github.com/ava-labs/avalanchego/utils/logging" "github.com/ava-labs/avalanchego/utils/set" @@ -73,7 +72,6 @@ type VM interface { State() (merkledb.MerkleDB, error) StateManager() StateManager - ValidatorState() validators.State Mempool() Mempool IsRepeat(context.Context, []*Transaction, set.Bits, bool) set.Bits diff --git a/vm/proposer_monitor.go b/internal/validators/proposer_monitor.go similarity index 82% rename from vm/proposer_monitor.go rename to internal/validators/proposer_monitor.go index 41f10ede1f..b97d3aecf9 100644 --- a/vm/proposer_monitor.go +++ b/internal/validators/proposer_monitor.go @@ -1,7 +1,7 @@ // Copyright (C) 2024, Ava Labs, Inc. All rights reserved. // See the file LICENSE for licensing terms. -package vm +package validators import ( "context" @@ -11,6 +11,7 @@ import ( "github.com/ava-labs/avalanchego/cache" "github.com/ava-labs/avalanchego/ids" + "github.com/ava-labs/avalanchego/snow" "github.com/ava-labs/avalanchego/snow/validators" "github.com/ava-labs/avalanchego/utils/crypto/bls" "github.com/ava-labs/avalanchego/utils/set" @@ -23,8 +24,13 @@ const ( proposerMonitorLRUSize = 60 ) +type Backend interface { + PreferredHeight(ctx context.Context) (uint64, error) +} + type ProposerMonitor struct { - vm *VM + backend Backend + snowCtx *snow.Context proposer proposer.Windower currentPHeight uint64 @@ -37,13 +43,14 @@ type ProposerMonitor struct { rl sync.Mutex } -func NewProposerMonitor(vm *VM) *ProposerMonitor { +func NewProposerMonitor(backend Backend, snowCtx *snow.Context) *ProposerMonitor { return &ProposerMonitor{ - vm: vm, + backend: backend, + snowCtx: snowCtx, proposer: proposer.New( - vm.snowCtx.ValidatorState, - vm.snowCtx.SubnetID, - vm.snowCtx.ChainID, + snowCtx.ValidatorState, + snowCtx.SubnetID, + snowCtx.ChainID, ), proposerCache: &cache.LRU[string, []ids.NodeID]{Size: proposerMonitorLRUSize}, } @@ -58,14 +65,14 @@ func (p *ProposerMonitor) refresh(ctx context.Context) error { return nil } start := time.Now() - pHeight, err := p.vm.snowCtx.ValidatorState.GetCurrentHeight(ctx) + pHeight, err := p.snowCtx.ValidatorState.GetCurrentHeight(ctx) if err != nil { return err } - p.validators, err = p.vm.snowCtx.ValidatorState.GetValidatorSet( + p.validators, err = p.snowCtx.ValidatorState.GetValidatorSet( ctx, pHeight, - p.vm.snowCtx.SubnetID, + p.snowCtx.SubnetID, ) if err != nil { return err @@ -78,7 +85,7 @@ func (p *ProposerMonitor) refresh(ctx context.Context) error { pks[string(bls.PublicKeyToCompressedBytes(v.PublicKey))] = struct{}{} } p.validatorPublicKeys = pks - p.vm.snowCtx.Log.Info( + p.snowCtx.Log.Info( "refreshed proposer monitor", zap.Uint64("previous", p.currentPHeight), zap.Uint64("new", pHeight), @@ -105,14 +112,14 @@ func (p *ProposerMonitor) Proposers( if err := p.refresh(ctx); err != nil { return nil, err } - preferredBlk, err := p.vm.GetStatefulBlock(ctx, p.vm.preferred) + preferredHeight, err := p.backend.PreferredHeight(ctx) if err != nil { return nil, err } proposersToGossip := set.NewSet[ids.NodeID](diff * depth) udepth := uint64(depth) for i := uint64(1); i <= uint64(diff); i++ { - height := preferredBlk.Hght + i + height := preferredHeight + i key := fmt.Sprintf("%d-%d", height, p.currentPHeight) var proposers []ids.NodeID if v, ok := p.proposerCache.Get(key); ok { diff --git a/vm/resolutions.go b/vm/resolutions.go index a8071511c1..577c0233e0 100644 --- a/vm/resolutions.go +++ b/vm/resolutions.go @@ -50,10 +50,6 @@ func (vm *VM) SubnetID() ids.ID { return vm.snowCtx.SubnetID } -func (vm *VM) ValidatorState() validators.State { - return vm.snowCtx.ValidatorState -} - func (vm *VM) ActionRegistry() chain.ActionRegistry { return vm.actionRegistry } @@ -329,6 +325,14 @@ func (vm *VM) PreferredBlock(ctx context.Context) (*chain.StatefulBlock, error) return vm.GetStatefulBlock(ctx, vm.preferred) } +func (vm *VM) PreferredHeight(ctx context.Context) (uint64, error) { + preferredBlk, err := vm.GetStatefulBlock(ctx, vm.preferred) + if err != nil { + return 0, err + } + return preferredBlk.Hght, nil +} + func (vm *VM) StopChan() chan struct{} { return vm.stop } diff --git a/vm/vm.go b/vm/vm.go index e7cde42232..826b21822b 100644 --- a/vm/vm.go +++ b/vm/vm.go @@ -39,6 +39,7 @@ import ( "github.com/ava-labs/hypersdk/internal/network" "github.com/ava-labs/hypersdk/internal/pebble" "github.com/ava-labs/hypersdk/internal/trace" + "github.com/ava-labs/hypersdk/internal/validators" "github.com/ava-labs/hypersdk/internal/workers" "github.com/ava-labs/hypersdk/state" "github.com/ava-labs/hypersdk/storage" @@ -66,7 +67,7 @@ type VM struct { snowCtx *snow.Context pkBytes []byte - proposerMonitor *ProposerMonitor + proposerMonitor *validators.ProposerMonitor config Config @@ -208,7 +209,7 @@ func (vm *VM) Initialize( return err } vm.metrics = metrics - vm.proposerMonitor = NewProposerMonitor(vm) + vm.proposerMonitor = validators.NewProposerMonitor(vm, vm.snowCtx) vm.networkManager = network.NewManager(vm.snowCtx.Log, vm.snowCtx.NodeID, appSender) pebbleConfig := pebble.NewDefaultConfig()