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

move interfaces to separate pkgs #1379

Merged
merged 41 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
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
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
df6ad02
use branch commit for ava version
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
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
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
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
733da4c
Merge branch 'master' into pausable-uptime-manager
ceyonur Nov 5, 2024
66fa2aa
reviews
ceyonur Nov 5, 2024
1e98831
move interfaces to separate pkgs
ceyonur Nov 5, 2024
ad44a00
regen mock
ceyonur Nov 5, 2024
e987b9b
Merge branch 'master' into uptime-interfaces-pkgs
ceyonur Nov 5, 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
16 changes: 16 additions & 0 deletions plugin/evm/uptime/interfaces/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package interfaces

import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/uptime"
validatorsinterfaces "github.com/ava-labs/subnet-evm/plugin/evm/validators/interfaces"
)

type PausableManager interface {
uptime.Manager
validatorsinterfaces.StateCallbackListener
IsPaused(nodeID ids.NodeID) bool
}
12 changes: 2 additions & 10 deletions plugin/evm/uptime/pausable_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,16 @@ package uptime
import (
"errors"

"github.com/ava-labs/subnet-evm/plugin/evm/validators"
"github.com/ava-labs/subnet-evm/plugin/evm/uptime/interfaces"
"github.com/ethereum/go-ethereum/log"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/uptime"
"github.com/ava-labs/avalanchego/utils/set"
)

var _ validators.StateCallbackListener = &pausableManager{}

var errPausedDisconnect = errors.New("paused node cannot be disconnected")

type PausableManager interface {
uptime.Manager
validators.StateCallbackListener
IsPaused(nodeID ids.NodeID) bool
}

type pausableManager struct {
uptime.Manager
pausedVdrs set.Set[ids.NodeID]
Expand All @@ -33,7 +25,7 @@ type pausableManager struct {
}

// NewPausableManager takes an uptime.Manager and returns a PausableManager
func NewPausableManager(manager uptime.Manager) PausableManager {
func NewPausableManager(manager uptime.Manager) interfaces.PausableManager {
return &pausableManager{
pausedVdrs: make(set.Set[ids.NodeID]),
connectedVdrs: make(set.Set[ids.NodeID]),
Expand Down
15 changes: 8 additions & 7 deletions plugin/evm/uptime/pausable_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/uptime"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/subnet-evm/plugin/evm/uptime/interfaces"
"github.com/stretchr/testify/require"
)

Expand All @@ -20,11 +21,11 @@ func TestPausableManager(t *testing.T) {

tests := []struct {
name string
testFunc func(t *testing.T, up PausableManager, clk *mockable.Clock, s uptime.State)
testFunc func(t *testing.T, up interfaces.PausableManager, clk *mockable.Clock, s uptime.State)
}{
{
name: "Case 1: Connect, pause, start tracking",
testFunc: func(t *testing.T, up PausableManager, clk *mockable.Clock, s uptime.State) {
testFunc: func(t *testing.T, up interfaces.PausableManager, clk *mockable.Clock, s uptime.State) {
require := require.New(t)

// Connect before tracking
Expand Down Expand Up @@ -53,7 +54,7 @@ func TestPausableManager(t *testing.T) {
},
{
name: "Case 2: Start tracking, connect, pause, re-connect, resume",
testFunc: func(t *testing.T, up PausableManager, clk *mockable.Clock, s uptime.State) {
testFunc: func(t *testing.T, up interfaces.PausableManager, clk *mockable.Clock, s uptime.State) {
require := require.New(t)

// Start tracking
Expand Down Expand Up @@ -104,7 +105,7 @@ func TestPausableManager(t *testing.T) {
},
{
name: "Case 3: Pause, start tracking, connect, re-connect, resume",
testFunc: func(t *testing.T, up PausableManager, clk *mockable.Clock, s uptime.State) {
testFunc: func(t *testing.T, up interfaces.PausableManager, clk *mockable.Clock, s uptime.State) {
require := require.New(t)

// Pause before tracking
Expand Down Expand Up @@ -146,7 +147,7 @@ func TestPausableManager(t *testing.T) {
},
{
name: "Case 4: Start tracking, connect, pause, stop tracking, resume tracking",
testFunc: func(t *testing.T, up PausableManager, clk *mockable.Clock, s uptime.State) {
testFunc: func(t *testing.T, up interfaces.PausableManager, clk *mockable.Clock, s uptime.State) {
require := require.New(t)

// Start tracking and connect
Expand Down Expand Up @@ -218,7 +219,7 @@ func TestPausableManager(t *testing.T) {
}
}

func setupTestEnv(nodeID ids.NodeID, startTime time.Time) (PausableManager, *mockable.Clock, uptime.State) {
func setupTestEnv(nodeID ids.NodeID, startTime time.Time) (interfaces.PausableManager, *mockable.Clock, uptime.State) {
clk := mockable.Clock{}
clk.Set(startTime)
s := uptime.NewTestState()
Expand All @@ -232,7 +233,7 @@ func addTime(clk *mockable.Clock, duration time.Duration) time.Time {
return clk.Time()
}

func checkUptime(t *testing.T, up PausableManager, nodeID ids.NodeID, expectedUptime time.Duration, expectedLastUpdate time.Time) {
func checkUptime(t *testing.T, up interfaces.PausableManager, nodeID ids.NodeID, expectedUptime time.Duration, expectedLastUpdate time.Time) {
t.Helper()
uptime, lastUpdated, err := up.CalculateUptime(nodeID)
require.NoError(t, err)
Expand Down
43 changes: 43 additions & 0 deletions plugin/evm/validators/interfaces/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package interfaces

import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/uptime"
"github.com/ava-labs/avalanchego/utils/set"
)

type State interface {
uptime.State
// AddValidator adds a new validator to the state
AddValidator(vID ids.ID, nodeID ids.NodeID, startTimestamp uint64, isActive bool) error
// DeleteValidator deletes the validator from the state
DeleteValidator(vID ids.ID) error
// WriteState writes the validator state to the disk
WriteState() error

// SetStatus sets the active status of the validator with the given vID
SetStatus(vID ids.ID, isActive bool) error
// GetStatus returns the active status of the validator with the given vID
GetStatus(vID ids.ID) (bool, error)

// GetValidationIDs returns the validation IDs in the state
GetValidationIDs() set.Set[ids.ID]
// GetValidatorIDs returns the validator node IDs in the state
GetValidatorIDs() set.Set[ids.NodeID]

// RegisterListener registers a listener to the state
RegisterListener(StateCallbackListener)
}

// StateCallbackListener is a listener for the validator state
type StateCallbackListener interface {
// OnValidatorAdded is called when a new validator is added
OnValidatorAdded(vID ids.ID, nodeID ids.NodeID, startTime uint64, isActive bool)
// OnValidatorRemoved is called when a validator is removed
OnValidatorRemoved(vID ids.ID, nodeID ids.NodeID)
// OnValidatorStatusUpdated is called when a validator status is updated
OnValidatorStatusUpdated(vID ids.ID, nodeID ids.NodeID, isActive bool)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 4 additions & 36 deletions plugin/evm/validators/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/uptime"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/subnet-evm/plugin/evm/validators/interfaces"
)

var _ uptime.State = &state{}
Expand All @@ -24,39 +25,6 @@ const (
deleted dbUpdateStatus = false
)

type State interface {
uptime.State
// AddValidator adds a new validator to the state
AddValidator(vID ids.ID, nodeID ids.NodeID, startTimestamp uint64, isActive bool) error
// DeleteValidator deletes the validator from the state
DeleteValidator(vID ids.ID) error
// WriteState writes the validator state to the disk
WriteState() error

// SetStatus sets the active status of the validator with the given vID
SetStatus(vID ids.ID, isActive bool) error
// GetStatus returns the active status of the validator with the given vID
GetStatus(vID ids.ID) (bool, error)

// GetValidationIDs returns the validation IDs in the state
GetValidationIDs() set.Set[ids.ID]
// GetValidatorIDs returns the validator node IDs in the state
GetValidatorIDs() set.Set[ids.NodeID]

// RegisterListener registers a listener to the state
RegisterListener(StateCallbackListener)
}

// StateCallbackListener is a listener for the validator state
type StateCallbackListener interface {
// OnValidatorAdded is called when a new validator is added
OnValidatorAdded(vID ids.ID, nodeID ids.NodeID, startTime uint64, isActive bool)
// OnValidatorRemoved is called when a validator is removed
OnValidatorRemoved(vID ids.ID, nodeID ids.NodeID)
// OnValidatorStatusUpdated is called when a validator status is updated
OnValidatorStatusUpdated(vID ids.ID, nodeID ids.NodeID, isActive bool)
}

type validatorData struct {
UpDuration time.Duration `serialize:"true"`
LastUpdated uint64 `serialize:"true"`
Expand All @@ -74,11 +42,11 @@ type state struct {
updatedData map[ids.ID]dbUpdateStatus // vID -> updated status
db database.Database

listeners []StateCallbackListener
listeners []interfaces.StateCallbackListener
}

// NewState creates a new State, it also loads the data from the disk
func NewState(db database.Database) (State, error) {
func NewState(db database.Database) (interfaces.State, error) {
s := &state{
index: make(map[ids.NodeID]ids.ID),
data: make(map[ids.ID]*validatorData),
Expand Down Expand Up @@ -243,7 +211,7 @@ func (s *state) GetValidatorIDs() set.Set[ids.NodeID] {

// RegisterListener registers a listener to the state
// OnValidatorAdded is called for all current validators on the provided listener before this function returns
func (s *state) RegisterListener(listener StateCallbackListener) {
func (s *state) RegisterListener(listener interfaces.StateCallbackListener) {
s.listeners = append(s.listeners, listener)

// notify the listener of the current state
Expand Down
3 changes: 2 additions & 1 deletion plugin/evm/validators/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/ava-labs/avalanchego/database/memdb"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/subnet-evm/plugin/evm/validators/interfaces"
)

func TestState(t *testing.T) {
Expand Down Expand Up @@ -223,7 +224,7 @@ func TestStateListener(t *testing.T) {
expectedvID := ids.GenerateTestID()
expectedNodeID := ids.GenerateTestNodeID()
expectedStartTime := time.Now()
mockListener := NewMockStateCallbackListener(ctrl)
mockListener := interfaces.NewMockStateCallbackListener(ctrl)
// add initial validator to test RegisterListener
initialvID := ids.GenerateTestID()
initialNodeID := ids.GenerateTestNodeID()
Expand Down
2 changes: 1 addition & 1 deletion scripts/mocks.mockgen.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
github.com/ava-labs/subnet-evm/precompile/precompileconfig=Predicater,Config,ChainConfig,Accepter=precompile/precompileconfig/mocks.go
github.com/ava-labs/subnet-evm/precompile/contract=BlockContext,AccessibleState,StateDB=precompile/contract/mocks.go
github.com/ava-labs/subnet-evm/plugin/evm/validators=StateCallbackListener=plugin/evm/validators/mock_listener.go
github.com/ava-labs/subnet-evm/plugin/evm/validators/interfaces=StateCallbackListener=plugin/evm/validators/interfaces/mock_listener.go
Loading