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"
|
2019-08-16 20:03:11 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
const genericError = "internal service error"
|
|
|
|
|
|
|
|
var errWrongForkVersion = errors.New("wrong fork version")
|
|
|
|
|
|
|
|
var responseCodeSuccess = byte(0x00)
|
|
|
|
var responseCodeInvalidRequest = byte(0x01)
|
|
|
|
var responseCodeServerError = byte(0x02)
|
|
|
|
|
|
|
|
func (r *RegularSync) generateErrorResponse(code byte, reason string) ([]byte, error) {
|
|
|
|
buf := bytes.NewBuffer([]byte{code})
|
2019-09-09 02:34:52 +00:00
|
|
|
if _, err := r.p2p.Encoding().EncodeWithLength(buf, &pb.ErrorMessage{ErrorMessage: reason}); 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
|
|
|
}
|
|
|
|
|
2019-09-11 18:38:35 +00:00
|
|
|
msg := make([]byte, 0)
|
|
|
|
if err := encoding.DecodeWithLength(stream, &msg); err != nil {
|
|
|
|
return 0, "", err
|
2019-08-16 20:03:11 +00:00
|
|
|
}
|
|
|
|
|
2019-09-11 18:38:35 +00:00
|
|
|
return uint8(b[0]), string(msg), nil
|
2019-08-16 20:03:11 +00:00
|
|
|
}
|