2019-08-16 17:13:04 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2019-08-16 20:03:11 +00:00
|
|
|
"bytes"
|
2019-08-16 17:13:04 +00:00
|
|
|
"context"
|
2019-08-16 20:03:11 +00:00
|
|
|
"time"
|
2019-08-16 17:13:04 +00:00
|
|
|
|
|
|
|
libp2pcore "github.com/libp2p/go-libp2p-core"
|
2019-08-29 16:32:52 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/network"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
"github.com/pkg/errors"
|
2019-09-20 17:54:32 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/sync/peerstatus"
|
2019-08-16 20:03:11 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2019-08-16 17:13:04 +00:00
|
|
|
)
|
|
|
|
|
2019-09-20 06:27:28 +00:00
|
|
|
// sendRPCStatusRequest for a given topic with an expected protobuf message type.
|
|
|
|
func (r *RegularSync) sendRPCStatusRequest(ctx context.Context, id peer.ID) error {
|
|
|
|
log := log.WithField("rpc", "status")
|
2019-08-29 16:32:52 +00:00
|
|
|
|
2019-09-03 18:06:35 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
2019-08-29 16:32:52 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2019-09-20 17:54:32 +00:00
|
|
|
// return if hello already exists
|
|
|
|
hello := peerstatus.Get(id)
|
2019-08-29 16:32:52 +00:00
|
|
|
if hello != nil {
|
|
|
|
log.Debugf("Peer %s already exists", id)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-20 06:27:28 +00:00
|
|
|
resp := &pb.Status{
|
|
|
|
HeadForkVersion: params.BeaconConfig().GenesisForkVersion,
|
|
|
|
FinalizedRoot: r.chain.FinalizedCheckpt().Root,
|
|
|
|
FinalizedEpoch: r.chain.FinalizedCheckpt().Epoch,
|
|
|
|
HeadRoot: r.chain.HeadRoot(),
|
|
|
|
HeadSlot: r.chain.HeadSlot(),
|
2019-08-29 16:32:52 +00:00
|
|
|
}
|
|
|
|
stream, err := r.p2p.Send(ctx, resp, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-30 20:15:40 +00:00
|
|
|
code, errMsg, err := ReadStatusCode(stream, r.p2p.Encoding())
|
2019-08-29 16:32:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if code != 0 {
|
2019-09-11 18:38:35 +00:00
|
|
|
return errors.New(errMsg)
|
2019-08-29 16:32:52 +00:00
|
|
|
}
|
|
|
|
|
2019-09-20 06:27:28 +00:00
|
|
|
msg := &pb.Status{}
|
2019-09-09 02:34:52 +00:00
|
|
|
if err := r.p2p.Encoding().DecodeWithLength(stream, msg); err != nil {
|
2019-08-29 16:32:52 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-09-20 17:54:32 +00:00
|
|
|
peerstatus.Set(stream.Conn().RemotePeer(), msg)
|
2019-08-29 16:32:52 +00:00
|
|
|
|
2019-09-20 06:27:28 +00:00
|
|
|
return r.validateStatusMessage(msg, stream)
|
2019-08-29 16:32:52 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 20:02:34 +00:00
|
|
|
func (r *RegularSync) removeDisconnectedPeerStatus(ctx context.Context, pid peer.ID) error {
|
2019-09-20 17:54:32 +00:00
|
|
|
peerstatus.Delete(pid)
|
2019-09-18 20:02:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-20 06:27:28 +00:00
|
|
|
// statusRPCHandler reads the incoming Status RPC from the peer and responds with our version of a status message.
|
2019-08-16 20:03:11 +00:00
|
|
|
// This handler will disconnect any peer that does not match our fork version.
|
2019-09-20 06:27:28 +00:00
|
|
|
func (r *RegularSync) statusRPCHandler(ctx context.Context, msg interface{}, stream libp2pcore.Stream) error {
|
2019-08-16 20:03:11 +00:00
|
|
|
defer stream.Close()
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
setRPCStreamDeadlines(stream)
|
2019-09-20 06:27:28 +00:00
|
|
|
log := log.WithField("handler", "status")
|
2019-08-29 16:32:52 +00:00
|
|
|
|
|
|
|
// return if hello already exists
|
2019-09-20 17:54:32 +00:00
|
|
|
hello := peerstatus.Get(stream.Conn().RemotePeer())
|
2019-08-29 16:32:52 +00:00
|
|
|
if hello != nil {
|
|
|
|
log.Debugf("Peer %s already exists", stream.Conn().RemotePeer())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-20 06:27:28 +00:00
|
|
|
m := msg.(*pb.Status)
|
2019-08-16 20:03:11 +00:00
|
|
|
|
2019-09-20 17:54:32 +00:00
|
|
|
peerstatus.Set(stream.Conn().RemotePeer(), m)
|
2019-08-29 16:32:52 +00:00
|
|
|
|
2019-09-20 06:27:28 +00:00
|
|
|
if err := r.validateStatusMessage(m, stream); err != nil {
|
2019-08-29 16:32:52 +00:00
|
|
|
originalErr := err
|
|
|
|
resp, err := r.generateErrorResponse(responseCodeInvalidRequest, err.Error())
|
2019-08-16 20:03:11 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to generate a response error")
|
|
|
|
} else {
|
|
|
|
if _, err := stream.Write(resp); err != nil {
|
|
|
|
log.WithError(err).Errorf("Failed to write to stream")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stream.Close() // Close before disconnecting.
|
|
|
|
// Add a short delay to allow the stream to flush before closing the connection.
|
|
|
|
// There is still a chance that the peer won't receive the message.
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
if err := r.p2p.Disconnect(stream.Conn().RemotePeer()); err != nil {
|
|
|
|
log.WithError(err).Error("Failed to disconnect from peer")
|
|
|
|
}
|
2019-08-29 16:32:52 +00:00
|
|
|
return originalErr
|
2019-08-16 20:03:11 +00:00
|
|
|
}
|
|
|
|
|
2019-09-20 06:27:28 +00:00
|
|
|
resp := &pb.Status{
|
|
|
|
HeadForkVersion: params.BeaconConfig().GenesisForkVersion,
|
|
|
|
FinalizedRoot: r.chain.FinalizedCheckpt().Root,
|
|
|
|
FinalizedEpoch: r.chain.FinalizedCheckpt().Epoch,
|
|
|
|
HeadRoot: r.chain.HeadRoot(),
|
|
|
|
HeadSlot: r.chain.HeadSlot(),
|
2019-08-16 20:03:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := stream.Write([]byte{responseCodeSuccess}); err != nil {
|
|
|
|
log.WithError(err).Error("Failed to write to stream")
|
|
|
|
}
|
2019-09-09 02:34:52 +00:00
|
|
|
_, err := r.p2p.Encoding().EncodeWithLength(stream, resp)
|
2019-08-16 20:03:11 +00:00
|
|
|
|
|
|
|
return err
|
2019-08-16 17:13:04 +00:00
|
|
|
}
|
2019-08-29 16:32:52 +00:00
|
|
|
|
2019-09-20 06:27:28 +00:00
|
|
|
func (r *RegularSync) validateStatusMessage(msg *pb.Status, stream network.Stream) error {
|
|
|
|
if !bytes.Equal(params.BeaconConfig().GenesisForkVersion, msg.HeadForkVersion) {
|
2019-08-29 16:32:52 +00:00
|
|
|
return errWrongForkVersion
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|