2019-08-23 17:10:25 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-09-24 14:56:50 +00:00
|
|
|
"io"
|
2019-08-23 17:10:25 +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"
|
2019-09-16 17:54:46 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2019-08-23 17:10:25 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-04-14 20:27:03 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
|
2020-05-05 04:30:24 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
2020-06-09 22:40:48 +00:00
|
|
|
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2019-08-23 17:10:25 +00:00
|
|
|
)
|
|
|
|
|
2019-09-16 17:54:46 +00:00
|
|
|
// sendRecentBeaconBlocksRequest sends a recent beacon blocks request to a peer to get
|
|
|
|
// those corresponding blocks from that peer.
|
2020-06-26 17:05:04 +00:00
|
|
|
func (s *Service) sendRecentBeaconBlocksRequest(ctx context.Context, blockRoots [][32]byte, id peer.ID) error {
|
2020-07-01 09:47:59 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, respTimeout)
|
2019-09-16 17:54:46 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2020-06-26 17:05:04 +00:00
|
|
|
stream, err := s.p2p.Send(ctx, blockRoots, p2p.RPCBlocksByRootTopic, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
2020-07-03 08:21:18 +00:00
|
|
|
if err := helpers.FullClose(stream); err != nil {
|
|
|
|
log.WithError(err).Debugf("Failed to reset stream with protocol %s", stream.Protocol())
|
2020-06-26 17:05:04 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
for i := 0; i < len(blockRoots); i++ {
|
2020-07-13 19:40:12 +00:00
|
|
|
isFirstChunk := i == 0
|
|
|
|
blk, err := ReadChunkedBlock(stream, s.p2p, isFirstChunk)
|
2020-06-26 17:05:04 +00:00
|
|
|
// Return error until #6408 is resolved.
|
|
|
|
if err == io.EOF {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Exit if peer sends more than max request blocks.
|
|
|
|
if uint64(i) >= params.BeaconNetworkConfig().MaxRequestBlocks {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Unable to retrieve block from stream")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
blkRoot, err := stateutil.BlockRoot(blk.Block)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.pendingQueueLock.Lock()
|
|
|
|
s.slotToPendingBlocks[blk.Block.Slot] = blk
|
|
|
|
s.seenPendingBlocks[blkRoot] = true
|
|
|
|
s.pendingQueueLock.Unlock()
|
|
|
|
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deprecated: sendRecentBeaconBlocksRequestFallback sends a recent beacon blocks request to a peer to get
|
|
|
|
// those corresponding blocks from that peer. This is a method implemented so that we are eventually
|
|
|
|
// backward compatible with old Onyx nodes.
|
|
|
|
// TODO(#6408)
|
|
|
|
func (s *Service) sendRecentBeaconBlocksRequestFallback(ctx context.Context, blockRoots [][32]byte, id peer.ID) error {
|
2020-07-01 09:47:59 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, respTimeout)
|
2020-06-26 17:05:04 +00:00
|
|
|
defer cancel()
|
|
|
|
req := &pbp2p.BeaconBlocksByRootRequest{}
|
|
|
|
for _, root := range blockRoots {
|
|
|
|
req.BlockRoots = append(req.BlockRoots, root[:])
|
|
|
|
}
|
2020-06-22 20:37:48 +00:00
|
|
|
stream, err := s.p2p.Send(ctx, req, p2p.RPCBlocksByRootTopic, id)
|
2019-09-16 17:54:46 +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
|
|
|
}
|
|
|
|
}()
|
2019-09-24 14:56:50 +00:00
|
|
|
for i := 0; i < len(blockRoots); i++ {
|
2020-07-13 19:40:12 +00:00
|
|
|
isFirstChunk := i == 0
|
|
|
|
blk, err := ReadChunkedBlock(stream, s.p2p, isFirstChunk)
|
2019-09-24 14:56:50 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2020-06-09 22:40:48 +00:00
|
|
|
// Exit if peer sends more than max request blocks.
|
|
|
|
if uint64(i) >= params.BeaconNetworkConfig().MaxRequestBlocks {
|
|
|
|
break
|
|
|
|
}
|
2019-09-24 14:56:50 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Unable to retrieve block from stream")
|
|
|
|
return err
|
|
|
|
}
|
2020-02-01 22:47:51 +00:00
|
|
|
|
2020-05-05 04:30:24 +00:00
|
|
|
blkRoot, err := stateutil.BlockRoot(blk.Block)
|
2019-09-20 17:08:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-09-16 17:54:46 +00:00
|
|
|
}
|
2020-06-22 20:37:48 +00:00
|
|
|
s.pendingQueueLock.Lock()
|
|
|
|
s.slotToPendingBlocks[blk.Block.Slot] = blk
|
|
|
|
s.seenPendingBlocks[blkRoot] = true
|
|
|
|
s.pendingQueueLock.Unlock()
|
2019-09-16 17:54:46 +00:00
|
|
|
|
2019-09-24 14:56:50 +00:00
|
|
|
}
|
2019-09-16 17:54:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-20 06:27:28 +00:00
|
|
|
// beaconBlocksRootRPCHandler looks up the request blocks from the database from the given block roots.
|
2020-06-22 20:37:48 +00:00
|
|
|
func (s *Service) beaconBlocksRootRPCHandler(ctx 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-07-01 09:47:59 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, ttfbTimeout)
|
2019-08-23 17:10:25 +00:00
|
|
|
defer cancel()
|
2020-06-25 22:36:18 +00:00
|
|
|
SetRPCStreamDeadlines(stream)
|
2019-09-20 06:27:28 +00:00
|
|
|
log := log.WithField("handler", "beacon_blocks_by_root")
|
2019-08-23 17:10:25 +00:00
|
|
|
|
2020-06-26 17:05:04 +00:00
|
|
|
blockRoots, ok := msg.([][32]byte)
|
2020-04-13 04:11:09 +00:00
|
|
|
if !ok {
|
2020-06-09 22:40:48 +00:00
|
|
|
return errors.New("message is not type BeaconBlocksByRootRequest")
|
2020-04-13 04:11:09 +00:00
|
|
|
}
|
2020-06-26 17:05:04 +00:00
|
|
|
if len(blockRoots) == 0 {
|
2020-06-22 20:37:48 +00:00
|
|
|
resp, err := s.generateErrorResponse(responseCodeInvalidRequest, "no block roots provided in request")
|
2019-08-23 17:10:25 +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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors.New("no block roots provided")
|
|
|
|
}
|
2020-07-17 08:58:51 +00:00
|
|
|
if err := s.rateLimiter.validateRequest(stream, uint64(len(blockRoots))); err != nil {
|
|
|
|
return err
|
2020-01-16 01:19:06 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 17:05:04 +00:00
|
|
|
if uint64(len(blockRoots)) > params.BeaconNetworkConfig().MaxRequestBlocks {
|
2020-06-22 20:37:48 +00:00
|
|
|
resp, err := s.generateErrorResponse(responseCodeInvalidRequest, "requested more than the max block limit")
|
2020-06-09 22:40:48 +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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors.New("requested more than the max block limit")
|
|
|
|
}
|
2020-07-17 08:58:51 +00:00
|
|
|
s.rateLimiter.add(stream, int64(len(blockRoots)))
|
2020-01-16 01:19:06 +00:00
|
|
|
|
2020-06-26 17:05:04 +00:00
|
|
|
for _, root := range blockRoots {
|
|
|
|
blk, err := s.db.Block(ctx, root)
|
2019-10-09 06:57:43 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to fetch block")
|
2020-06-22 20:37:48 +00:00
|
|
|
resp, err := s.generateErrorResponse(responseCodeServerError, genericError)
|
2019-08-23 17:10:25 +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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2019-10-09 06:57:43 +00:00
|
|
|
if blk == nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-06-22 20:37:48 +00:00
|
|
|
if err := s.chunkWriter(stream, blk); err != nil {
|
2019-09-24 14:56:50 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-08-23 17:10:25 +00:00
|
|
|
}
|
2019-09-24 14:56:50 +00:00
|
|
|
return nil
|
2019-08-23 17:10:25 +00:00
|
|
|
}
|