mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-25 21:17:16 +00:00
9123eceb0f
* p2p: add DialRatio for configuration of inbound vs. dialed connections * p2p: add connection flags to PeerInfo * p2p/netutil: add SameNet, DistinctNetSet * p2p/discover: improve revalidation and seeding This changes node revalidation to be periodic instead of on-demand. This should prevent issues where dead nodes get stuck in closer buckets because no other node will ever come along to replace them. Every 5 seconds (on average), the last node in a random bucket is checked and moved to the front of the bucket if it is still responding. If revalidation fails, the last node is replaced by an entry of the 'replacement list' containing recently-seen nodes. Most close buckets are removed because it's very unlikely we'll ever encounter a node that would fall into any of those buckets. Table seeding is also improved: we now require a few minutes of table membership before considering a node as a potential seed node. This should make it less likely to store short-lived nodes as potential seeds. * p2p/discover: fix nits in UDP transport We would skip sending neighbors replies if there were fewer than maxNeighbors results and CheckRelayIP returned an error for the last one. While here, also resolve a TODO about pong reply tokens.
137 lines
4.2 KiB
Go
137 lines
4.2 KiB
Go
// 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 (
|
|
"crypto/ecdsa"
|
|
"flag"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
|
|
"github.com/ethereum/go-ethereum/cmd/utils"
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
"github.com/ethereum/go-ethereum/log"
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
|
"github.com/ethereum/go-ethereum/p2p/discv5"
|
|
"github.com/ethereum/go-ethereum/p2p/nat"
|
|
"github.com/ethereum/go-ethereum/p2p/netutil"
|
|
)
|
|
|
|
func main() {
|
|
var (
|
|
listenAddr = flag.String("addr", ":30301", "listen address")
|
|
genKey = flag.String("genkey", "", "generate a node key")
|
|
writeAddr = flag.Bool("writeaddress", false, "write out the node's pubkey hash and quit")
|
|
nodeKeyFile = flag.String("nodekey", "", "private key filename")
|
|
nodeKeyHex = flag.String("nodekeyhex", "", "private key as hex (for testing)")
|
|
natdesc = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|extip:<IP>)")
|
|
netrestrict = flag.String("netrestrict", "", "restrict network communication to the given IP networks (CIDR masks)")
|
|
runv5 = flag.Bool("v5", false, "run a v5 topic discovery bootnode")
|
|
verbosity = flag.Int("verbosity", int(log.LvlInfo), "log verbosity (0-9)")
|
|
vmodule = flag.String("vmodule", "", "log verbosity pattern")
|
|
|
|
nodeKey *ecdsa.PrivateKey
|
|
err error
|
|
)
|
|
flag.Parse()
|
|
|
|
glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
|
|
glogger.Verbosity(log.Lvl(*verbosity))
|
|
glogger.Vmodule(*vmodule)
|
|
log.Root().SetHandler(glogger)
|
|
|
|
natm, err := nat.Parse(*natdesc)
|
|
if err != nil {
|
|
utils.Fatalf("-nat: %v", err)
|
|
}
|
|
switch {
|
|
case *genKey != "":
|
|
nodeKey, err = crypto.GenerateKey()
|
|
if err != nil {
|
|
utils.Fatalf("could not generate key: %v", err)
|
|
}
|
|
if err = crypto.SaveECDSA(*genKey, nodeKey); err != nil {
|
|
utils.Fatalf("%v", err)
|
|
}
|
|
return
|
|
case *nodeKeyFile == "" && *nodeKeyHex == "":
|
|
utils.Fatalf("Use -nodekey or -nodekeyhex to specify a private key")
|
|
case *nodeKeyFile != "" && *nodeKeyHex != "":
|
|
utils.Fatalf("Options -nodekey and -nodekeyhex are mutually exclusive")
|
|
case *nodeKeyFile != "":
|
|
if nodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil {
|
|
utils.Fatalf("-nodekey: %v", err)
|
|
}
|
|
case *nodeKeyHex != "":
|
|
if nodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil {
|
|
utils.Fatalf("-nodekeyhex: %v", err)
|
|
}
|
|
}
|
|
|
|
if *writeAddr {
|
|
fmt.Printf("%v\n", discover.PubkeyID(&nodeKey.PublicKey))
|
|
os.Exit(0)
|
|
}
|
|
|
|
var restrictList *netutil.Netlist
|
|
if *netrestrict != "" {
|
|
restrictList, err = netutil.ParseNetlist(*netrestrict)
|
|
if err != nil {
|
|
utils.Fatalf("-netrestrict: %v", err)
|
|
}
|
|
}
|
|
|
|
addr, err := net.ResolveUDPAddr("udp", *listenAddr)
|
|
if err != nil {
|
|
utils.Fatalf("-ResolveUDPAddr: %v", err)
|
|
}
|
|
conn, err := net.ListenUDP("udp", addr)
|
|
if err != nil {
|
|
utils.Fatalf("-ListenUDP: %v", err)
|
|
}
|
|
|
|
realaddr := conn.LocalAddr().(*net.UDPAddr)
|
|
if natm != nil {
|
|
if !realaddr.IP.IsLoopback() {
|
|
go nat.Map(natm, nil, "udp", realaddr.Port, realaddr.Port, "ethereum discovery")
|
|
}
|
|
// TODO: react to external IP changes over time.
|
|
if ext, err := natm.ExternalIP(); err == nil {
|
|
realaddr = &net.UDPAddr{IP: ext, Port: realaddr.Port}
|
|
}
|
|
}
|
|
|
|
if *runv5 {
|
|
if _, err := discv5.ListenUDP(nodeKey, conn, realaddr, "", restrictList); err != nil {
|
|
utils.Fatalf("%v", err)
|
|
}
|
|
} else {
|
|
cfg := discover.Config{
|
|
PrivateKey: nodeKey,
|
|
AnnounceAddr: realaddr,
|
|
NetRestrict: restrictList,
|
|
}
|
|
if _, err := discover.ListenUDP(conn, cfg); err != nil {
|
|
utils.Fatalf("%v", err)
|
|
}
|
|
}
|
|
|
|
select {}
|
|
}
|