github.com/Gessiux/neatchain@v1.3.1/chain/neatchain/config.go (about) 1 // Copyright 2017 The go-ethereum Authors 2 // This file is part of go-neatchain. 3 // 4 // go-ethereum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // go-ethereum is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-neatchain. If not, see <http://www.gnu.org/licenses/>. 16 17 package main 18 19 import ( 20 "bufio" 21 "errors" 22 "fmt" 23 "io" 24 "os" 25 "reflect" 26 "unicode" 27 28 "github.com/Gessiux/neatchain/chain/core" 29 "github.com/Gessiux/neatchain/chain/log" 30 31 "gopkg.in/urfave/cli.v1" 32 33 neatptc "github.com/Gessiux/neatchain/neatptc" 34 "github.com/Gessiux/neatchain/network/node" 35 "github.com/Gessiux/neatchain/params" 36 "github.com/Gessiux/neatchain/utilities/utils" 37 "github.com/naoina/toml" 38 ) 39 40 var ( 41 dumpConfigCommand = cli.Command{ 42 Action: utils.MigrateFlags(dumpConfig), 43 Name: "dumpconfig", 44 Usage: "Show configuration values", 45 ArgsUsage: "", 46 Category: "MISCELLANEOUS COMMANDS", 47 Description: `The dumpconfig command shows configuration values.`, 48 } 49 50 configFileFlag = cli.StringFlag{ 51 Name: "config", 52 Usage: "TOML configuration file", 53 } 54 ) 55 56 // These settings ensure that TOML keys use the same names as Go struct fields. 57 var tomlSettings = toml.Config{ 58 NormFieldName: func(rt reflect.Type, key string) string { 59 return key 60 }, 61 FieldToKey: func(rt reflect.Type, field string) string { 62 return field 63 }, 64 MissingField: func(rt reflect.Type, field string) error { 65 link := "" 66 if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" { 67 link = fmt.Sprintf(", see https://godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name()) 68 } 69 return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link) 70 }, 71 } 72 73 type ethstatsConfig struct { 74 URL string `toml:",omitempty"` 75 } 76 77 type gethConfig struct { 78 Eth neatptc.Config 79 Node node.Config 80 Ethstats ethstatsConfig 81 } 82 83 func loadConfig(file string, cfg *gethConfig) error { 84 f, err := os.Open(file) 85 if err != nil { 86 return err 87 } 88 defer f.Close() 89 90 err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg) 91 // Add file name to errors that have a line number. 92 if _, ok := err.(*toml.LineError); ok { 93 err = errors.New(file + ", " + err.Error()) 94 } 95 return err 96 } 97 98 func defaultNodeConfig() node.Config { 99 cfg := node.DefaultConfig 100 cfg.Name = clientIdentifier 101 cfg.Version = params.VersionWithCommit(gitCommit) 102 cfg.HTTPModules = append(cfg.HTTPModules, "int", "eth") 103 cfg.WSModules = append(cfg.WSModules, "int", "eth") 104 cfg.IPCPath = "neatchain.ipc" 105 return cfg 106 } 107 108 func makeConfigNode(ctx *cli.Context, chainId string) (*node.Node, gethConfig) { 109 // Load defaults. 110 cfg := gethConfig{ 111 Eth: neatptc.DefaultConfig, 112 Node: defaultNodeConfig(), 113 } 114 115 // Load config file. 116 if file := ctx.GlobalString(configFileFlag.Name); file != "" { 117 if err := loadConfig(file, &cfg); err != nil { 118 utils.Fatalf("%v", err) 119 } 120 } 121 122 // Apply flags. 123 cfg.Node.ChainId = chainId 124 125 // Setup Log 126 //logDir := path.Join(ctx.GlobalString("datadir"), ctx.GlobalString("logDir"), chainId) 127 cfg.Node.Logger = log.NewLogger(chainId, "", ctx.GlobalInt("verbosity"), ctx.GlobalBool("debug"), ctx.GlobalString("vmodule"), ctx.GlobalString("backtrace")) 128 129 utils.SetNodeConfig(ctx, &cfg.Node) 130 stack, err := node.New(&cfg.Node) 131 if err != nil { 132 utils.Fatalf("Failed to create the protocol stack: %v", err) 133 } 134 utils.SetEthConfig(ctx, stack, &cfg.Eth) 135 if ctx.GlobalIsSet(utils.EthStatsURLFlag.Name) { 136 cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name) 137 } 138 139 //utils.SetShhConfig(ctx, stack, &cfg.Shh) 140 utils.SetGeneralConfig(ctx) 141 142 return stack, cfg 143 } 144 145 func makeFullNode(ctx *cli.Context, cch core.CrossChainHelper, chainId string) *node.Node { 146 stack, cfg := makeConfigNode(ctx, chainId) 147 148 utils.RegisterIntService(stack, &cfg.Eth, ctx, cch) 149 150 // Whisper must be explicitly enabled by specifying at least 1 whisper flag or in dev mode 151 //shhEnabled := enableWhisper(ctx) 152 //shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DeveloperFlag.Name) 153 //if shhEnabled || shhAutoEnabled { 154 // if ctx.GlobalIsSet(utils.WhisperMaxMessageSizeFlag.Name) { 155 // cfg.Shh.MaxMessageSize = uint32(ctx.Int(utils.WhisperMaxMessageSizeFlag.Name)) 156 // } 157 // if ctx.GlobalIsSet(utils.WhisperMinPOWFlag.Name) { 158 // cfg.Shh.MinimumAcceptedPOW = ctx.Float64(utils.WhisperMinPOWFlag.Name) 159 // } 160 // utils.RegisterShhService(stack, &cfg.Shh) 161 //} 162 163 // Add the Ethereum Stats daemon if requested. 164 //if cfg.Ethstats.URL != "" { 165 // utils.RegisterEthStatsService(stack, cfg.Ethstats.URL) 166 //} 167 if err := stack.GatherServices(); err != nil { 168 return nil 169 } else { 170 return stack 171 } 172 } 173 174 // dumpConfig is the dumpconfig command. 175 func dumpConfig(ctx *cli.Context) error { 176 _, cfg := makeConfigNode(ctx, clientIdentifier) 177 comment := "" 178 179 if cfg.Eth.Genesis != nil { 180 cfg.Eth.Genesis = nil 181 comment += "# Note: this config doesn't contain the genesis block.\n\n" 182 } 183 184 out, err := tomlSettings.Marshal(&cfg) 185 if err != nil { 186 return err 187 } 188 io.WriteString(os.Stdout, comment) 189 os.Stdout.Write(out) 190 return nil 191 }