2019-08-16 17:13:04 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-09-18 20:48:16 +00:00
|
|
|
"reflect"
|
2019-08-16 17:13:04 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
libp2pcore "github.com/libp2p/go-libp2p-core"
|
|
|
|
"github.com/libp2p/go-libp2p-core/network"
|
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/roughtime"
|
2019-10-24 01:35:08 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/traceutil"
|
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.
|
2019-09-24 14:56:50 +00:00
|
|
|
const ttfbTimeout = 5 * time.Second
|
|
|
|
|
|
|
|
// maxChunkSize would be the maximum allowed size that a request/response chunk can be.
|
|
|
|
// any size beyond that would be rejected and the corresponding stream reset. This would
|
|
|
|
// be 1048576 bytes or 1 MiB.
|
|
|
|
const maxChunkSize = 1 << 20
|
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.
|
2019-12-17 01:53:55 +00:00
|
|
|
func (r *Service) registerRPCHandlers() {
|
2019-08-16 17:13:04 +00:00
|
|
|
r.registerRPC(
|
2019-09-20 06:27:28 +00:00
|
|
|
"/eth2/beacon_chain/req/status/1",
|
|
|
|
&pb.Status{},
|
|
|
|
r.statusRPCHandler,
|
2019-08-16 17:13:04 +00:00
|
|
|
)
|
|
|
|
r.registerRPC(
|
|
|
|
"/eth2/beacon_chain/req/goodbye/1",
|
2019-09-20 06:27:28 +00:00
|
|
|
new(uint64),
|
2019-08-23 16:53:38 +00:00
|
|
|
r.goodbyeRPCHandler,
|
2019-08-16 17:13:04 +00:00
|
|
|
)
|
2019-08-20 19:06:49 +00:00
|
|
|
r.registerRPC(
|
2019-09-20 06:27:28 +00:00
|
|
|
"/eth2/beacon_chain/req/beacon_blocks_by_range/1",
|
|
|
|
&pb.BeaconBlocksByRangeRequest{},
|
|
|
|
r.beaconBlocksByRangeRPCHandler,
|
2019-08-20 19:06:49 +00:00
|
|
|
)
|
2019-08-16 17:13:04 +00:00
|
|
|
r.registerRPC(
|
2019-09-20 06:27:28 +00:00
|
|
|
"/eth2/beacon_chain/req/beacon_blocks_by_root/1",
|
2019-09-18 20:48:16 +00:00
|
|
|
[][32]byte{},
|
2019-09-20 06:27:28 +00:00
|
|
|
r.beaconBlocksRootRPCHandler,
|
2019-08-16 17:13:04 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// registerRPC for a given topic with an expected protobuf message type.
|
2019-12-17 01:53:55 +00:00
|
|
|
func (r *Service) registerRPC(topic string, base interface{}, handle rpcHandler) {
|
2019-08-16 17:13:04 +00:00
|
|
|
topic += r.p2p.Encoding().ProtocolSuffix()
|
|
|
|
log := log.WithField("topic", topic)
|
|
|
|
r.p2p.SetStreamHandler(topic, func(stream network.Stream) {
|
2019-09-03 18:06:35 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), ttfbTimeout)
|
2019-08-16 17:13:04 +00:00
|
|
|
defer cancel()
|
|
|
|
defer stream.Close()
|
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()))
|
|
|
|
log := log.WithField("peer", stream.Conn().RemotePeer().Pretty())
|
2019-08-16 17:13:04 +00:00
|
|
|
|
|
|
|
if err := stream.SetReadDeadline(roughtime.Now().Add(ttfbTimeout)); err != nil {
|
|
|
|
log.WithError(err).Error("Could not set stream read deadline")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-14 17:02:50 +00:00
|
|
|
// Increment message received counter.
|
|
|
|
messageReceivedCounter.WithLabelValues(topic).Inc()
|
|
|
|
|
2019-09-18 20:48:16 +00:00
|
|
|
// Given we have an input argument that can be pointer or [][32]byte, this gives us
|
|
|
|
// a way to check for its reflect.Kind and based on the result, we can decode
|
|
|
|
// accordingly.
|
|
|
|
t := reflect.TypeOf(base)
|
|
|
|
if t.Kind() == reflect.Ptr {
|
|
|
|
msg := reflect.New(t.Elem())
|
|
|
|
if err := r.p2p.Encoding().DecodeWithLength(stream, msg.Interface()); err != nil {
|
|
|
|
log.WithError(err).Error("Failed to decode stream message")
|
2019-10-24 01:35:08 +00:00
|
|
|
traceutil.AnnotateError(span, err)
|
2019-09-18 20:48:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := handle(ctx, msg.Interface(), stream); err != nil {
|
2019-09-30 05:23:19 +00:00
|
|
|
messageFailedProcessingCounter.WithLabelValues(topic).Inc()
|
2020-01-08 19:18:21 +00:00
|
|
|
if err != errWrongForkVersion {
|
|
|
|
log.WithError(err).Error("Failed to handle p2p RPC")
|
|
|
|
}
|
2019-10-24 01:35:08 +00:00
|
|
|
traceutil.AnnotateError(span, err)
|
2019-09-18 20:48:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
msg := reflect.New(t)
|
|
|
|
if err := r.p2p.Encoding().DecodeWithLength(stream, msg.Interface()); err != nil {
|
|
|
|
log.WithError(err).Error("Failed to decode stream message")
|
2019-10-24 01:35:08 +00:00
|
|
|
traceutil.AnnotateError(span, err)
|
2019-09-18 20:48:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := handle(ctx, msg.Elem().Interface(), stream); err != nil {
|
2019-09-30 05:23:19 +00:00
|
|
|
messageFailedProcessingCounter.WithLabelValues(topic).Inc()
|
2020-01-08 19:18:21 +00:00
|
|
|
if err != errWrongForkVersion {
|
|
|
|
log.WithError(err).Error("Failed to handle p2p RPC")
|
|
|
|
}
|
2019-10-24 01:35:08 +00:00
|
|
|
traceutil.AnnotateError(span, err)
|
2019-09-18 20:48:16 +00:00
|
|
|
}
|
2019-08-16 17:13:04 +00:00
|
|
|
}
|
2019-09-20 17:08:32 +00:00
|
|
|
|
2019-08-16 17:13:04 +00:00
|
|
|
})
|
|
|
|
}
|