github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/neatio/config.go (about)

     1  package main
     2  
     3  import (
     4  	"bufio"
     5  	"errors"
     6  	"fmt"
     7  	"io"
     8  	"os"
     9  	"reflect"
    10  	"unicode"
    11  
    12  	"github.com/neatlab/neatio/chain/core"
    13  	"github.com/neatlab/neatio/chain/log"
    14  
    15  	"gopkg.in/urfave/cli.v1"
    16  
    17  	"github.com/naoina/toml"
    18  	neatptc "github.com/neatlab/neatio/neatptc"
    19  	"github.com/neatlab/neatio/network/node"
    20  	"github.com/neatlab/neatio/params"
    21  	"github.com/neatlab/neatio/utilities/utils"
    22  )
    23  
    24  var (
    25  	dumpConfigCommand = cli.Command{
    26  		Action:      utils.MigrateFlags(dumpConfig),
    27  		Name:        "dumpconfig",
    28  		Usage:       "Show configuration values",
    29  		ArgsUsage:   "",
    30  		Category:    "MISCELLANEOUS COMMANDS",
    31  		Description: `The dumpconfig command shows configuration values.`,
    32  	}
    33  
    34  	configFileFlag = cli.StringFlag{
    35  		Name:  "config",
    36  		Usage: "TOML configuration file",
    37  	}
    38  )
    39  
    40  var tomlSettings = toml.Config{
    41  	NormFieldName: func(rt reflect.Type, key string) string {
    42  		return key
    43  	},
    44  	FieldToKey: func(rt reflect.Type, field string) string {
    45  		return field
    46  	},
    47  	MissingField: func(rt reflect.Type, field string) error {
    48  		link := ""
    49  		if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" {
    50  			link = fmt.Sprintf(", see https:godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name())
    51  		}
    52  		return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link)
    53  	},
    54  }
    55  
    56  type ethstatsConfig struct {
    57  	URL string `toml:",omitempty"`
    58  }
    59  
    60  type gethConfig struct {
    61  	Eth      neatptc.Config
    62  	Node     node.Config
    63  	Ethstats ethstatsConfig
    64  }
    65  
    66  func loadConfig(file string, cfg *gethConfig) error {
    67  	f, err := os.Open(file)
    68  	if err != nil {
    69  		return err
    70  	}
    71  	defer f.Close()
    72  
    73  	err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg)
    74  
    75  	if _, ok := err.(*toml.LineError); ok {
    76  		err = errors.New(file + ", " + err.Error())
    77  	}
    78  	return err
    79  }
    80  
    81  func defaultNodeConfig() node.Config {
    82  	cfg := node.DefaultConfig
    83  	cfg.Name = clientIdentifier
    84  	cfg.Version = params.VersionWithCommit(gitCommit)
    85  	cfg.HTTPModules = append(cfg.HTTPModules, "neat", "eth")
    86  	cfg.WSModules = append(cfg.WSModules, "neat", "eth")
    87  	cfg.IPCPath = "neatio.ipc"
    88  	return cfg
    89  }
    90  
    91  func makeConfigNode(ctx *cli.Context, chainId string) (*node.Node, gethConfig) {
    92  
    93  	cfg := gethConfig{
    94  		Eth:  neatptc.DefaultConfig,
    95  		Node: defaultNodeConfig(),
    96  	}
    97  
    98  	if file := ctx.GlobalString(configFileFlag.Name); file != "" {
    99  		if err := loadConfig(file, &cfg); err != nil {
   100  			utils.Fatalf("%v", err)
   101  		}
   102  	}
   103  
   104  	cfg.Node.ChainId = chainId
   105  
   106  	cfg.Node.Logger = log.NewLogger(chainId, "", ctx.GlobalInt("verbosity"), ctx.GlobalBool("debug"), ctx.GlobalString("vmodule"), ctx.GlobalString("backtrace"))
   107  
   108  	utils.SetNodeConfig(ctx, &cfg.Node)
   109  	stack, err := node.New(&cfg.Node)
   110  	if err != nil {
   111  		utils.Fatalf("Failed to create the protocol stack: %v", err)
   112  	}
   113  	utils.SetEthConfig(ctx, stack, &cfg.Eth)
   114  	if ctx.GlobalIsSet(utils.EthStatsURLFlag.Name) {
   115  		cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name)
   116  	}
   117  
   118  	utils.SetGeneralConfig(ctx)
   119  
   120  	return stack, cfg
   121  }
   122  
   123  func makeFullNode(ctx *cli.Context, cch core.CrossChainHelper, chainId string) *node.Node {
   124  	stack, cfg := makeConfigNode(ctx, chainId)
   125  
   126  	utils.RegisterIntService(stack, &cfg.Eth, ctx, cch)
   127  
   128  	if err := stack.GatherServices(); err != nil {
   129  		return nil
   130  	} else {
   131  		return stack
   132  	}
   133  }
   134  
   135  func dumpConfig(ctx *cli.Context) error {
   136  	_, cfg := makeConfigNode(ctx, clientIdentifier)
   137  	comment := ""
   138  
   139  	if cfg.Eth.Genesis != nil {
   140  		cfg.Eth.Genesis = nil
   141  		comment += "# Note: this config doesn't contain the genesis block.\n\n"
   142  	}
   143  
   144  	out, err := tomlSettings.Marshal(&cfg)
   145  	if err != nil {
   146  		return err
   147  	}
   148  	io.WriteString(os.Stdout, comment)
   149  	os.Stdout.Write(out)
   150  	return nil
   151  }