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
|
|
|
|
2020-11-12 08:08:07 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2020-07-13 04:16:24 +00:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
|
|
pubsub_pb "github.com/libp2p/go-libp2p-pubsub/pb"
|
2020-12-14 17:22:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/encoder"
|
2021-03-02 19:36:03 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/cmd/beacon-chain/flags"
|
2021-01-19 12:13:08 +00:00
|
|
|
pbrpc "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
|
2020-11-12 08:08:07 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/featureconfig"
|
2020-07-13 04:16:24 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
2020-10-24 03:38:05 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2020-07-13 04:16:24 +00:00
|
|
|
)
|
|
|
|
|
2021-04-02 12:21:19 +00:00
|
|
|
const (
|
|
|
|
// overlay parameters
|
|
|
|
gossipSubD = 8 // topic stable mesh target count
|
|
|
|
gossipSubDlo = 6 // topic stable mesh low watermark
|
|
|
|
gossipSubDhi = 12 // topic stable mesh high watermark
|
|
|
|
|
|
|
|
// gossip parameters
|
|
|
|
gossipSubMcacheLen = 6 // number of windows to retain full messages in cache for `IWANT` responses
|
|
|
|
gossipSubMcacheGossip = 3 // number of windows to gossip about
|
|
|
|
gossipSubSeenTTL = 550 // number of heartbeat intervals to retain message IDs
|
|
|
|
|
|
|
|
// fanout ttl
|
|
|
|
gossipSubFanoutTTL = 60000000000 // TTL for fanout maps for topics we are not subscribed to but have published to, in nano seconds
|
|
|
|
|
|
|
|
// heartbeat interval
|
|
|
|
gossipSubHeartbeatInterval = 700 * time.Millisecond // frequency of heartbeat, milliseconds
|
|
|
|
|
|
|
|
// misc
|
|
|
|
randomSubD = 6 // random gossip target
|
|
|
|
)
|
|
|
|
|
2020-07-13 04:16:24 +00:00
|
|
|
// 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 {
|
2020-12-02 20:29:36 +00:00
|
|
|
if len(topicHandle.ListPeers()) > 0 || flags.Get().MinimumSyncPeers == 0 {
|
2020-08-10 15:27:50 +00:00
|
|
|
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
|
|
|
|
}
|
2021-04-13 02:20:13 +00:00
|
|
|
scoringParams, err := s.topicScoreParams(topic)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-02 12:21:19 +00:00
|
|
|
|
2021-04-13 02:20:13 +00:00
|
|
|
if scoringParams != nil {
|
|
|
|
if err := topicHandle.SetScoreParams(scoringParams); err != nil {
|
|
|
|
return nil, err
|
2020-11-12 08:08:07 +00:00
|
|
|
}
|
2021-04-13 02:20:13 +00:00
|
|
|
logGossipParameters(topic, scoringParams)
|
2020-11-12 08:08:07 +00:00
|
|
|
}
|
2020-07-13 04:16:24 +00:00
|
|
|
return topicHandle.Subscribe(opts...)
|
|
|
|
}
|
|
|
|
|
2020-11-12 08:08:07 +00:00
|
|
|
// peerInspector will scrape all the relevant scoring data and add it to our
|
|
|
|
// peer handler.
|
|
|
|
func (s *Service) peerInspector(peerMap map[peer.ID]*pubsub.PeerScoreSnapshot) {
|
2021-01-19 12:13:08 +00:00
|
|
|
// Iterate through all the connected peers and through any of their
|
|
|
|
// relevant topics.
|
|
|
|
for pid, snap := range peerMap {
|
|
|
|
s.peers.Scorers().GossipScorer().SetGossipData(pid, snap.Score,
|
|
|
|
snap.BehaviourPenalty, convertTopicScores(snap.Topics))
|
|
|
|
}
|
2020-11-12 08:08:07 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 04:16:24 +00:00
|
|
|
// Content addressable ID function.
|
|
|
|
//
|
|
|
|
// ETH2 spec defines the message ID as:
|
2020-10-24 03:38:05 +00:00
|
|
|
// The `message-id` of a gossipsub message MUST be the following 20 byte value computed from the message data:
|
|
|
|
// If `message.data` has a valid snappy decompression, set `message-id` to the first 20 bytes of the `SHA256` hash of
|
|
|
|
// the concatenation of `MESSAGE_DOMAIN_VALID_SNAPPY` with the snappy decompressed message data,
|
|
|
|
// i.e. `SHA256(MESSAGE_DOMAIN_VALID_SNAPPY + snappy_decompress(message.data))[:20]`.
|
|
|
|
//
|
|
|
|
// Otherwise, set `message-id` to the first 20 bytes of the `SHA256` hash of
|
|
|
|
// the concatenation of `MESSAGE_DOMAIN_INVALID_SNAPPY` with the raw message data,
|
|
|
|
// i.e. `SHA256(MESSAGE_DOMAIN_INVALID_SNAPPY + message.data)[:20]`.
|
2020-07-13 04:16:24 +00:00
|
|
|
func msgIDFunction(pmsg *pubsub_pb.Message) string {
|
2020-12-14 17:22:25 +00:00
|
|
|
decodedData, err := encoder.DecodeSnappy(pmsg.Data, params.BeaconNetworkConfig().GossipMaxSize)
|
2020-10-24 03:38:05 +00:00
|
|
|
if err != nil {
|
|
|
|
combinedData := append(params.BeaconNetworkConfig().MessageDomainInvalidSnappy[:], pmsg.Data...)
|
|
|
|
h := hashutil.Hash(combinedData)
|
|
|
|
return string(h[:20])
|
|
|
|
}
|
|
|
|
combinedData := append(params.BeaconNetworkConfig().MessageDomainValidSnappy[:], decodedData...)
|
|
|
|
h := hashutil.Hash(combinedData)
|
|
|
|
return string(h[:20])
|
2020-07-13 04:16:24 +00:00
|
|
|
}
|
2020-07-29 16:14:15 +00:00
|
|
|
|
|
|
|
func setPubSubParameters() {
|
2021-04-02 12:21:19 +00:00
|
|
|
pubsub.GossipSubDlo = gossipSubDlo
|
|
|
|
pubsub.GossipSubD = gossipSubD
|
|
|
|
pubsub.GossipSubHeartbeatInterval = gossipSubHeartbeatInterval
|
|
|
|
pubsub.GossipSubHistoryLength = gossipSubMcacheLen
|
|
|
|
pubsub.GossipSubHistoryGossip = gossipSubMcacheGossip
|
|
|
|
pubsub.TimeCacheDuration = 550 * gossipSubHeartbeatInterval
|
2020-11-20 15:36:02 +00:00
|
|
|
|
|
|
|
// Set a larger gossip history to ensure that slower
|
|
|
|
// messages have a longer time to be propagated. This
|
|
|
|
// comes with the tradeoff of larger memory usage and
|
|
|
|
// size of the seen message cache.
|
|
|
|
if featureconfig.Get().EnableLargerGossipHistory {
|
|
|
|
pubsub.GossipSubHistoryLength = 12
|
|
|
|
pubsub.GossipSubHistoryLength = 5
|
|
|
|
}
|
2020-07-29 16:14:15 +00:00
|
|
|
}
|
2021-01-19 12:13:08 +00:00
|
|
|
|
|
|
|
// convert from libp2p's internal schema to a compatible prysm protobuf format.
|
|
|
|
func convertTopicScores(topicMap map[string]*pubsub.TopicScoreSnapshot) map[string]*pbrpc.TopicScoreSnapshot {
|
|
|
|
newMap := make(map[string]*pbrpc.TopicScoreSnapshot, len(topicMap))
|
|
|
|
for t, s := range topicMap {
|
|
|
|
newMap[t] = &pbrpc.TopicScoreSnapshot{
|
|
|
|
TimeInMesh: uint64(s.TimeInMesh.Milliseconds()),
|
|
|
|
FirstMessageDeliveries: float32(s.FirstMessageDeliveries),
|
|
|
|
MeshMessageDeliveries: float32(s.MeshMessageDeliveries),
|
|
|
|
InvalidMessageDeliveries: float32(s.InvalidMessageDeliveries),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newMap
|
|
|
|
}
|