|
| 1 | +// Copyright 2015 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 | + |
| 17 | +// bootnode runs a bootstrap node for the Ethereum Discovery Protocol. |
| 18 | +package main |
| 19 | + |
| 20 | +import ( |
| 21 | + "flag" |
| 22 | + "fmt" |
| 23 | + "math/rand" |
| 24 | + "strconv" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/ethereum/go-ethereum/cmd/utils" |
| 28 | + "github.com/ethereum/go-ethereum/crypto" |
| 29 | + "github.com/ethereum/go-ethereum/logger/glog" |
| 30 | + "github.com/ethereum/go-ethereum/p2p/discv5" |
| 31 | + "github.com/ethereum/go-ethereum/p2p/nat" |
| 32 | +) |
| 33 | + |
| 34 | +func main() { |
| 35 | + var ( |
| 36 | + listenPort = flag.Int("addr", 31000, "beginning of listening port range") |
| 37 | + natdesc = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|extip:<IP>)") |
| 38 | + count = flag.Int("count", 1, "number of v5 topic discovery test nodes (adds default bootnodes to form a test network)") |
| 39 | + regtopic = flag.String("reg", "", "topic to register on the network") |
| 40 | + looktopic = flag.String("search", "", "topic to search on the network") |
| 41 | + ) |
| 42 | + flag.Var(glog.GetVerbosity(), "verbosity", "log verbosity (0-9)") |
| 43 | + flag.Var(glog.GetVModule(), "vmodule", "log verbosity pattern") |
| 44 | + glog.SetToStderr(true) |
| 45 | + flag.Parse() |
| 46 | + |
| 47 | + natm, err := nat.Parse(*natdesc) |
| 48 | + if err != nil { |
| 49 | + utils.Fatalf("-nat: %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + for i := 0; i < *count; i++ { |
| 53 | + listenAddr := ":" + strconv.Itoa(*listenPort+i) |
| 54 | + |
| 55 | + nodeKey, err := crypto.GenerateKey() |
| 56 | + if err != nil { |
| 57 | + utils.Fatalf("could not generate key: %v", err) |
| 58 | + } |
| 59 | + |
| 60 | + if net, err := discv5.ListenUDP(nodeKey, listenAddr, natm, ""); err != nil { |
| 61 | + utils.Fatalf("%v", err) |
| 62 | + } else { |
| 63 | + if err := net.SetFallbackNodes(discv5.BootNodes); err != nil { |
| 64 | + utils.Fatalf("%v", err) |
| 65 | + } |
| 66 | + go func() { |
| 67 | + if *looktopic == "" { |
| 68 | + for i := 0; i < 20; i++ { |
| 69 | + time.Sleep(time.Millisecond * time.Duration(2000+rand.Intn(2001))) |
| 70 | + net.BucketFill() |
| 71 | + } |
| 72 | + } |
| 73 | + switch { |
| 74 | + case *regtopic != "": |
| 75 | + // register topic |
| 76 | + fmt.Println("Starting topic register") |
| 77 | + stop := make(chan struct{}) |
| 78 | + net.RegisterTopic(discv5.Topic(*regtopic), stop) |
| 79 | + case *looktopic != "": |
| 80 | + // search topic |
| 81 | + fmt.Println("Starting topic search") |
| 82 | + stop := make(chan struct{}) |
| 83 | + found := make(chan string, 100) |
| 84 | + go net.SearchTopic(discv5.Topic(*looktopic), stop, found) |
| 85 | + for s := range found { |
| 86 | + fmt.Println(time.Now(), s) |
| 87 | + } |
| 88 | + default: |
| 89 | + // just keep doing lookups |
| 90 | + for { |
| 91 | + time.Sleep(time.Millisecond * time.Duration(40000+rand.Intn(40001))) |
| 92 | + net.BucketFill() |
| 93 | + } |
| 94 | + } |
| 95 | + }() |
| 96 | + } |
| 97 | + fmt.Printf("Started test node #%d with public key %v\n", i, discv5.PubkeyID(&nodeKey.PublicKey)) |
| 98 | + } |
| 99 | + |
| 100 | + select {} |
| 101 | +} |
0 commit comments