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.

116 lines
2.6 KiB

  1. //nolint: gosec
  2. package main
  3. import (
  4. "fmt"
  5. "math"
  6. "math/rand"
  7. "os"
  8. "path/filepath"
  9. "github.com/spf13/cobra"
  10. "github.com/tendermint/tendermint/libs/log"
  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. }
  23. // NewCLI sets up the CLI.
  24. func NewCLI() *CLI {
  25. cli := &CLI{}
  26. cli.root = &cobra.Command{
  27. Use: "generator",
  28. Short: "End-to-end testnet generator",
  29. SilenceUsage: true,
  30. SilenceErrors: true, // we'll output them ourselves in Run()
  31. RunE: func(cmd *cobra.Command, args []string) error {
  32. dir, err := cmd.Flags().GetString("dir")
  33. if err != nil {
  34. return err
  35. }
  36. groups, err := cmd.Flags().GetInt("groups")
  37. if err != nil {
  38. return err
  39. }
  40. p2pMode, err := cmd.Flags().GetString("p2p")
  41. if err != nil {
  42. return err
  43. }
  44. var opts Options
  45. switch mode := P2PMode(p2pMode); mode {
  46. case NewP2PMode, LegacyP2PMode, HybridP2PMode, MixedP2PMode:
  47. opts = Options{P2P: mode}
  48. default:
  49. return fmt.Errorf("p2p mode must be either new, legacy, hybrid or mixed got %s", p2pMode)
  50. }
  51. if groups == 0 {
  52. opts.Sorted = true
  53. }
  54. return cli.generate(dir, groups, opts)
  55. },
  56. }
  57. cli.root.PersistentFlags().StringP("dir", "d", "", "Output directory for manifests")
  58. _ = cli.root.MarkPersistentFlagRequired("dir")
  59. cli.root.PersistentFlags().IntP("groups", "g", 0, "Number of groups")
  60. cli.root.PersistentFlags().StringP("p2p", "p", string(MixedP2PMode),
  61. "P2P typology to be generated [\"new\", \"legacy\", \"hybrid\" or \"mixed\" ]")
  62. return cli
  63. }
  64. // generate generates manifests in a directory.
  65. func (cli *CLI) generate(dir string, groups int, opts Options) error {
  66. err := os.MkdirAll(dir, 0755)
  67. if err != nil {
  68. return err
  69. }
  70. manifests, err := Generate(rand.New(rand.NewSource(randomSeed)), opts)
  71. if err != nil {
  72. return err
  73. }
  74. if groups <= 0 {
  75. for i, manifest := range manifests {
  76. err = manifest.Save(filepath.Join(dir, fmt.Sprintf("gen-%04d.toml", i)))
  77. if err != nil {
  78. return err
  79. }
  80. }
  81. } else {
  82. groupSize := int(math.Ceil(float64(len(manifests)) / float64(groups)))
  83. for g := 0; g < groups; g++ {
  84. for i := 0; i < groupSize && g*groupSize+i < len(manifests); i++ {
  85. manifest := manifests[g*groupSize+i]
  86. err = manifest.Save(filepath.Join(dir, fmt.Sprintf("gen-group%02d-%04d.toml", g, i)))
  87. if err != nil {
  88. return err
  89. }
  90. }
  91. }
  92. }
  93. return nil
  94. }
  95. // Run runs the CLI.
  96. func (cli *CLI) Run() {
  97. if err := cli.root.Execute(); err != nil {
  98. logger.Error(err.Error())
  99. os.Exit(1)
  100. }
  101. }