code.vegaprotocol.io/vega@v0.79.0/core/config/config.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 //lint:file-ignore SA5008 duplicated struct tags are ok for config 17 18 package config 19 20 import ( 21 "errors" 22 "fmt" 23 "os" 24 25 "code.vegaprotocol.io/vega/core/admin" 26 "code.vegaprotocol.io/vega/core/api" 27 "code.vegaprotocol.io/vega/core/assets" 28 "code.vegaprotocol.io/vega/core/banking" 29 "code.vegaprotocol.io/vega/core/blockchain" 30 "code.vegaprotocol.io/vega/core/broker" 31 "code.vegaprotocol.io/vega/core/checkpoint" 32 "code.vegaprotocol.io/vega/core/client/eth" 33 "code.vegaprotocol.io/vega/core/collateral" 34 cfgencoding "code.vegaprotocol.io/vega/core/config/encoding" 35 "code.vegaprotocol.io/vega/core/coreapi" 36 "code.vegaprotocol.io/vega/core/datasource/spec" 37 "code.vegaprotocol.io/vega/core/delegation" 38 "code.vegaprotocol.io/vega/core/epochtime" 39 "code.vegaprotocol.io/vega/core/evtforward" 40 "code.vegaprotocol.io/vega/core/execution" 41 "code.vegaprotocol.io/vega/core/genesis" 42 "code.vegaprotocol.io/vega/core/governance" 43 "code.vegaprotocol.io/vega/core/limits" 44 "code.vegaprotocol.io/vega/core/metrics" 45 "code.vegaprotocol.io/vega/core/netparams" 46 "code.vegaprotocol.io/vega/core/nodewallets" 47 "code.vegaprotocol.io/vega/core/notary" 48 "code.vegaprotocol.io/vega/core/pow" 49 "code.vegaprotocol.io/vega/core/processor" 50 "code.vegaprotocol.io/vega/core/protocolupgrade" 51 "code.vegaprotocol.io/vega/core/rewards" 52 "code.vegaprotocol.io/vega/core/snapshot" 53 "code.vegaprotocol.io/vega/core/spam" 54 "code.vegaprotocol.io/vega/core/staking" 55 "code.vegaprotocol.io/vega/core/statevar" 56 "code.vegaprotocol.io/vega/core/stats" 57 "code.vegaprotocol.io/vega/core/validators" 58 "code.vegaprotocol.io/vega/core/validators/erc20multisig" 59 "code.vegaprotocol.io/vega/core/vegatime" 60 "code.vegaprotocol.io/vega/core/vesting" 61 vgfs "code.vegaprotocol.io/vega/libs/fs" 62 "code.vegaprotocol.io/vega/libs/pprof" 63 "code.vegaprotocol.io/vega/logging" 64 "code.vegaprotocol.io/vega/paths" 65 ) 66 67 // Config ties together all other application configuration types. 68 type Config struct { 69 Admin admin.Config `group:"Admin" namespace:"admin"` 70 API api.Config `group:"API" namespace:"api"` 71 Blockchain blockchain.Config `group:"Blockchain" namespace:"blockchain"` 72 Collateral collateral.Config `group:"Collateral" namespace:"collateral"` 73 CoreAPI coreapi.Config `group:"CoreAPI" namespace:"coreapi"` 74 Execution execution.Config `group:"Execution" namespace:"execution"` 75 Ethereum eth.Config `group:"Ethereum" namespace:"ethereum"` 76 Processor processor.Config `group:"Processor" namespace:"processor"` 77 Logging logging.Config `group:"Logging" namespace:"logging"` 78 Oracles spec.Config `group:"Oracles" namespace:"oracles"` 79 Time vegatime.Config `group:"Time" namespace:"time"` 80 Epoch epochtime.Config `group:"Epoch" namespace:"epochtime"` 81 Metrics metrics.Config `group:"Metrics" namespace:"metrics"` 82 Governance governance.Config `group:"Governance" namespace:"governance"` 83 NodeWallet nodewallets.Config `group:"NodeWallet" namespace:"nodewallet"` 84 Assets assets.Config `group:"Assets" namespace:"assets"` 85 Notary notary.Config `group:"Notary" namespace:"notary"` 86 EvtForward evtforward.Config `group:"EvtForward" namespace:"evtForward"` 87 Genesis genesis.Config `group:"Genesis" namespace:"genesis"` 88 Validators validators.Config `group:"Validators" namespace:"validators"` 89 Banking banking.Config `group:"Banking" namespace:"banking"` 90 Stats stats.Config `group:"Stats" namespace:"stats"` 91 NetworkParameters netparams.Config `group:"NetworkParameters" namespace:"netparams"` 92 Limits limits.Config `group:"Limits" namespace:"limits"` 93 Checkpoint checkpoint.Config `group:"Checkpoint" namespace:"checkpoint"` 94 Staking staking.Config `group:"Staking" namespace:"staking"` 95 Broker broker.Config `group:"Broker" namespace:"broker"` 96 Rewards rewards.Config `group:"Rewards" namespace:"rewards"` 97 Delegation delegation.Config `group:"Delegation" namespace:"delegation"` 98 Spam spam.Config `group:"Spam" namespace:"spam"` 99 PoW pow.Config `group:"ProofOfWork" namespace:"pow"` 100 Snapshot snapshot.Config `group:"Snapshot" namespace:"snapshot"` 101 StateVar statevar.Config `group:"StateVar" namespace:"statevar"` 102 ERC20MultiSig erc20multisig.Config `group:"ERC20MultiSig" namespace:"erc20multisig"` 103 ProtocolUpgrade protocolupgrade.Config `group:"ProtocolUpgrade" namespace:"protocolupgrade"` 104 Pprof pprof.Config `group:"Pprof" namespace:"pprof"` 105 Vesting vesting.Config `group:"Vesting" namespace:"vesting"` 106 107 NodeMode cfgencoding.NodeMode `description:"The mode of the vega node [validator, full]" long:"mode"` 108 MaxMemoryPercent uint8 `description:"The maximum amount of memory reserved for the vega node (default: 33%)" long:"max-memory-percent"` 109 } 110 111 // NewDefaultConfig returns a set of default configs for all vega packages, as specified at the per package 112 // config level, if there is an error initialising any of the configs then this is returned. 113 func NewDefaultConfig() Config { 114 return Config{ 115 NodeMode: cfgencoding.NodeModeValidator, 116 MaxMemoryPercent: 33, 117 Admin: admin.NewDefaultConfig(), 118 API: api.NewDefaultConfig(), 119 CoreAPI: coreapi.NewDefaultConfig(), 120 Blockchain: blockchain.NewDefaultConfig(), 121 Execution: execution.NewDefaultConfig(), 122 Ethereum: eth.NewDefaultConfig(), 123 Processor: processor.NewDefaultConfig(), 124 Oracles: spec.NewDefaultConfig(), 125 Time: vegatime.NewDefaultConfig(), 126 Epoch: epochtime.NewDefaultConfig(), 127 Pprof: pprof.NewDefaultConfig(), 128 Logging: logging.NewDefaultConfig(), 129 Collateral: collateral.NewDefaultConfig(), 130 Metrics: metrics.NewDefaultConfig(), 131 Governance: governance.NewDefaultConfig(), 132 NodeWallet: nodewallets.NewDefaultConfig(), 133 Assets: assets.NewDefaultConfig(), 134 Notary: notary.NewDefaultConfig(), 135 EvtForward: evtforward.NewDefaultConfig(), 136 Genesis: genesis.NewDefaultConfig(), 137 Validators: validators.NewDefaultConfig(), 138 Banking: banking.NewDefaultConfig(), 139 Stats: stats.NewDefaultConfig(), 140 NetworkParameters: netparams.NewDefaultConfig(), 141 Limits: limits.NewDefaultConfig(), 142 Checkpoint: checkpoint.NewDefaultConfig(), 143 Staking: staking.NewDefaultConfig(), 144 Broker: broker.NewDefaultConfig(), 145 Snapshot: snapshot.DefaultConfig(), 146 StateVar: statevar.NewDefaultConfig(), 147 ERC20MultiSig: erc20multisig.NewDefaultConfig(), 148 PoW: pow.NewDefaultConfig(), 149 ProtocolUpgrade: protocolupgrade.NewDefaultConfig(), 150 Vesting: vesting.NewDefaultConfig(), 151 } 152 } 153 154 func (c Config) IsValidator() bool { 155 return c.NodeMode == cfgencoding.NodeModeValidator 156 } 157 158 func (c *Config) SetDefaultMaxMemoryPercent() { 159 // disable restriction if node is a validator 160 if c.NodeMode == cfgencoding.NodeModeValidator { 161 c.MaxMemoryPercent = 100 162 } 163 } 164 165 func (c Config) GetMaxMemoryFactor() (float64, error) { 166 if c.MaxMemoryPercent <= 0 || c.MaxMemoryPercent > 100 { 167 return 0, errors.New("MaxMemoryPercent is out of range, expect > 0 and <= 100") 168 } 169 170 return float64(c.MaxMemoryPercent) / 100., nil 171 } 172 173 func (c Config) HaveEthClient() bool { 174 return c.IsValidator() && len(c.Ethereum.RPCEndpoint) > 0 && len(c.Ethereum.EVMBridgeConfigs) > 0 175 } 176 177 func (c Config) IsNullChain() bool { 178 return c.Blockchain.ChainProvider == blockchain.ProviderNullChain 179 } 180 181 type Loader struct { 182 configFilePath string 183 } 184 185 func InitialiseLoader(vegaPaths paths.Paths) (*Loader, error) { 186 configFilePath, err := vegaPaths.CreateConfigPathFor(paths.NodeDefaultConfigFile) 187 if err != nil { 188 return nil, fmt.Errorf("couldn't get path for %s: %w", paths.NodeDefaultConfigFile, err) 189 } 190 191 return &Loader{ 192 configFilePath: configFilePath, 193 }, nil 194 } 195 196 func (l *Loader) ConfigFilePath() string { 197 return l.configFilePath 198 } 199 200 func (l *Loader) ConfigExists() (bool, error) { 201 exists, err := vgfs.FileExists(l.configFilePath) 202 if err != nil { 203 return false, err 204 } 205 return exists, nil 206 } 207 208 func (l *Loader) Save(cfg *Config) error { 209 if err := paths.WriteStructuredFile(l.configFilePath, cfg); err != nil { 210 return fmt.Errorf("couldn't write configuration file at %s: %w", l.configFilePath, err) 211 } 212 return nil 213 } 214 215 func (l *Loader) Get() (*Config, error) { 216 cfg := NewDefaultConfig() 217 if err := paths.ReadStructuredFile(l.configFilePath, &cfg); err != nil { 218 return nil, fmt.Errorf("couldn't read configuration file at %s: %w", l.configFilePath, err) 219 } 220 return &cfg, nil 221 } 222 223 func (l *Loader) Remove() { 224 _ = os.RemoveAll(l.configFilePath) 225 }