forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_state.go
70 lines (59 loc) · 1.68 KB
/
test_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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package uptime
import (
"time"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
)
var _ State = (*TestState)(nil)
type uptime struct {
upDuration time.Duration
lastUpdated time.Time
startTime time.Time
}
type TestState struct {
dbReadError error
dbWriteError error
nodes map[ids.NodeID]map[ids.ID]*uptime
}
func NewTestState() *TestState {
return &TestState{
nodes: make(map[ids.NodeID]map[ids.ID]*uptime),
}
}
func (s *TestState) AddNode(nodeID ids.NodeID, subnetID ids.ID, startTime time.Time) {
subnetUptimes, ok := s.nodes[nodeID]
if !ok {
subnetUptimes = make(map[ids.ID]*uptime)
s.nodes[nodeID] = subnetUptimes
}
st := time.Unix(startTime.Unix(), 0)
subnetUptimes[subnetID] = &uptime{
lastUpdated: st,
startTime: st,
}
}
func (s *TestState) GetUptime(nodeID ids.NodeID, subnetID ids.ID) (time.Duration, time.Time, error) {
up, exists := s.nodes[nodeID][subnetID]
if !exists {
return 0, time.Time{}, database.ErrNotFound
}
return up.upDuration, up.lastUpdated, s.dbReadError
}
func (s *TestState) SetUptime(nodeID ids.NodeID, subnetID ids.ID, upDuration time.Duration, lastUpdated time.Time) error {
up, exists := s.nodes[nodeID][subnetID]
if !exists {
return database.ErrNotFound
}
up.upDuration = upDuration
up.lastUpdated = time.Unix(lastUpdated.Unix(), 0)
return s.dbWriteError
}
func (s *TestState) GetStartTime(nodeID ids.NodeID, subnetID ids.ID) (time.Time, error) {
up, exists := s.nodes[nodeID][subnetID]
if !exists {
return time.Time{}, database.ErrNotFound
}
return up.startTime, s.dbReadError
}