prysm-pulse/beacon-chain/p2p/pubsub.go
Nishant Das b5bd1260d0
Clean Up P2P Service (#6574)
* clean up
* clean up
* gaz
* preston's review
* preston's review
* Merge refs/heads/master into CleanUpP2P
2020-07-13 04:16:24 +00:00

63 lines
1.8 KiB
Go

package p2p
import (
"context"
"encoding/base64"
pubsub "github.com/libp2p/go-libp2p-pubsub"
pubsub_pb "github.com/libp2p/go-libp2p-pubsub/pb"
"github.com/prysmaticlabs/prysm/shared/hashutil"
)
// JoinTopic will join PubSub topic, if not already joined.
func (s *Service) JoinTopic(topic string, opts ...pubsub.TopicOpt) (*pubsub.Topic, error) {
if _, ok := s.joinedTopics[topic]; !ok {
topicHandle, err := s.pubsub.Join(topic, opts...)
if err != nil {
return nil, err
}
s.joinedTopics[topic] = topicHandle
}
return s.joinedTopics[topic], nil
}
// LeaveTopic closes topic and removes corresponding handler from list of joined topics.
// This method will return error if there are outstanding event handlers or subscriptions.
func (s *Service) LeaveTopic(topic string) error {
if t, ok := s.joinedTopics[topic]; ok {
if err := t.Close(); err != nil {
return err
}
delete(s.joinedTopics, topic)
}
return nil
}
// PublishToTopic joins (if necessary) and publishes a message to a PubSub topic.
func (s *Service) PublishToTopic(ctx context.Context, topic string, data []byte, opts ...pubsub.PubOpt) error {
topicHandle, err := s.JoinTopic(topic)
if err != nil {
return err
}
return topicHandle.Publish(ctx, data, opts...)
}
// SubscribeToTopic joins (if necessary) and subscribes to PubSub topic.
func (s *Service) SubscribeToTopic(topic string, opts ...pubsub.SubOpt) (*pubsub.Subscription, error) {
topicHandle, err := s.JoinTopic(topic)
if err != nil {
return nil, err
}
return topicHandle.Subscribe(opts...)
}
// Content addressable ID function.
//
// ETH2 spec defines the message ID as:
// message-id: base64(SHA256(message.data))
func msgIDFunction(pmsg *pubsub_pb.Message) string {
h := hashutil.FastSum256(pmsg.Data)
return base64.URLEncoding.EncodeToString(h[:])
}