Skip to content

Commit

Permalink
Separate proposer monitor from vm into internal package (#1574)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronbuchwald authored Sep 19, 2024
1 parent 7a8bb2d commit 75c6cfd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
2 changes: 0 additions & 2 deletions chain/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
33 changes: 20 additions & 13 deletions vm/proposer_monitor.go → internal/validators/proposer_monitor.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand All @@ -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
Expand All @@ -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},
}
Expand All @@ -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
Expand All @@ -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),
Expand All @@ -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 {
Expand Down
12 changes: 8 additions & 4 deletions vm/resolutions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -66,7 +67,7 @@ type VM struct {

snowCtx *snow.Context
pkBytes []byte
proposerMonitor *ProposerMonitor
proposerMonitor *validators.ProposerMonitor

config Config

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 75c6cfd

Please sign in to comment.