2018-11-10 17:32:45 +00:00
|
|
|
/**
|
|
|
|
* Bootnode
|
|
|
|
*
|
2019-08-18 19:54:20 +00:00
|
|
|
* A node which implements the DiscoveryV5 protocol for peer
|
2018-11-10 17:32:45 +00:00
|
|
|
* discovery. The purpose of this service is to provide a starting point for
|
|
|
|
* newly connected services to find other peers outside of their network.
|
|
|
|
*
|
|
|
|
* Usage: Run bootnode --help for flag options.
|
|
|
|
*/
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-08-18 19:54:20 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/rand"
|
2019-09-12 00:04:35 +00:00
|
|
|
"encoding/hex"
|
2018-11-10 17:32:45 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2019-08-18 19:54:20 +00:00
|
|
|
"net"
|
2019-09-17 23:24:08 +00:00
|
|
|
"os"
|
2019-08-18 19:54:20 +00:00
|
|
|
|
2019-08-21 15:30:22 +00:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
2019-09-18 08:44:25 +00:00
|
|
|
gethlog "github.com/ethereum/go-ethereum/log"
|
2019-09-06 19:20:20 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
2019-08-21 15:30:22 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/crypto"
|
2019-09-06 19:20:20 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-01-10 04:19:33 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/version"
|
2019-09-01 03:05:36 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-09-01 22:29:58 +00:00
|
|
|
_ "go.uber.org/automaxprocs"
|
2018-11-10 17:32:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
debug = flag.Bool("debug", false, "Enable debug logging")
|
|
|
|
privateKey = flag.String("private", "", "Private key to use for peer ID")
|
|
|
|
port = flag.Int("port", 4000, "Port to listen for connections")
|
2019-09-06 19:20:20 +00:00
|
|
|
externalIP = flag.String("external-ip", "127.0.0.1", "External IP for the bootnode")
|
2018-11-10 17:32:45 +00:00
|
|
|
|
2019-09-01 03:05:36 +00:00
|
|
|
log = logrus.WithField("prefix", "bootnode")
|
2018-11-10 17:32:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
2019-01-10 04:19:33 +00:00
|
|
|
|
|
|
|
fmt.Printf("Starting bootnode. Version: %s\n", version.GetVersion())
|
|
|
|
|
2018-11-10 17:32:45 +00:00
|
|
|
if *debug {
|
2019-09-01 03:05:36 +00:00
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
2019-09-17 23:24:08 +00:00
|
|
|
|
|
|
|
// Geth specific logging.
|
|
|
|
glogger := gethlog.NewGlogHandler(gethlog.StreamHandler(os.Stderr, gethlog.TerminalFormat(false)))
|
|
|
|
glogger.Verbosity(gethlog.LvlTrace)
|
|
|
|
gethlog.Root().SetHandler(glogger)
|
|
|
|
|
|
|
|
log.Debug("Debug logging enabled.")
|
2018-11-10 17:32:45 +00:00
|
|
|
}
|
2019-09-06 19:20:20 +00:00
|
|
|
cfg := discover.Config{
|
|
|
|
PrivateKey: extractPrivateKey(),
|
|
|
|
}
|
|
|
|
listener := createListener(*externalIP, *port, cfg)
|
2018-11-10 17:32:45 +00:00
|
|
|
|
2019-08-18 19:54:20 +00:00
|
|
|
node := listener.Self()
|
2019-09-06 19:20:20 +00:00
|
|
|
log.Infof("Running bootnode: %s", node.String())
|
2019-05-10 15:56:30 +00:00
|
|
|
|
2019-08-18 19:54:20 +00:00
|
|
|
select {}
|
|
|
|
}
|
2018-11-10 17:32:45 +00:00
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
func createListener(ipAddr string, port int, cfg discover.Config) *discover.UDPv5 {
|
2019-09-02 18:23:07 +00:00
|
|
|
ip := net.ParseIP(ipAddr)
|
|
|
|
if ip.To4() == nil {
|
|
|
|
log.Fatalf("IPV4 address not provided instead %s was provided", ipAddr)
|
|
|
|
}
|
2019-08-18 19:54:20 +00:00
|
|
|
udpAddr := &net.UDPAddr{
|
2019-09-06 19:20:20 +00:00
|
|
|
IP: ip,
|
2019-08-18 19:54:20 +00:00
|
|
|
Port: port,
|
2019-05-10 15:56:30 +00:00
|
|
|
}
|
2019-08-18 19:54:20 +00:00
|
|
|
conn, err := net.ListenUDP("udp4", udpAddr)
|
2019-05-10 15:56:30 +00:00
|
|
|
if err != nil {
|
2019-08-18 19:54:20 +00:00
|
|
|
log.Fatal(err)
|
2019-05-10 15:56:30 +00:00
|
|
|
}
|
2019-09-06 19:20:20 +00:00
|
|
|
localNode, err := createLocalNode(cfg.PrivateKey, ip, port)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-11-10 17:32:45 +00:00
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
network, err := discover.ListenV5(conn, localNode, cfg)
|
2019-08-18 19:54:20 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return network
|
2018-11-10 17:32:45 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
func createLocalNode(privKey *ecdsa.PrivateKey, ipAddr net.IP, port int) (*enode.LocalNode, error) {
|
|
|
|
db, err := enode.OpenDB("")
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Could not open node's peer database")
|
|
|
|
}
|
|
|
|
|
|
|
|
localNode := enode.NewLocalNode(db, privKey)
|
|
|
|
ipEntry := enr.IP(ipAddr)
|
|
|
|
udpEntry := enr.UDP(port)
|
|
|
|
localNode.Set(ipEntry)
|
|
|
|
localNode.Set(udpEntry)
|
|
|
|
|
|
|
|
return localNode, nil
|
|
|
|
}
|
|
|
|
|
2019-08-18 19:54:20 +00:00
|
|
|
func extractPrivateKey() *ecdsa.PrivateKey {
|
|
|
|
var privKey *ecdsa.PrivateKey
|
2018-11-10 17:32:45 +00:00
|
|
|
if *privateKey != "" {
|
2019-09-12 00:04:35 +00:00
|
|
|
dst, err := hex.DecodeString(*privateKey)
|
2018-11-10 17:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-09-12 00:04:35 +00:00
|
|
|
unmarshalledKey, err := crypto.UnmarshalSecp256k1PrivateKey(dst)
|
2018-11-10 17:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-08-21 15:30:22 +00:00
|
|
|
privKey = (*ecdsa.PrivateKey)((*btcec.PrivateKey)(unmarshalledKey.(*crypto.Secp256k1PrivateKey)))
|
2019-08-18 19:54:20 +00:00
|
|
|
|
2018-11-10 17:32:45 +00:00
|
|
|
} else {
|
2019-08-21 15:30:22 +00:00
|
|
|
privInterfaceKey, _, err := crypto.GenerateSecp256k1Key(rand.Reader)
|
2019-08-18 19:54:20 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-08-21 15:30:22 +00:00
|
|
|
privKey = (*ecdsa.PrivateKey)((*btcec.PrivateKey)(privInterfaceKey.(*crypto.Secp256k1PrivateKey)))
|
2018-11-10 17:32:45 +00:00
|
|
|
log.Warning("No private key was provided. Using default/random private key")
|
2019-09-12 00:04:35 +00:00
|
|
|
b, err := privInterfaceKey.Raw()
|
2019-09-01 03:05:36 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-09-12 00:04:35 +00:00
|
|
|
log.Debugf("Private key %x", b)
|
2018-11-10 17:32:45 +00:00
|
|
|
}
|
2019-09-01 03:05:36 +00:00
|
|
|
|
2019-08-18 19:54:20 +00:00
|
|
|
return privKey
|
2018-11-10 17:32:45 +00:00
|
|
|
}
|