2019-08-23 16:53:38 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-09-20 06:27:28 +00:00
|
|
|
"fmt"
|
2020-07-03 15:25:32 +00:00
|
|
|
"time"
|
2019-08-23 16:53:38 +00:00
|
|
|
|
|
|
|
libp2pcore "github.com/libp2p/go-libp2p-core"
|
2020-07-03 08:21:18 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/helpers"
|
2020-07-03 10:08:45 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/mux"
|
2020-04-23 19:46:24 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
|
2020-05-14 14:44:03 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-08-23 16:53:38 +00:00
|
|
|
)
|
|
|
|
|
2019-09-20 06:27:28 +00:00
|
|
|
const (
|
|
|
|
codeClientShutdown uint64 = iota
|
|
|
|
codeWrongNetwork
|
|
|
|
codeGenericError
|
|
|
|
)
|
|
|
|
|
|
|
|
var goodByes = map[uint64]string{
|
|
|
|
codeClientShutdown: "client shutdown",
|
|
|
|
codeWrongNetwork: "irrelevant network",
|
|
|
|
codeGenericError: "fault/error",
|
|
|
|
}
|
|
|
|
|
2020-07-03 15:25:32 +00:00
|
|
|
// Add a short delay to allow the stream to flush before resetting it.
|
|
|
|
// There is still a chance that the peer won't receive the message.
|
|
|
|
const flushDelay = 50 * time.Millisecond
|
|
|
|
|
2019-08-23 16:53:38 +00:00
|
|
|
// goodbyeRPCHandler reads the incoming goodbye rpc message from the peer.
|
2020-10-12 08:11:05 +00:00
|
|
|
func (s *Service) goodbyeRPCHandler(_ context.Context, msg interface{}, stream libp2pcore.Stream) error {
|
2020-04-13 04:11:09 +00:00
|
|
|
defer func() {
|
|
|
|
if err := stream.Close(); err != nil {
|
|
|
|
log.WithError(err).Error("Failed to close stream")
|
|
|
|
}
|
|
|
|
}()
|
2020-06-25 22:36:18 +00:00
|
|
|
SetRPCStreamDeadlines(stream)
|
2019-08-23 16:53:38 +00:00
|
|
|
|
2020-02-11 15:08:02 +00:00
|
|
|
m, ok := msg.(*uint64)
|
2020-02-09 08:41:50 +00:00
|
|
|
if !ok {
|
2020-02-11 15:08:02 +00:00
|
|
|
return fmt.Errorf("wrong message type for goodbye, got %T, wanted *uint64", msg)
|
2020-02-09 08:41:50 +00:00
|
|
|
}
|
2020-07-17 08:58:51 +00:00
|
|
|
if err := s.rateLimiter.validateRequest(stream, 1); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.rateLimiter.add(stream, 1)
|
2020-02-11 15:08:02 +00:00
|
|
|
log := log.WithField("Reason", goodbyeMessage(*m))
|
2020-05-11 04:29:23 +00:00
|
|
|
log.WithField("peer", stream.Conn().RemotePeer()).Debug("Peer has sent a goodbye message")
|
2019-08-23 16:53:38 +00:00
|
|
|
// closes all streams with the peer
|
2020-06-22 20:37:48 +00:00
|
|
|
return s.p2p.Disconnect(stream.Conn().RemotePeer())
|
2019-08-23 16:53:38 +00:00
|
|
|
}
|
2019-09-20 06:27:28 +00:00
|
|
|
|
2020-06-22 20:37:48 +00:00
|
|
|
func (s *Service) sendGoodByeAndDisconnect(ctx context.Context, code uint64, id peer.ID) error {
|
|
|
|
if err := s.sendGoodByeMessage(ctx, code, id); err != nil {
|
2020-05-14 14:44:03 +00:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"error": err,
|
|
|
|
"peer": id,
|
|
|
|
}).Debug("Could not send goodbye message to peer")
|
|
|
|
}
|
2020-10-09 23:39:23 +00:00
|
|
|
return s.p2p.Disconnect(id)
|
2020-05-14 14:44:03 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 20:37:48 +00:00
|
|
|
func (s *Service) sendGoodByeMessage(ctx context.Context, code uint64, id peer.ID) error {
|
2020-07-01 09:47:59 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, respTimeout)
|
2020-04-23 19:46:24 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2020-06-22 20:37:48 +00:00
|
|
|
stream, err := s.p2p.Send(ctx, &code, p2p.RPCGoodByeTopic, id)
|
2020-04-23 19:46:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-01 08:14:47 +00:00
|
|
|
defer func() {
|
2020-07-03 10:08:45 +00:00
|
|
|
if err := helpers.FullClose(stream); err != nil && err.Error() != mux.ErrReset.Error() {
|
2020-07-03 08:21:18 +00:00
|
|
|
log.WithError(err).Debugf("Failed to reset stream with protocol %s", stream.Protocol())
|
2020-06-01 08:14:47 +00:00
|
|
|
}
|
|
|
|
}()
|
2020-04-23 19:46:24 +00:00
|
|
|
log := log.WithField("Reason", goodbyeMessage(code))
|
|
|
|
log.WithField("peer", stream.Conn().RemotePeer()).Debug("Sending Goodbye message to peer")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-20 06:27:28 +00:00
|
|
|
func goodbyeMessage(num uint64) string {
|
|
|
|
reason, ok := goodByes[num]
|
|
|
|
if ok {
|
|
|
|
return reason
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("unknown goodbye value of %d Received", num)
|
|
|
|
}
|