You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
3.1 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/signal"
  6. "github.com/hermeznetwork/hermez-node/config"
  7. "github.com/hermeznetwork/hermez-node/log"
  8. "github.com/hermeznetwork/hermez-node/node"
  9. "github.com/urfave/cli/v2"
  10. )
  11. const (
  12. flagCfg = "cfg"
  13. flagCoordCfg = "coordcfg"
  14. flagMode = "mode"
  15. modeSync = "sync"
  16. modeCoord = "coord"
  17. )
  18. func cmdInit(c *cli.Context) error {
  19. log.Info("Init")
  20. cfg, err := parseCli(c)
  21. if err != nil {
  22. return err
  23. }
  24. fmt.Println("TODO", cfg)
  25. return err
  26. }
  27. func cmdRun(c *cli.Context) error {
  28. cfg, err := parseCli(c)
  29. if err != nil {
  30. return fmt.Errorf("error parsing flags and config: %w", err)
  31. }
  32. node, err := node.NewNode(cfg.mode, cfg.node, cfg.coord)
  33. if err != nil {
  34. return fmt.Errorf("error starting node: %w", err)
  35. }
  36. node.Start()
  37. stopCh := make(chan interface{})
  38. // catch ^C to send the stop signal
  39. ossig := make(chan os.Signal, 1)
  40. signal.Notify(ossig, os.Interrupt)
  41. go func() {
  42. for sig := range ossig {
  43. if sig == os.Interrupt {
  44. stopCh <- nil
  45. }
  46. }
  47. }()
  48. <-stopCh
  49. node.Stop()
  50. return nil
  51. }
  52. // Config is the configuration of the hermez node execution
  53. type Config struct {
  54. mode node.Mode
  55. node *config.Node
  56. coord *config.Coordinator
  57. }
  58. func parseCli(c *cli.Context) (*Config, error) {
  59. cfg, err := getConfig(c)
  60. if err != nil {
  61. if err := cli.ShowAppHelp(c); err != nil {
  62. panic(err)
  63. }
  64. return nil, err
  65. }
  66. return cfg, nil
  67. }
  68. func getConfig(c *cli.Context) (*Config, error) {
  69. var cfg Config
  70. mode := c.String(flagMode)
  71. switch mode {
  72. case modeSync:
  73. cfg.mode = node.ModeSynchronizer
  74. case modeCoord:
  75. cfg.mode = node.ModeCoordinator
  76. default:
  77. return nil, fmt.Errorf("invalid mode \"%v\"", mode)
  78. }
  79. if cfg.mode == node.ModeCoordinator {
  80. coordCfgPath := c.String(flagCoordCfg)
  81. if coordCfgPath == "" {
  82. return nil, fmt.Errorf("required flag \"%v\" not set", flagCoordCfg)
  83. }
  84. coordCfg, err := config.LoadCoordinator(coordCfgPath)
  85. if err != nil {
  86. return nil, err
  87. }
  88. cfg.coord = coordCfg
  89. }
  90. nodeCfgPath := c.String(flagCfg)
  91. if nodeCfgPath == "" {
  92. return nil, fmt.Errorf("required flag \"%v\" not set", flagCfg)
  93. }
  94. nodeCfg, err := config.LoadNode(nodeCfgPath)
  95. if err != nil {
  96. return nil, err
  97. }
  98. // nodeCfg.Synchronizer.InitialVariables.WDelayer.HermezRollupAddress = nodeCfg.SmartContracts.Rollup
  99. cfg.node = nodeCfg
  100. return &cfg, nil
  101. }
  102. func main() {
  103. app := cli.NewApp()
  104. app.Name = "hermez-node"
  105. app.Version = "0.1.0-alpha"
  106. app.Flags = []cli.Flag{
  107. &cli.StringFlag{
  108. Name: flagMode,
  109. Usage: fmt.Sprintf("Set node `MODE` (can be \"%v\" or \"%v\")", modeSync, modeCoord),
  110. Required: true,
  111. },
  112. &cli.StringFlag{
  113. Name: flagCfg,
  114. Usage: "Node configuration `FILE`",
  115. Required: true,
  116. },
  117. &cli.StringFlag{
  118. Name: flagCoordCfg,
  119. Usage: "Coordinator configuration `FILE`",
  120. },
  121. }
  122. app.Commands = []*cli.Command{
  123. {
  124. Name: "init",
  125. Aliases: []string{},
  126. Usage: "Initialize the hermez-node",
  127. Action: cmdInit,
  128. },
  129. {
  130. Name: "run",
  131. Aliases: []string{},
  132. Usage: "Run the hermez-node in the indicated mode",
  133. Action: cmdRun,
  134. },
  135. }
  136. err := app.Run(os.Args)
  137. if err != nil {
  138. fmt.Printf("\nError: %v\n", err)
  139. os.Exit(1)
  140. }
  141. }