-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
64 lines (48 loc) · 1.41 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
// Copyright (C) 2019-2021, Dijets Inc, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package validators
import (
"sync"
"github.com/lasthyphen/dijetsgogo/ids"
)
var _ State = &lockedState{}
// State allows the lookup of validator sets on specified subnets at the
// requested P-chain height.
type State interface {
// GetCurrentHeight returns the current height of the P-chain.
GetCurrentHeight() (uint64, error)
// GetValidatorSet returns the weights of the nodeIDs for the provided
// subnet at the requested P-chain height.
// The returned map should not be modified.
GetValidatorSet(height uint64, subnetID ids.ID) (map[ids.ShortID]uint64, 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) GetCurrentHeight() (uint64, error) {
s.lock.Lock()
defer s.lock.Unlock()
return s.s.GetCurrentHeight()
}
func (s *lockedState) GetValidatorSet(height uint64, subnetID ids.ID) (map[ids.ShortID]uint64, error) {
s.lock.Lock()
defer s.lock.Unlock()
return s.s.GetValidatorSet(height, subnetID)
}
type noState struct{}
func NewNoState() State {
return &noState{}
}
func (s *noState) GetCurrentHeight() (uint64, error) {
return 0, nil
}
func (s *noState) GetValidatorSet(height uint64, subnetID ids.ID) (map[ids.ShortID]uint64, error) {
return nil, nil
}