-
Notifications
You must be signed in to change notification settings - Fork 40
/
mode.go
60 lines (50 loc) · 1.05 KB
/
mode.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
package runtime
import (
"fmt"
"os"
"path/filepath"
"sync"
"github.com/tendermint/tendermint/config"
"github.com/bnb-chain/node/common/log"
)
type Mode uint8
const (
NormalMode Mode = iota
TransferOnlyMode
RecoverOnlyMode
)
var (
runningMode = NormalMode
mtx = new(sync.RWMutex)
)
func GetRunningMode() Mode {
mtx.RLock()
defer mtx.RUnlock()
return runningMode
}
func setRunningMode(mode Mode) error {
if mode != NormalMode && mode != TransferOnlyMode && mode != RecoverOnlyMode {
return fmt.Errorf("invalid mode %v", mode)
}
mtx.Lock()
runningMode = mode
mtx.Unlock()
return nil
}
func UpdateRunningMode(cfg *config.Config, mode Mode) error {
err := setRunningMode(mode)
if err != nil {
return err
}
var params *runtimeParams
path := filepath.Join(cfg.RootDir, "config", fileName)
if _, err := os.Stat(path); os.IsNotExist(err) {
log.Debug("path does not exist", "path", path)
params = &runtimeParams{Mode: mode}
} else {
params = mustReadFromFile(path)
params.Mode = mode
}
mustSaveToFile(path, params)
return nil
}