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"
|
2023-12-13 11:01:01 +00:00
|
|
|
"strings"
|
2023-12-19 14:59:30 +00:00
|
|
|
"time"
|
2019-08-16 17:13:04 +00:00
|
|
|
|
2022-10-07 07:24:51 +00:00
|
|
|
libp2pcore "github.com/libp2p/go-libp2p/core"
|
|
|
|
"github.com/libp2p/go-libp2p/core/network"
|
|
|
|
"github.com/libp2p/go-libp2p/core/protocol"
|
2022-06-28 13:03:24 +00:00
|
|
|
ssz "github.com/prysmaticlabs/fastssz"
|
2024-02-15 05:46:47 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v5/beacon-chain/p2p"
|
|
|
|
p2ptypes "github.com/prysmaticlabs/prysm/v5/beacon-chain/p2p/types"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/config/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/monitoring/tracing"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/time/slots"
|
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.
|
2023-12-19 14:59:30 +00:00
|
|
|
var ttfbTimeout = params.BeaconConfig().TtfbTimeoutDuration()
|
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.
|
2023-12-19 14:59:30 +00:00
|
|
|
var respTimeout = params.BeaconConfig().RespTimeoutDuration()
|
2020-07-01 09:47:59 +00:00
|
|
|
|
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() {
|
2023-05-03 04:34:01 +00:00
|
|
|
currEpoch := slots.ToEpoch(s.cfg.clock.CurrentSlot())
|
2021-09-10 03:52:56 +00:00
|
|
|
// 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()
|
2023-06-15 19:38:08 +00:00
|
|
|
if currEpoch >= params.BeaconConfig().DenebForkEpoch {
|
|
|
|
s.registerRPCHandlersDeneb()
|
|
|
|
}
|
2021-09-10 03:52:56 +00:00
|
|
|
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,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-06-15 19:38:08 +00:00
|
|
|
func (s *Service) registerRPCHandlersDeneb() {
|
|
|
|
s.registerRPC(
|
|
|
|
p2p.RPCBlobSidecarsByRangeTopicV1,
|
|
|
|
s.blobSidecarsByRangeRPCHandler,
|
|
|
|
)
|
|
|
|
s.registerRPC(
|
|
|
|
p2p.RPCBlobSidecarsByRootTopicV1,
|
|
|
|
s.blobSidecarByRootRPCHandler,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-09-10 03:52:56 +00:00
|
|
|
// Remove all v1 Stream handlers that are no longer supported
|
|
|
|
// from altair onwards.
|
|
|
|
func (s *Service) unregisterPhase0Handlers() {
|
2021-11-05 19:08:58 +00:00
|
|
|
fullBlockRangeTopic := p2p.RPCBlocksByRangeTopicV1 + s.cfg.p2p.Encoding().ProtocolSuffix()
|
|
|
|
fullBlockRootTopic := p2p.RPCBlocksByRootTopicV1 + s.cfg.p2p.Encoding().ProtocolSuffix()
|
|
|
|
fullMetadataTopic := p2p.RPCMetaDataTopicV1 + s.cfg.p2p.Encoding().ProtocolSuffix()
|
2021-09-10 03:52:56 +00:00
|
|
|
|
2021-11-05 19:08:58 +00:00
|
|
|
s.cfg.p2p.Host().RemoveStreamHandler(protocol.ID(fullBlockRangeTopic))
|
|
|
|
s.cfg.p2p.Host().RemoveStreamHandler(protocol.ID(fullBlockRootTopic))
|
|
|
|
s.cfg.p2p.Host().RemoveStreamHandler(protocol.ID(fullMetadataTopic))
|
2021-09-10 03:52:56 +00:00
|
|
|
}
|
|
|
|
|
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-11-05 19:08:58 +00:00
|
|
|
topic := baseTopic + s.cfg.p2p.Encoding().ProtocolSuffix()
|
2019-08-16 17:13:04 +00:00
|
|
|
log := log.WithField("topic", topic)
|
2021-11-05 19:08:58 +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 {
|
2023-07-10 19:02:44 +00:00
|
|
|
log.WithField("error", r).
|
2024-02-22 22:40:36 +00:00
|
|
|
WithField("recoveredAt", "registerRPC").
|
2023-07-10 19:02:44 +00:00
|
|
|
WithField("stack", string(debug.Stack())).
|
|
|
|
Error("Panic occurred")
|
2021-09-08 15:16:53 +00:00
|
|
|
}
|
|
|
|
}()
|
2023-07-10 19:02:44 +00:00
|
|
|
|
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))
|
2023-12-21 16:09:54 +00:00
|
|
|
span.AddAttributes(trace.StringAttribute("peer", stream.Conn().RemotePeer().String()))
|
|
|
|
log := log.WithField("peer", stream.Conn().RemotePeer().String()).WithField("topic", string(stream.Protocol()))
|
2021-02-16 07:45:34 +00:00
|
|
|
|
2020-11-13 12:58:13 +00:00
|
|
|
// Check before hand that peer is valid.
|
2021-11-05 19:08:58 +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 {
|
2022-08-05 10:52:02 +00:00
|
|
|
log.WithError(err).Debug("Could not disconnect from peer")
|
2020-11-13 12:58:13 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2021-02-16 07:45:34 +00:00
|
|
|
// Validate request according to peer limits.
|
|
|
|
if err := s.rateLimiter.validateRawRpcRequest(stream); err != nil {
|
2022-08-05 10:52:02 +00:00
|
|
|
log.WithError(err).Debug("Could not validate rpc request from peer")
|
2021-02-16 07:45:34 +00:00
|
|
|
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
|
|
|
|
}
|
2021-11-05 19:08:58 +00:00
|
|
|
if err := s.cfg.p2p.Encoding().DecodeWithMaxLength(stream, msg); err != nil {
|
2023-12-13 11:01:01 +00:00
|
|
|
logStreamErrors(err, topic)
|
2021-09-14 20:59:51 +00:00
|
|
|
tracing.AnnotateError(span, err)
|
2022-07-28 13:30:47 +00:00
|
|
|
s.cfg.p2p.Peers().Scorers().BadResponsesScorer().Increment(stream.Conn().RemotePeer())
|
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
|
|
|
|
}
|
2021-11-05 19:08:58 +00:00
|
|
|
if err := s.cfg.p2p.Encoding().DecodeWithMaxLength(stream, msg); err != nil {
|
2023-12-13 11:01:01 +00:00
|
|
|
logStreamErrors(err, topic)
|
2021-09-14 20:59:51 +00:00
|
|
|
tracing.AnnotateError(span, err)
|
2022-07-28 13:30:47 +00:00
|
|
|
s.cfg.p2p.Peers().Scorers().BadResponsesScorer().Increment(stream.Conn().RemotePeer())
|
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
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2023-12-13 11:01:01 +00:00
|
|
|
|
|
|
|
func logStreamErrors(err error, topic string) {
|
2024-02-09 05:13:57 +00:00
|
|
|
if isUnwantedError(err) {
|
|
|
|
return
|
|
|
|
}
|
2023-12-13 11:01:01 +00:00
|
|
|
if strings.Contains(topic, p2p.RPCGoodByeTopicV1) {
|
|
|
|
log.WithError(err).WithField("topic", topic).Trace("Could not decode goodbye stream message")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.WithError(err).WithField("topic", topic).Debug("Could not decode stream message")
|
|
|
|
}
|