Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate proposer monitor from vm into internal package #1574

Merged
merged 4 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading