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.

82 lines
1.8 KiB

  1. // nolint: gosec
  2. package main
  3. import (
  4. "flag"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "math/rand"
  9. "os"
  10. "path/filepath"
  11. "github.com/tendermint/tendermint/crypto/ed25519"
  12. "github.com/tendermint/tendermint/p2p"
  13. tmp2p "github.com/tendermint/tendermint/proto/tendermint/p2p"
  14. )
  15. func main() {
  16. baseDir := flag.String("base", ".", `where the "corpus" directory will live`)
  17. flag.Parse()
  18. initCorpus(*baseDir)
  19. }
  20. func initCorpus(rootDir string) {
  21. log.SetFlags(0)
  22. corpusDir := filepath.Join(rootDir, "corpus")
  23. if err := os.MkdirAll(corpusDir, 0755); err != nil {
  24. log.Fatalf("Creating %q err: %v", corpusDir, err)
  25. }
  26. sizes := []int{0, 1, 2, 17, 5, 31}
  27. // Make the PRNG predictable
  28. rand.Seed(10)
  29. for _, n := range sizes {
  30. var addrs []*p2p.NetAddress
  31. // IPv4 addresses
  32. for i := 0; i < n; i++ {
  33. privKey := ed25519.GenPrivKey()
  34. addr := fmt.Sprintf(
  35. "%s@%v.%v.%v.%v:26656",
  36. p2p.NodeIDFromPubKey(privKey.PubKey()),
  37. rand.Int()%256,
  38. rand.Int()%256,
  39. rand.Int()%256,
  40. rand.Int()%256,
  41. )
  42. netAddr, _ := p2p.NewNetAddressString(addr)
  43. addrs = append(addrs, netAddr)
  44. }
  45. // IPv6 addresses
  46. privKey := ed25519.GenPrivKey()
  47. ipv6a, err := p2p.NewNetAddressString(
  48. fmt.Sprintf("%s@[ff02::1:114]:26656", p2p.NodeIDFromPubKey(privKey.PubKey())))
  49. if err != nil {
  50. log.Fatalf("can't create a new netaddress: %v", err)
  51. }
  52. addrs = append(addrs, ipv6a)
  53. msg := tmp2p.PexMessage{
  54. Sum: &tmp2p.PexMessage_PexResponse{
  55. PexResponse: &tmp2p.PexResponse{Addresses: p2p.NetAddressesToProto(addrs)},
  56. },
  57. }
  58. bz, err := msg.Marshal()
  59. if err != nil {
  60. log.Fatalf("unable to marshal: %v", err)
  61. }
  62. filename := filepath.Join(rootDir, "corpus", fmt.Sprintf("%d", n))
  63. if err := ioutil.WriteFile(filename, bz, 0644); err != nil {
  64. log.Fatalf("can't write %X to %q: %v", bz, filename, err)
  65. }
  66. log.Printf("wrote %q", filename)
  67. }
  68. }