2019-08-21 06:08:30 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p"
|
2023-05-22 11:18:13 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/network"
|
2022-10-07 07:24:51 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2022-08-04 15:04:51 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/muxer/mplex"
|
2022-09-12 14:03:20 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/security/noise"
|
2022-07-01 15:34:11 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
|
2019-08-21 06:08:30 +00:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2020-03-25 17:19:11 +00:00
|
|
|
"github.com/pkg/errors"
|
2023-05-22 11:18:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/config/features"
|
2023-03-17 18:52:56 +00:00
|
|
|
ecdsaprysm "github.com/prysmaticlabs/prysm/v4/crypto/ecdsa"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/runtime/version"
|
2019-08-21 06:08:30 +00:00
|
|
|
)
|
|
|
|
|
2022-08-17 06:38:57 +00:00
|
|
|
// MultiAddressBuilder takes in an ip address string and port to produce a go multiaddr format.
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2019-08-21 06:08:30 +00:00
|
|
|
// buildOptions for the libp2p host.
|
2020-06-14 07:35:05 +00:00
|
|
|
func (s *Service) buildOptions(ip net.IP, priKey *ecdsa.PrivateKey) []libp2p.Option {
|
|
|
|
cfg := s.cfg
|
2022-08-17 06:38:57 +00:00
|
|
|
listen, err := MultiAddressBuilder(ip.String(), cfg.TCPPort)
|
2019-08-21 06:08:30 +00:00
|
|
|
if err != nil {
|
2022-08-05 10:52:02 +00:00
|
|
|
log.WithError(err).Fatal("Failed to p2p listen")
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
2020-07-30 20:23:30 +00:00
|
|
|
if cfg.LocalIP != "" {
|
|
|
|
if net.ParseIP(cfg.LocalIP) == nil {
|
|
|
|
log.Fatalf("Invalid local ip provided: %s", cfg.LocalIP)
|
|
|
|
}
|
2022-08-17 06:38:57 +00:00
|
|
|
listen, err = MultiAddressBuilder(cfg.LocalIP, cfg.TCPPort)
|
2020-07-30 20:23:30 +00:00
|
|
|
if err != nil {
|
2022-08-05 10:52:02 +00:00
|
|
|
log.WithError(err).Fatal("Failed to p2p listen")
|
2020-07-30 20:23:30 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-01 15:34:11 +00:00
|
|
|
ifaceKey, err := ecdsaprysm.ConvertToInterfacePrivkey(priKey)
|
|
|
|
if err != nil {
|
2022-08-05 10:52:02 +00:00
|
|
|
log.WithError(err).Fatal("Failed to retrieve private key")
|
2022-07-01 15:34:11 +00:00
|
|
|
}
|
2022-02-02 19:13:52 +00:00
|
|
|
id, err := peer.IDFromPublicKey(ifaceKey.GetPublic())
|
|
|
|
if err != nil {
|
2022-08-05 10:52:02 +00:00
|
|
|
log.WithError(err).Fatal("Failed to retrieve peer id")
|
2022-02-02 19:13:52 +00:00
|
|
|
}
|
|
|
|
log.Infof("Running node with peer id of %s ", id.String())
|
|
|
|
|
2019-08-21 06:08:30 +00:00
|
|
|
options := []libp2p.Option{
|
|
|
|
privKeyOption(priKey),
|
|
|
|
libp2p.ListenAddrs(listen),
|
2021-01-25 21:27:30 +00:00
|
|
|
libp2p.UserAgent(version.BuildData()),
|
2020-06-14 07:35:05 +00:00
|
|
|
libp2p.ConnectionGater(s),
|
2021-02-26 09:26:39 +00:00
|
|
|
libp2p.Transport(tcp.NewTCPTransport),
|
2022-08-04 15:04:51 +00:00
|
|
|
libp2p.DefaultMuxers,
|
2023-06-08 04:02:46 +00:00
|
|
|
libp2p.Muxer("/mplex/6.7.0", mplex.DefaultTransport),
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
2020-10-14 23:28:49 +00:00
|
|
|
|
2020-12-02 16:45:28 +00:00
|
|
|
options = append(options, libp2p.Security(noise.ID, noise.New))
|
2020-10-14 23:28:49 +00:00
|
|
|
|
2019-08-21 06:08:30 +00:00
|
|
|
if cfg.EnableUPnP {
|
2021-01-05 13:09:41 +00:00
|
|
|
options = append(options, libp2p.NATPortMap()) // Allow to use UPnP
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
2019-09-23 21:43:53 +00:00
|
|
|
if cfg.RelayNodeAddr != "" {
|
|
|
|
options = append(options, libp2p.AddrsFactory(withRelayAddrs(cfg.RelayNodeAddr)))
|
2020-09-25 12:33:28 +00:00
|
|
|
} else {
|
|
|
|
// Disable relay if it has not been set.
|
|
|
|
options = append(options, libp2p.DisableRelay())
|
2019-09-23 21:43:53 +00:00
|
|
|
}
|
2019-10-04 03:22:17 +00:00
|
|
|
if cfg.HostAddress != "" {
|
2020-04-14 20:27:03 +00:00
|
|
|
options = append(options, libp2p.AddrsFactory(func(addrs []ma.Multiaddr) []ma.Multiaddr {
|
2022-08-17 06:38:57 +00:00
|
|
|
external, err := MultiAddressBuilder(cfg.HostAddress, cfg.TCPPort)
|
2019-10-04 03:22:17 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Unable to create external multiaddress")
|
|
|
|
} else {
|
|
|
|
addrs = append(addrs, external)
|
|
|
|
}
|
|
|
|
return addrs
|
2020-01-22 22:07:22 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
if cfg.HostDNS != "" {
|
2020-04-14 20:27:03 +00:00
|
|
|
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))
|
2020-01-22 22:07:22 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Unable to create external multiaddress")
|
|
|
|
} else {
|
|
|
|
addrs = append(addrs, external)
|
|
|
|
}
|
|
|
|
return addrs
|
2019-10-04 03:22:17 +00:00
|
|
|
}))
|
|
|
|
}
|
2020-09-25 12:33:28 +00:00
|
|
|
// Disable Ping Service.
|
|
|
|
options = append(options, libp2p.Ping(false))
|
2023-05-22 11:18:13 +00:00
|
|
|
if features.Get().DisableResourceManager {
|
|
|
|
options = append(options, libp2p.ResourceManager(&network.NullResourceManager{}))
|
|
|
|
}
|
2019-08-21 06:08:30 +00:00
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2020-12-17 18:03:18 +00:00
|
|
|
func multiAddressBuilderWithID(ipAddr, protocol string, port uint, id peer.ID) (ma.Multiaddr, error) {
|
2020-09-16 00:17:22 +00:00
|
|
|
parsedIP := net.ParseIP(ipAddr)
|
|
|
|
if parsedIP.To4() == nil && parsedIP.To16() == nil {
|
|
|
|
return nil, errors.Errorf("invalid ip address provided: %s", ipAddr)
|
|
|
|
}
|
|
|
|
if id.String() == "" {
|
|
|
|
return nil, errors.New("empty peer id given")
|
|
|
|
}
|
|
|
|
if parsedIP.To4() != nil {
|
2020-12-17 18:03:18 +00:00
|
|
|
return ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/%s/%d/p2p/%s", ipAddr, protocol, port, id.String()))
|
2020-09-16 00:17:22 +00:00
|
|
|
}
|
2020-12-17 18:03:18 +00:00
|
|
|
return ma.NewMultiaddr(fmt.Sprintf("/ip6/%s/%s/%d/p2p/%s", ipAddr, protocol, port, id.String()))
|
2020-09-16 00:17:22 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 06:08:30 +00:00
|
|
|
// 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 {
|
2022-07-01 15:34:11 +00:00
|
|
|
ifaceKey, err := ecdsaprysm.ConvertToInterfacePrivkey(privkey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-01 20:05:17 +00:00
|
|
|
log.Debug("ECDSA private key generated")
|
2022-07-01 15:34:11 +00:00
|
|
|
return cfg.Apply(libp2p.Identity(ifaceKey))
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
|
|
|
}
|