github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/config/config.go (about) 1 package config 2 3 import ( 4 "bytes" 5 "path/filepath" 6 "strings" 7 8 "github.com/0xPolygon/supernets2-node/aggregator" 9 "github.com/0xPolygon/supernets2-node/db" 10 "github.com/0xPolygon/supernets2-node/etherman" 11 "github.com/0xPolygon/supernets2-node/ethtxmanager" 12 "github.com/0xPolygon/supernets2-node/event" 13 "github.com/0xPolygon/supernets2-node/gasprice" 14 "github.com/0xPolygon/supernets2-node/jsonrpc" 15 "github.com/0xPolygon/supernets2-node/log" 16 "github.com/0xPolygon/supernets2-node/merkletree" 17 "github.com/0xPolygon/supernets2-node/metrics" 18 "github.com/0xPolygon/supernets2-node/pool" 19 "github.com/0xPolygon/supernets2-node/pricegetter" 20 "github.com/0xPolygon/supernets2-node/sequencer" 21 "github.com/0xPolygon/supernets2-node/sequencesender" 22 "github.com/0xPolygon/supernets2-node/state/runtime/executor" 23 "github.com/0xPolygon/supernets2-node/synchronizer" 24 "github.com/mitchellh/mapstructure" 25 "github.com/spf13/viper" 26 "github.com/urfave/cli/v2" 27 ) 28 29 const ( 30 // FlagYes is the flag for yes. 31 FlagYes = "yes" 32 // FlagCfg is the flag for cfg. 33 FlagCfg = "cfg" 34 // FlagNetwork is the flag for the network name. Valid values: ["testnet", "mainnet", "custom"]. 35 FlagNetwork = "network" 36 // FlagCustomNetwork is the flag for the custom network file. This is required if --network=custom 37 FlagCustomNetwork = "custom-network-file" 38 // FlagAmount is the flag for amount. 39 FlagAmount = "amount" 40 // FlagRemoteMT is the flag for remote-merkletree. 41 FlagRemoteMT = "remote-merkletree" 42 // FlagComponents is the flag for components. 43 FlagComponents = "components" 44 // FlagHTTPAPI is the flag for http.api. 45 FlagHTTPAPI = "http.api" 46 // FlagKeyStorePath is the path of the key store file containing the private key of the account going to sing and approve the tokens 47 FlagKeyStorePath = "key-store-path" 48 // FlagPassword is the password needed to decrypt the key store 49 FlagPassword = "password" 50 // FlagMigrations is the flag for migrations. 51 FlagMigrations = "migrations" 52 // FlagMaxAmount is the flag to avoid to use the flag FlagAmount 53 FlagMaxAmount = "max-amount" 54 ) 55 56 // Config represents the configuration of the entire Hermez Node 57 type Config struct { 58 IsTrustedSequencer bool `mapstructure:"IsTrustedSequencer"` 59 Log log.Config 60 Etherman etherman.Config 61 EthTxManager ethtxmanager.Config 62 Pool pool.Config 63 RPC jsonrpc.Config 64 Synchronizer synchronizer.Config 65 Sequencer sequencer.Config 66 SequenceSender sequencesender.Config 67 PriceGetter pricegetter.Config 68 Aggregator aggregator.Config 69 NetworkConfig NetworkConfig 70 L2GasPriceSuggester gasprice.Config 71 Executor executor.Config 72 MTClient merkletree.Config 73 StateDB db.Config 74 Metrics metrics.Config 75 EventLog event.Config 76 } 77 78 // Default parses the default configuration values. 79 func Default() (*Config, error) { 80 var cfg Config 81 viper.SetConfigType("toml") 82 83 err := viper.ReadConfig(bytes.NewBuffer([]byte(DefaultValues))) 84 if err != nil { 85 return nil, err 86 } 87 err = viper.Unmarshal(&cfg, viper.DecodeHook(mapstructure.TextUnmarshallerHookFunc())) 88 if err != nil { 89 return nil, err 90 } 91 return &cfg, nil 92 } 93 94 // Load loads the configuration 95 func Load(ctx *cli.Context) (*Config, error) { 96 cfg, err := Default() 97 if err != nil { 98 return nil, err 99 } 100 configFilePath := ctx.String(FlagCfg) 101 if configFilePath != "" { 102 dirName, fileName := filepath.Split(configFilePath) 103 104 fileExtension := strings.TrimPrefix(filepath.Ext(fileName), ".") 105 fileNameWithoutExtension := strings.TrimSuffix(fileName, "."+fileExtension) 106 107 viper.AddConfigPath(dirName) 108 viper.SetConfigName(fileNameWithoutExtension) 109 viper.SetConfigType(fileExtension) 110 } 111 viper.AutomaticEnv() 112 replacer := strings.NewReplacer(".", "_") 113 viper.SetEnvKeyReplacer(replacer) 114 viper.SetEnvPrefix("SUPERNETS2_NODE") 115 err = viper.ReadInConfig() 116 if err != nil { 117 _, ok := err.(viper.ConfigFileNotFoundError) 118 if ok { 119 log.Infof("config file not found") 120 } else { 121 log.Infof("error reading config file: ", err) 122 return nil, err 123 } 124 } 125 126 decodeHooks := []viper.DecoderConfigOption{ 127 // this allows arrays to be decoded from env var separated by ",", example: MY_VAR="value1,value2,value3" 128 viper.DecodeHook(mapstructure.ComposeDecodeHookFunc(mapstructure.TextUnmarshallerHookFunc(), mapstructure.StringToSliceHookFunc(","))), 129 } 130 131 err = viper.Unmarshal(&cfg, decodeHooks...) 132 if err != nil { 133 return nil, err 134 } 135 // Load genesis parameters 136 cfg.loadNetworkConfig(ctx) 137 return cfg, nil 138 }