mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 04:47:18 +00:00
2428880058
* Update go-libp2p to 0.12.0 go-libp2p 0.12.0 made some significant changes to the stream interfaces around stream closing: * Close now closes in both directions and frees the stream. However, unlike FullClose did, it doesn't _wait_ for the remote peer to respond with an EOF. * To close for writing, call CloseWrite (like one would on a TCP connection, etc.). This patch: * Replaces calls to FullClose with Close where appropriate. * Replaces calls to Close with CloseWrite where appropriate. * Removes redundant Close calls. * Calls Reset to where appropriate to indicate that the request/response was aborted. Unlike Close, this will not flush and will not cause the remote peer to read an EOF. Instead, the remote peer will read an ErrReset error. * Ensures we always either close or reset streams. Send wasn't closing the stream on some error paths. * Now that stream closing is async, we explicitly wait for a response when "hanging up" on a peer (so we don't hang up before they receive our response/goodbye message). * update bazel * Gazelle * revert unintentional bazel workspace change * appease an overzealous linter * update to latest * Refactor encoder * gazelle * Gazelle Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> Co-authored-by: Nishant Das <nishdas93@gmail.com> Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
137 lines
4.0 KiB
Go
137 lines
4.0 KiB
Go
package sync
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
libp2pcore "github.com/libp2p/go-libp2p-core"
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/types"
|
|
"github.com/prysmaticlabs/prysm/shared/timeutils"
|
|
)
|
|
|
|
// pingHandler reads the incoming ping rpc message from the peer.
|
|
func (s *Service) pingHandler(_ context.Context, msg interface{}, stream libp2pcore.Stream) error {
|
|
SetRPCStreamDeadlines(stream)
|
|
|
|
m, ok := msg.(*types.SSZUint64)
|
|
if !ok {
|
|
return fmt.Errorf("wrong message type for ping, got %T, wanted *uint64", msg)
|
|
}
|
|
if err := s.rateLimiter.validateRequest(stream, 1); err != nil {
|
|
return err
|
|
}
|
|
s.rateLimiter.add(stream, 1)
|
|
valid, err := s.validateSequenceNum(*m, stream.Conn().RemotePeer())
|
|
if err != nil {
|
|
// Descore peer for giving us a bad sequence number.
|
|
if errors.Is(err, types.ErrInvalidSequenceNum) {
|
|
s.p2p.Peers().Scorers().BadResponsesScorer().Increment(stream.Conn().RemotePeer())
|
|
s.writeErrorResponseToStream(responseCodeInvalidRequest, types.ErrInvalidSequenceNum.Error(), stream)
|
|
}
|
|
return err
|
|
}
|
|
if _, err := stream.Write([]byte{responseCodeSuccess}); err != nil {
|
|
return err
|
|
}
|
|
sq := types.SSZUint64(s.p2p.MetadataSeq())
|
|
if _, err := s.p2p.Encoding().EncodeWithMaxLength(stream, &sq); err != nil {
|
|
return err
|
|
}
|
|
|
|
closeStream(stream, log)
|
|
|
|
if valid {
|
|
// If the sequence number was valid we're done.
|
|
return nil
|
|
}
|
|
|
|
// The sequence number was not valid. Start our own ping back to the peer.
|
|
go func() {
|
|
// New context so the calling function doesn't cancel on us.
|
|
ctx, cancel := context.WithTimeout(context.Background(), ttfbTimeout)
|
|
defer cancel()
|
|
md, err := s.sendMetaDataRequest(ctx, stream.Conn().RemotePeer())
|
|
if err != nil {
|
|
// We cannot compare errors directly as the stream muxer error
|
|
// type isn't compatible with the error we have, so a direct
|
|
// equality checks fails.
|
|
if !strings.Contains(err.Error(), types.ErrIODeadline.Error()) {
|
|
log.WithField("peer", stream.Conn().RemotePeer()).WithError(err).Debug("Could not send metadata request")
|
|
}
|
|
return
|
|
}
|
|
// update metadata if there is no error
|
|
s.p2p.Peers().SetMetadata(stream.Conn().RemotePeer(), md)
|
|
}()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) sendPingRequest(ctx context.Context, id peer.ID) error {
|
|
ctx, cancel := context.WithTimeout(ctx, respTimeout)
|
|
defer cancel()
|
|
|
|
metadataSeq := types.SSZUint64(s.p2p.MetadataSeq())
|
|
stream, err := s.p2p.Send(ctx, &metadataSeq, p2p.RPCPingTopic, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
currentTime := timeutils.Now()
|
|
defer closeStream(stream, log)
|
|
|
|
code, errMsg, err := ReadStatusCode(stream, s.p2p.Encoding())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// Records the latency of the ping request for that peer.
|
|
s.p2p.Host().Peerstore().RecordLatency(id, timeutils.Now().Sub(currentTime))
|
|
|
|
if code != 0 {
|
|
s.p2p.Peers().Scorers().BadResponsesScorer().Increment(stream.Conn().RemotePeer())
|
|
return errors.New(errMsg)
|
|
}
|
|
msg := new(types.SSZUint64)
|
|
if err := s.p2p.Encoding().DecodeWithMaxLength(stream, msg); err != nil {
|
|
return err
|
|
}
|
|
valid, err := s.validateSequenceNum(*msg, stream.Conn().RemotePeer())
|
|
if err != nil {
|
|
// Descore peer for giving us a bad sequence number.
|
|
if errors.Is(err, types.ErrInvalidSequenceNum) {
|
|
s.p2p.Peers().Scorers().BadResponsesScorer().Increment(stream.Conn().RemotePeer())
|
|
}
|
|
return err
|
|
}
|
|
if valid {
|
|
return nil
|
|
}
|
|
md, err := s.sendMetaDataRequest(ctx, stream.Conn().RemotePeer())
|
|
if err != nil {
|
|
// do not increment bad responses, as its
|
|
// already done in the request method.
|
|
return err
|
|
}
|
|
s.p2p.Peers().SetMetadata(stream.Conn().RemotePeer(), md)
|
|
return nil
|
|
}
|
|
|
|
// validates the peer's sequence number.
|
|
func (s *Service) validateSequenceNum(seq types.SSZUint64, id peer.ID) (bool, error) {
|
|
md, err := s.p2p.Peers().Metadata(id)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if md == nil {
|
|
return false, nil
|
|
}
|
|
// Return error on invalid sequence number.
|
|
if md.SeqNumber > uint64(seq) {
|
|
return false, types.ErrInvalidSequenceNum
|
|
}
|
|
return md.SeqNumber == uint64(seq), nil
|
|
}
|