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.

112 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. return cli.generate(dir, groups, opts)
  52. },
  53. }
  54. cli.root.PersistentFlags().StringP("dir", "d", "", "Output directory for manifests")
  55. _ = cli.root.MarkPersistentFlagRequired("dir")
  56. cli.root.PersistentFlags().IntP("groups", "g", 0, "Number of groups")
  57. cli.root.PersistentFlags().StringP("p2p", "p", string(MixedP2PMode),
  58. "P2P typology to be generated [\"new\", \"legacy\", \"hybrid\" or \"mixed\" ]")
  59. return cli
  60. }
  61. // generate generates manifests in a directory.
  62. func (cli *CLI) generate(dir string, groups int, opts Options) error {
  63. err := os.MkdirAll(dir, 0755)
  64. if err != nil {
  65. return err
  66. }
  67. manifests, err := Generate(rand.New(rand.NewSource(randomSeed)), opts)
  68. if err != nil {
  69. return err
  70. }
  71. if groups <= 0 {
  72. for i, manifest := range manifests {
  73. err = manifest.Save(filepath.Join(dir, fmt.Sprintf("gen-%04d.toml", i)))
  74. if err != nil {
  75. return err
  76. }
  77. }
  78. } else {
  79. groupSize := int(math.Ceil(float64(len(manifests)) / float64(groups)))
  80. for g := 0; g < groups; g++ {
  81. for i := 0; i < groupSize && g*groupSize+i < len(manifests); i++ {
  82. manifest := manifests[g*groupSize+i]
  83. err = manifest.Save(filepath.Join(dir, fmt.Sprintf("gen-group%02d-%04d.toml", g, i)))
  84. if err != nil {
  85. return err
  86. }
  87. }
  88. }
  89. }
  90. return nil
  91. }
  92. // Run runs the CLI.
  93. func (cli *CLI) Run() {
  94. if err := cli.root.Execute(); err != nil {
  95. logger.Error(err.Error())
  96. os.Exit(1)
  97. }
  98. }