Skip to content

Commit

Permalink
Discovery Fixes (#5050)
Browse files Browse the repository at this point in the history
* connect to dv5 bootnodes

* fix test

* change polling period

* ignore

* Update beacon-chain/p2p/service.go

* Update beacon-chain/p2p/service_test.go

* fix test

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
Co-authored-by: terence tsao <terence@prysmaticlabs.com>
  • Loading branch information
3 people committed Mar 10, 2020
1 parent edb6590 commit 9d27449
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion beacon-chain/p2p/discovery_test.go
Expand Up @@ -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
Expand Down
34 changes: 15 additions & 19 deletions beacon-chain/p2p/service.go
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
})
Expand All @@ -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
}
Expand All @@ -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
}

Expand Down
8 changes: 8 additions & 0 deletions beacon-chain/p2p/service_test.go
Expand Up @@ -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
Expand Down

0 comments on commit 9d27449

Please sign in to comment.