-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathconfig.go
More file actions
233 lines (197 loc) · 7.59 KB
/
config.go
File metadata and controls
233 lines (197 loc) · 7.59 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* Copyright 2018 The CovenantSQL Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package conf
import (
"io/ioutil"
"path"
"time"
yaml "gopkg.in/yaml.v2"
"github.com/CovenantSQL/CovenantSQL/crypto"
"github.com/CovenantSQL/CovenantSQL/crypto/asymmetric"
"github.com/CovenantSQL/CovenantSQL/crypto/hash"
"github.com/CovenantSQL/CovenantSQL/pow/cpuminer"
"github.com/CovenantSQL/CovenantSQL/proto"
"github.com/CovenantSQL/CovenantSQL/utils/log"
)
// these const specify the role of this app, which can be "miner", "blockProducer".
const (
MinerBuildTag = "M"
BlockProducerBuildTag = "B"
ClientBuildTag = "C"
UnknownBuildTag = "U"
)
// StartSucceedMessage is printed when CovenantSQL started successfully.
const StartSucceedMessage = "CovenantSQL Started Successfully"
// RoleTag indicate which role the daemon is playing.
var RoleTag = UnknownBuildTag
// BaseAccountInfo defines base info to build a BaseAccount.
type BaseAccountInfo struct {
Address hash.Hash `yaml:"Address"`
StableCoinBalance uint64 `yaml:"StableCoinBalance"`
CovenantCoinBalance uint64 `yaml:"CovenantCoinBalance"`
}
// BPGenesisInfo hold all genesis info fields.
type BPGenesisInfo struct {
// Version defines the block version
Version int32 `yaml:"Version"`
// Timestamp defines the initial time of chain
Timestamp time.Time `yaml:"Timestamp"`
// BaseAccounts defines the base accounts for testnet
BaseAccounts []BaseAccountInfo `yaml:"BaseAccounts"`
}
// BPInfo hold all BP info fields.
type BPInfo struct {
// PublicKey point to BlockProducer public key
PublicKey *asymmetric.PublicKey `yaml:"PublicKey"`
// NodeID is the node id of Block Producer
NodeID proto.NodeID `yaml:"NodeID"`
// RawNodeID
RawNodeID proto.RawNodeID `yaml:"-"`
// Nonce is the nonce, SEE: cmd/cql for more
Nonce cpuminer.Uint256 `yaml:"Nonce"`
// ChainFileName is the chain db's name
ChainFileName string `yaml:"ChainFileName"`
// BPGenesis is the genesis block filed
BPGenesis BPGenesisInfo `yaml:"BPGenesisInfo,omitempty"`
}
// MinerDatabaseFixture config.
type MinerDatabaseFixture struct {
DatabaseID proto.DatabaseID `yaml:"DatabaseID"`
Term uint64 `yaml:"Term"`
Leader proto.NodeID `yaml:"Leader"`
Servers []proto.NodeID `yaml:"Servers"`
GenesisBlockFile string `yaml:"GenesisBlockFile"`
AutoGenerateGenesisBlock bool `yaml:"AutoGenerateGenesisBlock,omitempty"`
}
// MinerInfo for miner config.
type MinerInfo struct {
// node basic config.
RootDir string `yaml:"RootDir"`
MaxReqTimeGap time.Duration `yaml:"MaxReqTimeGap,omitempty"`
ProvideServiceInterval time.Duration `yaml:"ProvideServiceInterval,omitempty"`
DiskUsageInterval time.Duration `yaml:"DiskUsageInterval,omitempty"`
TargetUsers []proto.AccountAddress `yaml:"TargetUsers,omitempty"`
}
// DNSSeed defines seed DNS info.
type DNSSeed struct {
EnforcedDNSSEC bool `yaml:"EnforcedDNSSEC"`
DNSServers []string `yaml:"DNSServers"`
Domain string `yaml:"Domain"`
BPCount int `yaml:"BPCount"`
}
// Config holds all the config read from yaml config file.
type Config struct {
UseTestMasterKey bool `yaml:"UseTestMasterKey,omitempty"` // when UseTestMasterKey use default empty masterKey
// StartupSyncHoles indicates synchronizing hole blocks from other peers on BP
// startup/reloading.
StartupSyncHoles bool `yaml:"StartupSyncHoles,omitempty"`
GenerateKeyPair bool `yaml:"-"`
//TODO(auxten): set yaml key for config
WorkingRoot string `yaml:"WorkingRoot"`
PubKeyStoreFile string `yaml:"PubKeyStoreFile"`
PrivateKeyFile string `yaml:"PrivateKeyFile"`
WalletAddress string `yaml:"WalletAddress"`
DHTFileName string `yaml:"DHTFileName"`
ListenAddr string `yaml:"ListenAddr"`
ListenDirectAddr string `yaml:"ListenDirectAddr,omitempty"`
ExternalListenAddr string `yaml:"-"` // for metric purpose
ThisNodeID proto.NodeID `yaml:"ThisNodeID"`
ValidDNSKeys map[string]string `yaml:"ValidDNSKeys"` // map[DNSKEY]domain
// Check By BP DHT.Ping
MinNodeIDDifficulty int `yaml:"MinNodeIDDifficulty"`
DNSSeed DNSSeed `yaml:"DNSSeed"`
BP *BPInfo `yaml:"BlockProducer"`
Miner *MinerInfo `yaml:"Miner,omitempty"`
KnownNodes []proto.Node `yaml:"KnownNodes"`
SeedBPNodes []proto.Node `yaml:"-"`
QPS uint32 `yaml:"QPS"`
ChainBusPeriod time.Duration `yaml:"ChainBusPeriod"`
BillingBlockCount uint64 `yaml:"BillingBlockCount"` // BillingBlockCount is for sql chain miners syncing billing with main chain
BPPeriod time.Duration `yaml:"BPPeriod"`
BPTick time.Duration `yaml:"BPTick"`
SQLChainPeriod time.Duration `yaml:"SQLChainPeriod"`
SQLChainTick time.Duration `yaml:"SQLChainTick"`
SQLChainTTL int32 `yaml:"SQLChainTTL"`
MinProviderDeposit uint64 `yaml:"MinProviderDeposit"`
}
// GConf is the global config pointer.
var GConf *Config
// LoadConfig loads config from configPath.
func LoadConfig(configPath string) (config *Config, err error) {
configBytes, err := ioutil.ReadFile(configPath)
if err != nil {
log.WithError(err).Error("read config file failed")
return
}
config = &Config{}
err = yaml.Unmarshal(configBytes, config)
if err != nil {
log.WithError(err).Error("unmarshal config file failed")
return
}
if config.BPPeriod == time.Duration(0) {
config.BPPeriod = 10 * time.Second
}
if config.WorkingRoot == "" {
config.WorkingRoot = "./"
}
if config.PrivateKeyFile == "" {
config.PrivateKeyFile = "private.key"
}
if config.PubKeyStoreFile == "" {
config.PubKeyStoreFile = "public.keystore"
}
if config.DHTFileName == "" {
config.DHTFileName = "dht.db"
}
configDir := path.Dir(configPath)
if !path.IsAbs(config.PubKeyStoreFile) {
config.PubKeyStoreFile = path.Join(configDir, config.PubKeyStoreFile)
}
if !path.IsAbs(config.PrivateKeyFile) {
config.PrivateKeyFile = path.Join(configDir, config.PrivateKeyFile)
}
if !path.IsAbs(config.DHTFileName) {
config.DHTFileName = path.Join(configDir, config.DHTFileName)
}
if !path.IsAbs(config.WorkingRoot) {
config.WorkingRoot = path.Join(configDir, config.WorkingRoot)
}
if config.BP != nil && !path.IsAbs(config.BP.ChainFileName) {
config.BP.ChainFileName = path.Join(configDir, config.BP.ChainFileName)
}
if config.Miner != nil && !path.IsAbs(config.Miner.RootDir) {
config.Miner.RootDir = path.Join(configDir, config.Miner.RootDir)
}
if len(config.KnownNodes) > 0 {
for _, node := range config.KnownNodes {
if node.ID == config.ThisNodeID {
if config.WalletAddress == "" && node.PublicKey != nil {
var walletHash proto.AccountAddress
if walletHash, err = crypto.PubKeyHash(node.PublicKey); err != nil {
return
}
config.WalletAddress = walletHash.String()
}
if config.ExternalListenAddr == "" {
config.ExternalListenAddr = node.Addr
}
break
}
}
}
return
}