prysm-pulse/beacon-chain/sync/rpc_metadata.go
Preston Van Loon 7cc32c4dda
Various code inspection resolutions (#7438)
* remove unused code

* remove defer use in loop

* Remove unused methods and constants

* gofmt and gaz

* nilness check

* remove unused args

* Add TODO for refactoring subscribeWithBase to remove unused arg. It seems too involved to include in this sweeping PR. https://github.com/prysmaticlabs/prysm/issues/7437

* replace empty slice declaration

* Remove unnecessary type conversions

* remove redundant type declaration

* rename receivers to be consistent

* Remove bootnode query tool. It is now obsolete by discv5

* Remove relay node. It is no longer used or supported

* Revert "Remove relay node. It is no longer used or supported"

This reverts commit 4bd7717334dad85ef4766ed9bc4da711fb5fa810.

* Delete unused test directory

* Delete unsupported gcp startup script

* Delete old k8s script

* build fixes

* fix build

* go mod tidy

* revert slasher/db/kv/block_header.go

* fix build

* remove redundant nil check

* combine func args

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
Co-authored-by: Victor Farazdagi <simple.square@gmail.com>
2020-10-12 08:11:05 +00:00

65 lines
1.9 KiB
Go

package sync
import (
"context"
libp2pcore "github.com/libp2p/go-libp2p-core"
"github.com/libp2p/go-libp2p-core/helpers"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
)
// metaDataHandler reads the incoming metadata rpc request from the peer.
func (s *Service) metaDataHandler(_ context.Context, _ interface{}, stream libp2pcore.Stream) error {
defer func() {
if err := stream.Close(); err != nil {
log.WithError(err).Debug("Failed to close stream")
}
}()
SetRPCStreamDeadlines(stream)
if err := s.rateLimiter.validateRequest(stream, 1); err != nil {
return err
}
s.rateLimiter.add(stream, 1)
if _, err := stream.Write([]byte{responseCodeSuccess}); err != nil {
return err
}
_, err := s.p2p.Encoding().EncodeWithMaxLength(stream, s.p2p.Metadata())
return err
}
func (s *Service) sendMetaDataRequest(ctx context.Context, id peer.ID) (*pb.MetaData, error) {
ctx, cancel := context.WithTimeout(ctx, respTimeout)
defer cancel()
stream, err := s.p2p.Send(ctx, new(interface{}), p2p.RPCMetaDataTopic, id)
if err != nil {
return nil, err
}
// we close the stream outside of `send` because
// metadata requests send no payload, so closing the
// stream early leads it to a reset.
defer func() {
if err := helpers.FullClose(stream); isValidStreamError(err) {
log.WithError(err).Debugf("Failed to reset stream for protocol %s", stream.Protocol())
}
}()
code, errMsg, err := ReadStatusCode(stream, s.p2p.Encoding())
if err != nil {
return nil, err
}
if code != 0 {
s.p2p.Peers().Scorers().BadResponsesScorer().Increment(stream.Conn().RemotePeer())
return nil, errors.New(errMsg)
}
msg := new(pb.MetaData)
if err := s.p2p.Encoding().DecodeWithMaxLength(stream, msg); err != nil {
return nil, err
}
return msg, nil
}