mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 04:47:18 +00:00
28c4f28d32
* add forked connMgr * gaz * add license header * Merge branch 'master' of https://github.com/prysmaticlabs/geth-sharding into connMgr * add conn manager test * gaz * fix connManager * Merge branch 'master' of https://github.com/prysmaticlabs/geth-sharding into connMgr * gaz * remove todo * add new dep * lint * lint * lint * space * visibility * Merge branch 'master' into connMgr * Merge branch 'master' into connMgr * Merge refs/heads/master into connMgr
83 lines
2.4 KiB
Go
83 lines
2.4 KiB
Go
package p2p
|
|
|
|
import (
|
|
"crypto/ecdsa"
|
|
"fmt"
|
|
"net"
|
|
"time"
|
|
|
|
"github.com/libp2p/go-libp2p"
|
|
filter "github.com/libp2p/go-maddr-filter"
|
|
"github.com/multiformats/go-multiaddr"
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/connmgr"
|
|
)
|
|
|
|
// buildOptions for the libp2p host.
|
|
func buildOptions(cfg *Config, ip net.IP, priKey *ecdsa.PrivateKey) []libp2p.Option {
|
|
listen, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ip, cfg.TCPPort))
|
|
if err != nil {
|
|
log.Fatalf("Failed to p2p listen: %v", err)
|
|
}
|
|
options := []libp2p.Option{
|
|
privKeyOption(priKey),
|
|
libp2p.EnableRelay(),
|
|
libp2p.ListenAddrs(listen),
|
|
whitelistSubnet(cfg.WhitelistCIDR),
|
|
libp2p.ConnectionManager(connmgr.NewConnManager(int(cfg.MaxPeers), int(cfg.MaxPeers), 1*time.Second)),
|
|
}
|
|
if cfg.EnableUPnP {
|
|
options = append(options, libp2p.NATPortMap()) //Allow to use UPnP
|
|
}
|
|
if cfg.RelayNodeAddr != "" {
|
|
options = append(options, libp2p.AddrsFactory(withRelayAddrs(cfg.RelayNodeAddr)))
|
|
}
|
|
if cfg.HostAddress != "" {
|
|
options = append(options, libp2p.AddrsFactory(func(addrs []multiaddr.Multiaddr) []multiaddr.Multiaddr {
|
|
external, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", cfg.HostAddress, cfg.TCPPort))
|
|
if err != nil {
|
|
log.WithError(err).Error("Unable to create external multiaddress")
|
|
} else {
|
|
addrs = append(addrs, external)
|
|
}
|
|
return addrs
|
|
}))
|
|
}
|
|
return options
|
|
}
|
|
|
|
// 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)))
|
|
}
|
|
}
|
|
|
|
// whitelistSubnet adds a whitelist multiaddress filter for a given CIDR subnet.
|
|
// Example: 192.168.0.0/16 may be used to accept only connections on your local
|
|
// network.
|
|
func whitelistSubnet(cidr string) libp2p.Option {
|
|
if cidr == "" {
|
|
return func(_ *libp2p.Config) error {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
return func(cfg *libp2p.Config) error {
|
|
_, ipnet, err := net.ParseCIDR(cidr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if cfg.Filters == nil {
|
|
cfg.Filters = filter.NewFilters()
|
|
}
|
|
cfg.Filters.AddFilter(*ipnet, filter.ActionAccept)
|
|
|
|
return nil
|
|
}
|
|
}
|