mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-23 20:07:17 +00:00
2916d183e8
* Ensure better topicIDs validation, add tests * go mod tidy * fix tests Co-authored-by: Victor Farazdagi <simple.square@gmail.com> Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
package sync
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
|
|
)
|
|
|
|
var errTooManyTopics = errors.New("too many topic IDs")
|
|
var errNilPubsubMessage = errors.New("nil pubsub message")
|
|
var errInvalidTopic = errors.New("invalid topic format")
|
|
|
|
func (s *Service) decodePubsubMessage(msg *pubsub.Message) (proto.Message, error) {
|
|
if msg == nil || msg.TopicIDs == nil {
|
|
return nil, errNilPubsubMessage
|
|
}
|
|
if len(msg.TopicIDs) != 1 {
|
|
return nil, errTooManyTopics
|
|
}
|
|
topic := msg.TopicIDs[0]
|
|
topic = strings.TrimSuffix(topic, s.p2p.Encoding().ProtocolSuffix())
|
|
topic, err := s.replaceForkDigest(topic)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
base, ok := p2p.GossipTopicMappings[topic]
|
|
if !ok {
|
|
return nil, p2p.ErrMessageNotMapped
|
|
}
|
|
m := proto.Clone(base)
|
|
if err := s.p2p.Encoding().DecodeGossip(msg.Data, m); err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// Replaces our fork digest with the formatter.
|
|
func (s *Service) replaceForkDigest(topic string) (string, error) {
|
|
subStrings := strings.Split(topic, "/")
|
|
if len(subStrings) != 4 {
|
|
return "", errInvalidTopic
|
|
}
|
|
subStrings[2] = "%x"
|
|
return strings.Join(subStrings, "/"), nil
|
|
}
|