aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/v5test/main.go
blob: e537760c60c8e7e65701d9662435a34ca497e7d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2015 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.

// bootnode runs a bootstrap node for the Ethereum Discovery Protocol.
package main

import (
    "flag"
    "fmt"
    "math/rand"
    "strconv"
    "time"

    "github.com/ethereum/go-ethereum/cmd/utils"
    "github.com/ethereum/go-ethereum/crypto"
    "github.com/ethereum/go-ethereum/logger/glog"
    "github.com/ethereum/go-ethereum/p2p/discv5"
    "github.com/ethereum/go-ethereum/p2p/nat"
)

func main() {
    var (
        listenPort = flag.Int("addr", 31000, "beginning of listening port range")
        natdesc    = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|extip:<IP>)")
        count      = flag.Int("count", 1, "number of v5 topic discovery test nodes (adds default bootnodes to form a test network)")
        regtopic   = flag.String("reg", "", "topic to register on the network")
        looktopic  = flag.String("search", "", "topic to search on the network")
    )
    flag.Var(glog.GetVerbosity(), "verbosity", "log verbosity (0-9)")
    flag.Var(glog.GetVModule(), "vmodule", "log verbosity pattern")
    glog.SetToStderr(true)
    flag.Parse()

    natm, err := nat.Parse(*natdesc)
    if err != nil {
        utils.Fatalf("-nat: %v", err)
    }

    for i := 0; i < *count; i++ {
        listenAddr := ":" + strconv.Itoa(*listenPort+i)

        nodeKey, err := crypto.GenerateKey()
        if err != nil {
            utils.Fatalf("could not generate key: %v", err)
        }

        if net, err := discv5.ListenUDP(nodeKey, listenAddr, natm, ""); err != nil {
            utils.Fatalf("%v", err)
        } else {
            if err := net.SetFallbackNodes(discv5.BootNodes); err != nil {
                utils.Fatalf("%v", err)
            }
            go func() {
                if *looktopic == "" {
                    for i := 0; i < 20; i++ {
                        time.Sleep(time.Millisecond * time.Duration(2000+rand.Intn(2001)))
                        net.BucketFill()
                    }
                }
                switch {
                case *regtopic != "":
                    // register topic
                    fmt.Println("Starting topic register")
                    stop := make(chan struct{})
                    net.RegisterTopic(discv5.Topic(*regtopic), stop)
                case *looktopic != "":
                    // search topic
                    fmt.Println("Starting topic search")
                    stop := make(chan struct{})
                    found := make(chan string, 100)
                    go net.SearchTopic(discv5.Topic(*looktopic), stop, found)
                    for s := range found {
                        fmt.Println(time.Now(), s)
                    }
                default:
                    // just keep doing lookups
                    for {
                        time.Sleep(time.Millisecond * time.Duration(40000+rand.Intn(40001)))
                        net.BucketFill()
                    }
                }
            }()
        }
        fmt.Printf("Started test node #%d with public key %v\n", i, discv5.PubkeyID(&nodeKey.PublicKey))
    }

    select {}
}