2019-08-21 06:08:30 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
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-22 15:23:16 +00:00
|
|
|
iaddr "github.com/ipfs/go-ipfs-addr"
|
2020-06-06 03:46:36 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/network"
|
2019-08-21 06:08:30 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2019-08-22 15:23:16 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-06-06 03:46:36 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-08-21 06:08:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Listener defines the discovery V5 network interface that is used
|
|
|
|
// to communicate with other peers.
|
|
|
|
type Listener interface {
|
2019-09-06 19:20:20 +00:00
|
|
|
Self() *enode.Node
|
2019-08-21 06:08:30 +00:00
|
|
|
Close()
|
2019-09-06 19:20:20 +00:00
|
|
|
Lookup(enode.ID) []*enode.Node
|
|
|
|
Resolve(*enode.Node) *enode.Node
|
2020-06-01 22:03:33 +00:00
|
|
|
RandomNodes() enode.Iterator
|
2019-09-06 19:20:20 +00:00
|
|
|
Ping(*enode.Node) error
|
|
|
|
RequestENR(*enode.Node) (*enode.Node, error)
|
2020-03-23 14:41:47 +00:00
|
|
|
LocalNode() *enode.LocalNode
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 20:27:03 +00:00
|
|
|
func (s *Service) createListener(
|
|
|
|
ipAddr net.IP,
|
|
|
|
privKey *ecdsa.PrivateKey,
|
|
|
|
) *discover.UDPv5 {
|
2019-08-21 06:08:30 +00:00
|
|
|
udpAddr := &net.UDPAddr{
|
|
|
|
IP: ipAddr,
|
2020-04-14 20:27:03 +00:00
|
|
|
Port: int(s.cfg.UDPPort),
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
2020-03-25 17:19:11 +00:00
|
|
|
// assume ip is either ipv4 or ipv6
|
|
|
|
networkVersion := ""
|
|
|
|
if ipAddr.To4() != nil {
|
|
|
|
networkVersion = "udp4"
|
|
|
|
} else {
|
|
|
|
networkVersion = "udp6"
|
|
|
|
}
|
|
|
|
conn, err := net.ListenUDP(networkVersion, udpAddr)
|
2019-08-21 06:08:30 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-04-14 20:27:03 +00:00
|
|
|
localNode, err := s.createLocalNode(
|
|
|
|
privKey,
|
|
|
|
ipAddr,
|
|
|
|
int(s.cfg.UDPPort),
|
|
|
|
int(s.cfg.TCPPort),
|
|
|
|
)
|
2019-09-06 19:20:20 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-04-14 20:27:03 +00:00
|
|
|
if s.cfg.HostAddress != "" {
|
|
|
|
hostIP := net.ParseIP(s.cfg.HostAddress)
|
2020-03-25 04:03:51 +00:00
|
|
|
if hostIP.To4() == nil && hostIP.To16() == nil {
|
2019-10-04 03:22:17 +00:00
|
|
|
log.Errorf("Invalid host address given: %s", hostIP.String())
|
|
|
|
} else {
|
|
|
|
localNode.SetFallbackIP(hostIP)
|
2020-05-09 00:58:11 +00:00
|
|
|
localNode.SetStaticIP(hostIP)
|
2019-10-04 03:22:17 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-06 19:20:20 +00:00
|
|
|
dv5Cfg := discover.Config{
|
|
|
|
PrivateKey: privKey,
|
|
|
|
}
|
2019-09-23 17:24:16 +00:00
|
|
|
dv5Cfg.Bootnodes = []*enode.Node{}
|
2020-04-14 20:27:03 +00:00
|
|
|
for _, addr := range s.cfg.Discv5BootStrapAddr {
|
2019-09-23 17:24:16 +00:00
|
|
|
bootNode, err := enode.Parse(enode.ValidSchemes, addr)
|
2019-09-06 19:20:20 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-09-23 17:24:16 +00:00
|
|
|
dv5Cfg.Bootnodes = append(dv5Cfg.Bootnodes, bootNode)
|
2019-09-06 19:20:20 +00:00
|
|
|
}
|
2019-08-21 06:08:30 +00:00
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
network, err := discover.ListenV5(conn, localNode, dv5Cfg)
|
2019-08-21 06:08:30 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return network
|
|
|
|
}
|
|
|
|
|
2020-04-14 20:27:03 +00:00
|
|
|
func (s *Service) createLocalNode(
|
|
|
|
privKey *ecdsa.PrivateKey,
|
|
|
|
ipAddr net.IP,
|
|
|
|
udpPort int,
|
|
|
|
tcpPort int,
|
|
|
|
) (*enode.LocalNode, error) {
|
2019-09-06 19:20:20 +00:00
|
|
|
db, err := enode.OpenDB("")
|
2019-08-21 06:08:30 +00:00
|
|
|
if err != nil {
|
2019-09-06 19:20:20 +00:00
|
|
|
return nil, errors.Wrap(err, "could not open node's peer database")
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
2019-09-06 19:20:20 +00:00
|
|
|
localNode := enode.NewLocalNode(db, privKey)
|
|
|
|
ipEntry := enr.IP(ipAddr)
|
2019-09-12 04:52:27 +00:00
|
|
|
udpEntry := enr.UDP(udpPort)
|
|
|
|
tcpEntry := enr.TCP(tcpPort)
|
2019-09-06 19:20:20 +00:00
|
|
|
localNode.Set(ipEntry)
|
|
|
|
localNode.Set(udpEntry)
|
2019-09-12 04:52:27 +00:00
|
|
|
localNode.Set(tcpEntry)
|
2019-12-10 05:35:16 +00:00
|
|
|
localNode.SetFallbackIP(ipAddr)
|
|
|
|
localNode.SetFallbackUDP(udpPort)
|
2019-09-06 19:20:20 +00:00
|
|
|
|
2020-04-14 20:27:03 +00:00
|
|
|
localNode, err = addForkEntry(localNode, s.genesisTime, s.genesisValidatorsRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not add eth2 fork version entry to enr")
|
|
|
|
}
|
2020-03-23 14:41:47 +00:00
|
|
|
return intializeAttSubnets(localNode), nil
|
2019-09-06 19:20:20 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 20:27:03 +00:00
|
|
|
func (s *Service) startDiscoveryV5(
|
|
|
|
addr net.IP,
|
|
|
|
privKey *ecdsa.PrivateKey,
|
|
|
|
) (*discover.UDPv5, error) {
|
|
|
|
listener := s.createListener(addr, privKey)
|
2020-03-23 14:41:47 +00:00
|
|
|
record := listener.Self()
|
|
|
|
log.WithField("ENR", record.String()).Info("Started discovery v5")
|
2019-08-21 06:08:30 +00:00
|
|
|
return listener, nil
|
|
|
|
}
|
|
|
|
|
2020-06-06 03:46:36 +00:00
|
|
|
// filterPeer validates each node that we retrieve from our dht. We
|
|
|
|
// try to ascertain that the peer can be a valid protocol peer.
|
|
|
|
// Validity Conditions:
|
|
|
|
// 1) The local node is still actively looking for peers to
|
|
|
|
// connect to.
|
|
|
|
// 2) Peer has a valid IP and TCP port set in their enr.
|
|
|
|
// 3) Peer hasn't been marked as 'bad'
|
|
|
|
// 4) Peer is not currently active or connected.
|
|
|
|
// 5) Peer's fork digest in their ENR matches that of
|
|
|
|
// our localnodes.
|
|
|
|
func (s *Service) filterPeer(node *enode.Node) bool {
|
2020-06-14 07:35:05 +00:00
|
|
|
numOfConns := len(s.host.Network().Peers())
|
|
|
|
maxPeers := int(s.cfg.MaxPeers)
|
|
|
|
activePeers := len(s.Peers().Active())
|
|
|
|
if activePeers >= maxPeers || numOfConns >= maxPeers {
|
2020-06-06 03:46:36 +00:00
|
|
|
log.WithFields(logrus.Fields{"peer": node.String(),
|
|
|
|
"reason": "at peer limit"}).Trace("Not dialing peer")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// ignore nodes with no ip address stored.
|
|
|
|
if node.IP() == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// do not dial nodes with their tcp ports not set
|
|
|
|
if err := node.Record().Load(enr.WithEntry("tcp", new(enr.TCP))); err != nil {
|
|
|
|
if !enr.IsNotFound(err) {
|
|
|
|
log.WithError(err).Debug("Could not retrieve tcp port")
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
peerData, multiAddr, err := convertToAddrInfo(node)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Debug("Could not convert to peer data")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if s.peers.IsBad(peerData.ID) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if s.peers.IsActive(peerData.ID) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if s.host.Network().Connectedness(peerData.ID) == network.Connected {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
nodeENR := node.Record()
|
|
|
|
// Decide whether or not to connect to peer that does not
|
|
|
|
// match the proper fork ENR data with our local node.
|
|
|
|
if s.genesisValidatorsRoot != nil {
|
|
|
|
if err := s.compareForkENR(nodeENR); err != nil {
|
|
|
|
log.WithError(err).Trace("Fork ENR mismatches between peer and local node")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add peer to peer handler.
|
|
|
|
s.peers.Add(nodeENR, peerData.ID, multiAddr, network.DirUnknown)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-06-23 02:53:11 +00:00
|
|
|
func parseBootStrapAddrs(addrs []string) (discv5Nodes []string) {
|
|
|
|
discv5Nodes, _ = parseGenericAddrs(addrs)
|
|
|
|
if len(discv5Nodes) == 0 {
|
2020-03-05 14:49:26 +00:00
|
|
|
log.Warn("No bootstrap addresses supplied")
|
|
|
|
}
|
2020-06-23 02:53:11 +00:00
|
|
|
return discv5Nodes
|
2020-03-05 14:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseGenericAddrs(addrs []string) (enodeString []string, multiAddrString []string) {
|
2019-09-23 17:24:16 +00:00
|
|
|
for _, addr := range addrs {
|
2019-09-27 12:05:16 +00:00
|
|
|
if addr == "" {
|
|
|
|
// Ignore empty entries
|
|
|
|
continue
|
|
|
|
}
|
2019-09-23 17:24:16 +00:00
|
|
|
_, err := enode.Parse(enode.ValidSchemes, addr)
|
|
|
|
if err == nil {
|
2020-03-05 14:49:26 +00:00
|
|
|
enodeString = append(enodeString, addr)
|
2019-09-23 17:24:16 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, err = multiAddrFromString(addr)
|
|
|
|
if err == nil {
|
2020-03-05 14:49:26 +00:00
|
|
|
multiAddrString = append(multiAddrString, addr)
|
2019-09-23 17:24:16 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-06-23 02:53:11 +00:00
|
|
|
log.Errorf("Invalid address of %s provided: %v", addr, err)
|
2019-09-27 12:05:16 +00:00
|
|
|
}
|
2020-03-05 14:49:26 +00:00
|
|
|
return enodeString, multiAddrString
|
2019-09-23 17:24:16 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
func convertToMultiAddr(nodes []*enode.Node) []ma.Multiaddr {
|
2019-08-21 06:08:30 +00:00
|
|
|
var multiAddrs []ma.Multiaddr
|
|
|
|
for _, node := range nodes {
|
2019-10-04 08:05:52 +00:00
|
|
|
// ignore nodes with no ip address stored
|
|
|
|
if node.IP() == nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-09-01 22:29:58 +00:00
|
|
|
multiAddr, err := convertToSingleMultiAddr(node)
|
2019-08-21 06:08:30 +00:00
|
|
|
if err != nil {
|
2019-09-01 22:29:58 +00:00
|
|
|
log.WithError(err).Error("Could not convert to multiAddr")
|
2019-08-21 06:08:30 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
multiAddrs = append(multiAddrs, multiAddr)
|
|
|
|
}
|
|
|
|
return multiAddrs
|
|
|
|
}
|
2019-08-22 15:23:16 +00:00
|
|
|
|
2020-06-06 03:46:36 +00:00
|
|
|
func convertToAddrInfo(node *enode.Node) (*peer.AddrInfo, ma.Multiaddr, error) {
|
|
|
|
multiAddr, err := convertToSingleMultiAddr(node)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
info, err := peer.AddrInfoFromP2pAddr(multiAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return info, multiAddr, nil
|
|
|
|
}
|
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
func convertToSingleMultiAddr(node *enode.Node) (ma.Multiaddr, error) {
|
|
|
|
ip4 := node.IP().To4()
|
2019-09-01 22:29:58 +00:00
|
|
|
if ip4 == nil {
|
2019-09-23 04:47:11 +00:00
|
|
|
return nil, errors.Errorf("node doesn't have an ip4 address, it's stated IP is %s", node.IP().String())
|
2019-09-01 22:29:58 +00:00
|
|
|
}
|
2019-09-06 19:20:20 +00:00
|
|
|
pubkey := node.Pubkey()
|
2019-09-01 22:29:58 +00:00
|
|
|
assertedKey := convertToInterfacePubkey(pubkey)
|
|
|
|
id, err := peer.IDFromPublicKey(assertedKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not get peer id")
|
|
|
|
}
|
2019-09-12 04:52:27 +00:00
|
|
|
multiAddrString := fmt.Sprintf("/ip4/%s/tcp/%d/p2p/%s", ip4.String(), node.TCP(), id)
|
2019-09-01 22:29:58 +00:00
|
|
|
multiAddr, err := ma.NewMultiaddr(multiAddrString)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not get multiaddr")
|
|
|
|
}
|
|
|
|
return multiAddr, nil
|
|
|
|
}
|
|
|
|
|
2020-03-05 14:49:26 +00:00
|
|
|
func peersFromStringAddrs(addrs []string) ([]ma.Multiaddr, error) {
|
2019-08-22 15:23:16 +00:00
|
|
|
var allAddrs []ma.Multiaddr
|
2020-03-05 14:49:26 +00:00
|
|
|
enodeString, multiAddrString := parseGenericAddrs(addrs)
|
|
|
|
for _, stringAddr := range multiAddrString {
|
2019-08-22 15:23:16 +00:00
|
|
|
addr, err := multiAddrFromString(stringAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "Could not get multiaddr from string")
|
|
|
|
}
|
|
|
|
allAddrs = append(allAddrs, addr)
|
|
|
|
}
|
2020-03-05 14:49:26 +00:00
|
|
|
for _, stringAddr := range enodeString {
|
|
|
|
enodeAddr, err := enode.Parse(enode.ValidSchemes, stringAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "Could not get enode from string")
|
|
|
|
}
|
|
|
|
addr, err := convertToSingleMultiAddr(enodeAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "Could not get multiaddr")
|
|
|
|
}
|
|
|
|
allAddrs = append(allAddrs, addr)
|
|
|
|
}
|
2019-08-22 15:23:16 +00:00
|
|
|
return allAddrs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func multiAddrFromString(address string) (ma.Multiaddr, error) {
|
|
|
|
addr, err := iaddr.ParseString(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return addr.Multiaddr(), nil
|
|
|
|
}
|