feat(direct peers): configure static peers to be direct peers in pubsub options (#13773)

This commit is contained in:
Lorenzo 2024-03-29 05:00:40 +01:00 committed by GitHub
parent 9f17e65860
commit 5b1da7353c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,6 +3,7 @@ package p2p
import ( import (
"context" "context"
"encoding/hex" "encoding/hex"
"fmt"
"strings" "strings"
"time" "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 { func (s *Service) pubsubOptions() []pubsub.Option {
psOpts := []pubsub.Option{ psOpts := []pubsub.Option{
pubsub.WithMessageSignaturePolicy(pubsub.StrictNoSign), pubsub.WithMessageSignaturePolicy(pubsub.StrictNoSign),
@ -147,7 +148,33 @@ func (s *Service) pubsubOptions() []pubsub.Option {
pubsub.WithGossipSubParams(pubsubGossipParam()), pubsub.WithGossipSubParams(pubsubGossipParam()),
pubsub.WithRawTracer(gossipTracer{host: s.host}), 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 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. // creates a custom gossipsub parameter set.