-
Notifications
You must be signed in to change notification settings - Fork 986
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
504 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
package sync | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
libp2pcore "github.com/libp2p/go-libp2p/core" | ||
"github.com/pkg/errors" | ||
"github.com/prysmaticlabs/prysm/v4/beacon-chain/db" | ||
"github.com/prysmaticlabs/prysm/v4/beacon-chain/p2p" | ||
p2ptypes "github.com/prysmaticlabs/prysm/v4/beacon-chain/p2p/types" | ||
"github.com/prysmaticlabs/prysm/v4/cmd/beacon-chain/flags" | ||
"github.com/prysmaticlabs/prysm/v4/config/params" | ||
"github.com/prysmaticlabs/prysm/v4/consensus-types/primitives" | ||
"github.com/prysmaticlabs/prysm/v4/monitoring/tracing" | ||
pb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1" | ||
"github.com/prysmaticlabs/prysm/v4/time/slots" | ||
"go.opencensus.io/trace" | ||
) | ||
|
||
type BlobSidecarProcessor func(sidecar *pb.BlobSidecar) error | ||
|
||
func (s *Service) streamBlobBatch(ctx context.Context, batch blockBatch, stream libp2pcore.Stream) (uint64, error) { | ||
ctx, span := trace.StartSpan(ctx, "sync.streamBlobBatch") | ||
defer span.End() | ||
var writes uint64 | ||
for _, b := range batch.canonical() { | ||
root := b.Root() | ||
scs, err := s.cfg.beaconDB.BlobSidecarsByRoot(ctx, b.Root()) | ||
if errors.Is(err, db.ErrNotFound) { | ||
continue | ||
} | ||
if err != nil { | ||
s.writeErrorResponseToStream(responseCodeServerError, p2ptypes.ErrGeneric.Error(), stream) | ||
return writes, errors.Wrapf(err, "could not retrieve sidecars for block root %#x", root) | ||
} | ||
for _, sc := range scs { | ||
SetStreamWriteDeadline(stream, defaultWriteDuration) | ||
if chunkErr := WriteBlobSidecarChunk(stream, s.cfg.chain, s.cfg.p2p.Encoding(), sc); chunkErr != nil { | ||
log.WithError(chunkErr).Debug("Could not send a chunked response") | ||
s.writeErrorResponseToStream(responseCodeServerError, p2ptypes.ErrGeneric.Error(), stream) | ||
tracing.AnnotateError(span, chunkErr) | ||
return writes, chunkErr | ||
} | ||
s.rateLimiter.add(stream, 1) | ||
writes += 1 | ||
} | ||
} | ||
return writes, nil | ||
} | ||
|
||
// blobsSidecarsByRangeRPCHandler looks up the request blobs from the database from a given start slot index | ||
func (s *Service) blobSidecarsByRangeRPCHandler(ctx context.Context, msg interface{}, stream libp2pcore.Stream) error { | ||
ctx, span := trace.StartSpan(ctx, "sync.BlobsSidecarsByRangeHandler") | ||
defer span.End() | ||
ctx, cancel := context.WithTimeout(ctx, respTimeout) | ||
defer cancel() | ||
SetRPCStreamDeadlines(stream) | ||
log := log.WithField("handler", p2p.BlobSidecarsByRangeName[1:]) // slice the leading slash off the name var | ||
|
||
r, ok := msg.(*pb.BlobSidecarsByRangeRequest) | ||
if !ok { | ||
return errors.New("message is not type *pb.BlobsSidecarsByRangeRequest") | ||
} | ||
if err := s.rateLimiter.validateRequest(stream, 1); err != nil { | ||
return err | ||
} | ||
rp, err := validateBlobsByRange(r, s.cfg.chain.CurrentSlot()) | ||
if err != nil { | ||
s.writeErrorResponseToStream(responseCodeInvalidRequest, err.Error(), stream) | ||
s.cfg.p2p.Peers().Scorers().BadResponsesScorer().Increment(stream.Conn().RemotePeer()) | ||
tracing.AnnotateError(span, err) | ||
return err | ||
} | ||
|
||
// Ticker to stagger out large requests. | ||
ticker := time.NewTicker(time.Second) | ||
defer ticker.Stop() | ||
batcher, err := newBlockRangeBatcher(rp, s.cfg.beaconDB, s.rateLimiter, s.cfg.chain.IsCanonical, ticker) | ||
|
||
var batch blockBatch | ||
var totalWrites uint64 | ||
for batch, ok = batcher.next(ctx, stream); ok; batch, ok = batcher.next(ctx, stream) { | ||
batchStart := time.Now() | ||
writes, err := s.streamBlobBatch(ctx, batch, stream) | ||
rpcBlobsByRangeResponseLatency.Observe(float64(time.Since(batchStart).Milliseconds())) | ||
if err != nil { | ||
return err | ||
} | ||
totalWrites += writes | ||
// once we have written MAX_REQUEST_BLOB_SIDECARS, we're done serving the request | ||
if totalWrites >= params.BeaconNetworkConfig().MaxRequestBlobsSidecars { | ||
break | ||
} | ||
} | ||
if err := batch.error(); err != nil { | ||
log.WithError(err).Debug("error in BlocksByRange batch") | ||
s.writeErrorResponseToStream(responseCodeServerError, p2ptypes.ErrGeneric.Error(), stream) | ||
tracing.AnnotateError(span, err) | ||
return err | ||
} | ||
|
||
closeStream(stream, log) | ||
return nil | ||
} | ||
|
||
func blobsByRangeMinStartSlot(current primitives.Slot) (primitives.Slot, error) { | ||
minReqEpochs := params.BeaconNetworkConfig().MinEpochsForBlobsSidecarsRequest | ||
currEpoch := slots.ToEpoch(current) | ||
minStart := params.BeaconConfig().DenebForkEpoch | ||
if currEpoch > minReqEpochs && currEpoch-minReqEpochs > minStart { | ||
minStart = currEpoch - minReqEpochs | ||
} | ||
return slots.EpochStart(minStart) | ||
} | ||
|
||
func blobBatchLimit() uint64 { | ||
return uint64(flags.Get().BlockBatchLimit) / params.BeaconConfig().MaxBlobsPerBlock | ||
} | ||
|
||
func validateBlobsByRange(r *pb.BlobSidecarsByRangeRequest, current primitives.Slot) (rangeParams, error) { | ||
rp := rangeParams{ | ||
start: r.StartSlot, | ||
size: r.Count, | ||
} | ||
// Peers may overshoot the current slot when in initial sync, so we don't want to penalize them by treating the | ||
// request as an error. So instead we return a set of params that acts as a noop. | ||
if rp.start > current { | ||
return rangeParams{start: current, end: current, size: 0}, nil | ||
} | ||
|
||
var err error | ||
rp.end, err = rp.start.SafeAdd((rp.size - 1)) | ||
if err != nil { | ||
return rangeParams{}, errors.Wrap(p2ptypes.ErrInvalidRequest, "overflow start + count -1") | ||
} | ||
|
||
maxRequest := params.BeaconNetworkConfig().MaxRequestBlocksDeneb | ||
// Allow some wiggle room, up to double the MaxRequestBlocks past the current slot, | ||
// to give nodes syncing close to the head of the chain some margin for error. | ||
maxStart, err := current.SafeAdd(maxRequest * 2) | ||
if err != nil { | ||
return rangeParams{}, errors.Wrap(p2ptypes.ErrInvalidRequest, "current + maxRequest * 2 > max uint") | ||
} | ||
|
||
// Clients MUST keep a record of signed blobs sidecars seen on the epoch range | ||
// [max(current_epoch - MIN_EPOCHS_FOR_BLOB_SIDECARS_REQUESTS, DENEB_FORK_EPOCH), current_epoch] | ||
// where current_epoch is defined by the current wall-clock time, | ||
// and clients MUST support serving requests of blobs on this range. | ||
minStartSlot, err := blobsByRangeMinStartSlot(current) | ||
if err != nil { | ||
return rangeParams{}, errors.Wrap(p2ptypes.ErrInvalidRequest, "blobsByRangeMinStartSlot error") | ||
} | ||
if rp.start > maxStart { | ||
return rangeParams{}, errors.Wrap(p2ptypes.ErrInvalidRequest, "start > maxStart") | ||
} | ||
if rp.start < minStartSlot { | ||
rp.start = minStartSlot | ||
} | ||
|
||
if rp.end > current { | ||
rp.end = current | ||
} | ||
if rp.end < rp.start { | ||
rp.end = rp.start | ||
} | ||
|
||
limit := blobBatchLimit() | ||
if limit > maxRequest { | ||
limit = maxRequest | ||
} | ||
if rp.size > limit { | ||
rp.size = limit | ||
} | ||
|
||
return rp, nil | ||
} |
Oops, something went wrong.