2019-08-16 17:13:04 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-08-24 01:15:02 +00:00
|
|
|
"runtime/debug"
|
2019-08-22 05:34:25 +00:00
|
|
|
"time"
|
2019-08-16 17:13:04 +00:00
|
|
|
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
|
|
|
|
)
|
|
|
|
|
2019-08-28 15:14:22 +00:00
|
|
|
const oneYear = 365 * 24 * time.Hour
|
2019-08-22 05:34:25 +00:00
|
|
|
|
|
|
|
// prefix to add to keys, so that we can represent invalid objects
|
2019-08-28 15:14:22 +00:00
|
|
|
const invalid = "invalidObject"
|
2019-08-22 05:34:25 +00:00
|
|
|
|
2019-08-16 17:13:04 +00:00
|
|
|
// subHandler represents handler for a given subscription.
|
|
|
|
type subHandler func(context.Context, proto.Message) error
|
|
|
|
|
|
|
|
func notImplementedSubHandler(_ context.Context, _ proto.Message) error {
|
|
|
|
return errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// validator should verify the contents of the message, propagate the message
|
|
|
|
// as expected, and return true or false to continue the message processing
|
|
|
|
// pipeline.
|
|
|
|
type validator func(context.Context, proto.Message, p2p.Broadcaster) bool
|
|
|
|
|
|
|
|
// noopValidator is a no-op that always returns true and does not propagate any
|
|
|
|
// message.
|
|
|
|
func noopValidator(_ context.Context, _ proto.Message, _ p2p.Broadcaster) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register PubSub subscribers
|
|
|
|
func (r *RegularSync) registerSubscribers() {
|
|
|
|
r.subscribe(
|
|
|
|
"/eth2/beacon_block",
|
2019-08-22 18:11:52 +00:00
|
|
|
r.validateBeaconBlockPubSub,
|
|
|
|
r.beaconBlockSubscriber,
|
2019-08-16 17:13:04 +00:00
|
|
|
)
|
|
|
|
r.subscribe(
|
|
|
|
"/eth2/beacon_attestation",
|
2019-08-23 19:46:04 +00:00
|
|
|
r.validateBeaconAttestation,
|
2019-08-23 21:34:03 +00:00
|
|
|
r.beaconAttestationSubscriber,
|
2019-08-16 17:13:04 +00:00
|
|
|
)
|
|
|
|
r.subscribe(
|
|
|
|
"/eth2/voluntary_exit",
|
2019-08-18 15:33:58 +00:00
|
|
|
r.validateVoluntaryExit,
|
|
|
|
r.voluntaryExitSubscriber,
|
2019-08-16 17:13:04 +00:00
|
|
|
)
|
|
|
|
r.subscribe(
|
|
|
|
"/eth2/proposer_slashing",
|
2019-08-22 14:31:56 +00:00
|
|
|
r.validateProposerSlashing,
|
|
|
|
r.proposerSlashingSubscriber,
|
2019-08-16 17:13:04 +00:00
|
|
|
)
|
|
|
|
r.subscribe(
|
|
|
|
"/eth2/attester_slashing",
|
2019-08-22 05:34:25 +00:00
|
|
|
r.validateAttesterSlashing,
|
|
|
|
r.attesterSlashingSubscriber,
|
2019-08-16 17:13:04 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// subscribe to a given topic with a given validator and subscription handler.
|
|
|
|
// The base protobuf message is used to initialize new messages for decoding.
|
|
|
|
func (r *RegularSync) subscribe(topic string, validate validator, handle subHandler) {
|
|
|
|
base := p2p.GossipTopicMappings[topic]
|
|
|
|
if base == nil {
|
|
|
|
panic(fmt.Sprintf("%s is not mapped to any message in GossipTopicMappings", topic))
|
|
|
|
}
|
|
|
|
|
|
|
|
topic += r.p2p.Encoding().ProtocolSuffix()
|
|
|
|
log := log.WithField("topic", topic)
|
|
|
|
|
|
|
|
sub, err := r.p2p.PubSub().Subscribe(topic)
|
|
|
|
if err != nil {
|
|
|
|
// Any error subscribing to a PubSub topic would be the result of a misconfiguration of
|
|
|
|
// libp2p PubSub library. This should not happen at normal runtime, unless the config
|
|
|
|
// changes to a fatal configuration.
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pipeline decodes the incoming subscription data, runs the validation, and handles the
|
|
|
|
// message.
|
|
|
|
pipeline := func(data []byte) {
|
2019-08-24 01:15:02 +00:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
log.WithField("error", r).Error("Panic occurred")
|
|
|
|
debug.PrintStack()
|
|
|
|
}
|
|
|
|
}()
|
2019-08-24 12:50:43 +00:00
|
|
|
|
2019-08-16 17:13:04 +00:00
|
|
|
if data == nil {
|
|
|
|
log.Warn("Received nil message on pubsub")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := proto.Clone(base)
|
|
|
|
if err := r.p2p.Encoding().Decode(bytes.NewBuffer(data), msg); err != nil {
|
|
|
|
log.WithError(err).Warn("Failed to decode pubsub message")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !validate(r.ctx, msg, r.p2p) {
|
|
|
|
log.WithField("message", msg.String()).Debug("Message did not verify")
|
|
|
|
|
|
|
|
// TODO(3147): Increment metrics.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := handle(r.ctx, msg); err != nil {
|
|
|
|
// TODO(3147): Increment metrics.
|
|
|
|
log.WithError(err).Error("Failed to handle p2p pubsub")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The main message loop for receiving incoming messages from this subscription.
|
|
|
|
messageLoop := func() {
|
|
|
|
for {
|
|
|
|
msg, err := sub.Next(r.ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Subscription next failed")
|
|
|
|
// TODO(3147): Mark status unhealthy.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-24 18:41:24 +00:00
|
|
|
if msg.GetFrom() == r.p2p.PeerID() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-28 15:14:22 +00:00
|
|
|
messageReceivedCounter.WithLabelValues(topic + r.p2p.Encoding().ProtocolSuffix()).Inc()
|
|
|
|
|
2019-08-16 17:13:04 +00:00
|
|
|
go pipeline(msg.Data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go messageLoop()
|
|
|
|
}
|