prysm-pulse/beacon-chain/sync/rpc_beacon_blocks_by_root.go

98 lines
2.7 KiB
Go
Raw Normal View History

package sync
import (
"context"
"time"
libp2pcore "github.com/libp2p/go-libp2p-core"
2019-09-16 17:54:46 +00:00
"github.com/libp2p/go-libp2p-core/peer"
"github.com/pkg/errors"
"github.com/prysmaticlabs/go-ssz"
2019-09-10 14:24:14 +00:00
eth "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
)
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.
func (r *RegularSync) sendRecentBeaconBlocksRequest(ctx context.Context, blockRoots [][32]byte, id peer.ID) error {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
stream, err := r.p2p.Send(ctx, blockRoots, id)
if err != nil {
return err
}
code, errMsg, err := ReadStatusCode(stream, r.p2p.Encoding())
if err != nil {
return err
}
if code != 0 {
return errors.New(errMsg)
}
resp := make([]*eth.BeaconBlock, 0)
if err := r.p2p.Encoding().DecodeWithLength(stream, &resp); err != nil {
return err
}
r.pendingQueueLock.Lock()
defer r.pendingQueueLock.Unlock()
2019-09-16 17:54:46 +00:00
for _, blk := range resp {
r.slotToPendingBlocks[blk.Slot] = blk
blkRoot, err := ssz.SigningRoot(blk)
if err != nil {
return err
2019-09-16 17:54:46 +00:00
}
r.seenPendingBlocks[blkRoot] = true
2019-09-16 17:54:46 +00:00
}
return nil
}
// beaconBlocksRootRPCHandler looks up the request blocks from the database from the given block roots.
func (r *RegularSync) beaconBlocksRootRPCHandler(ctx context.Context, msg interface{}, stream libp2pcore.Stream) error {
defer stream.Close()
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
setRPCStreamDeadlines(stream)
log := log.WithField("handler", "beacon_blocks_by_root")
2019-09-16 17:54:46 +00:00
blockRoots := msg.([][32]byte)
if len(blockRoots) == 0 {
resp, err := r.generateErrorResponse(responseCodeInvalidRequest, "no block roots provided in request")
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")
}
2019-09-10 14:24:14 +00:00
ret := make([]*eth.BeaconBlock, 0)
for _, root := range blockRoots {
2019-09-16 17:54:46 +00:00
blk, err := r.db.Block(ctx, root)
if err != nil {
log.WithError(err).Error("Failed to fetch block")
resp, err := r.generateErrorResponse(responseCodeServerError, genericError)
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
}
// if block returned is nil, it appends nil to the slice
2019-09-10 14:24:14 +00:00
ret = append(ret, blk)
}
if _, err := stream.Write([]byte{responseCodeSuccess}); err != nil {
log.WithError(err).Error("Failed to write to stream")
}
_, err := r.p2p.Encoding().EncodeWithLength(stream, ret)
return err
}