2019-08-23 17:10:25 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
libp2pcore "github.com/libp2p/go-libp2p-core"
|
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-11-04 20:09:19 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
2020-10-14 07:55:28 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/types"
|
2020-06-09 22:40:48 +00:00
|
|
|
"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-10-14 07:55:28 +00:00
|
|
|
func (s *Service) sendRecentBeaconBlocksRequest(ctx context.Context, blockRoots *types.BeaconBlockByRootsReq, 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-11-04 20:09:19 +00:00
|
|
|
_, err := SendBeaconBlocksByRootRequest(ctx, s.p2p, id, blockRoots, func(blk *ethpb.SignedBeaconBlock) error {
|
2020-08-27 18:13:32 +00:00
|
|
|
blkRoot, err := blk.Block.HashTreeRoot()
|
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()
|
2020-08-18 16:21:25 +00:00
|
|
|
s.insertBlockToPendingQueue(blk.Block.Slot, blk, blkRoot)
|
2020-06-22 20:37:48 +00:00
|
|
|
s.pendingQueueLock.Unlock()
|
2020-11-04 20:09:19 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return err
|
2019-09-16 17:54:46 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2020-08-04 20:30:40 +00:00
|
|
|
log.WithError(err).Debug("Failed to close stream")
|
2020-04-13 04:11:09 +00:00
|
|
|
}
|
|
|
|
}()
|
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-10-14 07:55:28 +00:00
|
|
|
rawMsg, ok := msg.(*types.BeaconBlockByRootsReq)
|
2020-04-13 04:11:09 +00:00
|
|
|
if !ok {
|
2020-10-14 07:55:28 +00:00
|
|
|
return errors.New("message is not type BeaconBlockByRootsReq")
|
2020-04-13 04:11:09 +00:00
|
|
|
}
|
2020-10-14 07:55:28 +00:00
|
|
|
blockRoots := *rawMsg
|
2020-09-01 19:09:09 +00:00
|
|
|
if err := s.rateLimiter.validateRequest(stream, uint64(len(blockRoots))); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-26 17:05:04 +00:00
|
|
|
if len(blockRoots) == 0 {
|
2020-09-01 19:09:09 +00:00
|
|
|
// Add to rate limiter in the event no
|
|
|
|
// roots are requested.
|
|
|
|
s.rateLimiter.add(stream, 1)
|
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 {
|
2020-08-04 20:30:40 +00:00
|
|
|
log.WithError(err).Debug("Failed to generate a response error")
|
2020-08-10 16:16:45 +00:00
|
|
|
} else if _, err := stream.Write(resp); err != nil {
|
|
|
|
log.WithError(err).Debugf("Failed to write to stream")
|
2019-08-23 17:10:25 +00:00
|
|
|
}
|
|
|
|
return errors.New("no block roots provided")
|
|
|
|
}
|
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 {
|
2020-08-04 20:30:40 +00:00
|
|
|
log.WithError(err).Debug("Failed to generate a response error")
|
2020-08-10 16:16:45 +00:00
|
|
|
} else if _, err := stream.Write(resp); err != nil {
|
|
|
|
log.WithError(err).Debugf("Failed to write to stream")
|
2020-06-09 22:40:48 +00:00
|
|
|
}
|
|
|
|
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 {
|
2020-08-04 20:30:40 +00:00
|
|
|
log.WithError(err).Debug("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 {
|
2020-08-04 20:30:40 +00:00
|
|
|
log.WithError(err).Debug("Failed to generate a response error")
|
2020-08-10 16:16:45 +00:00
|
|
|
} else if _, err := stream.Write(resp); err != nil {
|
|
|
|
log.WithError(err).Debugf("Failed to write to stream")
|
2019-08-23 17:10:25 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|