forked from needkane/angine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
136 lines (114 loc) · 4.39 KB
/
config.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
133
134
135
136
// Copyright 2017 ZhongAn Information Technology Services Co.,Ltd.
//
// 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 config
import (
"os"
"path"
"strings"
"github.com/annchain/ann-module/lib/go-common"
"github.com/annchain/ann-module/lib/go-config"
)
const (
RUNTIME_ENV = "ANN_RUNTIME"
DEFAULT_RUNTIME = ".ann_runtime"
DATADIR = "data"
CONFIGFILE = "config.toml"
)
func parseConfigTpl(moniker string, root string) (conf string) {
conf = strings.Replace(CONFIGTPL, "__MONIKER__", moniker, -1)
conf = strings.Replace(conf, "__CONFROOT__", root, -1)
return
}
func RuntimeDir(root string) string {
if root != "" {
return root
}
if runtimePath, exists := os.LookupEnv(RUNTIME_ENV); exists {
return runtimePath
}
return path.Join(os.Getenv("HOME"), DEFAULT_RUNTIME)
}
func InitRuntime(root string) {
common.EnsureDir(root, 0700)
common.EnsureDir(path.Join(root, DATADIR), 0700)
configFilePath := path.Join(root, CONFIGFILE)
if !common.FileExists(configFilePath) {
common.MustWriteFile(configFilePath, []byte(parseConfigTpl("anonymous", root)), 0644)
}
}
func GetConfig(root string) (conf *config.MapConfig) {
var err error
runtime := RuntimeDir(root)
configAbs := path.Join(runtime, CONFIGFILE)
InitRuntime(runtime)
if conf, err = config.ReadMapConfigFromFile(configAbs); err != nil {
common.Exit(common.Fmt("Could not read config: %v", err))
}
// Set defaults or panic
if conf.IsSet("chain_id") {
common.Exit("Cannot set 'chain_id' via config.toml")
}
if conf.IsSet("revision_file") {
common.Exit("Cannot set 'revision_file' via config.toml. It must match what's in the Makefile")
}
FillInDefaults(runtime, conf)
return
}
func FillInDefaults(root string, conf *config.MapConfig) *config.MapConfig {
conf.SetRequired("chain_id") // blows up if you try to use it before setting.
conf.SetRequired("environment")
conf.SetDefault("environment", "development")
conf.SetDefault("datadir", root)
conf.SetDefault("genesis_file", path.Join(root, "genesis.json"))
conf.SetDefault("moniker", "anonymous")
conf.SetDefault("node_laddr", "tcp://0.0.0.0:46656")
conf.SetDefault("seeds", "")
conf.SetDefault("fast_sync", true)
conf.SetDefault("skip_upnp", false)
conf.SetDefault("addrbook_file", path.Join(root, "addrbook.json"))
conf.SetDefault("addrbook_strict", false) // disable to allow connections locally
conf.SetDefault("pex_reactor", false) // enable for peer exchange
conf.SetDefault("priv_validator_file", path.Join(root, "priv_validator.json"))
conf.SetDefault("db_backend", "leveldb")
conf.SetDefault("db_dir", path.Join(root, DATADIR))
conf.SetDefault("rpc_laddr", "tcp://0.0.0.0:46657")
conf.SetDefault("grpc_laddr", "")
conf.SetDefault("api_laddr", "")
conf.SetDefault("revision_file", path.Join(root, "revision"))
conf.SetDefault("cs_wal_dir", path.Join(root, DATADIR, "cs.wal"))
conf.SetDefault("cs_wal_light", false)
conf.SetDefault("filter_peers", false)
conf.SetDefault("block_size", 3000) // max number of txs
conf.SetDefault("block_part_size", 65536) // part size 64K
conf.SetDefault("disable_data_hash", false)
conf.SetDefault("timeout_propose", 3000)
conf.SetDefault("timeout_propose_delta", 500)
conf.SetDefault("timeout_prevote", 1000)
conf.SetDefault("timeout_prevote_delta", 500)
conf.SetDefault("timeout_precommit", 1000)
conf.SetDefault("timeout_precommit_delta", 500)
conf.SetDefault("timeout_commit", 1000)
conf.SetDefault("skip_timeout_commit", false)
conf.SetDefault("mempool_recheck", true)
conf.SetDefault("mempool_recheck_empty", true)
conf.SetDefault("mempool_broadcast", true)
conf.SetDefault("mempool_wal_dir", path.Join(root, DATADIR, "mempool.wal"))
conf.SetDefault("mempool_enable_txs_limits", false)
conf.SetDefault("signbyCA", "")
conf.SetDefault("p2p", map[string]interface{}{"connection_reset_wait": 300})
conf.SetDefault("test", false)
conf.SetDefault("pprof", false)
conf.SetDefault("log_path", "")
return conf
}