/
config.go
128 lines (107 loc) · 3.86 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
package config
import (
"fmt"
apiConfig "github.com/spacemeshos/go-spacemesh/api/config"
consensusConfig "github.com/spacemeshos/go-spacemesh/consensus/config"
"github.com/spacemeshos/go-spacemesh/filesystem"
hareConfig "github.com/spacemeshos/go-spacemesh/hare/config"
"github.com/spacemeshos/go-spacemesh/log"
p2pConfig "github.com/spacemeshos/go-spacemesh/p2p/config"
timeConfig "github.com/spacemeshos/go-spacemesh/timesync/config"
"github.com/spf13/viper"
"path/filepath"
"time"
)
const (
defaultConfigFileName = "./config.toml"
defaultLogFileName = "spacemesh.log"
defaultAccountFileName = "accounts"
defaultDataDirName = "spacemesh"
Genesis = 0
GenesisId = 420
)
var (
defaultHomeDir = filesystem.GetUserHomeDirectory()
defaultDataDir = filepath.Join(defaultHomeDir, defaultDataDirName)
defaultConfigFile = filepath.Join(defaultHomeDir, defaultConfigFileName)
defaultLogDir = filepath.Join(defaultHomeDir, defaultLogFileName)
defaultAccountDir = filepath.Join(defaultHomeDir, defaultAccountFileName)
defaultTestMode = false
)
// Config defines the top level configuration for a spacemesh node
type Config struct {
BaseConfig `mapstructure:"main"`
P2P p2pConfig.Config `mapstructure:"p2p"`
API apiConfig.Config `mapstructure:"api"`
CONSENSUS consensusConfig.Config `mapstructure:"consensus"`
HARE hareConfig.Config `mapstructure:"hare"`
TIME timeConfig.TimeConfig `mapstructure:"time"`
}
// BaseConfig defines the default configuration options for spacemesh app
type BaseConfig struct {
HomeDir string
DataDir string `mapstructure:"data-folder"`
ConfigFile string `mapstructure:"config"`
LogDir string `mapstructure:"log-dir"`
AccountDir string `mapstructure:"account-dir"`
TestMode bool `mapstructure:"test-mode"`
CollectMetrics bool `mapstructure:"metrics"`
MetricsPort int `mapstructure:"metrics-port"`
OracleServer string `mapstructure:"oracle_server"`
OracleServerWorldId uint64 `mapstructure:"oracle_server_worldid"`
GenesisTime string `mapstructure:"genesis-time"`
LayerDurationSec int `mapstructure:"layer-duration-sec"`
}
// DefaultConfig returns the default configuration for a spacemesh node
func DefaultConfig() Config {
return Config{
BaseConfig: defaultBaseConfig(),
P2P: p2pConfig.DefaultConfig(),
API: apiConfig.DefaultConfig(),
CONSENSUS: consensusConfig.DefaultConfig(),
HARE: hareConfig.DefaultConfig(),
TIME: timeConfig.DefaultConfig(),
}
}
// DefaultBaseConfig returns a default configuration for spacemesh
func defaultBaseConfig() BaseConfig {
return BaseConfig{
HomeDir: defaultHomeDir,
DataDir: defaultDataDir,
ConfigFile: defaultConfigFileName,
LogDir: defaultLogDir,
AccountDir: defaultAccountDir,
TestMode: defaultTestMode,
CollectMetrics: false,
MetricsPort: 1010,
OracleServer: "http://localhost:3030",
OracleServerWorldId: 0,
GenesisTime: time.Now().Format(time.RFC3339),
LayerDurationSec: 10,
}
}
// LoadConfig load the config file
func LoadConfig(fileLocation string, vip *viper.Viper) (err error) {
log.Info("Parsing config file at location: %s", fileLocation)
if fileLocation == "" {
fileLocation = defaultConfigFileName
}
vip.SetConfigFile(fileLocation)
err = vip.ReadInConfig()
if err != nil {
if fileLocation != defaultConfigFileName {
log.Warning("failed loading %v trying %v", fileLocation, defaultConfigFileName)
vip.SetConfigFile(defaultConfigFileName)
err = vip.ReadInConfig()
}
// we change err so check again
if err != nil {
return fmt.Errorf("failed to read config file %v", err)
}
}
return nil
}
// SetConfigFile overrides the default config file path
func (cfg *BaseConfig) SetConfigFile(file string) {
cfg.ConfigFile = file
}