/
config.go
184 lines (161 loc) · 5.14 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
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
package commands
import (
"bytes"
"os"
"path"
"path/filepath"
"text/template"
"github.com/spf13/viper"
"github.com/blockservice/echoin/utils"
"github.com/ethereum/go-ethereum/node"
tmcfg "github.com/tendermint/tendermint/config"
cmn "github.com/tendermint/tendermint/libs/common"
)
const (
defaultConfigDir = "config"
defaultDataDir = "data"
configFile = "config.toml"
)
type EchoinConfig struct {
BaseConfig BaseConfig `mapstructure:",squash"`
TMConfig tmcfg.Config `mapstructure:",squash"`
EMConfig EthermintConfig `mapstructure:"vm"`
}
func DefaultConfig() *EchoinConfig {
return &EchoinConfig{
BaseConfig: DefaultBaseConfig(),
TMConfig: *tmcfg.DefaultConfig(),
EMConfig: DefaultEthermintConfig(),
}
}
type BaseConfig struct {
// The root directory for all data.
// This should be set in viper so it can unmarshal into this struct
RootDir string `mapstructure:"home"`
}
func DefaultBaseConfig() BaseConfig {
return BaseConfig{}
}
type EthermintConfig struct {
ChainId uint `mapstructure:"chainid"`
RootDir string `mapstructure:"home"`
ABCIAddr string `mapstructure:"abci_laddr"`
ABCIProtocol string `mapstructure:"abci_protocol"`
RPCEnabledFlag bool `mapstructure:"rpc"`
RPCListenAddrFlag string `mapstructure:"rpcaddr"`
RPCPortFlag uint `mapstructure:"rpcport"`
RPCCORSDomainFlag string `mapstructure:"rpccorsdomain"`
RPCApiFlag string `mapstructure:"rpcapi"`
RPCVirtualHostsFlag string `mapstructure:"rpcvhosts"`
WSEnabledFlag bool `mapstructure:"ws"`
WSListenAddrFlag string `mapstructure:"wsaddr"`
WSPortFlag uint `mapstructure:"wsport"`
WSApiFlag string `mapstructure:"wsapi"`
IPCDisabledFlag bool `mapstructure:"ipcdisable"`
VerbosityFlag uint `mapstructure:"verbosity"`
GCMode string `mapstructure:"gcmode"`
ListenPortFlag uint `mapstructure:"listenport"`
}
func DefaultEthermintConfig() EthermintConfig {
return EthermintConfig{
ChainId: utils.TestNet,
ABCIAddr: "tcp://0.0.0.0:8848",
ABCIProtocol: "socket",
RPCEnabledFlag: true,
RPCListenAddrFlag: "0.0.0.0",
RPCPortFlag: node.DefaultHTTPPort,
RPCCORSDomainFlag: "*",
RPCApiFlag: "ec,eth,net,web3,personal",
RPCVirtualHostsFlag: "localhost",
WSEnabledFlag: false,
WSListenAddrFlag: node.DefaultWSHost,
WSPortFlag: node.DefaultWSPort,
WSApiFlag: "",
IPCDisabledFlag: false,
VerbosityFlag: 3,
GCMode: "full",
ListenPortFlag: 30333,
}
}
// copied from tendermint/commands/root.go
// to call our revised EnsureRoot
func ParseConfig() (*EchoinConfig, error) {
conf := DefaultConfig()
// set chainid as per --env
switch viper.GetString(FlagENV) {
case "staging":
conf.EMConfig.ChainId = utils.Staging
case "mainnet":
conf.EMConfig.ChainId = utils.MainNet
case "testnet":
conf.EMConfig.ChainId = utils.TestNet
default:
conf.EMConfig.ChainId = utils.PrivateChain
}
err := viper.Unmarshal(&conf)
if err != nil {
return nil, err
}
conf.TMConfig.SetRoot(conf.TMConfig.RootDir)
// replace EnsureRoot of tendermint with our own
ensureRoot(conf)
return conf, nil
}
// copied from tendermint/config/toml.go
// modified to override some defaults and append vm configs
func ensureRoot(conf *EchoinConfig) {
rootDir := conf.TMConfig.RootDir
if err := cmn.EnsureDir(rootDir, 0700); err != nil {
cmn.PanicSanity(err.Error())
}
if err := cmn.EnsureDir(filepath.Join(rootDir, defaultConfigDir), 0700); err != nil {
cmn.PanicSanity(err.Error())
}
if err := cmn.EnsureDir(filepath.Join(rootDir, defaultDataDir), 0700); err != nil {
cmn.PanicSanity(err.Error())
}
configFilePath := path.Join(rootDir, defaultConfigDir, configFile)
// Write default config file if missing.
if !cmn.FileExists(configFilePath) {
// override some defaults
conf.TMConfig.Consensus.TimeoutCommit = 10000
//conf.TMConfig.LogLevel = "app:debug,*:error"
//conf.TMConfig.Consensus.MaxBlockSizeTxs = 50000
// write config file
tmcfg.WriteConfigFile(configFilePath, &conf.TMConfig)
// append vm configs
AppendVMConfig(configFilePath, conf)
}
}
func AppendVMConfig(configFilePath string, conf *EchoinConfig) {
var configTemplate *template.Template
var err error
if configTemplate, err = template.New("vmConfigTemplate").Parse(defaultVmTemplate); err != nil {
panic(err)
}
var buffer bytes.Buffer
if err := configTemplate.Execute(&buffer, conf); err != nil {
panic(err)
}
f, err := os.OpenFile(configFilePath, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
defer f.Close()
if _, err := f.Write(buffer.Bytes()); err != nil {
panic(err)
}
}
var defaultVmTemplate = `
[vm]
chainid = {{ .EMConfig.ChainId }}
rpc = {{ .EMConfig.RPCEnabledFlag }}
rpcapi = "{{ .EMConfig.RPCApiFlag }}"
rpcaddr = "{{ .EMConfig.RPCListenAddrFlag }}"
rpcport = {{ .EMConfig.RPCPortFlag }}
rpccorsdomain = "{{ .EMConfig.RPCCORSDomainFlag }}"
rpcvhosts = "{{ .EMConfig.RPCVirtualHostsFlag }}"
ws = {{ .EMConfig.WSEnabledFlag }}
ipcdisable = {{ .EMConfig.IPCDisabledFlag }}
verbosity = "{{ .EMConfig.VerbosityFlag }}"
`