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"
|
2021-02-16 07:45:34 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/network"
|
2020-04-23 19:46:24 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2021-02-25 13:51:26 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2021-09-09 15:12:49 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core"
|
2020-04-23 19:46:24 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
|
2021-02-25 13:51:26 +00:00
|
|
|
p2ptypes "github.com/prysmaticlabs/prysm/beacon-chain/p2p/types"
|
2021-02-16 07:45:34 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/mputil"
|
2020-05-14 14:44:03 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-08-23 16:53:38 +00:00
|
|
|
)
|
|
|
|
|
2020-11-05 05:52:49 +00:00
|
|
|
var backOffTime = map[types.SSZUint64]time.Duration{
|
|
|
|
// Do not dial peers which are from a different/unverifiable
|
|
|
|
// network.
|
2021-02-25 13:51:26 +00:00
|
|
|
p2ptypes.GoodbyeCodeWrongNetwork: 24 * time.Hour,
|
|
|
|
p2ptypes.GoodbyeCodeUnableToVerifyNetwork: 24 * time.Hour,
|
2020-11-05 05:52:49 +00:00
|
|
|
// If local peer is banned, we back off for
|
|
|
|
// 2 hours to let the remote peer score us
|
|
|
|
// back up again.
|
2021-02-25 13:51:26 +00:00
|
|
|
p2ptypes.GoodbyeCodeBadScore: 2 * time.Hour,
|
|
|
|
p2ptypes.GoodbyeCodeBanned: 2 * time.Hour,
|
|
|
|
p2ptypes.GoodbyeCodeClientShutdown: 1 * time.Hour,
|
2020-11-05 05:52:49 +00:00
|
|
|
// Wait 5 minutes before dialing a peer who is
|
|
|
|
// 'full'
|
2021-02-25 13:51:26 +00:00
|
|
|
p2ptypes.GoodbyeCodeTooManyPeers: 5 * time.Minute,
|
|
|
|
p2ptypes.GoodbyeCodeGenericError: 2 * time.Minute,
|
2020-11-05 05:52:49 +00:00
|
|
|
}
|
|
|
|
|
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-06-25 22:36:18 +00:00
|
|
|
SetRPCStreamDeadlines(stream)
|
2019-08-23 16:53:38 +00:00
|
|
|
|
2020-10-14 07:55:28 +00:00
|
|
|
m, ok := msg.(*types.SSZUint64)
|
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")
|
2021-03-21 19:07:42 +00:00
|
|
|
s.cfg.P2P.Peers().SetNextValidTime(stream.Conn().RemotePeer(), goodByeBackoff(*m))
|
2019-08-23 16:53:38 +00:00
|
|
|
// closes all streams with the peer
|
2021-03-21 19:07:42 +00:00
|
|
|
return s.cfg.P2P.Disconnect(stream.Conn().RemotePeer())
|
2019-08-23 16:53:38 +00:00
|
|
|
}
|
2019-09-20 06:27:28 +00:00
|
|
|
|
2020-11-18 15:51:42 +00:00
|
|
|
// disconnectBadPeer checks whether peer is considered bad by some scorer, and tries to disconnect
|
|
|
|
// the peer, if that is the case. Additionally, disconnection reason is obtained from scorer.
|
|
|
|
func (s *Service) disconnectBadPeer(ctx context.Context, id peer.ID) {
|
2021-03-21 19:07:42 +00:00
|
|
|
if !s.cfg.P2P.Peers().IsBad(id) {
|
2020-11-18 15:51:42 +00:00
|
|
|
return
|
|
|
|
}
|
2021-03-21 19:07:42 +00:00
|
|
|
goodbyeCode := p2ptypes.ErrToGoodbyeCode(s.cfg.P2P.Peers().Scorers().ValidationError(id))
|
2020-11-18 15:51:42 +00:00
|
|
|
if err := s.sendGoodByeAndDisconnect(ctx, goodbyeCode, id); err != nil {
|
|
|
|
log.Debugf("Error when disconnecting with bad peer: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 12:58:13 +00:00
|
|
|
// A custom goodbye method that is used by our connection handler, in the
|
|
|
|
// event we receive bad peers.
|
|
|
|
func (s *Service) sendGoodbye(ctx context.Context, id peer.ID) error {
|
2021-02-25 13:51:26 +00:00
|
|
|
return s.sendGoodByeAndDisconnect(ctx, p2ptypes.GoodbyeCodeGenericError, id)
|
2020-11-13 12:58:13 +00:00
|
|
|
}
|
|
|
|
|
2021-02-25 13:51:26 +00:00
|
|
|
func (s *Service) sendGoodByeAndDisconnect(ctx context.Context, code p2ptypes.RPCGoodbyeCode, id peer.ID) error {
|
2021-02-16 07:45:34 +00:00
|
|
|
lock := mputil.NewMultilock(id.String())
|
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
// In the event we are already disconnected, exit early from the
|
|
|
|
// goodbye method to prevent redundant streams from being created.
|
2021-03-21 19:07:42 +00:00
|
|
|
if s.cfg.P2P.Host().Network().Connectedness(id) == network.NotConnected {
|
2021-02-16 07:45:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-06-22 20:37:48 +00:00
|
|
|
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")
|
|
|
|
}
|
2021-03-21 19:07:42 +00:00
|
|
|
return s.cfg.P2P.Disconnect(id)
|
2020-05-14 14:44:03 +00:00
|
|
|
}
|
|
|
|
|
2021-02-25 13:51:26 +00:00
|
|
|
func (s *Service) sendGoodByeMessage(ctx context.Context, code p2ptypes.RPCGoodbyeCode, 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()
|
|
|
|
|
2021-09-09 15:12:49 +00:00
|
|
|
topic, err := p2p.TopicFromMessage(p2p.GoodbyeMessageName, core.SlotToEpoch(s.cfg.Chain.CurrentSlot()))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
stream, err := s.cfg.P2P.Send(ctx, &code, topic, id)
|
2020-04-23 19:46:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-14 17:22:25 +00:00
|
|
|
defer closeStream(stream, log)
|
|
|
|
|
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")
|
2020-12-14 17:22:25 +00:00
|
|
|
|
|
|
|
// Wait up to the response timeout for the peer to receive the goodbye
|
|
|
|
// and close the stream (or disconnect). We usually don't bother waiting
|
|
|
|
// around for an EOF, but we're going to close this connection
|
|
|
|
// immediately after we say goodbye.
|
|
|
|
//
|
|
|
|
// NOTE: we don't actually check the response as there's nothing we can
|
|
|
|
// do if something fails. We just need to wait for it.
|
|
|
|
SetStreamReadDeadline(stream, respTimeout)
|
|
|
|
_, _err := stream.Read([]byte{0})
|
|
|
|
_ = _err
|
|
|
|
|
2020-04-23 19:46:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-25 13:51:26 +00:00
|
|
|
func goodbyeMessage(num p2ptypes.RPCGoodbyeCode) string {
|
|
|
|
reason, ok := p2ptypes.GoodbyeCodeMessages[num]
|
2019-09-20 06:27:28 +00:00
|
|
|
if ok {
|
|
|
|
return reason
|
|
|
|
}
|
2020-11-18 04:17:42 +00:00
|
|
|
return fmt.Sprintf("unknown goodbye value of %d received", num)
|
2019-09-20 06:27:28 +00:00
|
|
|
}
|
2020-11-05 05:52:49 +00:00
|
|
|
|
|
|
|
// determines which backoff time to use depending on the
|
|
|
|
// goodbye code provided.
|
2021-02-25 13:51:26 +00:00
|
|
|
func goodByeBackoff(num p2ptypes.RPCGoodbyeCode) time.Time {
|
2020-11-05 05:52:49 +00:00
|
|
|
duration, ok := backOffTime[num]
|
|
|
|
if !ok {
|
|
|
|
return time.Time{}
|
|
|
|
}
|
|
|
|
return time.Now().Add(duration)
|
|
|
|
}
|