2019-08-23 17:10:25 +00:00
|
|
|
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"
|
2019-08-23 17:10:25 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-09-10 14:24:14 +00:00
|
|
|
eth "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
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.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-08-23 17:10:25 +00:00
|
|
|
// 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 {
|
2019-08-23 17:10:25 +00:00
|
|
|
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)
|
2019-08-23 17:10:25 +00:00
|
|
|
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)
|
2019-08-23 17:10:25 +00:00
|
|
|
for _, root := range blockRoots {
|
2019-09-16 17:54:46 +00:00
|
|
|
blk, err := r.db.Block(ctx, root)
|
2019-08-23 17:10:25 +00:00
|
|
|
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)
|
2019-08-23 17:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := stream.Write([]byte{responseCodeSuccess}); err != nil {
|
|
|
|
log.WithError(err).Error("Failed to write to stream")
|
|
|
|
}
|
2019-09-09 02:34:52 +00:00
|
|
|
_, err := r.p2p.Encoding().EncodeWithLength(stream, ret)
|
2019-08-23 17:10:25 +00:00
|
|
|
return err
|
|
|
|
}
|