config.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  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-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "bufio"
  19. "crypto/tls"
  20. "errors"
  21. "fmt"
  22. "math/big"
  23. "os"
  24. "reflect"
  25. "strings"
  26. "unicode"
  27. "github.com/ethereum/go-ethereum/cmd/utils"
  28. "github.com/ethereum/go-ethereum/common/http"
  29. "github.com/ethereum/go-ethereum/eth"
  30. "github.com/ethereum/go-ethereum/eth/catalyst"
  31. "github.com/ethereum/go-ethereum/eth/ethconfig"
  32. "github.com/ethereum/go-ethereum/extension/privacyExtension"
  33. "github.com/ethereum/go-ethereum/internal/ethapi"
  34. "github.com/ethereum/go-ethereum/log"
  35. "github.com/ethereum/go-ethereum/metrics"
  36. "github.com/ethereum/go-ethereum/node"
  37. "github.com/ethereum/go-ethereum/p2p"
  38. "github.com/ethereum/go-ethereum/p2p/enode"
  39. "github.com/ethereum/go-ethereum/params"
  40. "github.com/ethereum/go-ethereum/permission/core"
  41. "github.com/ethereum/go-ethereum/private"
  42. "github.com/ethereum/go-ethereum/private/engine"
  43. "github.com/ethereum/go-ethereum/qlight"
  44. "github.com/naoina/toml"
  45. "gopkg.in/urfave/cli.v1"
  46. )
  47. var (
  48. dumpConfigCommand = cli.Command{
  49. Action: utils.MigrateFlags(dumpConfig),
  50. Name: "dumpconfig",
  51. Usage: "Show configuration values",
  52. ArgsUsage: "",
  53. Flags: append(nodeFlags, rpcFlags...),
  54. Category: "MISCELLANEOUS COMMANDS",
  55. Description: `The dumpconfig command shows configuration values.`,
  56. }
  57. configFileFlag = cli.StringFlag{
  58. Name: "config",
  59. Usage: "TOML configuration file",
  60. }
  61. )
  62. // These settings ensure that TOML keys use the same names as Go struct fields.
  63. var tomlSettings = toml.Config{
  64. NormFieldName: func(rt reflect.Type, key string) string {
  65. return key
  66. },
  67. FieldToKey: func(rt reflect.Type, field string) string {
  68. return field
  69. },
  70. MissingField: func(rt reflect.Type, field string) error {
  71. link := ""
  72. if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" {
  73. link = fmt.Sprintf(", see https://godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name())
  74. }
  75. return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link)
  76. },
  77. }
  78. type ethstatsConfig struct {
  79. URL string `toml:",omitempty"`
  80. }
  81. type gethConfig struct {
  82. Eth ethconfig.Config
  83. Node node.Config
  84. Ethstats ethstatsConfig
  85. Metrics metrics.Config
  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. err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg)
  94. // Add file name to errors that have a line number.
  95. if _, ok := err.(*toml.LineError); ok {
  96. err = errors.New(file + ", " + err.Error())
  97. }
  98. return err
  99. }
  100. func defaultNodeConfig() node.Config {
  101. cfg := node.DefaultConfig
  102. cfg.Name = clientIdentifier
  103. cfg.Version = params.VersionWithCommit(gitCommit, gitDate)
  104. cfg.HTTPModules = append(cfg.HTTPModules, "eth")
  105. cfg.WSModules = append(cfg.WSModules, "eth")
  106. cfg.IPCPath = "geth.ipc"
  107. return cfg
  108. }
  109. // makeConfigNode loads geth configuration and creates a blank node instance.
  110. func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
  111. // Quorum: Must occur before setQuorumConfig, as it needs an initialised PTM to be enabled
  112. // Extension Service and Multitenancy feature validation also depend on PTM availability
  113. if err := quorumInitialisePrivacy(ctx); err != nil {
  114. utils.Fatalf("Error initialising Private Transaction Manager: %s", err.Error())
  115. }
  116. // Load defaults.
  117. cfg := gethConfig{
  118. Eth: ethconfig.Defaults,
  119. Node: defaultNodeConfig(),
  120. Metrics: metrics.DefaultConfig,
  121. }
  122. // Load config file.
  123. if file := ctx.GlobalString(configFileFlag.Name); file != "" {
  124. if err := loadConfig(file, &cfg); err != nil {
  125. utils.Fatalf("%v", err)
  126. }
  127. }
  128. // Apply flags.
  129. utils.SetNodeConfig(ctx, &cfg.Node)
  130. utils.SetQLightConfig(ctx, &cfg.Node, &cfg.Eth)
  131. stack, err := node.New(&cfg.Node)
  132. if err != nil {
  133. utils.Fatalf("Failed to create the protocol stack: %v", err)
  134. }
  135. utils.SetEthConfig(ctx, stack, &cfg.Eth)
  136. if ctx.GlobalIsSet(utils.EthStatsURLFlag.Name) {
  137. cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name)
  138. }
  139. applyMetricConfig(ctx, &cfg)
  140. // Quorum
  141. if cfg.Eth.QuorumLightServer {
  142. p2p.SetQLightTLSConfig(readQLightServerTLSConfig(ctx))
  143. // permissioning for the qlight P2P server
  144. stack.QServer().SetNewTransportFunc(p2p.NewQlightServerTransport)
  145. if ctx.GlobalIsSet(utils.QuorumLightServerP2PPermissioningFlag.Name) {
  146. prefix := "qlight"
  147. if ctx.GlobalIsSet(utils.QuorumLightServerP2PPermissioningPrefixFlag.Name) {
  148. prefix = ctx.GlobalString(utils.QuorumLightServerP2PPermissioningPrefixFlag.Name)
  149. }
  150. fbp := core.NewFileBasedPermissoningWithPrefix(prefix)
  151. stack.QServer().SetIsNodePermissioned(fbp.IsNodePermissionedEnode)
  152. }
  153. }
  154. if cfg.Eth.QuorumLightClient.Enabled() {
  155. p2p.SetQLightTLSConfig(readQLightClientTLSConfig(ctx))
  156. stack.Server().SetNewTransportFunc(p2p.NewQlightClientTransport)
  157. }
  158. // End Quorum
  159. return stack, cfg
  160. }
  161. // makeFullNode loads geth configuration and creates the Ethereum backend.
  162. func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) {
  163. stack, cfg := makeConfigNode(ctx)
  164. if ctx.GlobalIsSet(utils.OverrideBerlinFlag.Name) {
  165. cfg.Eth.OverrideBerlin = new(big.Int).SetUint64(ctx.GlobalUint64(utils.OverrideBerlinFlag.Name))
  166. }
  167. // Quorum: Must occur before registering the extension service, as it needs an initialised PTM to be enabled
  168. if err := quorumInitialisePrivacy(ctx); err != nil {
  169. utils.Fatalf("Error initialising Private Transaction Manager: %s", err.Error())
  170. }
  171. backend, eth := utils.RegisterEthService(stack, &cfg.Eth)
  172. // Configure catalyst.
  173. if ctx.GlobalBool(utils.CatalystFlag.Name) {
  174. if eth == nil {
  175. utils.Fatalf("Catalyst does not work in light client mode.")
  176. }
  177. if err := catalyst.Register(stack, eth); err != nil {
  178. utils.Fatalf("%v", err)
  179. }
  180. }
  181. // Quorum
  182. // plugin service must be after eth service so that eth service will be stopped gradually if any of the plugin
  183. // fails to start
  184. if cfg.Node.Plugins != nil {
  185. utils.RegisterPluginService(stack, &cfg.Node, ctx.Bool(utils.PluginSkipVerifyFlag.Name), ctx.Bool(utils.PluginLocalVerifyFlag.Name), ctx.String(utils.PluginPublicKeyFlag.Name))
  186. log.Debug("plugin manager", "value", stack.PluginManager())
  187. err := eth.NotifyRegisteredPluginService(stack.PluginManager())
  188. if err != nil {
  189. utils.Fatalf("Error initialising QLight Token Manager: %s", err.Error())
  190. }
  191. }
  192. if cfg.Node.IsPermissionEnabled() {
  193. utils.RegisterPermissionService(stack, ctx.Bool(utils.RaftDNSEnabledFlag.Name), backend.ChainConfig().ChainID)
  194. }
  195. if ctx.GlobalBool(utils.RaftModeFlag.Name) && !cfg.Eth.QuorumLightClient.Enabled() {
  196. utils.RegisterRaftService(stack, ctx, &cfg.Node, eth)
  197. }
  198. if private.IsQuorumPrivacyEnabled() {
  199. utils.RegisterExtensionService(stack, eth)
  200. }
  201. // End Quorum
  202. // Configure GraphQL if requested
  203. if ctx.GlobalIsSet(utils.GraphQLEnabledFlag.Name) {
  204. utils.RegisterGraphQLService(stack, backend, cfg.Node)
  205. }
  206. // Add the Ethereum Stats daemon if requested.
  207. if cfg.Ethstats.URL != "" {
  208. utils.RegisterEthStatsService(stack, backend, cfg.Ethstats.URL)
  209. }
  210. return stack, backend
  211. }
  212. // dumpConfig is the dumpconfig command.
  213. func dumpConfig(ctx *cli.Context) error {
  214. _, cfg := makeConfigNode(ctx)
  215. comment := ""
  216. if cfg.Eth.Genesis != nil {
  217. cfg.Eth.Genesis = nil
  218. comment += "# Note: this config doesn't contain the genesis block.\n\n"
  219. }
  220. out, err := tomlSettings.Marshal(&cfg)
  221. if err != nil {
  222. return err
  223. }
  224. dump := os.Stdout
  225. if ctx.NArg() > 0 {
  226. dump, err = os.OpenFile(ctx.Args().Get(0), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
  227. if err != nil {
  228. return err
  229. }
  230. defer dump.Close()
  231. }
  232. dump.WriteString(comment)
  233. dump.Write(out)
  234. return nil
  235. }
  236. func applyMetricConfig(ctx *cli.Context, cfg *gethConfig) {
  237. if ctx.GlobalIsSet(utils.MetricsEnabledFlag.Name) {
  238. cfg.Metrics.Enabled = ctx.GlobalBool(utils.MetricsEnabledFlag.Name)
  239. }
  240. if ctx.GlobalIsSet(utils.MetricsEnabledExpensiveFlag.Name) {
  241. cfg.Metrics.EnabledExpensive = ctx.GlobalBool(utils.MetricsEnabledExpensiveFlag.Name)
  242. }
  243. if ctx.GlobalIsSet(utils.MetricsHTTPFlag.Name) {
  244. cfg.Metrics.HTTP = ctx.GlobalString(utils.MetricsHTTPFlag.Name)
  245. }
  246. if ctx.GlobalIsSet(utils.MetricsPortFlag.Name) {
  247. cfg.Metrics.Port = ctx.GlobalInt(utils.MetricsPortFlag.Name)
  248. }
  249. if ctx.GlobalIsSet(utils.MetricsEnableInfluxDBFlag.Name) {
  250. cfg.Metrics.EnableInfluxDB = ctx.GlobalBool(utils.MetricsEnableInfluxDBFlag.Name)
  251. }
  252. if ctx.GlobalIsSet(utils.MetricsInfluxDBEndpointFlag.Name) {
  253. cfg.Metrics.InfluxDBEndpoint = ctx.GlobalString(utils.MetricsInfluxDBEndpointFlag.Name)
  254. }
  255. if ctx.GlobalIsSet(utils.MetricsInfluxDBDatabaseFlag.Name) {
  256. cfg.Metrics.InfluxDBDatabase = ctx.GlobalString(utils.MetricsInfluxDBDatabaseFlag.Name)
  257. }
  258. if ctx.GlobalIsSet(utils.MetricsInfluxDBUsernameFlag.Name) {
  259. cfg.Metrics.InfluxDBUsername = ctx.GlobalString(utils.MetricsInfluxDBUsernameFlag.Name)
  260. }
  261. if ctx.GlobalIsSet(utils.MetricsInfluxDBPasswordFlag.Name) {
  262. cfg.Metrics.InfluxDBPassword = ctx.GlobalString(utils.MetricsInfluxDBPasswordFlag.Name)
  263. }
  264. if ctx.GlobalIsSet(utils.MetricsInfluxDBTagsFlag.Name) {
  265. cfg.Metrics.InfluxDBTags = ctx.GlobalString(utils.MetricsInfluxDBTagsFlag.Name)
  266. }
  267. }
  268. // Quorum
  269. func readQLightClientTLSConfig(ctx *cli.Context) *tls.Config {
  270. if !ctx.GlobalIsSet(utils.QuorumLightTLSFlag.Name) {
  271. return nil
  272. }
  273. if !ctx.GlobalIsSet(utils.QuorumLightTLSCACertsFlag.Name) {
  274. utils.Fatalf("QLight tls flag is set but no client certificate authorities has been provided")
  275. }
  276. tlsConfig, err := qlight.NewTLSConfig(&qlight.TLSConfig{
  277. CACertFileName: ctx.GlobalString(utils.QuorumLightTLSCACertsFlag.Name),
  278. CertFileName: ctx.GlobalString(utils.QuorumLightTLSCertFlag.Name),
  279. KeyFileName: ctx.GlobalString(utils.QuorumLightTLSKeyFlag.Name),
  280. ServerName: enode.MustParse(ctx.GlobalString(utils.QuorumLightClientServerNodeFlag.Name)).IP().String(),
  281. CipherSuites: ctx.GlobalString(utils.QuorumLightTLSCipherSuitesFlag.Name),
  282. })
  283. if err != nil {
  284. utils.Fatalf("Unable to load the specified tls configuration: %v", err)
  285. }
  286. return tlsConfig
  287. }
  288. func readQLightServerTLSConfig(ctx *cli.Context) *tls.Config {
  289. if !ctx.GlobalIsSet(utils.QuorumLightTLSFlag.Name) {
  290. return nil
  291. }
  292. if !ctx.GlobalIsSet(utils.QuorumLightTLSCertFlag.Name) {
  293. utils.Fatalf("QLight TLS is enabled but no server certificate has been provided")
  294. }
  295. if !ctx.GlobalIsSet(utils.QuorumLightTLSKeyFlag.Name) {
  296. utils.Fatalf("QLight TLS is enabled but no server key has been provided")
  297. }
  298. tlsConfig, err := qlight.NewTLSConfig(&qlight.TLSConfig{
  299. CertFileName: ctx.GlobalString(utils.QuorumLightTLSCertFlag.Name),
  300. KeyFileName: ctx.GlobalString(utils.QuorumLightTLSKeyFlag.Name),
  301. ClientCACertFileName: ctx.GlobalString(utils.QuorumLightTLSCACertsFlag.Name),
  302. ClientAuth: ctx.GlobalInt(utils.QuorumLightTLSClientAuthFlag.Name),
  303. CipherSuites: ctx.GlobalString(utils.QuorumLightTLSCipherSuitesFlag.Name),
  304. })
  305. if err != nil {
  306. utils.Fatalf("QLight TLS - unable to read server tls configuration: %v", err)
  307. }
  308. return tlsConfig
  309. }
  310. // quorumValidateEthService checks quorum features that depend on the ethereum service
  311. func quorumValidateEthService(stack *node.Node, isRaft bool) {
  312. var ethereum *eth.Ethereum
  313. err := stack.Lifecycle(&ethereum)
  314. if err != nil {
  315. utils.Fatalf("Error retrieving Ethereum service: %v", err)
  316. }
  317. quorumValidateConsensus(ethereum, isRaft)
  318. quorumValidatePrivacyEnhancements(ethereum)
  319. }
  320. // quorumValidateConsensus checks if a consensus was used. The node is killed if consensus was not used
  321. func quorumValidateConsensus(ethereum *eth.Ethereum, isRaft bool) {
  322. transitionAlgorithmOnBlockZero := false
  323. ethereum.BlockChain().Config().GetTransitionValue(big.NewInt(0), func(transition params.Transition) {
  324. transitionAlgorithmOnBlockZero = strings.EqualFold(transition.Algorithm, params.IBFT) || strings.EqualFold(transition.Algorithm, params.QBFT)
  325. })
  326. if !transitionAlgorithmOnBlockZero && !isRaft && ethereum.BlockChain().Config().Istanbul == nil && ethereum.BlockChain().Config().IBFT == nil && ethereum.BlockChain().Config().QBFT == nil && ethereum.BlockChain().Config().Clique == nil {
  327. utils.Fatalf("Consensus not specified. Exiting!!")
  328. }
  329. }
  330. // quorumValidatePrivacyEnhancements checks if privacy enhancements are configured the transaction manager supports
  331. // the PrivacyEnhancements feature
  332. func quorumValidatePrivacyEnhancements(ethereum *eth.Ethereum) {
  333. privacyEnhancementsBlock := ethereum.BlockChain().Config().PrivacyEnhancementsBlock
  334. for _, transition := range ethereum.BlockChain().Config().Transitions {
  335. if transition.PrivacyPrecompileEnabled != nil && *transition.PrivacyEnhancementsEnabled {
  336. privacyEnhancementsBlock = transition.Block
  337. break
  338. }
  339. }
  340. if privacyEnhancementsBlock != nil {
  341. log.Info("Privacy enhancements is configured to be enabled from block ", "height", privacyEnhancementsBlock)
  342. if !private.P.HasFeature(engine.PrivacyEnhancements) {
  343. utils.Fatalf("Cannot start quorum with privacy enhancements enabled while the transaction manager does not support it")
  344. }
  345. }
  346. }
  347. // configure and set up quorum transaction privacy
  348. func quorumInitialisePrivacy(ctx *cli.Context) error {
  349. cfg, err := QuorumSetupPrivacyConfiguration(ctx)
  350. if err != nil {
  351. return err
  352. }
  353. err = private.InitialiseConnection(cfg, ctx.GlobalIsSet(utils.QuorumLightClientFlag.Name))
  354. if err != nil {
  355. return err
  356. }
  357. privacyExtension.Init()
  358. return nil
  359. }
  360. // Get private transaction manager configuration
  361. func QuorumSetupPrivacyConfiguration(ctx *cli.Context) (http.Config, error) {
  362. // get default configuration
  363. cfg, err := private.GetLegacyEnvironmentConfig()
  364. if err != nil {
  365. return http.Config{}, err
  366. }
  367. // override the config with command line parameters
  368. if ctx.GlobalIsSet(utils.QuorumPTMUnixSocketFlag.Name) {
  369. cfg.SetSocket(ctx.GlobalString(utils.QuorumPTMUnixSocketFlag.Name))
  370. }
  371. if ctx.GlobalIsSet(utils.QuorumPTMUrlFlag.Name) {
  372. cfg.SetHttpUrl(ctx.GlobalString(utils.QuorumPTMUrlFlag.Name))
  373. }
  374. if ctx.GlobalIsSet(utils.QuorumPTMTimeoutFlag.Name) {
  375. cfg.SetTimeout(ctx.GlobalUint(utils.QuorumPTMTimeoutFlag.Name))
  376. }
  377. if ctx.GlobalIsSet(utils.QuorumPTMDialTimeoutFlag.Name) {
  378. cfg.SetDialTimeout(ctx.GlobalUint(utils.QuorumPTMDialTimeoutFlag.Name))
  379. }
  380. if ctx.GlobalIsSet(utils.QuorumPTMHttpIdleTimeoutFlag.Name) {
  381. cfg.SetHttpIdleConnTimeout(ctx.GlobalUint(utils.QuorumPTMHttpIdleTimeoutFlag.Name))
  382. }
  383. if ctx.GlobalIsSet(utils.QuorumPTMHttpWriteBufferSizeFlag.Name) {
  384. cfg.SetHttpWriteBufferSize(ctx.GlobalInt(utils.QuorumPTMHttpWriteBufferSizeFlag.Name))
  385. }
  386. if ctx.GlobalIsSet(utils.QuorumPTMHttpReadBufferSizeFlag.Name) {
  387. cfg.SetHttpReadBufferSize(ctx.GlobalInt(utils.QuorumPTMHttpReadBufferSizeFlag.Name))
  388. }
  389. if ctx.GlobalIsSet(utils.QuorumPTMTlsModeFlag.Name) {
  390. cfg.SetTlsMode(ctx.GlobalString(utils.QuorumPTMTlsModeFlag.Name))
  391. }
  392. if ctx.GlobalIsSet(utils.QuorumPTMTlsRootCaFlag.Name) {
  393. cfg.SetTlsRootCA(ctx.GlobalString(utils.QuorumPTMTlsRootCaFlag.Name))
  394. }
  395. if ctx.GlobalIsSet(utils.QuorumPTMTlsClientCertFlag.Name) {
  396. cfg.SetTlsClientCert(ctx.GlobalString(utils.QuorumPTMTlsClientCertFlag.Name))
  397. }
  398. if ctx.GlobalIsSet(utils.QuorumPTMTlsClientKeyFlag.Name) {
  399. cfg.SetTlsClientKey(ctx.GlobalString(utils.QuorumPTMTlsClientKeyFlag.Name))
  400. }
  401. if ctx.GlobalIsSet(utils.QuorumPTMTlsInsecureSkipVerify.Name) {
  402. cfg.SetTlsInsecureSkipVerify(ctx.Bool(utils.QuorumPTMTlsInsecureSkipVerify.Name))
  403. }
  404. if err = cfg.Validate(); err != nil {
  405. return cfg, err
  406. }
  407. return cfg, nil
  408. }