2020-07-13 04:16:24 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-07-29 16:14:15 +00:00
|
|
|
"time"
|
2020-07-13 04:16:24 +00:00
|
|
|
|
|
|
|
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) {
|
2020-07-27 00:56:55 +00:00
|
|
|
s.joinedTopicsLock.Lock()
|
|
|
|
defer s.joinedTopicsLock.Unlock()
|
|
|
|
|
2020-07-13 04:16:24 +00:00
|
|
|
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 {
|
2020-07-27 00:56:55 +00:00
|
|
|
s.joinedTopicsLock.Lock()
|
|
|
|
defer s.joinedTopicsLock.Unlock()
|
|
|
|
|
2020-07-13 04:16:24 +00:00
|
|
|
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
|
|
|
|
}
|
2020-08-10 15:27:50 +00:00
|
|
|
|
|
|
|
// Wait for at least 1 peer to be available to receive the published message.
|
|
|
|
for {
|
|
|
|
if len(topicHandle.ListPeers()) > 0 {
|
|
|
|
return topicHandle.Publish(ctx, data, opts...)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
2020-07-13 04:16:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeToTopic joins (if necessary) and subscribes to PubSub topic.
|
|
|
|
func (s *Service) SubscribeToTopic(topic string, opts ...pubsub.SubOpt) (*pubsub.Subscription, error) {
|
2020-10-16 07:05:40 +00:00
|
|
|
s.awaitStateInitialized() // Genesis time and genesis validator root are required to subscribe.
|
|
|
|
|
2020-07-13 04:16:24 +00:00
|
|
|
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:
|
2020-10-02 15:08:51 +00:00
|
|
|
// message-id: SHA256(message.data)
|
2020-07-13 04:16:24 +00:00
|
|
|
func msgIDFunction(pmsg *pubsub_pb.Message) string {
|
2020-09-06 18:05:16 +00:00
|
|
|
h := hashutil.Hash(pmsg.Data)
|
2020-10-02 15:08:51 +00:00
|
|
|
return string(h[:])
|
2020-07-13 04:16:24 +00:00
|
|
|
}
|
2020-07-29 16:14:15 +00:00
|
|
|
|
|
|
|
func setPubSubParameters() {
|
|
|
|
pubsub.GossipSubDlo = 5
|
|
|
|
pubsub.GossipSubHeartbeatInterval = 700 * time.Millisecond
|
|
|
|
pubsub.GossipSubHistoryLength = 6
|
|
|
|
pubsub.GossipSubHistoryGossip = 3
|
|
|
|
}
|