github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/cmd/smc/config.go (about) 1 // Copyright 2017 The Spectrum Authors 2 // This file is part of Spectrum. 3 // 4 // Spectrum 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 // Spectrum 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 Spectrum. If not, see <http://www.gnu.org/licenses/>. 16 17 package main 18 19 import ( 20 "bufio" 21 "errors" 22 "fmt" 23 "github.com/SmartMeshFoundation/Spectrum/contracts/chief" 24 "github.com/SmartMeshFoundation/Spectrum/contracts/statute" 25 "io" 26 "os" 27 "reflect" 28 "unicode" 29 30 "gopkg.in/urfave/cli.v1" 31 32 "github.com/SmartMeshFoundation/Spectrum/cmd/utils" 33 "github.com/SmartMeshFoundation/Spectrum/dashboard" 34 "github.com/SmartMeshFoundation/Spectrum/eth" 35 "github.com/SmartMeshFoundation/Spectrum/node" 36 "github.com/SmartMeshFoundation/Spectrum/params" 37 whisper "github.com/SmartMeshFoundation/Spectrum/whisper/whisperv5" 38 "github.com/naoina/toml" 39 ) 40 41 var ( 42 dumpConfigCommand = cli.Command{ 43 Action: utils.MigrateFlags(dumpConfig), 44 Name: "dumpconfig", 45 Usage: "Show configuration values", 46 ArgsUsage: "", 47 Flags: append(append(nodeFlags, rpcFlags...), whisperFlags...), 48 Category: "MISCELLANEOUS COMMANDS", 49 Description: `The dumpconfig command shows configuration values.`, 50 } 51 52 configFileFlag = cli.StringFlag{ 53 Name: "config", 54 Usage: "TOML configuration file", 55 } 56 ) 57 58 // These settings ensure that TOML keys use the same names as Go struct fields. 59 var tomlSettings = toml.Config{ 60 NormFieldName: func(rt reflect.Type, key string) string { 61 return key 62 }, 63 FieldToKey: func(rt reflect.Type, field string) string { 64 return field 65 }, 66 MissingField: func(rt reflect.Type, field string) error { 67 link := "" 68 if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" { 69 link = fmt.Sprintf(", see https://godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name()) 70 } 71 return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link) 72 }, 73 } 74 75 type ethstatsConfig struct { 76 URL string `toml:",omitempty"` 77 } 78 79 type gethConfig struct { 80 Eth eth.Config 81 Shh whisper.Config 82 Node node.Config 83 Ethstats ethstatsConfig 84 Dashboard dashboard.Config 85 } 86 87 func loadConfig(file string, cfg *gethConfig) error { 88 f, err := os.Open(file) 89 if err != nil { 90 return err 91 } 92 defer f.Close() 93 94 err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg) 95 // Add file name to errors that have a line number. 96 if _, ok := err.(*toml.LineError); ok { 97 err = errors.New(file + ", " + err.Error()) 98 } 99 return err 100 } 101 102 func defaultNodeConfig() node.Config { 103 cfg := node.DefaultConfig 104 cfg.Name = clientIdentifier 105 cfg.Version = params.VersionWithCommit(gitCommit) 106 cfg.HTTPModules = append(cfg.HTTPModules, "eth", "shh") 107 cfg.WSModules = append(cfg.WSModules, "eth", "shh") 108 cfg.IPCPath = "smc.ipc" 109 return cfg 110 } 111 112 func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) { 113 // Load defaults. 114 cfg := gethConfig{ 115 Eth: eth.DefaultConfig, 116 Shh: whisper.DefaultConfig, 117 Node: defaultNodeConfig(), 118 Dashboard: dashboard.DefaultConfig, 119 } 120 121 // Load config file. 122 if file := ctx.GlobalString(configFileFlag.Name); file != "" { 123 if err := loadConfig(file, &cfg); err != nil { 124 utils.Fatalf("%v", err) 125 } 126 } 127 128 // Apply flags. 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.SetDashboardConfig(ctx, &cfg.Dashboard) 141 return stack, cfg 142 } 143 144 // enableWhisper returns true in case one of the whisper flags is set. 145 func enableWhisper(ctx *cli.Context) bool { 146 for _, flag := range whisperFlags { 147 if ctx.GlobalIsSet(flag.GetName()) { 148 return true 149 } 150 } 151 return false 152 } 153 154 func makeFullNode(ctx *cli.Context) *node.Node { 155 stack, cfg := makeConfigNode(ctx) 156 157 utils.RegisterEthService(stack, &cfg.Eth) 158 159 // add by liangc : add meshbox service 160 if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { 161 return statute.NewStatuteService(ctx) 162 }); err != nil { 163 utils.Fatalf("Meshbox Service Start Fail : %v", err) 164 } 165 166 // add by liangc : add chief service 167 if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { 168 return chief.NewTribeService(ctx) 169 }); err != nil { 170 fmt.Println("error", err) 171 } 172 173 if ctx.GlobalBool(utils.DashboardEnabledFlag.Name) { 174 utils.RegisterDashboardService(stack, &cfg.Dashboard) 175 } 176 // Whisper must be explicitly enabled by specifying at least 1 whisper flag or in dev mode 177 shhEnabled := enableWhisper(ctx) 178 shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DeveloperFlag.Name) 179 if shhEnabled || shhAutoEnabled { 180 if ctx.GlobalIsSet(utils.WhisperMaxMessageSizeFlag.Name) { 181 cfg.Shh.MaxMessageSize = uint32(ctx.Int(utils.WhisperMaxMessageSizeFlag.Name)) 182 } 183 if ctx.GlobalIsSet(utils.WhisperMinPOWFlag.Name) { 184 cfg.Shh.MinimumAcceptedPOW = ctx.Float64(utils.WhisperMinPOWFlag.Name) 185 } 186 utils.RegisterShhService(stack, &cfg.Shh) 187 } 188 189 // Add the Ethereum Stats daemon if requested. 190 if cfg.Ethstats.URL != "" { 191 utils.RegisterEthStatsService(stack, cfg.Ethstats.URL) 192 } 193 /* 194 // Add the release oracle service so it boots along with node. 195 if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { 196 config := release.Config{ 197 Oracle: relOracle, 198 Major: uint32(params.VersionMajor), 199 Minor: uint32(params.VersionMinor), 200 Patch: uint32(params.VersionPatch), 201 } 202 commit, _ := hex.DecodeString(gitCommit) 203 copy(config.Commit[:], commit) 204 return release.NewReleaseService(ctx, config) 205 }); err != nil { 206 utils.Fatalf("Failed to register the Geth release oracle service: %v", err) 207 } 208 */ 209 return stack 210 } 211 212 // dumpConfig is the dumpconfig command. 213 func dumpConfig(ctx *cli.Context) error { 214 _, cfg := makeConfigNode(ctx) 215 comment := "" 216 217 if cfg.Eth.Genesis != nil { 218 cfg.Eth.Genesis = nil 219 comment += "# Note: this config doesn't contain the genesis block.\n\n" 220 } 221 222 out, err := tomlSettings.Marshal(&cfg) 223 if err != nil { 224 return err 225 } 226 io.WriteString(os.Stdout, comment) 227 os.Stdout.Write(out) 228 return nil 229 }