-
Notifications
You must be signed in to change notification settings - Fork 7
/
inicnf.go
132 lines (116 loc) · 3.1 KB
/
inicnf.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package sys
import (
"fmt"
"github.com/hacash/core/sys/inicnf"
"math/rand"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
)
// Global development test tag
var TestDebugLocalDevelopmentMark bool = false
var NotCheckBlockDifficultyForMiner = false
var TransactionSystemCheckChainID uint64 = 0 // fork or test chain ID
type Inicnf struct {
inicnf.File
// cnf cache
mustDataDirVersion string
mustDataDir string
// The minimum version number of the currently compatible blockchain database (blockdata only)
blockChainStateDatabaseLowestCompatibleVersion int
// Currently used blockchain database version number
blockChainStateDatabaseCurrentUseVersion int
mux sync.Mutex
}
// Must call
func (i *Inicnf) SetDatabaseVersion(curversion, compatible int) {
i.blockChainStateDatabaseCurrentUseVersion = curversion
i.blockChainStateDatabaseLowestCompatibleVersion = compatible
}
func (i *Inicnf) GetDatabaseVersion() (int, int) {
return i.blockChainStateDatabaseCurrentUseVersion, i.blockChainStateDatabaseLowestCompatibleVersion
}
// val list
func (i *Inicnf) StringValueList(section string, name string) []string {
valstr := i.Section(section).Key(name).MustString("")
valstr = regexp.MustCompile(`[,,\s]+`).ReplaceAllString(valstr, ",")
valstr = strings.Trim(valstr, ",")
if valstr == "" {
return []string{}
}
return strings.Split(valstr, ",")
}
func AbsDir(dir string) string {
if filepath.IsAbs(dir) == false {
exep := os.Args[0]
ppp, err := filepath.Abs(exep)
if err != nil {
fmt.Println(err.Error())
os.Exit(0)
}
dir = filepath.Join(filepath.Dir(ppp), dir)
}
return dir
}
// data dir version
func (i *Inicnf) MustDataDirWithVersion() string {
i.mux.Lock()
defer i.mux.Unlock()
if i.mustDataDirVersion != "" {
return i.mustDataDirVersion
}
dir := i.mustDataDirUnsafe()
dir = filepath.Join(dir, fmt.Sprintf("v%d", i.blockChainStateDatabaseCurrentUseVersion))
i.mustDataDirVersion = dir
return dir
}
// data dir
func (i *Inicnf) MustDataDir() string {
i.mux.Lock()
defer i.mux.Unlock()
if len(i.mustDataDir) > 0 {
return i.mustDataDir
}
dir := i.mustDataDirUnsafe()
i.mustDataDir = dir
return dir
}
// data dir
func (i *Inicnf) mustDataDirUnsafe() string {
dir := i.Section("").Key("data_dir").MustString("~/.hacash_mainnet")
if strings.HasPrefix(dir, "~/") {
dir = os.Getenv("HOME") + string([]byte(dir)[1:])
}
dir = AbsDir(dir)
return dir
}
// data dir Check Version
func (i *Inicnf) MustDataDirCheckVersion(version int) (string, bool) {
dir := i.Section("").Key("data_dir").MustString("~/.hacash_mainnet")
if strings.HasPrefix(dir, "~/") {
dir = os.Getenv("HOME") + string([]byte(dir)[1:])
}
dir = AbsDir(dir)
dir = filepath.Join(dir, fmt.Sprintf("v%d", version))
// Check for presence
_, nte := os.Stat(dir)
if nte != nil {
return dir, false // non-existent
}
// Directory exists
return dir, true
}
//////////////////////////////
func LoadInicnf(source_file string) (*Inicnf, error) {
rand.Seed(time.Now().Unix())
inifile, err := inicnf.LooseLoad(source_file)
if err != nil {
return nil, err
}
cnf := &Inicnf{}
cnf.File = *inifile
return cnf, nil
}