2019-08-16 20:03:11 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
|
2019-08-30 20:15:40 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/encoder"
|
2020-06-09 22:40:48 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2019-08-16 20:03:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const genericError = "internal service error"
|
2020-01-16 01:19:06 +00:00
|
|
|
const rateLimitedError = "rate limited"
|
2020-04-26 05:49:47 +00:00
|
|
|
const stepError = "invalid range or step"
|
2019-08-16 20:03:11 +00:00
|
|
|
|
2020-04-14 20:27:03 +00:00
|
|
|
var errWrongForkDigestVersion = errors.New("wrong fork digest version")
|
2020-01-02 08:09:28 +00:00
|
|
|
var errInvalidEpoch = errors.New("invalid epoch")
|
2020-05-12 03:22:01 +00:00
|
|
|
var errInvalidFinalizedRoot = errors.New("invalid finalized root")
|
2020-05-14 14:44:03 +00:00
|
|
|
var errGeneric = errors.New(genericError)
|
2019-08-16 20:03:11 +00:00
|
|
|
|
|
|
|
var responseCodeSuccess = byte(0x00)
|
|
|
|
var responseCodeInvalidRequest = byte(0x01)
|
|
|
|
var responseCodeServerError = byte(0x02)
|
|
|
|
|
2020-06-22 20:37:48 +00:00
|
|
|
func (s *Service) generateErrorResponse(code byte, reason string) ([]byte, error) {
|
2019-08-16 20:03:11 +00:00
|
|
|
buf := bytes.NewBuffer([]byte{code})
|
2020-06-09 22:40:48 +00:00
|
|
|
resp := &pb.ErrorResponse{
|
|
|
|
Message: []byte(reason),
|
|
|
|
}
|
2020-06-22 20:37:48 +00:00
|
|
|
if _, err := s.p2p.Encoding().EncodeWithLength(buf, resp); err != nil {
|
2019-08-16 20:03:11 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2019-08-30 20:15:40 +00:00
|
|
|
// ReadStatusCode response from a RPC stream.
|
2019-09-11 18:38:35 +00:00
|
|
|
func ReadStatusCode(stream io.Reader, encoding encoder.NetworkEncoding) (uint8, string, error) {
|
2019-08-16 20:03:11 +00:00
|
|
|
b := make([]byte, 1)
|
|
|
|
_, err := stream.Read(b)
|
|
|
|
if err != nil {
|
2019-09-11 18:38:35 +00:00
|
|
|
return 0, "", err
|
2019-08-16 20:03:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if b[0] == responseCodeSuccess {
|
2019-09-11 18:38:35 +00:00
|
|
|
return 0, "", nil
|
2019-08-16 20:03:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-09 22:40:48 +00:00
|
|
|
msg := &pb.ErrorResponse{
|
|
|
|
Message: []byte{},
|
|
|
|
}
|
|
|
|
if err := encoding.DecodeWithLength(stream, msg); err != nil {
|
2019-09-11 18:38:35 +00:00
|
|
|
return 0, "", err
|
2019-08-16 20:03:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-09 22:40:48 +00:00
|
|
|
return b[0], string(msg.Message), nil
|
2019-08-16 20:03:11 +00:00
|
|
|
}
|