diff --git a/beacon-chain/p2p/discovery_test.go b/beacon-chain/p2p/discovery_test.go index f4f5fd7be5a..7482b67b588 100644 --- a/beacon-chain/p2p/discovery_test.go +++ b/beacon-chain/p2p/discovery_test.go @@ -132,7 +132,7 @@ func TestMultiAddrConversion_OK(t *testing.T) { } func TestStaticPeering_PeersAreAdded(t *testing.T) { - cfg := &Config{Encoding: "ssz"} + cfg := &Config{Encoding: "ssz", MaxPeers: 30} port := 3000 var staticPeers []string var hosts []host.Host diff --git a/beacon-chain/p2p/service.go b/beacon-chain/p2p/service.go index 2d268cc3a65..9e85c20b834 100644 --- a/beacon-chain/p2p/service.go +++ b/beacon-chain/p2p/service.go @@ -26,11 +26,13 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/p2p/peers" "github.com/prysmaticlabs/prysm/shared" "github.com/prysmaticlabs/prysm/shared/runutil" + "github.com/sirupsen/logrus" ) var _ = shared.Service(&Service{}) -var pollingPeriod = 1 * time.Second +// Check local table every 5 seconds for newly added peers. +var pollingPeriod = 5 * time.Second const prysmProtocolPrefix = "/prysm/0.0.0" @@ -158,7 +160,7 @@ func (s *Service) Start() { s.startupErr = err return } - err = s.addBootNodesToExclusionList() + err = s.connectToBootnodes() if err != nil { log.WithError(err).Error("Could not add bootnode to the exclusion list") s.startupErr = err @@ -293,12 +295,8 @@ func (s *Service) Peers() *peers.Status { // listen for new nodes watches for new nodes in the network and adds them to the peerstore. func (s *Service) listenForNewNodes() { - bootNode, err := enode.Parse(enode.ValidSchemes, s.cfg.Discv5BootStrapAddr[0]) - if err != nil { - log.Fatal(err) - } runutil.RunEvery(s.ctx, pollingPeriod, func() { - nodes := s.dv5Listener.Lookup(bootNode.ID()) + nodes := s.dv5Listener.LookupRandom() multiAddresses := convertToMultiAddr(nodes) s.connectWithAllPeers(multiAddresses) }) @@ -313,6 +311,11 @@ func (s *Service) connectWithAllPeers(multiAddrs []ma.Multiaddr) { for _, info := range addrInfos { // make each dial non-blocking go func(info peer.AddrInfo) { + if len(s.Peers().Active()) >= int(s.cfg.MaxPeers) { + log.WithFields(logrus.Fields{"peer": info.ID.String(), + "reason": "at peer limit"}).Trace("Not dialing peer") + return + } if info.ID == s.host.ID() { return } @@ -327,24 +330,17 @@ func (s *Service) connectWithAllPeers(multiAddrs []ma.Multiaddr) { } } -func (s *Service) addBootNodesToExclusionList() error { +func (s *Service) connectToBootnodes() error { + nodes := make([]*enode.Node, 0, len(s.cfg.Discv5BootStrapAddr)) for _, addr := range s.cfg.Discv5BootStrapAddr { bootNode, err := enode.Parse(enode.ValidSchemes, addr) if err != nil { return err } - multAddr, err := convertToSingleMultiAddr(bootNode) - if err != nil { - return err - } - addrInfo, err := peer.AddrInfoFromP2pAddr(multAddr) - if err != nil { - return err - } - // bootnode is never dialled, so ttl is tentatively 1 year - s.exclusionList.Set(addrInfo.ID.String(), true, 1) + nodes = append(nodes, bootNode) } - + multiAddresses := convertToMultiAddr(nodes) + s.connectWithAllPeers(multiAddresses) return nil } diff --git a/beacon-chain/p2p/service_test.go b/beacon-chain/p2p/service_test.go index c8071d3bb26..ec96b55a976 100644 --- a/beacon-chain/p2p/service_test.go +++ b/beacon-chain/p2p/service_test.go @@ -130,12 +130,20 @@ func TestListenForNewNodes(t *testing.T) { bootListener := createListener(ipAddr, pkey, cfg) defer bootListener.Close() + // Use shorter period for testing. + currentPeriod := pollingPeriod + pollingPeriod = 1 * time.Second + defer func() { + pollingPeriod = currentPeriod + }() + bootNode := bootListener.Self() cfg = &Config{ BootstrapNodeAddr: []string{bootNode.String()}, Discv5BootStrapAddr: []string{bootNode.String()}, Encoding: "ssz", + MaxPeers: 30, } var listeners []*discover.UDPv5 var hosts []host.Host