9 log "github.com/sirupsen/logrus"
10 "github.com/vapor/common"
14 // CommonConfig means config object
19 // Top level options use an anonymous struct
20 BaseConfig `mapstructure:",squash"`
21 // Options for services
22 P2P *P2PConfig `mapstructure:"p2p"`
23 Wallet *WalletConfig `mapstructure:"wallet"`
24 Auth *RPCAuthConfig `mapstructure:"auth"`
25 Web *WebConfig `mapstructure:"web"`
26 Side *SideChainConfig `mapstructure:"side"`
27 MainChain *MainChainRpcConfig `mapstructure:"mainchain"`
28 Websocket *WebsocketConfig `mapstructure:"ws"`
29 Consensus *ConsensusConfig `mapstructure:"consensus"`
32 // Default configurable parameters.
33 func DefaultConfig() *Config {
35 BaseConfig: DefaultBaseConfig(),
36 P2P: DefaultP2PConfig(),
37 Wallet: DefaultWalletConfig(),
38 Auth: DefaultRPCAuthConfig(),
39 Web: DefaultWebConfig(),
40 Side: DefaultSideChainConfig(),
41 MainChain: DefaultMainChainRpc(),
42 Websocket: DefaultWebsocketConfig(),
43 Consensus: DefaultConsensusCOnfig(),
47 // Set the RootDir for all Config structs
48 func (cfg *Config) SetRoot(root string) *Config {
49 cfg.BaseConfig.RootDir = root
53 //-----------------------------------------------------------------------------
55 type BaseConfig struct {
56 // The root directory for all data.
57 // This should be set in viper so it can unmarshal into this struct
58 RootDir string `mapstructure:"home"`
60 //The ID of the network to json
61 ChainID string `mapstructure:"chain_id"`
64 LogLevel string `mapstructure:"log_level"`
66 // A custom human readable name for this node
67 Moniker string `mapstructure:"moniker"`
69 // TCP or UNIX socket address for the profiling server to listen on
70 ProfListenAddress string `mapstructure:"prof_laddr"`
72 Mining bool `mapstructure:"mining"`
74 // Database backend: leveldb | memdb
75 DBBackend string `mapstructure:"db_backend"`
78 DBPath string `mapstructure:"db_dir"`
81 KeysPath string `mapstructure:"keys_dir"`
83 ApiAddress string `mapstructure:"api_addr"`
85 VaultMode bool `mapstructure:"vault_mode"`
88 LogFile string `mapstructure:"log_file"`
90 // Validate pegin proof by checking bytom transaction inclusion in mainchain.
91 ValidatePegin bool `mapstructure:"validate_pegin"`
92 Signer string `mapstructure:"signer"`
94 ConsensusConfigFile string `mapstructure:"consensus_config_file"`
97 // Default configurable base parameters.
98 func DefaultBaseConfig() BaseConfig {
100 Moniker: "anonymous",
101 ProfListenAddress: "",
103 DBBackend: "leveldb",
105 KeysPath: "keystore",
109 func (b BaseConfig) DBDir() string {
110 return rootify(b.DBPath, b.RootDir)
113 func (b BaseConfig) KeysDir() string {
114 return rootify(b.KeysPath, b.RootDir)
118 type P2PConfig struct {
119 ListenAddress string `mapstructure:"laddr"`
120 Seeds string `mapstructure:"seeds"`
121 SkipUPNP bool `mapstructure:"skip_upnp"`
122 MaxNumPeers int `mapstructure:"max_num_peers"`
123 HandshakeTimeout int `mapstructure:"handshake_timeout"`
124 DialTimeout int `mapstructure:"dial_timeout"`
125 ProxyAddress string `mapstructure:"proxy_address"`
126 ProxyUsername string `mapstructure:"proxy_username"`
127 ProxyPassword string `mapstructure:"proxy_password"`
130 // Default configurable p2p parameters.
131 func DefaultP2PConfig() *P2PConfig {
133 ListenAddress: "tcp://0.0.0.0:46656",
136 HandshakeTimeout: 30,
144 //-----------------------------------------------------------------------------
145 type WalletConfig struct {
146 Disable bool `mapstructure:"disable"`
147 Rescan bool `mapstructure:"rescan"`
148 MaxTxFee uint64 `mapstructure:"max_tx_fee"`
151 type RPCAuthConfig struct {
152 Disable bool `mapstructure:"disable"`
155 type WebConfig struct {
156 Closed bool `mapstructure:"closed"`
159 type SideChainConfig struct {
160 FedpegXPubs string `mapstructure:"fedpeg_xpubs"`
161 SignBlockXPubs string `mapstructure:"sign_block_xpubs"`
162 PeginMinDepth uint64 `mapstructure:"pegin_confirmation_depth"`
163 ParentGenesisBlockHash string `mapstructure:"parent_genesis_block_hash"`
166 type MainChainRpcConfig struct {
167 MainchainRpcHost string `mapstructure:"mainchain_rpc_host"`
168 MainchainRpcPort string `mapstructure:"mainchain_rpc_port"`
169 MainchainToken string `mapstructure:"mainchain_rpc_token"`
172 type WebsocketConfig struct {
173 MaxNumWebsockets int `mapstructure:"max_num_websockets"`
174 MaxNumConcurrentReqs int `mapstructure:"max_num_concurrent_reqs"`
177 type ConsensusConfig struct {
178 Dpos *DposConfig `mapstructure:"dpos"`
181 type DposConfig struct {
182 Period uint64 `json:"period"` // Number of seconds between blocks to enforce
183 Epoch uint64 `json:"epoch"` // Epoch length to reset votes and checkpoint
184 MaxSignerCount uint64 `json:"max_signers_count"` // Max count of signers
185 MinVoterBalance uint64 `json:"min_boter_balance"` // Min voter balance to valid this vote
186 GenesisTimestamp uint64 `json:"genesis_timestamp"` // The LoopStartTime of first Block
187 Coinbase string `json:"coinbase"`
188 XPrv string `json:"xprv"`
189 SelfVoteSigners []string `json:"signers"` // Signers vote by themselves to seal the block, make sure the signer accounts are pre-funded
190 Signers []common.Address
193 // Default configurable rpc's auth parameters.
194 func DefaultRPCAuthConfig() *RPCAuthConfig {
195 return &RPCAuthConfig{
200 // Default configurable web parameters.
201 func DefaultWebConfig() *WebConfig {
207 // Default configurable wallet parameters.
208 func DefaultWalletConfig() *WalletConfig {
209 return &WalletConfig{
212 MaxTxFee: uint64(1000000000),
216 // DeafultSideChainConfig for sidechain
217 func DefaultSideChainConfig() *SideChainConfig {
218 return &SideChainConfig{
220 ParentGenesisBlockHash: "a75483474799ea1aa6bb910a1a5025b4372bf20bef20f246a2c2dc5e12e8a053",
224 func DefaultMainChainRpc() *MainChainRpcConfig {
225 return &MainChainRpcConfig{
226 MainchainRpcHost: "127.0.0.1",
227 MainchainRpcPort: "9888",
231 func DefaultWebsocketConfig() *WebsocketConfig {
232 return &WebsocketConfig{
233 MaxNumWebsockets: 25,
234 MaxNumConcurrentReqs: 20,
238 func DefaultDposConfig() *DposConfig {
244 GenesisTimestamp: 1524549600,
248 func DefaultConsensusCOnfig() *ConsensusConfig {
249 return &ConsensusConfig{Dpos: DefaultDposConfig()}
252 //-----------------------------------------------------------------------------
255 // helper function to make config creation independent of root dir
256 func rootify(path, root string) string {
257 if filepath.IsAbs(path) {
260 return filepath.Join(root, path)
263 // DefaultDataDir is the default data directory to use for the databases and other
264 // persistence requirements.
265 func DefaultDataDir() string {
266 // Try to place the data folder in the user's home dir
269 return "./.bytom_sidechain"
271 switch runtime.GOOS {
273 // In order to be compatible with old data path,
274 // copy the data from the old path to the new path
275 oldPath := filepath.Join(home, "Library", "Bytom_sidechain")
276 newPath := filepath.Join(home, "Library", "Application Support", "Bytom_sidechain")
277 if !isFolderNotExists(oldPath) && isFolderNotExists(newPath) {
278 if err := os.Rename(oldPath, newPath); err != nil {
279 log.Errorf("DefaultDataDir: %v", err)
285 return filepath.Join(home, "AppData", "Roaming", "Bytom_sidechain")
287 return filepath.Join(home, ".bytom_sidechain")
291 func isFolderNotExists(path string) bool {
292 _, err := os.Stat(path)
293 return os.IsNotExist(err)
296 func homeDir() string {
297 if home := os.Getenv("HOME"); home != "" {
300 if usr, err := user.Current(); err == nil {