From ff4aef512bb8c646932b33fe102e901a28c7ae52 Mon Sep 17 00:00:00 2001 From: Aaron Buchwald Date: Wed, 18 Sep 2024 19:45:14 -0400 Subject: [PATCH 1/2] Separate proposer monitor from vm into internal package --- .../validators}/proposer_monitor.go | 33 +++++++++++-------- vm/resolutions.go | 8 +++++ vm/vm.go | 5 +-- 3 files changed, 31 insertions(+), 15 deletions(-) rename {vm => internal/validators}/proposer_monitor.go (82%) 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 c2a64a8a7a..6772669670 100644 --- a/vm/resolutions.go +++ b/vm/resolutions.go @@ -321,6 +321,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 fd2448e720..0a7e8bcd29 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 @@ -205,7 +206,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() From 96ba38dd9eb93e69e127aa593c97dba0cd6e8bde Mon Sep 17 00:00:00 2001 From: Aaron Buchwald Date: Wed, 18 Sep 2024 19:48:30 -0400 Subject: [PATCH 2/2] Remove unused ValidatorsState from vm resolutions and chain deps --- chain/dependencies.go | 2 -- vm/resolutions.go | 4 ---- 2 files changed, 6 deletions(-) diff --git a/chain/dependencies.go b/chain/dependencies.go index 413485d272..b5a49cf180 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" @@ -72,7 +71,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/resolutions.go b/vm/resolutions.go index 6772669670..26295b1709 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) Registry() (chain.ActionRegistry, chain.AuthRegistry) { return vm.actionRegistry, vm.authRegistry }