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"`
96 IpfsAddress string `mapstructure:"ipfs_addr"`
99 // Default configurable base parameters.
100 func DefaultBaseConfig() BaseConfig {
102 Moniker: "anonymous",
103 ProfListenAddress: "",
105 DBBackend: "leveldb",
107 KeysPath: "keystore",
108 IpfsAddress: "127.0.0.1:5001",
112 func (b BaseConfig) DBDir() string {
113 return rootify(b.DBPath, b.RootDir)
116 func (b BaseConfig) KeysDir() string {
117 return rootify(b.KeysPath, b.RootDir)
121 type P2PConfig struct {
122 ListenAddress string `mapstructure:"laddr"`
123 Seeds string `mapstructure:"seeds"`
124 SkipUPNP bool `mapstructure:"skip_upnp"`
125 MaxNumPeers int `mapstructure:"max_num_peers"`
126 HandshakeTimeout int `mapstructure:"handshake_timeout"`
127 DialTimeout int `mapstructure:"dial_timeout"`
128 ProxyAddress string `mapstructure:"proxy_address"`
129 ProxyUsername string `mapstructure:"proxy_username"`
130 ProxyPassword string `mapstructure:"proxy_password"`
133 // Default configurable p2p parameters.
134 func DefaultP2PConfig() *P2PConfig {
136 ListenAddress: "tcp://0.0.0.0:46656",
139 HandshakeTimeout: 30,
147 //-----------------------------------------------------------------------------
148 type WalletConfig struct {
149 Disable bool `mapstructure:"disable"`
150 Rescan bool `mapstructure:"rescan"`
151 MaxTxFee uint64 `mapstructure:"max_tx_fee"`
154 type RPCAuthConfig struct {
155 Disable bool `mapstructure:"disable"`
158 type WebConfig struct {
159 Closed bool `mapstructure:"closed"`
162 type SideChainConfig struct {
163 FedpegXPubs string `mapstructure:"fedpeg_xpubs"`
164 SignBlockXPubs string `mapstructure:"sign_block_xpubs"`
165 PeginMinDepth uint64 `mapstructure:"pegin_confirmation_depth"`
166 ParentGenesisBlockHash string `mapstructure:"parent_genesis_block_hash"`
169 type MainChainRpcConfig struct {
170 MainchainRpcHost string `mapstructure:"mainchain_rpc_host"`
171 MainchainRpcPort string `mapstructure:"mainchain_rpc_port"`
172 MainchainToken string `mapstructure:"mainchain_rpc_token"`
175 type WebsocketConfig struct {
176 MaxNumWebsockets int `mapstructure:"max_num_websockets"`
177 MaxNumConcurrentReqs int `mapstructure:"max_num_concurrent_reqs"`
180 type ConsensusConfig struct {
181 Type string `mapstructure:"consensus_type"`
182 Period uint64 `json:"period"` // Number of seconds between blocks to enforce
183 MaxSignerCount uint64 `json:"max_signers_count"` // Max count of signers
184 MinVoterBalance uint64 `json:"min_boter_balance"` // Min voter balance to valid this vote
185 GenesisTimestamp uint64 `json:"genesis_timestamp"` // The LoopStartTime of first Block
186 Coinbase string `json:"coinbase"`
187 XPrv string `json:"xprv"`
188 SelfVoteSigners []string `json:"signers"` // Signers vote by themselves to seal the block, make sure the signer accounts are pre-funded
189 Signers []common.Address
192 type DposConfig struct {
193 Period uint64 `json:"period"` // Number of seconds between blocks to enforce
194 MaxSignerCount uint64 `json:"max_signers_count"` // Max count of signers
195 MinVoterBalance uint64 `json:"min_boter_balance"` // Min voter balance to valid this vote
196 GenesisTimestamp uint64 `json:"genesis_timestamp"` // The LoopStartTime of first Block
197 Coinbase string `json:"coinbase"`
198 XPrv string `json:"xprv"`
199 SelfVoteSigners []string `json:"signers"` // Signers vote by themselves to seal the block, make sure the signer accounts are pre-funded
200 Signers []common.Address
203 // Default configurable rpc's auth parameters.
204 func DefaultRPCAuthConfig() *RPCAuthConfig {
205 return &RPCAuthConfig{
210 // Default configurable web parameters.
211 func DefaultWebConfig() *WebConfig {
217 // Default configurable wallet parameters.
218 func DefaultWalletConfig() *WalletConfig {
219 return &WalletConfig{
222 MaxTxFee: uint64(1000000000),
226 // DeafultSideChainConfig for sidechain
227 func DefaultSideChainConfig() *SideChainConfig {
228 return &SideChainConfig{
230 ParentGenesisBlockHash: "a75483474799ea1aa6bb910a1a5025b4372bf20bef20f246a2c2dc5e12e8a053",
234 func DefaultMainChainRpc() *MainChainRpcConfig {
235 return &MainChainRpcConfig{
236 MainchainRpcHost: "127.0.0.1",
237 MainchainRpcPort: "9888",
241 func DefaultWebsocketConfig() *WebsocketConfig {
242 return &WebsocketConfig{
243 MaxNumWebsockets: 25,
244 MaxNumConcurrentReqs: 20,
248 func DefaultDposConfig() *DposConfig {
253 GenesisTimestamp: 1524549600,
257 func DefaultConsensusCOnfig() *ConsensusConfig {
258 return &ConsensusConfig{
263 GenesisTimestamp: 1524549600}
266 //-----------------------------------------------------------------------------
269 // helper function to make config creation independent of root dir
270 func rootify(path, root string) string {
271 if filepath.IsAbs(path) {
274 return filepath.Join(root, path)
277 // DefaultDataDir is the default data directory to use for the databases and other
278 // persistence requirements.
279 func DefaultDataDir() string {
280 // Try to place the data folder in the user's home dir
283 return "./.bytom_sidechain"
285 switch runtime.GOOS {
287 // In order to be compatible with old data path,
288 // copy the data from the old path to the new path
289 oldPath := filepath.Join(home, "Library", "Bytom_sidechain")
290 newPath := filepath.Join(home, "Library", "Application Support", "Bytom_sidechain")
291 if !isFolderNotExists(oldPath) && isFolderNotExists(newPath) {
292 if err := os.Rename(oldPath, newPath); err != nil {
293 log.Errorf("DefaultDataDir: %v", err)
299 return filepath.Join(home, "AppData", "Roaming", "Bytom_sidechain")
301 return filepath.Join(home, ".bytom_sidechain")
305 func isFolderNotExists(path string) bool {
306 _, err := os.Stat(path)
307 return os.IsNotExist(err)
310 func homeDir() string {
311 if home := os.Getenv("HOME"); home != "" {
314 if usr, err := user.Current(); err == nil {