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

94 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"
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 {
log := log.WithField("rpc", "recent_beacon_blocks")
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
}
for _, blk := range resp {
if err := r.chain.ReceiveBlock(ctx, blk); err != nil {
log.WithError(err).Error("Unable to process block")
return nil
}
}
return nil
}
// recentBeaconBlocksRPCHandler looks up the request blocks from the database from the given block roots.
2019-09-16 17:54:46 +00:00
func (r *RegularSync) recentBeaconBlocksRPCHandler(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", "recent_beacon_blocks")
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
}