github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/sync/decode_pubsub.go (about) 1 package sync 2 3 import ( 4 "errors" 5 "strings" 6 7 pubsub "github.com/libp2p/go-libp2p-pubsub" 8 "github.com/prysmaticlabs/prysm/beacon-chain/p2p" 9 "google.golang.org/protobuf/proto" 10 ) 11 12 var errNilPubsubMessage = errors.New("nil pubsub message") 13 var errInvalidTopic = errors.New("invalid topic format") 14 15 func (s *Service) decodePubsubMessage(msg *pubsub.Message) (proto.Message, error) { 16 if msg == nil || msg.Topic == nil || *msg.Topic == "" { 17 return nil, errNilPubsubMessage 18 } 19 topic := *msg.Topic 20 topic = strings.TrimSuffix(topic, s.cfg.P2P.Encoding().ProtocolSuffix()) 21 topic, err := s.replaceForkDigest(topic) 22 if err != nil { 23 return nil, err 24 } 25 base, ok := p2p.GossipTopicMappings[topic] 26 if !ok { 27 return nil, p2p.ErrMessageNotMapped 28 } 29 m := proto.Clone(base) 30 if err := s.cfg.P2P.Encoding().DecodeGossip(msg.Data, m); err != nil { 31 return nil, err 32 } 33 return m, nil 34 } 35 36 // Replaces our fork digest with the formatter. 37 func (s *Service) replaceForkDigest(topic string) (string, error) { 38 subStrings := strings.Split(topic, "/") 39 if len(subStrings) != 4 { 40 return "", errInvalidTopic 41 } 42 subStrings[2] = "%x" 43 return strings.Join(subStrings, "/"), nil 44 }