-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
135 lines (114 loc) · 5.07 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
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.DATANODE file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
//lint:file-ignore SA5008 duplicated struct tags are ok for config
package config
import (
"errors"
"fmt"
"os"
"github.com/zeta-protocol/zeta/datanode/admin"
"github.com/zeta-protocol/zeta/datanode/networkhistory"
"github.com/zeta-protocol/zeta/datanode/api"
"github.com/zeta-protocol/zeta/datanode/broker"
"github.com/zeta-protocol/zeta/datanode/candlesv2"
"github.com/zeta-protocol/zeta/datanode/config/encoding"
"github.com/zeta-protocol/zeta/datanode/gateway"
"github.com/zeta-protocol/zeta/datanode/metrics"
"github.com/zeta-protocol/zeta/datanode/service"
"github.com/zeta-protocol/zeta/datanode/sqlstore"
vgfs "github.com/zeta-protocol/zeta/libs/fs"
"github.com/zeta-protocol/zeta/libs/pprof"
"github.com/zeta-protocol/zeta/logging"
"github.com/zeta-protocol/zeta/paths"
)
// Config ties together all other application configuration types.
type Config struct {
Admin admin.Config `group:"Admin" namespace:"admin"`
API api.Config `group:"API" namespace:"api"`
CandlesV2 candlesv2.Config `group:"CandlesV2" namespace:"candlesv2"`
Logging logging.Config `group:"Logging" namespace:"logging"`
SQLStore sqlstore.Config `group:"Sqlstore" namespace:"sqlstore"`
Gateway gateway.Config `group:"Gateway" namespace:"gateway"`
Metrics metrics.Config `group:"Metrics" namespace:"metrics"`
Broker broker.Config `group:"Broker" namespace:"broker"`
Service service.Config `group:"Service" namespace:"service"`
Pprof pprof.Config `group:"Pprof" namespace:"pprof"`
GatewayEnabled encoding.Bool `long:"gateway-enabled" choice:"true" choice:"false" description:" "`
NetworkHistory networkhistory.Config `group:"NetworkHistory" namespace:"networkhistory"`
AutoInitialiseFromNetworkHistory encoding.Bool `long:"auto-initialise" choice:"true" choice:"false" description:"if true the node will attempt to load the latest history segment(s) from network history if the node is empty"`
ChainID string `long:"chainID"`
MaxMemoryPercent uint8 `long:"max-memory-percent" description:"The maximum amount of memory reserved for the data node (default: 33%)"`
}
// NewDefaultConfig returns a set of default configs for all zeta packages, as specified at the per package
// config level, if there is an error initialising any of the configs then this is returned.
func NewDefaultConfig() Config {
return Config{
MaxMemoryPercent: 33,
Admin: admin.NewDefaultConfig(),
API: api.NewDefaultConfig(),
CandlesV2: candlesv2.NewDefaultConfig(),
SQLStore: sqlstore.NewDefaultConfig(),
Pprof: pprof.NewDefaultConfig(),
Logging: logging.NewDefaultConfig(),
Gateway: gateway.NewDefaultConfig(),
Metrics: metrics.NewDefaultConfig(),
Broker: broker.NewDefaultConfig(),
Service: service.NewDefaultConfig(),
GatewayEnabled: true,
NetworkHistory: networkhistory.NewDefaultConfig(),
AutoInitialiseFromNetworkHistory: false,
}
}
func (c Config) GetMaxMemoryFactor() (float64, error) {
if c.MaxMemoryPercent <= 0 || c.MaxMemoryPercent >= 100 {
return 0, errors.New("MaxMemoryPercent is out of range, expect > 0 and < 100")
}
return float64(c.MaxMemoryPercent) / 100., nil
}
type Loader struct {
configFilePath string
}
func InitialiseLoader(zetaPaths paths.Paths) (*Loader, error) {
configFilePath, err := zetaPaths.CreateConfigPathFor(paths.DataNodeDefaultConfigFile)
if err != nil {
return nil, fmt.Errorf("couldn't get path for %s: %w", paths.NodeDefaultConfigFile, err)
}
return &Loader{
configFilePath: configFilePath,
}, nil
}
func (l *Loader) ConfigFilePath() string {
return l.configFilePath
}
func (l *Loader) ConfigExists() (bool, error) {
exists, err := vgfs.FileExists(l.configFilePath)
if err != nil {
return false, err
}
return exists, nil
}
func (l *Loader) Save(cfg *Config) error {
if err := paths.WriteStructuredFile(l.configFilePath, cfg); err != nil {
return fmt.Errorf("couldn't write configuration file at %s: %w", l.configFilePath, err)
}
return nil
}
func (l *Loader) Get() (*Config, error) {
cfg := NewDefaultConfig()
if err := paths.ReadStructuredFile(l.configFilePath, &cfg); err != nil {
return nil, fmt.Errorf("couldn't read configuration file at %s: %w", l.configFilePath, err)
}
return &cfg, nil
}
func (l *Loader) Remove() {
_ = os.RemoveAll(l.configFilePath)
}