2019-08-16 17:13:04 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-09-18 20:48:16 +00:00
|
|
|
"reflect"
|
2021-09-08 15:16:53 +00:00
|
|
|
"runtime/debug"
|
2020-04-14 20:27:03 +00:00
|
|
|
"strings"
|
2019-08-16 17:13:04 +00:00
|
|
|
|
2021-09-08 15:16:53 +00:00
|
|
|
ssz "github.com/ferranbt/fastssz"
|
2019-08-16 17:13:04 +00:00
|
|
|
libp2pcore "github.com/libp2p/go-libp2p-core"
|
|
|
|
"github.com/libp2p/go-libp2p-core/network"
|
2021-09-10 03:52:56 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/protocol"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core"
|
2020-04-14 20:27:03 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
|
2020-11-18 04:17:42 +00:00
|
|
|
p2ptypes "github.com/prysmaticlabs/prysm/beacon-chain/p2p/types"
|
2021-09-14 20:59:51 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/monitoring/tracing"
|
2020-04-16 22:21:44 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2021-09-15 00:09:04 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/time"
|
2019-09-03 18:06:35 +00:00
|
|
|
"go.opencensus.io/trace"
|
2019-08-16 17:13:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Time to first byte timeout. The maximum time to wait for first byte of
|
|
|
|
// request response (time-to-first-byte). The client is expected to give up if
|
|
|
|
// they don't receive the first byte within 5 seconds.
|
2020-04-16 22:21:44 +00:00
|
|
|
var ttfbTimeout = params.BeaconNetworkConfig().TtfbTimeout
|
2019-09-24 14:56:50 +00:00
|
|
|
|
2020-07-01 09:47:59 +00:00
|
|
|
// respTimeout is the maximum time for complete response transfer.
|
|
|
|
var respTimeout = params.BeaconNetworkConfig().RespTimeout
|
|
|
|
|
2019-08-16 17:13:04 +00:00
|
|
|
// rpcHandler is responsible for handling and responding to any incoming message.
|
|
|
|
// This method may return an error to internal monitoring, but the error will
|
|
|
|
// not be relayed to the peer.
|
2019-09-16 17:54:46 +00:00
|
|
|
type rpcHandler func(context.Context, interface{}, libp2pcore.Stream) error
|
2019-08-16 17:13:04 +00:00
|
|
|
|
|
|
|
// registerRPCHandlers for p2p RPC.
|
2020-06-22 20:37:48 +00:00
|
|
|
func (s *Service) registerRPCHandlers() {
|
2021-09-10 03:52:56 +00:00
|
|
|
currEpoch := core.SlotToEpoch(s.cfg.Chain.CurrentSlot())
|
|
|
|
// Register V2 handlers if we are past altair fork epoch.
|
|
|
|
if currEpoch >= params.BeaconConfig().AltairForkEpoch {
|
|
|
|
s.registerRPC(
|
|
|
|
p2p.RPCStatusTopicV1,
|
|
|
|
s.statusRPCHandler,
|
|
|
|
)
|
|
|
|
s.registerRPC(
|
|
|
|
p2p.RPCGoodByeTopicV1,
|
|
|
|
s.goodbyeRPCHandler,
|
|
|
|
)
|
|
|
|
s.registerRPC(
|
|
|
|
p2p.RPCPingTopicV1,
|
|
|
|
s.pingHandler,
|
|
|
|
)
|
|
|
|
s.registerRPCHandlersAltair()
|
|
|
|
return
|
|
|
|
}
|
2020-06-22 20:37:48 +00:00
|
|
|
s.registerRPC(
|
2021-05-15 00:58:56 +00:00
|
|
|
p2p.RPCStatusTopicV1,
|
2020-06-22 20:37:48 +00:00
|
|
|
s.statusRPCHandler,
|
2019-08-16 17:13:04 +00:00
|
|
|
)
|
2020-06-22 20:37:48 +00:00
|
|
|
s.registerRPC(
|
2021-05-15 00:58:56 +00:00
|
|
|
p2p.RPCGoodByeTopicV1,
|
2020-06-22 20:37:48 +00:00
|
|
|
s.goodbyeRPCHandler,
|
2019-08-16 17:13:04 +00:00
|
|
|
)
|
2020-06-22 20:37:48 +00:00
|
|
|
s.registerRPC(
|
2021-05-15 00:58:56 +00:00
|
|
|
p2p.RPCBlocksByRangeTopicV1,
|
2020-06-22 20:37:48 +00:00
|
|
|
s.beaconBlocksByRangeRPCHandler,
|
2019-08-20 19:06:49 +00:00
|
|
|
)
|
2020-06-22 20:37:48 +00:00
|
|
|
s.registerRPC(
|
2021-05-15 00:58:56 +00:00
|
|
|
p2p.RPCBlocksByRootTopicV1,
|
2020-06-22 20:37:48 +00:00
|
|
|
s.beaconBlocksRootRPCHandler,
|
2019-08-16 17:13:04 +00:00
|
|
|
)
|
2020-06-22 20:37:48 +00:00
|
|
|
s.registerRPC(
|
2021-05-15 00:58:56 +00:00
|
|
|
p2p.RPCPingTopicV1,
|
2020-06-22 20:37:48 +00:00
|
|
|
s.pingHandler,
|
2020-04-14 20:27:03 +00:00
|
|
|
)
|
2020-06-22 20:37:48 +00:00
|
|
|
s.registerRPC(
|
2021-05-15 00:58:56 +00:00
|
|
|
p2p.RPCMetaDataTopicV1,
|
2020-06-22 20:37:48 +00:00
|
|
|
s.metaDataHandler,
|
2020-04-14 20:27:03 +00:00
|
|
|
)
|
2019-08-16 17:13:04 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 03:52:56 +00:00
|
|
|
// registerRPCHandlers for altair.
|
|
|
|
func (s *Service) registerRPCHandlersAltair() {
|
|
|
|
s.registerRPC(
|
|
|
|
p2p.RPCBlocksByRangeTopicV2,
|
|
|
|
s.beaconBlocksByRangeRPCHandler,
|
|
|
|
)
|
|
|
|
s.registerRPC(
|
|
|
|
p2p.RPCBlocksByRootTopicV2,
|
|
|
|
s.beaconBlocksRootRPCHandler,
|
|
|
|
)
|
|
|
|
s.registerRPC(
|
|
|
|
p2p.RPCMetaDataTopicV2,
|
|
|
|
s.metaDataHandler,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all v1 Stream handlers that are no longer supported
|
|
|
|
// from altair onwards.
|
|
|
|
func (s *Service) unregisterPhase0Handlers() {
|
|
|
|
fullBlockRangeTopic := p2p.RPCBlocksByRangeTopicV1 + s.cfg.P2P.Encoding().ProtocolSuffix()
|
|
|
|
fullBlockRootTopic := p2p.RPCBlocksByRootTopicV1 + s.cfg.P2P.Encoding().ProtocolSuffix()
|
|
|
|
fullMetadataTopic := p2p.RPCMetaDataTopicV1 + s.cfg.P2P.Encoding().ProtocolSuffix()
|
|
|
|
|
|
|
|
s.cfg.P2P.Host().RemoveStreamHandler(protocol.ID(fullBlockRangeTopic))
|
|
|
|
s.cfg.P2P.Host().RemoveStreamHandler(protocol.ID(fullBlockRootTopic))
|
|
|
|
s.cfg.P2P.Host().RemoveStreamHandler(protocol.ID(fullMetadataTopic))
|
|
|
|
}
|
|
|
|
|
2019-08-16 17:13:04 +00:00
|
|
|
// registerRPC for a given topic with an expected protobuf message type.
|
2020-07-13 01:20:53 +00:00
|
|
|
func (s *Service) registerRPC(baseTopic string, handle rpcHandler) {
|
2021-03-21 19:07:42 +00:00
|
|
|
topic := baseTopic + s.cfg.P2P.Encoding().ProtocolSuffix()
|
2019-08-16 17:13:04 +00:00
|
|
|
log := log.WithField("topic", topic)
|
2021-03-21 19:07:42 +00:00
|
|
|
s.cfg.P2P.SetStreamHandler(topic, func(stream network.Stream) {
|
2021-09-08 15:16:53 +00:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
log.WithField("error", r).Error("Panic occurred")
|
|
|
|
log.Errorf("%s", debug.Stack())
|
|
|
|
}
|
|
|
|
}()
|
2020-09-09 09:48:52 +00:00
|
|
|
ctx, cancel := context.WithTimeout(s.ctx, ttfbTimeout)
|
2019-08-16 17:13:04 +00:00
|
|
|
defer cancel()
|
2020-12-14 17:22:25 +00:00
|
|
|
|
|
|
|
// Resetting after closing is a no-op so defer a reset in case something goes wrong.
|
|
|
|
// It's up to the handler to Close the stream (send an EOF) if
|
|
|
|
// it successfully writes a response. We don't blindly call
|
|
|
|
// Close here because we may have only written a partial
|
|
|
|
// response.
|
2020-04-13 04:11:09 +00:00
|
|
|
defer func() {
|
2020-12-14 17:22:25 +00:00
|
|
|
_err := stream.Reset()
|
|
|
|
_ = _err
|
2020-04-13 04:11:09 +00:00
|
|
|
}()
|
2020-12-14 17:22:25 +00:00
|
|
|
|
2019-09-03 18:06:35 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "sync.rpc")
|
|
|
|
defer span.End()
|
|
|
|
span.AddAttributes(trace.StringAttribute("topic", topic))
|
2019-10-24 01:35:08 +00:00
|
|
|
span.AddAttributes(trace.StringAttribute("peer", stream.Conn().RemotePeer().Pretty()))
|
2021-02-16 07:45:34 +00:00
|
|
|
log := log.WithField("peer", stream.Conn().RemotePeer().Pretty()).WithField("topic", string(stream.Protocol()))
|
|
|
|
|
2020-11-13 12:58:13 +00:00
|
|
|
// Check before hand that peer is valid.
|
2021-03-21 19:07:42 +00:00
|
|
|
if s.cfg.P2P.Peers().IsBad(stream.Conn().RemotePeer()) {
|
2020-11-18 04:17:42 +00:00
|
|
|
if err := s.sendGoodByeAndDisconnect(ctx, p2ptypes.GoodbyeCodeBanned, stream.Conn().RemotePeer()); err != nil {
|
2020-11-13 12:58:13 +00:00
|
|
|
log.Debugf("Could not disconnect from peer: %v", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2021-02-16 07:45:34 +00:00
|
|
|
// Validate request according to peer limits.
|
|
|
|
if err := s.rateLimiter.validateRawRpcRequest(stream); err != nil {
|
|
|
|
log.Debugf("Could not validate rpc request from peer: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.rateLimiter.addRawStream(stream)
|
|
|
|
|
2021-09-15 00:09:04 +00:00
|
|
|
if err := stream.SetReadDeadline(time.Now().Add(ttfbTimeout)); err != nil {
|
2020-08-04 20:30:40 +00:00
|
|
|
log.WithError(err).Debug("Could not set stream read deadline")
|
2019-08-16 17:13:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-13 01:20:53 +00:00
|
|
|
base, ok := p2p.RPCTopicMappings[baseTopic]
|
|
|
|
if !ok {
|
2020-11-23 18:29:27 +00:00
|
|
|
log.Errorf("Could not retrieve base message for topic %s", baseTopic)
|
2020-07-13 01:20:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
t := reflect.TypeOf(base)
|
|
|
|
// Copy Base
|
|
|
|
base = reflect.New(t)
|
|
|
|
|
2020-01-14 17:02:50 +00:00
|
|
|
// Increment message received counter.
|
|
|
|
messageReceivedCounter.WithLabelValues(topic).Inc()
|
|
|
|
|
2020-04-14 20:27:03 +00:00
|
|
|
// since metadata requests do not have any data in the payload, we
|
|
|
|
// do not decode anything.
|
2021-09-10 03:52:56 +00:00
|
|
|
if baseTopic == p2p.RPCMetaDataTopicV1 || baseTopic == p2p.RPCMetaDataTopicV2 {
|
2020-07-13 01:20:53 +00:00
|
|
|
if err := handle(ctx, base, stream); err != nil {
|
2020-04-14 20:27:03 +00:00
|
|
|
messageFailedProcessingCounter.WithLabelValues(topic).Inc()
|
2020-11-18 04:17:42 +00:00
|
|
|
if err != p2ptypes.ErrWrongForkDigestVersion {
|
2020-11-22 20:31:55 +00:00
|
|
|
log.WithError(err).Debug("Could not handle p2p RPC")
|
2020-04-14 20:27:03 +00:00
|
|
|
}
|
2021-09-14 20:59:51 +00:00
|
|
|
tracing.AnnotateError(span, err)
|
2020-04-14 20:27:03 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-13 01:20:53 +00:00
|
|
|
// Given we have an input argument that can be pointer or the actual object, this gives us
|
2019-09-18 20:48:16 +00:00
|
|
|
// a way to check for its reflect.Kind and based on the result, we can decode
|
|
|
|
// accordingly.
|
|
|
|
if t.Kind() == reflect.Ptr {
|
2021-09-08 15:16:53 +00:00
|
|
|
msg, ok := reflect.New(t.Elem()).Interface().(ssz.Unmarshaler)
|
|
|
|
if !ok {
|
|
|
|
log.Errorf("message of %T does not support marshaller interface", msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := s.cfg.P2P.Encoding().DecodeWithMaxLength(stream, msg); err != nil {
|
2020-04-24 04:10:53 +00:00
|
|
|
// Debug logs for goodbye/status errors
|
2021-05-15 00:58:56 +00:00
|
|
|
if strings.Contains(topic, p2p.RPCGoodByeTopicV1) || strings.Contains(topic, p2p.RPCStatusTopicV1) {
|
2020-11-22 20:31:55 +00:00
|
|
|
log.WithError(err).Debug("Could not decode goodbye stream message")
|
2021-09-14 20:59:51 +00:00
|
|
|
tracing.AnnotateError(span, err)
|
2020-04-24 04:10:53 +00:00
|
|
|
return
|
|
|
|
}
|
2020-11-22 20:31:55 +00:00
|
|
|
log.WithError(err).Debug("Could not decode stream message")
|
2021-09-14 20:59:51 +00:00
|
|
|
tracing.AnnotateError(span, err)
|
2019-09-18 20:48:16 +00:00
|
|
|
return
|
|
|
|
}
|
2021-09-08 15:16:53 +00:00
|
|
|
if err := handle(ctx, msg, stream); err != nil {
|
2019-09-30 05:23:19 +00:00
|
|
|
messageFailedProcessingCounter.WithLabelValues(topic).Inc()
|
2020-11-18 04:17:42 +00:00
|
|
|
if err != p2ptypes.ErrWrongForkDigestVersion {
|
2020-11-22 20:31:55 +00:00
|
|
|
log.WithError(err).Debug("Could not handle p2p RPC")
|
2020-01-08 19:18:21 +00:00
|
|
|
}
|
2021-09-14 20:59:51 +00:00
|
|
|
tracing.AnnotateError(span, err)
|
2019-09-18 20:48:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-09-08 15:16:53 +00:00
|
|
|
nTyp := reflect.New(t)
|
|
|
|
msg, ok := nTyp.Interface().(ssz.Unmarshaler)
|
|
|
|
if !ok {
|
|
|
|
log.Errorf("message of %T does not support marshaller interface", msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := s.cfg.P2P.Encoding().DecodeWithMaxLength(stream, msg); err != nil {
|
2020-11-22 20:31:55 +00:00
|
|
|
log.WithError(err).Debug("Could not decode stream message")
|
2021-09-14 20:59:51 +00:00
|
|
|
tracing.AnnotateError(span, err)
|
2019-09-18 20:48:16 +00:00
|
|
|
return
|
|
|
|
}
|
2021-09-08 15:16:53 +00:00
|
|
|
if err := handle(ctx, nTyp.Elem().Interface(), stream); err != nil {
|
2019-09-30 05:23:19 +00:00
|
|
|
messageFailedProcessingCounter.WithLabelValues(topic).Inc()
|
2020-11-18 04:17:42 +00:00
|
|
|
if err != p2ptypes.ErrWrongForkDigestVersion {
|
2020-11-22 20:31:55 +00:00
|
|
|
log.WithError(err).Debug("Could not handle p2p RPC")
|
2020-01-08 19:18:21 +00:00
|
|
|
}
|
2021-09-14 20:59:51 +00:00
|
|
|
tracing.AnnotateError(span, err)
|
2019-09-18 20:48:16 +00:00
|
|
|
}
|
2019-08-16 17:13:04 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|