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

Refactor uptime tracking #1388

Merged
merged 152 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
152 commits
Select commit Hold shift + click to select a range
c2da67d
Bump avalanchego to master
darioush Aug 6, 2024
33466ce
always sign uptime messages (testing branch)
darioush Aug 6, 2024
964e193
nits
darioush Aug 6, 2024
a5cde37
cleanup
darioush Aug 6, 2024
c363886
assign to correct `err`
darioush Aug 6, 2024
0bf1de6
fix handler
darioush Aug 6, 2024
dd66fa7
Merge branches 'always-sign-uptime' and 'master' of github.com:ava-la…
darioush Aug 19, 2024
8b3fb1c
move ValidatorUptime type to subnet-evm
darioush Aug 19, 2024
008bc37
disable always signing
darioush Aug 19, 2024
48f0ab7
implement on the type itself
darioush Aug 20, 2024
991ff46
remove unneeded code
darioush Aug 20, 2024
fe05d33
fix ut
darioush Aug 20, 2024
0747262
Merge branch 'master' into always-sign-uptime-types
darioush Aug 20, 2024
c1074a5
Merge branch 'master' into always-sign-uptime-types
darioush Aug 26, 2024
ce36343
add validator state
ceyonur Sep 16, 2024
549d319
add pausable uptime manager
ceyonur Sep 16, 2024
9987248
remove stuttering name
ceyonur Sep 16, 2024
e1ef744
Merge branch 'master' into validator-state
ceyonur Sep 16, 2024
b33ffbe
Merge branch 'validator-state' into pausable-uptime-manager
ceyonur Sep 16, 2024
0f16af2
rename state listener
ceyonur Sep 16, 2024
df3ce63
Merge branch 'validator-state' into pausable-uptime-manager
ceyonur Sep 16, 2024
5f64be3
add uptime tracking to VM
ceyonur Sep 16, 2024
6ff4954
remove unused param
ceyonur Sep 16, 2024
55927b2
add wg for update validators
ceyonur Sep 16, 2024
92fba2e
Merge branch 'pausable-uptime-manager' into uptime-tracking
ceyonur Sep 16, 2024
bdc2cc3
update state before network shutdown
ceyonur Sep 16, 2024
657e542
restart bootstrapping status in test
ceyonur Sep 16, 2024
9d6d40e
Merge branch 'uptime-tracking' of https://github.com/ava-labs/subnet-…
ceyonur Sep 16, 2024
9eaa3f3
add get validator to state
ceyonur Sep 17, 2024
66747f8
rename uptime to validator
ceyonur Sep 17, 2024
86a68c8
fix mock state
ceyonur Sep 17, 2024
f43bae2
tests
ceyonur Sep 18, 2024
92f6b7e
Update plugin/evm/validators/state.go
ceyonur Sep 19, 2024
0db2041
use update enum
ceyonur Sep 19, 2024
c5520bc
Update plugin/evm/validators/state.go
ceyonur Sep 19, 2024
dea94af
Update plugin/evm/validators/state.go
ceyonur Sep 19, 2024
c0f6ff4
respond to comments
ceyonur Sep 19, 2024
b7de0f6
Merge branch 'validator-state' of https://github.com/ava-labs/subnet-…
ceyonur Sep 19, 2024
b566103
update avalanchego dep branch
ceyonur Sep 19, 2024
66ab74b
update avalanchego dep branch
ceyonur Sep 19, 2024
ad3a35a
reviews
ceyonur Sep 19, 2024
64fe238
reword errs
ceyonur Sep 19, 2024
33d24d1
Merge branch 'pausable-uptime-manager' of https://github.com/ava-labs…
ceyonur Sep 19, 2024
d7338da
fix test changes
ceyonur Sep 19, 2024
8eab611
Merge branch 'master' into uptime-tracking-base
ceyonur Sep 19, 2024
9ad5528
fix upgrades after deactivating latest in context
ceyonur Sep 19, 2024
4536590
Merge branch 'uptime-tracking-base' into validator-state
ceyonur Sep 19, 2024
fc71949
Merge branch 'validator-state' into pausable-uptime-manager
ceyonur Sep 19, 2024
4304ac5
Merge branch 'pausable-uptime-manager' into uptime-tracking
ceyonur Sep 19, 2024
3225a32
use test branch from avalanchego
ceyonur Sep 20, 2024
df6ad02
use branch commit for ava version
ceyonur Sep 20, 2024
738003f
update e2e ava version
ceyonur Sep 20, 2024
29a501d
update avago dep
ceyonur Sep 20, 2024
71c7de0
remove extra line...
ceyonur Sep 20, 2024
cc6ce95
Merge branch 'master' into uptime-tracking-base
ceyonur Sep 20, 2024
bcd4c9c
Merge branch 'uptime-tracking-base' into validator-state
ceyonur Sep 20, 2024
a49dd8d
Merge branch 'validator-state' into pausable-uptime-manager
ceyonur Sep 20, 2024
27884f4
Merge branch 'pausable-uptime-manager' into uptime-tracking
ceyonur Sep 20, 2024
b9d6336
export struct
ceyonur Sep 26, 2024
2841bdb
Merge branch 'master' into always-sign-uptime-types
ceyonur Oct 9, 2024
beea962
implement acp118 signer and verifier
ceyonur Oct 9, 2024
701e2ec
avoid revalidating in sign
ceyonur Oct 9, 2024
1c98afa
refactor warp backend to use acp118 handler
ceyonur Oct 13, 2024
8851256
prune warp db before backend init
ceyonur Oct 13, 2024
1a75f6c
add cache tests
ceyonur Oct 14, 2024
7143fc5
remove uptime msg type
ceyonur Oct 14, 2024
243fe5f
add cache test
ceyonur Oct 14, 2024
02eb89b
fix linter
ceyonur Oct 14, 2024
f3554f6
Merge branch 'master' into use-acp-118-handler
ceyonur Oct 14, 2024
aced003
add validator uptimes
ceyonur Oct 14, 2024
752a5d9
Merge branch 'use-acp-118-handler' of github.com:ava-labs/subnet-evm …
ceyonur Oct 14, 2024
557ea62
Merge branch 'use-acp-118-handler' into validator-uptime-warp-msg
ceyonur Oct 14, 2024
1e46030
bump avago getcurrentvalidators branch
ceyonur Oct 14, 2024
6133246
rename get validator IDs to NodeIDs
ceyonur Oct 15, 2024
72bba65
sign uptime warp msg base on uptime calculator
ceyonur Oct 15, 2024
4db9689
add tests
ceyonur Oct 17, 2024
734b201
Merge branch 'master' into validator-state
ceyonur Oct 28, 2024
3e94861
Merge branch 'validator-state' into pausable-uptime-manager
ceyonur Oct 28, 2024
cab1ddf
reviews
ceyonur Oct 29, 2024
d1a0ae8
Merge branch 'pausable-uptime-manager' into uptime-tracking
ceyonur Oct 29, 2024
5f8bf5b
conflict fix
ceyonur Oct 29, 2024
0ba2869
custom err msg
ceyonur Oct 29, 2024
374d885
add listener mock
ceyonur Oct 29, 2024
1fcca58
Merge branch 'master' into validator-state
ceyonur Oct 29, 2024
c41f39c
Merge branch 'validator-state' into pausable-uptime-manager
ceyonur Oct 29, 2024
f828d53
Merge branch 'pausable-uptime-manager' into uptime-tracking
ceyonur Oct 29, 2024
9e707ad
bump avago test branch
ceyonur Oct 29, 2024
cc73266
remove config
ceyonur Oct 29, 2024
8ef763f
remove api changes
ceyonur Oct 29, 2024
2a9da82
Revert "remove api changes"
ceyonur Oct 29, 2024
1e1c4c7
Merge branch 'uptime-tracking' into sign-validator-uptime-warp-msg
ceyonur Oct 29, 2024
46c3316
remove wrapped cache
ceyonur Oct 29, 2024
9a50ee8
use non-version db for validatorsDB
ceyonur Oct 29, 2024
58b9c35
Merge branch 'uptime-tracking' into uptime-tracking-api
ceyonur Oct 29, 2024
b45725d
Merge branch 'uptime-tracking' into sign-validator-uptime-warp-msg
ceyonur Oct 29, 2024
af39b2c
remove errs from resume and pause
ceyonur Oct 30, 2024
d5d3545
check after stopping
ceyonur Oct 30, 2024
6fffc2b
use expectedTime in tests
ceyonur Oct 31, 2024
4625f7c
reviews
ceyonur Oct 31, 2024
e1975cf
Update plugin/evm/vm.go
ceyonur Oct 31, 2024
74bd249
fix len
ceyonur Oct 31, 2024
e5ab9f2
Merge branch 'uptime-tracking' into uptime-tracking-api
ceyonur Oct 31, 2024
7c33221
Merge branch 'pausable-uptime-manager' into uptime-tracking
ceyonur Oct 31, 2024
e00a059
Merge branch 'uptime-tracking-api' into sign-validator-uptime-warp-msg
ceyonur Oct 31, 2024
0a98101
fix tests
ceyonur Nov 1, 2024
52b83e2
update avago branch
ceyonur Nov 4, 2024
93bedf3
use ctx from utils
ceyonur Nov 5, 2024
2ebf56b
add empty check for source address
ceyonur Nov 5, 2024
733da4c
Merge branch 'master' into pausable-uptime-manager
ceyonur Nov 5, 2024
347055c
Merge branch 'pausable-uptime-manager' into uptime-tracking
ceyonur Nov 5, 2024
335d8bc
Merge branch 'uptime-tracking' into uptime-tracking-api
ceyonur Nov 5, 2024
967765c
Merge branch 'uptime-tracking-api' into sign-validator-uptime-warp-msg
ceyonur Nov 5, 2024
33a30f0
nits
ceyonur Nov 5, 2024
2aebfe6
remove log
ceyonur Nov 5, 2024
25076c3
disable validators api by default
ceyonur Nov 5, 2024
9d3a1b3
fix test context
ceyonur Nov 5, 2024
d737c11
Merge branch 'master' into uptime-tracking
ceyonur Nov 5, 2024
6468d36
use interfaces from pkgs
ceyonur Nov 5, 2024
10c4bd4
improve comments
ceyonur Nov 6, 2024
9490aae
Merge branch 'uptime-tracking' into uptime-tracking-api
ceyonur Nov 6, 2024
3912f84
Uptime validation nits (#1378)
ceyonur Nov 6, 2024
2e7f70e
Update plugin/evm/validators/state.go
ceyonur Nov 6, 2024
3b69173
pass locker
ceyonur Nov 6, 2024
fd462c5
Merge branch 'uptime-tracking-api' of https://github.com/ava-labs/sub…
ceyonur Nov 6, 2024
1b3b7c9
Merge branch 'uptime-tracking-api' into sign-validator-uptime-warp-msg
ceyonur Nov 6, 2024
8c7f8d3
rename addresscall verifier fn
ceyonur Nov 7, 2024
5974170
new fields and refactorings
ceyonur Nov 8, 2024
0388e84
Merge branch 'uptime-tracking' into uptime-tracking-api
ceyonur Nov 8, 2024
7dca70e
add new fields
ceyonur Nov 8, 2024
4632c72
Merge branch 'uptime-tracking-api' into sign-validator-uptime-warp-msg
ceyonur Nov 8, 2024
85db94f
merge nits
ceyonur Nov 8, 2024
48ef70f
Merge branch 'master' into uptime-tracking
ceyonur Nov 8, 2024
424e7a7
fix linter
ceyonur Nov 8, 2024
4fa19b9
clarify comments
ceyonur Nov 8, 2024
4559ab9
Merge branch 'uptime-tracking' into uptime-tracking-api
ceyonur Nov 8, 2024
341006f
Merge branch 'uptime-tracking-api' into sign-validator-uptime-warp-msg
ceyonur Nov 8, 2024
3d5261d
update comment
ceyonur Nov 8, 2024
b5db9ae
remove getnodeID
ceyonur Nov 9, 2024
9f80c5d
Merge branch 'sign-validator-uptime-warp-msg' of https://github.com/a…
ceyonur Nov 9, 2024
a80c028
bump to poc branch
ceyonur Nov 9, 2024
a417614
Merge branch 'uptime-tracking' into uptime-tracking-api
ceyonur Nov 9, 2024
6fdeeec
Merge branch 'uptime-tracking-api' into sign-validator-uptime-warp-msg
ceyonur Nov 9, 2024
c39c7e0
enable validators API by default
ceyonur Nov 12, 2024
64eb51c
Merge branch 'uptime-tracking-api' into sign-validator-uptime-warp-msg
ceyonur Nov 12, 2024
07205f4
refactor uptime tracking
ceyonur Nov 14, 2024
d82a29c
Merge branch 'master' into refactor-uptime-tracking
ceyonur Nov 14, 2024
377a22f
Remove unused var
ceyonur Nov 14, 2024
8390b5f
regen mocks
ceyonur Nov 14, 2024
42532d1
add GetValidatorAndUptime to validator manager
ceyonur Nov 15, 2024
c881884
Update plugin/evm/validators/interfaces/interfaces.go
ceyonur Nov 15, 2024
c91b3a7
Update plugin/evm/validators/manager.go
ceyonur Nov 15, 2024
5793d25
Merge branch 'master' into refactor-uptime-tracking
darioush Nov 15, 2024
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
8 changes: 4 additions & 4 deletions plugin/evm/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ func (api *ValidatorsAPI) GetCurrentValidators(_ *http.Request, _ *struct{}, rep
api.vm.ctx.Lock.RLock()
defer api.vm.ctx.Lock.RUnlock()

vIDs := api.vm.validatorState.GetValidationIDs()
vIDs := api.vm.validatorsManager.GetValidationIDs()

reply.Validators = make([]CurrentValidator, 0, vIDs.Len())

for _, vID := range vIDs.List() {
validator, err := api.vm.validatorState.GetValidator(vID)
validator, err := api.vm.validatorsManager.GetValidator(vID)
if err != nil {
return err
}

isConnected := api.vm.uptimeManager.IsConnected(validator.NodeID)
isConnected := api.vm.validatorsManager.IsConnected(validator.NodeID)

uptime, _, err := api.vm.uptimeManager.CalculateUptime(validator.NodeID)
uptime, _, err := api.vm.validatorsManager.CalculateUptime(validator.NodeID)
if err != nil {
return err
}
Expand Down
30 changes: 30 additions & 0 deletions plugin/evm/validators/interfaces/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package interfaces

import (
"context"
"time"

"github.com/ava-labs/avalanchego/ids"
avalancheuptime "github.com/ava-labs/avalanchego/snow/uptime"
stateinterfaces "github.com/ava-labs/subnet-evm/plugin/evm/validators/state/interfaces"
)

type ValidatorReader interface {
// GetValidatorAndUptime returns the uptime of the validator specified by validationID
GetValidatorAndUptime(validationID ids.ID) (stateinterfaces.Validator, time.Duration, time.Time, error)
}

type Manager interface {
stateinterfaces.State
avalancheuptime.Manager
ValidatorReader

// Sync updates the validator set managed
// by the manager
Sync(ctx context.Context) error
// DispatchSync starts the sync process
DispatchSync(ctx context.Context)
}
161 changes: 161 additions & 0 deletions plugin/evm/validators/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package validators

import (
"context"
"fmt"
"time"

"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
avalancheuptime "github.com/ava-labs/avalanchego/snow/uptime"
avalanchevalidators "github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/subnet-evm/plugin/evm/validators/interfaces"
validators "github.com/ava-labs/subnet-evm/plugin/evm/validators/state"
stateinterfaces "github.com/ava-labs/subnet-evm/plugin/evm/validators/state/interfaces"
"github.com/ava-labs/subnet-evm/plugin/evm/validators/uptime"
uptimeinterfaces "github.com/ava-labs/subnet-evm/plugin/evm/validators/uptime/interfaces"

"github.com/ethereum/go-ethereum/log"
)

const (
SyncFrequency = 1 * time.Minute
)

type manager struct {
chainCtx *snow.Context
stateinterfaces.State
uptimeinterfaces.PausableManager
}

// NewManager returns a new validator manager
// that manages the validator state and the uptime manager.
func NewManager(
ctx *snow.Context,
db database.Database,
clock *mockable.Clock,
) (interfaces.Manager, error) {
validatorState, err := validators.NewState(db)
if err != nil {
return nil, fmt.Errorf("failed to initialize validator state: %w", err)
}

// Initialize uptime manager
uptimeManager := uptime.NewPausableManager(avalancheuptime.NewManager(validatorState, clock))
validatorState.RegisterListener(uptimeManager)

return &manager{
chainCtx: ctx,
State: validatorState,
PausableManager: uptimeManager,
}, nil
}

// GetValidatorAndUptime returns the calculated uptime of the validator specified by validationID
// and the last updated time.
// GetValidatorAndUptime holds the chain context lock while performing the operation and can be called concurrently.
func (m *manager) GetValidatorAndUptime(validationID ids.ID) (stateinterfaces.Validator, time.Duration, time.Time, error) {
// lock the state
m.chainCtx.Lock.RLock()
defer m.chainCtx.Lock.RUnlock()

// Get validator first
vdr, err := m.GetValidator(validationID)
if err != nil {
return stateinterfaces.Validator{}, 0, time.Time{}, fmt.Errorf("failed to get validator: %w", err)
}

uptime, lastUpdated, err := m.CalculateUptime(vdr.NodeID)
if err != nil {
return stateinterfaces.Validator{}, 0, time.Time{}, fmt.Errorf("failed to get uptime: %w", err)
}

return vdr, uptime, lastUpdated, nil
}

// DispatchSync starts the sync process
// DispatchSync holds the chain context lock while performing the sync.
func (m *manager) DispatchSync(ctx context.Context) {
ticker := time.NewTicker(SyncFrequency)
defer ticker.Stop()

for {
select {
case <-ticker.C:
m.chainCtx.Lock.Lock()
darioush marked this conversation as resolved.
Show resolved Hide resolved
if err := m.Sync(ctx); err != nil {
log.Error("failed to sync validators", "error", err)
}
m.chainCtx.Lock.Unlock()
case <-ctx.Done():
return
}
}
}

// Sync synchronizes the validator state with the current validator set
// and writes the state to the database.
// Sync is not safe to call concurrently and should be called with the chain context locked.
func (m *manager) Sync(ctx context.Context) error {
now := time.Now()
log.Debug("performing validator sync")
// get current validator set
currentValidatorSet, _, err := m.chainCtx.ValidatorState.GetCurrentValidatorSet(ctx, m.chainCtx.SubnetID)
if err != nil {
return fmt.Errorf("failed to get current validator set: %w", err)
}

// load the current validator set into the validator state
if err := loadValidators(m.State, currentValidatorSet); err != nil {
return fmt.Errorf("failed to load current validators: %w", err)
}

// write validators to the database
if err := m.State.WriteState(); err != nil {
return fmt.Errorf("failed to write validator state: %w", err)
}

// TODO: add metrics
log.Debug("validator sync complete", "duration", time.Since(now))
return nil
}

// loadValidators loads the [validators] into the validator state [validatorState]
func loadValidators(validatorState stateinterfaces.State, newValidators map[ids.ID]*avalanchevalidators.GetCurrentValidatorOutput) error {
currentValidationIDs := validatorState.GetValidationIDs()
// first check if we need to delete any existing validators
for vID := range currentValidationIDs {
// if the validator is not in the new set of validators
// delete the validator
if _, exists := newValidators[vID]; !exists {
validatorState.DeleteValidator(vID)
}
}

// then load the new validators
for newVID, newVdr := range newValidators {
currentVdr := stateinterfaces.Validator{
ValidationID: newVID,
NodeID: newVdr.NodeID,
Weight: newVdr.Weight,
StartTimestamp: newVdr.StartTime,
IsActive: newVdr.IsActive,
IsSoV: newVdr.IsSoV,
}
if currentValidationIDs.Contains(newVID) {
if err := validatorState.UpdateValidator(currentVdr); err != nil {
return err
}
} else {
if err := validatorState.AddValidator(currentVdr); err != nil {
return err
}
}
}
return nil
}
Loading
Loading