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.

125 lines
2.8 KiB

  1. //nolint: gosec
  2. package main
  3. import (
  4. "fmt"
  5. "math/rand"
  6. "os"
  7. "path/filepath"
  8. "github.com/spf13/cobra"
  9. "github.com/tendermint/tendermint/libs/log"
  10. e2e "github.com/tendermint/tendermint/test/e2e/pkg"
  11. )
  12. const (
  13. randomSeed int64 = 4827085738
  14. )
  15. var logger = log.MustNewDefaultLogger(log.LogFormatPlain, log.LogLevelInfo, false)
  16. func main() {
  17. NewCLI().Run()
  18. }
  19. // CLI is the Cobra-based command-line interface.
  20. type CLI struct {
  21. root *cobra.Command
  22. opts Options
  23. }
  24. // NewCLI sets up the CLI.
  25. func NewCLI() *CLI {
  26. cli := &CLI{}
  27. cli.root = &cobra.Command{
  28. Use: "generator",
  29. Short: "End-to-end testnet generator",
  30. SilenceUsage: true,
  31. SilenceErrors: true, // we'll output them ourselves in Run()
  32. RunE: func(cmd *cobra.Command, args []string) error {
  33. var opts Options
  34. var err error
  35. cli.opts.Directory, err = cmd.Flags().GetString("dir")
  36. if err != nil {
  37. return err
  38. }
  39. cli.opts.NumGroups, err = cmd.Flags().GetInt("groups")
  40. if err != nil {
  41. return err
  42. }
  43. cli.opts.MinNetworkSize, err = cmd.Flags().GetInt("min-size")
  44. if err != nil {
  45. return err
  46. }
  47. p2pMode, err := cmd.Flags().GetString("p2p")
  48. if err != nil {
  49. return err
  50. }
  51. switch mode := P2PMode(p2pMode); mode {
  52. case NewP2PMode, LegacyP2PMode, HybridP2PMode, MixedP2PMode:
  53. opts.P2P = mode
  54. default:
  55. return fmt.Errorf("p2p mode must be either new, legacy, hybrid or mixed got %s", p2pMode)
  56. }
  57. return cli.generate()
  58. },
  59. }
  60. cli.root.PersistentFlags().StringP("dir", "d", "", "Output directory for manifests")
  61. _ = cli.root.MarkPersistentFlagRequired("dir")
  62. cli.root.PersistentFlags().IntP("groups", "g", 0, "Number of groups")
  63. cli.root.PersistentFlags().StringP("p2p", "p", string(MixedP2PMode),
  64. "P2P typology to be generated [\"new\", \"legacy\", \"hybrid\" or \"mixed\" ]")
  65. cli.root.PersistentFlags().IntP("min-size", "m", 1, "Minimum Network Size")
  66. return cli
  67. }
  68. // generate generates manifests in a directory.
  69. func (cli *CLI) generate() error {
  70. err := os.MkdirAll(cli.opts.Directory, 0755)
  71. if err != nil {
  72. return err
  73. }
  74. manifests, err := Generate(rand.New(rand.NewSource(randomSeed)), cli.opts)
  75. if err != nil {
  76. return err
  77. }
  78. switch {
  79. case cli.opts.NumGroups <= 0:
  80. e2e.SortManifests(manifests)
  81. if err := e2e.WriteManifests(filepath.Join(cli.opts.Directory, "gen"), manifests); err != nil {
  82. return err
  83. }
  84. default:
  85. groupManifests := e2e.SplitGroups(cli.opts.NumGroups, manifests)
  86. for idx, gm := range groupManifests {
  87. e2e.SortManifests(gm)
  88. prefix := filepath.Join(cli.opts.Directory, fmt.Sprintf("gen-group%02d", idx))
  89. if err := e2e.WriteManifests(prefix, gm); err != nil {
  90. return err
  91. }
  92. }
  93. }
  94. return nil
  95. }
  96. // Run runs the CLI.
  97. func (cli *CLI) Run() {
  98. if err := cli.root.Execute(); err != nil {
  99. logger.Error(err.Error())
  100. os.Exit(1)
  101. }
  102. }