prysm-pulse/beacon-chain/p2p/options.go
Raul Jordan ac77a5c054
QSP-25 Only Enable Relay if Flag Provided (#6386)
* only add relay if flag is on
* Merge branch 'master' into qsp-25
* Merge refs/heads/master into qsp-25
* Merge refs/heads/master into qsp-25
2020-06-24 19:30:40 +00:00

106 lines
3.3 KiB
Go

package p2p
import (
"crypto/ecdsa"
"fmt"
"net"
"time"
"github.com/libp2p/go-libp2p"
noise "github.com/libp2p/go-libp2p-noise"
ma "github.com/multiformats/go-multiaddr"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"github.com/prysmaticlabs/prysm/shared/version"
)
const (
// Period that we allocate each new peer before we mark them as valid
// for trimming.
gracePeriod = 2 * time.Minute
// Buffer for the number of peers allowed to connect above max peers before the
// connection manager begins trimming them.
peerBuffer = 5
)
// buildOptions for the libp2p host.
func (s *Service) buildOptions(ip net.IP, priKey *ecdsa.PrivateKey) []libp2p.Option {
cfg := s.cfg
listen, err := multiAddressBuilder(ip.String(), cfg.TCPPort)
if err != nil {
log.Fatalf("Failed to p2p listen: %v", err)
}
options := []libp2p.Option{
privKeyOption(priKey),
libp2p.ListenAddrs(listen),
libp2p.UserAgent(version.GetBuildData()),
libp2p.ConnectionGater(s),
}
if featureconfig.Get().EnableNoise {
// Enable NOISE for the beacon node
options = append(options, libp2p.Security(noise.ID, noise.New))
}
if cfg.EnableUPnP {
options = append(options, libp2p.NATPortMap()) //Allow to use UPnP
}
if cfg.RelayNodeAddr != "" {
options = append(options, libp2p.AddrsFactory(withRelayAddrs(cfg.RelayNodeAddr)))
options = append(options, libp2p.EnableRelay())
}
if cfg.HostAddress != "" {
options = append(options, libp2p.AddrsFactory(func(addrs []ma.Multiaddr) []ma.Multiaddr {
external, err := multiAddressBuilder(cfg.HostAddress, cfg.TCPPort)
if err != nil {
log.WithError(err).Error("Unable to create external multiaddress")
} else {
addrs = append(addrs, external)
}
return addrs
}))
}
if cfg.HostDNS != "" {
options = append(options, libp2p.AddrsFactory(func(addrs []ma.Multiaddr) []ma.Multiaddr {
external, err := ma.NewMultiaddr(fmt.Sprintf("/dns4/%s/tcp/%d", cfg.HostDNS, cfg.TCPPort))
if err != nil {
log.WithError(err).Error("Unable to create external multiaddress")
} else {
addrs = append(addrs, external)
}
return addrs
}))
}
if cfg.LocalIP != "" {
if net.ParseIP(cfg.LocalIP) == nil {
log.Errorf("Invalid local ip provided: %s", cfg.LocalIP)
return options
}
listen, err = multiAddressBuilder(cfg.LocalIP, cfg.TCPPort)
if err != nil {
log.Fatalf("Failed to p2p listen: %v", err)
}
options = append(options, libp2p.ListenAddrs(listen))
}
return options
}
func multiAddressBuilder(ipAddr string, port uint) (ma.Multiaddr, error) {
parsedIP := net.ParseIP(ipAddr)
if parsedIP.To4() == nil && parsedIP.To16() == nil {
return nil, errors.Errorf("invalid ip address provided: %s", ipAddr)
}
if parsedIP.To4() != nil {
return ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ipAddr, port))
}
return ma.NewMultiaddr(fmt.Sprintf("/ip6/%s/tcp/%d", ipAddr, port))
}
// Adds a private key to the libp2p option if the option was provided.
// If the private key file is missing or cannot be read, or if the
// private key contents cannot be marshaled, an exception is thrown.
func privKeyOption(privkey *ecdsa.PrivateKey) libp2p.Option {
return func(cfg *libp2p.Config) error {
log.Debug("ECDSA private key generated")
return cfg.Apply(libp2p.Identity(convertToInterfacePrivkey(privkey)))
}
}