Skip to content

Commit 75c6cfd

Browse files
Separate proposer monitor from vm into internal package (#1574)
1 parent 7a8bb2d commit 75c6cfd

File tree

4 files changed

+31
-21
lines changed

4 files changed

+31
-21
lines changed

chain/dependencies.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"github.com/ava-labs/avalanchego/ids"
1111
"github.com/ava-labs/avalanchego/snow/engine/snowman/block"
12-
"github.com/ava-labs/avalanchego/snow/validators"
1312
"github.com/ava-labs/avalanchego/trace"
1413
"github.com/ava-labs/avalanchego/utils/logging"
1514
"github.com/ava-labs/avalanchego/utils/set"
@@ -73,7 +72,6 @@ type VM interface {
7372

7473
State() (merkledb.MerkleDB, error)
7574
StateManager() StateManager
76-
ValidatorState() validators.State
7775

7876
Mempool() Mempool
7977
IsRepeat(context.Context, []*Transaction, set.Bits, bool) set.Bits

vm/proposer_monitor.go renamed to internal/validators/proposer_monitor.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4-
package vm
4+
package validators
55

66
import (
77
"context"
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/ava-labs/avalanchego/cache"
1313
"github.com/ava-labs/avalanchego/ids"
14+
"github.com/ava-labs/avalanchego/snow"
1415
"github.com/ava-labs/avalanchego/snow/validators"
1516
"github.com/ava-labs/avalanchego/utils/crypto/bls"
1617
"github.com/ava-labs/avalanchego/utils/set"
@@ -23,8 +24,13 @@ const (
2324
proposerMonitorLRUSize = 60
2425
)
2526

27+
type Backend interface {
28+
PreferredHeight(ctx context.Context) (uint64, error)
29+
}
30+
2631
type ProposerMonitor struct {
27-
vm *VM
32+
backend Backend
33+
snowCtx *snow.Context
2834
proposer proposer.Windower
2935

3036
currentPHeight uint64
@@ -37,13 +43,14 @@ type ProposerMonitor struct {
3743
rl sync.Mutex
3844
}
3945

40-
func NewProposerMonitor(vm *VM) *ProposerMonitor {
46+
func NewProposerMonitor(backend Backend, snowCtx *snow.Context) *ProposerMonitor {
4147
return &ProposerMonitor{
42-
vm: vm,
48+
backend: backend,
49+
snowCtx: snowCtx,
4350
proposer: proposer.New(
44-
vm.snowCtx.ValidatorState,
45-
vm.snowCtx.SubnetID,
46-
vm.snowCtx.ChainID,
51+
snowCtx.ValidatorState,
52+
snowCtx.SubnetID,
53+
snowCtx.ChainID,
4754
),
4855
proposerCache: &cache.LRU[string, []ids.NodeID]{Size: proposerMonitorLRUSize},
4956
}
@@ -58,14 +65,14 @@ func (p *ProposerMonitor) refresh(ctx context.Context) error {
5865
return nil
5966
}
6067
start := time.Now()
61-
pHeight, err := p.vm.snowCtx.ValidatorState.GetCurrentHeight(ctx)
68+
pHeight, err := p.snowCtx.ValidatorState.GetCurrentHeight(ctx)
6269
if err != nil {
6370
return err
6471
}
65-
p.validators, err = p.vm.snowCtx.ValidatorState.GetValidatorSet(
72+
p.validators, err = p.snowCtx.ValidatorState.GetValidatorSet(
6673
ctx,
6774
pHeight,
68-
p.vm.snowCtx.SubnetID,
75+
p.snowCtx.SubnetID,
6976
)
7077
if err != nil {
7178
return err
@@ -78,7 +85,7 @@ func (p *ProposerMonitor) refresh(ctx context.Context) error {
7885
pks[string(bls.PublicKeyToCompressedBytes(v.PublicKey))] = struct{}{}
7986
}
8087
p.validatorPublicKeys = pks
81-
p.vm.snowCtx.Log.Info(
88+
p.snowCtx.Log.Info(
8289
"refreshed proposer monitor",
8390
zap.Uint64("previous", p.currentPHeight),
8491
zap.Uint64("new", pHeight),
@@ -105,14 +112,14 @@ func (p *ProposerMonitor) Proposers(
105112
if err := p.refresh(ctx); err != nil {
106113
return nil, err
107114
}
108-
preferredBlk, err := p.vm.GetStatefulBlock(ctx, p.vm.preferred)
115+
preferredHeight, err := p.backend.PreferredHeight(ctx)
109116
if err != nil {
110117
return nil, err
111118
}
112119
proposersToGossip := set.NewSet[ids.NodeID](diff * depth)
113120
udepth := uint64(depth)
114121
for i := uint64(1); i <= uint64(diff); i++ {
115-
height := preferredBlk.Hght + i
122+
height := preferredHeight + i
116123
key := fmt.Sprintf("%d-%d", height, p.currentPHeight)
117124
var proposers []ids.NodeID
118125
if v, ok := p.proposerCache.Get(key); ok {

vm/resolutions.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ func (vm *VM) SubnetID() ids.ID {
5050
return vm.snowCtx.SubnetID
5151
}
5252

53-
func (vm *VM) ValidatorState() validators.State {
54-
return vm.snowCtx.ValidatorState
55-
}
56-
5753
func (vm *VM) ActionRegistry() chain.ActionRegistry {
5854
return vm.actionRegistry
5955
}
@@ -329,6 +325,14 @@ func (vm *VM) PreferredBlock(ctx context.Context) (*chain.StatefulBlock, error)
329325
return vm.GetStatefulBlock(ctx, vm.preferred)
330326
}
331327

328+
func (vm *VM) PreferredHeight(ctx context.Context) (uint64, error) {
329+
preferredBlk, err := vm.GetStatefulBlock(ctx, vm.preferred)
330+
if err != nil {
331+
return 0, err
332+
}
333+
return preferredBlk.Hght, nil
334+
}
335+
332336
func (vm *VM) StopChan() chan struct{} {
333337
return vm.stop
334338
}

vm/vm.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"github.com/ava-labs/hypersdk/internal/network"
4040
"github.com/ava-labs/hypersdk/internal/pebble"
4141
"github.com/ava-labs/hypersdk/internal/trace"
42+
"github.com/ava-labs/hypersdk/internal/validators"
4243
"github.com/ava-labs/hypersdk/internal/workers"
4344
"github.com/ava-labs/hypersdk/state"
4445
"github.com/ava-labs/hypersdk/storage"
@@ -66,7 +67,7 @@ type VM struct {
6667

6768
snowCtx *snow.Context
6869
pkBytes []byte
69-
proposerMonitor *ProposerMonitor
70+
proposerMonitor *validators.ProposerMonitor
7071

7172
config Config
7273

@@ -208,7 +209,7 @@ func (vm *VM) Initialize(
208209
return err
209210
}
210211
vm.metrics = metrics
211-
vm.proposerMonitor = NewProposerMonitor(vm)
212+
vm.proposerMonitor = validators.NewProposerMonitor(vm, vm.snowCtx)
212213
vm.networkManager = network.NewManager(vm.snowCtx.Log, vm.snowCtx.NodeID, appSender)
213214

214215
pebbleConfig := pebble.NewDefaultConfig()

0 commit comments

Comments
 (0)