-
Notifications
You must be signed in to change notification settings - Fork 672
/
state.go
93 lines (73 loc) · 2.12 KB
/
state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package validators
import (
"context"
"sync"
"github.com/ava-labs/avalanchego/ids"
)
var _ State = (*lockedState)(nil)
// State allows the lookup of validator sets on specified subnets at the
// requested P-chain height.
type State interface {
// GetMinimumHeight returns the minimum height of the block still in the
// proposal window.
GetMinimumHeight(context.Context) (uint64, error)
// GetCurrentHeight returns the current height of the P-chain.
GetCurrentHeight(context.Context) (uint64, error)
// GetSubnetID returns the subnetID of the provided chain.
GetSubnetID(ctx context.Context, chainID ids.ID) (ids.ID, error)
// GetValidatorSet returns the validators of the provided subnet at the
// requested P-chain height.
// The returned map should not be modified.
GetValidatorSet(
ctx context.Context,
height uint64,
subnetID ids.ID,
) (map[ids.NodeID]*GetValidatorOutput, error)
}
type lockedState struct {
lock sync.Locker
s State
}
func NewLockedState(lock sync.Locker, s State) State {
return &lockedState{
lock: lock,
s: s,
}
}
func (s *lockedState) GetMinimumHeight(ctx context.Context) (uint64, error) {
s.lock.Lock()
defer s.lock.Unlock()
return s.s.GetMinimumHeight(ctx)
}
func (s *lockedState) GetCurrentHeight(ctx context.Context) (uint64, error) {
s.lock.Lock()
defer s.lock.Unlock()
return s.s.GetCurrentHeight(ctx)
}
func (s *lockedState) GetSubnetID(ctx context.Context, chainID ids.ID) (ids.ID, error) {
s.lock.Lock()
defer s.lock.Unlock()
return s.s.GetSubnetID(ctx, chainID)
}
func (s *lockedState) GetValidatorSet(
ctx context.Context,
height uint64,
subnetID ids.ID,
) (map[ids.NodeID]*GetValidatorOutput, error) {
s.lock.Lock()
defer s.lock.Unlock()
return s.s.GetValidatorSet(ctx, height, subnetID)
}
type noValidators struct {
State
}
func NewNoValidatorsState(state State) State {
return &noValidators{
State: state,
}
}
func (*noValidators) GetValidatorSet(context.Context, uint64, ids.ID) (map[ids.NodeID]*GetValidatorOutput, error) {
return nil, nil
}