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.

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