github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/cmd/intchain/config.go (about)

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