2021-08-20 21:32:56 +00:00
|
|
|
package helpers
|
|
|
|
|
|
|
|
import (
|
2022-03-23 22:54:07 +00:00
|
|
|
"errors"
|
|
|
|
|
2024-02-15 05:46:47 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v5/beacon-chain/rpc/lookup"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/beacon-chain/state/stategen"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/consensus-types/blocks"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/consensus-types/interfaces"
|
2021-08-20 21:32:56 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PrepareStateFetchGRPCError returns an appropriate gRPC error based on the supplied argument.
|
|
|
|
// The argument error should be a result of fetching state.
|
|
|
|
func PrepareStateFetchGRPCError(err error) error {
|
2022-03-28 21:01:55 +00:00
|
|
|
if errors.Is(err, stategen.ErrNoDataForSlot) {
|
2022-03-23 22:54:07 +00:00
|
|
|
return status.Errorf(codes.NotFound, "lacking historical data needed to fulfill request")
|
|
|
|
}
|
2023-03-28 16:44:41 +00:00
|
|
|
if stateNotFoundErr, ok := err.(*lookup.StateNotFoundError); ok {
|
2021-08-20 21:32:56 +00:00
|
|
|
return status.Errorf(codes.NotFound, "State not found: %v", stateNotFoundErr)
|
2022-03-23 22:54:07 +00:00
|
|
|
}
|
2023-03-28 16:44:41 +00:00
|
|
|
if parseErr, ok := err.(*lookup.StateIdParseError); ok {
|
2021-08-20 21:32:56 +00:00
|
|
|
return status.Errorf(codes.InvalidArgument, "Invalid state ID: %v", parseErr)
|
|
|
|
}
|
|
|
|
return status.Errorf(codes.Internal, "Invalid state ID: %v", err)
|
|
|
|
}
|
2021-09-02 16:54:53 +00:00
|
|
|
|
|
|
|
// IndexedVerificationFailure represents a collection of verification failures.
|
|
|
|
type IndexedVerificationFailure struct {
|
|
|
|
Failures []*SingleIndexedVerificationFailure `json:"failures"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// SingleIndexedVerificationFailure represents an issue when verifying a single indexed object e.g. an item in an array.
|
|
|
|
type SingleIndexedVerificationFailure struct {
|
|
|
|
Index int `json:"index"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
2023-10-02 15:34:34 +00:00
|
|
|
|
|
|
|
func HandleGetBlockError(blk interfaces.ReadOnlySignedBeaconBlock, err error) error {
|
|
|
|
if invalidBlockIdErr, ok := err.(*lookup.BlockIdParseError); ok {
|
|
|
|
return status.Errorf(codes.InvalidArgument, "Invalid block ID: %v", invalidBlockIdErr)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return status.Errorf(codes.Internal, "Could not get block from block ID: %v", err)
|
|
|
|
}
|
|
|
|
if err := blocks.BeaconBlockIsNil(blk); err != nil {
|
|
|
|
return status.Errorf(codes.NotFound, "Could not find requested block: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|