Skip to content

Commit 05f179c

Browse files
ceyonurDarioush Jalali
andauthored
move interfaces to separate pkgs (#1379)
* add validator state * add pausable uptime manager * remove stuttering name * rename state listener * Update plugin/evm/validators/state.go Co-authored-by: Darioush Jalali <[email protected]> Signed-off-by: Ceyhun Onur <[email protected]> * use update enum * Update plugin/evm/validators/state.go Co-authored-by: Darioush Jalali <[email protected]> Signed-off-by: Ceyhun Onur <[email protected]> * Update plugin/evm/validators/state.go Co-authored-by: Darioush Jalali <[email protected]> Signed-off-by: Ceyhun Onur <[email protected]> * respond to comments * update avalanchego dep branch * reviews * reword errs * fix test changes * fix upgrades after deactivating latest in context * use branch commit for ava version * reviews * add listener mock * remove errs from resume and pause * check after stopping * use expectedTime in tests * reviews * move interfaces to separate pkgs * regen mock --------- Signed-off-by: Ceyhun Onur <[email protected]> Co-authored-by: Darioush Jalali <[email protected]>
1 parent 8a6df9e commit 05f179c

File tree

8 files changed

+80
-59
lines changed

8 files changed

+80
-59
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package interfaces
5+
6+
import (
7+
"github.com/ava-labs/avalanchego/ids"
8+
"github.com/ava-labs/avalanchego/snow/uptime"
9+
validatorsinterfaces "github.com/ava-labs/subnet-evm/plugin/evm/validators/interfaces"
10+
)
11+
12+
type PausableManager interface {
13+
uptime.Manager
14+
validatorsinterfaces.StateCallbackListener
15+
IsPaused(nodeID ids.NodeID) bool
16+
}

plugin/evm/uptime/pausable_manager.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,16 @@ package uptime
66
import (
77
"errors"
88

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

1212
"github.com/ava-labs/avalanchego/ids"
1313
"github.com/ava-labs/avalanchego/snow/uptime"
1414
"github.com/ava-labs/avalanchego/utils/set"
1515
)
1616

17-
var _ validators.StateCallbackListener = &pausableManager{}
18-
1917
var errPausedDisconnect = errors.New("paused node cannot be disconnected")
2018

21-
type PausableManager interface {
22-
uptime.Manager
23-
validators.StateCallbackListener
24-
IsPaused(nodeID ids.NodeID) bool
25-
}
26-
2719
type pausableManager struct {
2820
uptime.Manager
2921
pausedVdrs set.Set[ids.NodeID]
@@ -33,7 +25,7 @@ type pausableManager struct {
3325
}
3426

3527
// NewPausableManager takes an uptime.Manager and returns a PausableManager
36-
func NewPausableManager(manager uptime.Manager) PausableManager {
28+
func NewPausableManager(manager uptime.Manager) interfaces.PausableManager {
3729
return &pausableManager{
3830
pausedVdrs: make(set.Set[ids.NodeID]),
3931
connectedVdrs: make(set.Set[ids.NodeID]),

plugin/evm/uptime/pausable_manager_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/ava-labs/avalanchego/ids"
1111
"github.com/ava-labs/avalanchego/snow/uptime"
1212
"github.com/ava-labs/avalanchego/utils/timer/mockable"
13+
"github.com/ava-labs/subnet-evm/plugin/evm/uptime/interfaces"
1314
"github.com/stretchr/testify/require"
1415
)
1516

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

2122
tests := []struct {
2223
name string
23-
testFunc func(t *testing.T, up PausableManager, clk *mockable.Clock, s uptime.State)
24+
testFunc func(t *testing.T, up interfaces.PausableManager, clk *mockable.Clock, s uptime.State)
2425
}{
2526
{
2627
name: "Case 1: Connect, pause, start tracking",
27-
testFunc: func(t *testing.T, up PausableManager, clk *mockable.Clock, s uptime.State) {
28+
testFunc: func(t *testing.T, up interfaces.PausableManager, clk *mockable.Clock, s uptime.State) {
2829
require := require.New(t)
2930

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

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

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

152153
// Start tracking and connect
@@ -218,7 +219,7 @@ func TestPausableManager(t *testing.T) {
218219
}
219220
}
220221

221-
func setupTestEnv(nodeID ids.NodeID, startTime time.Time) (PausableManager, *mockable.Clock, uptime.State) {
222+
func setupTestEnv(nodeID ids.NodeID, startTime time.Time) (interfaces.PausableManager, *mockable.Clock, uptime.State) {
222223
clk := mockable.Clock{}
223224
clk.Set(startTime)
224225
s := uptime.NewTestState()
@@ -232,7 +233,7 @@ func addTime(clk *mockable.Clock, duration time.Duration) time.Time {
232233
return clk.Time()
233234
}
234235

235-
func checkUptime(t *testing.T, up PausableManager, nodeID ids.NodeID, expectedUptime time.Duration, expectedLastUpdate time.Time) {
236+
func checkUptime(t *testing.T, up interfaces.PausableManager, nodeID ids.NodeID, expectedUptime time.Duration, expectedLastUpdate time.Time) {
236237
t.Helper()
237238
uptime, lastUpdated, err := up.CalculateUptime(nodeID)
238239
require.NoError(t, err)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package interfaces
5+
6+
import (
7+
"github.com/ava-labs/avalanchego/ids"
8+
"github.com/ava-labs/avalanchego/snow/uptime"
9+
"github.com/ava-labs/avalanchego/utils/set"
10+
)
11+
12+
type State interface {
13+
uptime.State
14+
// AddValidator adds a new validator to the state
15+
AddValidator(vID ids.ID, nodeID ids.NodeID, startTimestamp uint64, isActive bool) error
16+
// DeleteValidator deletes the validator from the state
17+
DeleteValidator(vID ids.ID) error
18+
// WriteState writes the validator state to the disk
19+
WriteState() error
20+
21+
// SetStatus sets the active status of the validator with the given vID
22+
SetStatus(vID ids.ID, isActive bool) error
23+
// GetStatus returns the active status of the validator with the given vID
24+
GetStatus(vID ids.ID) (bool, error)
25+
26+
// GetValidationIDs returns the validation IDs in the state
27+
GetValidationIDs() set.Set[ids.ID]
28+
// GetValidatorIDs returns the validator node IDs in the state
29+
GetValidatorIDs() set.Set[ids.NodeID]
30+
31+
// RegisterListener registers a listener to the state
32+
RegisterListener(StateCallbackListener)
33+
}
34+
35+
// StateCallbackListener is a listener for the validator state
36+
type StateCallbackListener interface {
37+
// OnValidatorAdded is called when a new validator is added
38+
OnValidatorAdded(vID ids.ID, nodeID ids.NodeID, startTime uint64, isActive bool)
39+
// OnValidatorRemoved is called when a validator is removed
40+
OnValidatorRemoved(vID ids.ID, nodeID ids.NodeID)
41+
// OnValidatorStatusUpdated is called when a validator status is updated
42+
OnValidatorStatusUpdated(vID ids.ID, nodeID ids.NodeID, isActive bool)
43+
}

plugin/evm/validators/mock_listener.go renamed to plugin/evm/validators/interfaces/mock_listener.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugin/evm/validators/state.go

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/ava-labs/avalanchego/ids"
1212
"github.com/ava-labs/avalanchego/snow/uptime"
1313
"github.com/ava-labs/avalanchego/utils/set"
14+
"github.com/ava-labs/subnet-evm/plugin/evm/validators/interfaces"
1415
)
1516

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

27-
type State interface {
28-
uptime.State
29-
// AddValidator adds a new validator to the state
30-
AddValidator(vID ids.ID, nodeID ids.NodeID, startTimestamp uint64, isActive bool) error
31-
// DeleteValidator deletes the validator from the state
32-
DeleteValidator(vID ids.ID) error
33-
// WriteState writes the validator state to the disk
34-
WriteState() error
35-
36-
// SetStatus sets the active status of the validator with the given vID
37-
SetStatus(vID ids.ID, isActive bool) error
38-
// GetStatus returns the active status of the validator with the given vID
39-
GetStatus(vID ids.ID) (bool, error)
40-
41-
// GetValidationIDs returns the validation IDs in the state
42-
GetValidationIDs() set.Set[ids.ID]
43-
// GetValidatorIDs returns the validator node IDs in the state
44-
GetValidatorIDs() set.Set[ids.NodeID]
45-
46-
// RegisterListener registers a listener to the state
47-
RegisterListener(StateCallbackListener)
48-
}
49-
50-
// StateCallbackListener is a listener for the validator state
51-
type StateCallbackListener interface {
52-
// OnValidatorAdded is called when a new validator is added
53-
OnValidatorAdded(vID ids.ID, nodeID ids.NodeID, startTime uint64, isActive bool)
54-
// OnValidatorRemoved is called when a validator is removed
55-
OnValidatorRemoved(vID ids.ID, nodeID ids.NodeID)
56-
// OnValidatorStatusUpdated is called when a validator status is updated
57-
OnValidatorStatusUpdated(vID ids.ID, nodeID ids.NodeID, isActive bool)
58-
}
59-
6028
type validatorData struct {
6129
UpDuration time.Duration `serialize:"true"`
6230
LastUpdated uint64 `serialize:"true"`
@@ -74,11 +42,11 @@ type state struct {
7442
updatedData map[ids.ID]dbUpdateStatus // vID -> updated status
7543
db database.Database
7644

77-
listeners []StateCallbackListener
45+
listeners []interfaces.StateCallbackListener
7846
}
7947

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

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

249217
// notify the listener of the current state

plugin/evm/validators/state_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/ava-labs/avalanchego/database/memdb"
1616
"github.com/ava-labs/avalanchego/ids"
1717
"github.com/ava-labs/avalanchego/utils/wrappers"
18+
"github.com/ava-labs/subnet-evm/plugin/evm/validators/interfaces"
1819
)
1920

2021
func TestState(t *testing.T) {
@@ -223,7 +224,7 @@ func TestStateListener(t *testing.T) {
223224
expectedvID := ids.GenerateTestID()
224225
expectedNodeID := ids.GenerateTestNodeID()
225226
expectedStartTime := time.Now()
226-
mockListener := NewMockStateCallbackListener(ctrl)
227+
mockListener := interfaces.NewMockStateCallbackListener(ctrl)
227228
// add initial validator to test RegisterListener
228229
initialvID := ids.GenerateTestID()
229230
initialNodeID := ids.GenerateTestNodeID()

scripts/mocks.mockgen.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
github.com/ava-labs/subnet-evm/precompile/precompileconfig=Predicater,Config,ChainConfig,Accepter=precompile/precompileconfig/mocks.go
22
github.com/ava-labs/subnet-evm/precompile/contract=BlockContext,AccessibleState,StateDB=precompile/contract/mocks.go
3-
github.com/ava-labs/subnet-evm/plugin/evm/validators=StateCallbackListener=plugin/evm/validators/mock_listener.go
3+
github.com/ava-labs/subnet-evm/plugin/evm/validators/interfaces=StateCallbackListener=plugin/evm/validators/interfaces/mock_listener.go

0 commit comments

Comments
 (0)