/
minerState.go
94 lines (75 loc) · 1.93 KB
/
minerState.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
94
/*
Copyright (C) CESS. All rights reserved.
Copyright (C) Cumulus Encrypted Storage System. All rights reserved.
SPDX-License-Identifier: Apache-2.0
*/
package node
import (
"fmt"
"sync"
"github.com/CESSProject/cess-go-sdk/core/pattern"
)
type MinerState interface {
// set
SaveMinerState(state string) error
SaveMinerSpaceInfo(decSpace, validSpace, usedSpace, lockedSpace uint64)
//get
GetMinerState() string
GetMinerSpaceInfo() (uint64, uint64, uint64, uint64)
}
type MinerStateType struct {
lock *sync.RWMutex
state string
decSpace uint64
validSpace uint64
usedSpace uint64
lockedSpace uint64
}
var _ MinerState = (*MinerStateType)(nil)
func NewMinerState() MinerState {
return &MinerStateType{
lock: new(sync.RWMutex),
}
}
func (m *MinerStateType) SaveMinerState(state string) error {
m.lock.Lock()
defer m.lock.Unlock()
switch state {
case pattern.MINER_STATE_POSITIVE:
m.state = pattern.MINER_STATE_POSITIVE
case pattern.MINER_STATE_FROZEN:
m.state = pattern.MINER_STATE_FROZEN
case pattern.MINER_STATE_LOCK:
m.state = pattern.MINER_STATE_LOCK
case pattern.MINER_STATE_EXIT:
m.state = pattern.MINER_STATE_EXIT
case pattern.MINER_STATE_OFFLINE:
m.state = pattern.MINER_STATE_OFFLINE
default:
return fmt.Errorf("invalid miner state: %s", state)
}
return nil
}
func (m *MinerStateType) SaveMinerSpaceInfo(decSpace, validSpace, usedSpace, lockedSpace uint64) {
m.lock.Lock()
m.decSpace = decSpace
m.validSpace = validSpace
m.usedSpace = usedSpace
m.lockedSpace = lockedSpace
m.lock.Unlock()
}
func (m *MinerStateType) GetMinerState() string {
m.lock.RLock()
result := m.state
m.lock.RUnlock()
return result
}
func (m *MinerStateType) GetMinerSpaceInfo() (uint64, uint64, uint64, uint64) {
m.lock.RLock()
decSpace := m.decSpace
validSpace := m.validSpace
usedSpace := m.usedSpace
lockedSpace := m.lockedSpace
m.lock.RUnlock()
return decSpace, validSpace, usedSpace, lockedSpace
}