diff --git a/beacon-chain/p2p/pubsub.go b/beacon-chain/p2p/pubsub.go index 7b6f91c2d71c..0cc0b4f13013 100644 --- a/beacon-chain/p2p/pubsub.go +++ b/beacon-chain/p2p/pubsub.go @@ -3,6 +3,7 @@ package p2p import ( "context" "encoding/hex" + "fmt" "strings" "time" @@ -130,7 +131,7 @@ func (s *Service) peerInspector(peerMap map[peer.ID]*pubsub.PeerScoreSnapshot) { } } -// Creates a list of pubsub options to configure out router with. +// pubsubOptions creates a list of options to configure our router with. func (s *Service) pubsubOptions() []pubsub.Option { psOpts := []pubsub.Option{ pubsub.WithMessageSignaturePolicy(pubsub.StrictNoSign), @@ -147,9 +148,35 @@ func (s *Service) pubsubOptions() []pubsub.Option { pubsub.WithGossipSubParams(pubsubGossipParam()), pubsub.WithRawTracer(gossipTracer{host: s.host}), } + + if len(s.cfg.StaticPeers) > 0 { + directPeersAddrInfos, err := parsePeersEnr(s.cfg.StaticPeers) + if err != nil { + log.WithError(err).Error("Could not add direct peer option") + return psOpts + } + psOpts = append(psOpts, pubsub.WithDirectPeers(directPeersAddrInfos)) + } + return psOpts } +// parsePeersEnr takes a list of raw ENRs and converts them into a list of AddrInfos. +func parsePeersEnr(peers []string) ([]peer.AddrInfo, error) { + addrs, err := PeersFromStringAddrs(peers) + if err != nil { + return nil, fmt.Errorf("Cannot convert peers raw ENRs into multiaddresses: %v", err) + } + if len(addrs) == 0 { + return nil, fmt.Errorf("Converting peers raw ENRs into multiaddresses resulted in an empty list") + } + directAddrInfos, err := peer.AddrInfosFromP2pAddrs(addrs...) + if err != nil { + return nil, fmt.Errorf("Cannot convert peers multiaddresses into AddrInfos: %v", err) + } + return directAddrInfos, nil +} + // creates a custom gossipsub parameter set. func pubsubGossipParam() pubsub.GossipSubParams { gParams := pubsub.DefaultGossipSubParams()