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.

156 lines
3.0 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. cfg.node = nodeCfg
  99. return &cfg, nil
  100. }
  101. func main() {
  102. app := cli.NewApp()
  103. app.Name = "hermez-node"
  104. app.Version = "0.1.0-alpha"
  105. app.Flags = []cli.Flag{
  106. &cli.StringFlag{
  107. Name: flagMode,
  108. Usage: fmt.Sprintf("Set node `MODE` (can be \"%v\" or \"%v\")", modeSync, modeCoord),
  109. Required: true,
  110. },
  111. &cli.StringFlag{
  112. Name: flagCfg,
  113. Usage: "Node configuration `FILE`",
  114. Required: true,
  115. },
  116. &cli.StringFlag{
  117. Name: flagCoordCfg,
  118. Usage: "Coordinator configuration `FILE`",
  119. },
  120. }
  121. app.Commands = []*cli.Command{
  122. {
  123. Name: "init",
  124. Aliases: []string{},
  125. Usage: "Initialize the hermez-node",
  126. Action: cmdInit,
  127. },
  128. {
  129. Name: "run",
  130. Aliases: []string{},
  131. Usage: "Run the hermez-node in the indicated mode",
  132. Action: cmdRun,
  133. },
  134. }
  135. err := app.Run(os.Args)
  136. if err != nil {
  137. fmt.Printf("\nError: %v\n", err)
  138. os.Exit(1)
  139. }
  140. }