main.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. // bootnode runs a bootstrap node for the Ethereum Discovery Protocol.
  17. package main
  18. import (
  19. "flag"
  20. "fmt"
  21. "math/rand"
  22. "strconv"
  23. "time"
  24. "github.com/ethereum/go-ethereum/cmd/utils"
  25. "github.com/ethereum/go-ethereum/crypto"
  26. "github.com/ethereum/go-ethereum/logger/glog"
  27. "github.com/ethereum/go-ethereum/p2p/discv5"
  28. "github.com/ethereum/go-ethereum/p2p/nat"
  29. )
  30. func main() {
  31. var (
  32. listenPort = flag.Int("addr", 31000, "beginning of listening port range")
  33. natdesc = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|extip:<IP>)")
  34. count = flag.Int("count", 1, "number of v5 topic discovery test nodes (adds default bootnodes to form a test network)")
  35. regtopic = flag.String("reg", "", "topic to register on the network")
  36. looktopic = flag.String("search", "", "topic to search on the network")
  37. )
  38. flag.Var(glog.GetVerbosity(), "verbosity", "log verbosity (0-9)")
  39. flag.Var(glog.GetVModule(), "vmodule", "log verbosity pattern")
  40. glog.SetToStderr(true)
  41. flag.Parse()
  42. natm, err := nat.Parse(*natdesc)
  43. if err != nil {
  44. utils.Fatalf("-nat: %v", err)
  45. }
  46. for i := 0; i < *count; i++ {
  47. listenAddr := ":" + strconv.Itoa(*listenPort+i)
  48. nodeKey, err := crypto.GenerateKey()
  49. if err != nil {
  50. utils.Fatalf("could not generate key: %v", err)
  51. }
  52. if net, err := discv5.ListenUDP(nodeKey, listenAddr, natm, ""); err != nil {
  53. utils.Fatalf("%v", err)
  54. } else {
  55. if err := net.SetFallbackNodes(discv5.BootNodes); err != nil {
  56. utils.Fatalf("%v", err)
  57. }
  58. go func() {
  59. if *looktopic == "" {
  60. for i := 0; i < 20; i++ {
  61. time.Sleep(time.Millisecond * time.Duration(2000+rand.Intn(2001)))
  62. net.BucketFill()
  63. }
  64. }
  65. switch {
  66. case *regtopic != "":
  67. // register topic
  68. fmt.Println("Starting topic register")
  69. stop := make(chan struct{})
  70. net.RegisterTopic(discv5.Topic(*regtopic), stop)
  71. case *looktopic != "":
  72. // search topic
  73. fmt.Println("Starting topic search")
  74. stop := make(chan struct{})
  75. found := make(chan string, 100)
  76. go net.SearchTopic(discv5.Topic(*looktopic), stop, found)
  77. for s := range found {
  78. fmt.Println(time.Now(), s)
  79. }
  80. default:
  81. // just keep doing lookups
  82. for {
  83. time.Sleep(time.Millisecond * time.Duration(40000+rand.Intn(40001)))
  84. net.BucketFill()
  85. }
  86. }
  87. }()
  88. }
  89. fmt.Printf("Started test node #%d with public key %v\n", i, discv5.PubkeyID(&nodeKey.PublicKey))
  90. }
  91. select {}
  92. }