Return status.Errorf instead of plain errors from gRPC functions (#8619)

* Return status.Errorf instead of plain errors from gRPC functions

* return plain errors from helper functions

* change errors to lowercase in node

* correct test expectations

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
Radosław Kapka
2021-03-17 17:47:44 +01:00
committed by GitHub
parent 034a28710e
commit 0a73be7389
3 changed files with 47 additions and 49 deletions

View File

@@ -3,10 +3,12 @@ package beaconv1
import (
"bytes"
"context"
"fmt"
"strconv"
"strings"
ptypes "github.com/gogo/protobuf/types"
"github.com/pkg/errors"
types "github.com/prysmaticlabs/eth2-types"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1"
eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
@@ -58,7 +60,7 @@ func (bs *Server) GetStateRoot(ctx context.Context, req *ethpb.StateRequest) (*e
root, err = bs.stateRoot(ctx, req.StateId)
if err != nil {
return nil, err
return nil, status.Errorf(codes.Internal, "Could not get state root: %v", err)
}
return &ethpb.StateRootResponse{
@@ -80,7 +82,7 @@ func (bs *Server) GetStateFork(ctx context.Context, req *ethpb.StateRequest) (*e
state, err = bs.state(ctx, req.StateId)
if err != nil {
return nil, err
return nil, status.Errorf(codes.Internal, "Could not get state: %v", err)
}
fork := state.Fork()
@@ -106,7 +108,7 @@ func (bs *Server) GetFinalityCheckpoints(ctx context.Context, req *ethpb.StateRe
state, err = bs.state(ctx, req.StateId)
if err != nil {
return nil, err
return nil, status.Errorf(codes.Internal, "Could not get state: %v", err)
}
return &ethpb.StateFinalityCheckpointResponse{
@@ -137,7 +139,7 @@ func (bs *Server) stateRoot(ctx context.Context, stateId []byte) ([]byte, error)
default:
ok, matchErr := bytesutil.IsBytes32Hex(stateId)
if matchErr != nil {
return nil, status.Errorf(codes.Internal, "Could not parse ID: %v", err)
return nil, errors.Wrap(err, "could not parse ID")
}
if ok {
root, err = bs.stateRootByHex(ctx, stateId)
@@ -145,7 +147,7 @@ func (bs *Server) stateRoot(ctx context.Context, stateId []byte) ([]byte, error)
slotNumber, parseErr := strconv.ParseUint(stateIdString, 10, 64)
if parseErr != nil {
// ID format does not match any valid options.
return nil, status.Errorf(codes.Internal, "Invalid state ID: "+stateIdString)
return nil, errors.New("invalid state ID: " + stateIdString)
}
root, err = bs.stateRootBySlot(ctx, types.Slot(slotNumber))
}
@@ -165,29 +167,29 @@ func (bs *Server) state(ctx context.Context, stateId []byte) (iface.BeaconState,
case "head":
s, err = bs.ChainInfoFetcher.HeadState(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get head state: %v", err)
return nil, errors.Wrap(err, "could not get head state")
}
case "genesis":
s, err = bs.BeaconDB.GenesisState(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get genesis state: %v", err)
return nil, errors.Wrap(err, "could not get genesis state")
}
case "finalized":
checkpoint := bs.ChainInfoFetcher.FinalizedCheckpt()
s, err = bs.StateGenService.StateByRoot(ctx, bytesutil.ToBytes32(checkpoint.Root))
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get finalized state: %v", err)
return nil, errors.Wrap(err, "could not get finalized state")
}
case "justified":
checkpoint := bs.ChainInfoFetcher.CurrentJustifiedCheckpt()
s, err = bs.StateGenService.StateByRoot(ctx, bytesutil.ToBytes32(checkpoint.Root))
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get justified state: %v", err)
return nil, errors.Wrap(err, "could not get justified state")
}
default:
ok, matchErr := bytesutil.IsBytes32Hex(stateId)
if matchErr != nil {
return nil, status.Errorf(codes.Internal, "Could not parse ID: %v", err)
return nil, errors.Wrap(err, "could not parse ID")
}
if ok {
s, err = bs.stateByHex(ctx, stateId)
@@ -195,7 +197,7 @@ func (bs *Server) state(ctx context.Context, stateId []byte) (iface.BeaconState,
slotNumber, parseErr := strconv.ParseUint(stateIdString, 10, 64)
if parseErr != nil {
// ID format does not match any valid options.
return nil, status.Errorf(codes.Internal, "Invalid state ID: "+stateIdString)
return nil, errors.New("invalid state ID: " + stateIdString)
}
s, err = bs.stateBySlot(ctx, types.Slot(slotNumber))
}
@@ -207,7 +209,7 @@ func (bs *Server) state(ctx context.Context, stateId []byte) (iface.BeaconState,
func (bs *Server) headStateRoot(ctx context.Context) ([]byte, error) {
b, err := bs.ChainInfoFetcher.HeadBlock(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get head block: %v", err)
return nil, errors.Wrap(err, "could not get head block")
}
if err := helpers.VerifyNilBeaconBlock(b); err != nil {
return nil, err
@@ -218,7 +220,7 @@ func (bs *Server) headStateRoot(ctx context.Context) ([]byte, error) {
func (bs *Server) genesisStateRoot(ctx context.Context) ([]byte, error) {
b, err := bs.BeaconDB.GenesisBlock(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get genesis block: %v", err)
return nil, errors.Wrap(err, "could not get genesis block")
}
if err := helpers.VerifyNilBeaconBlock(b); err != nil {
return nil, err
@@ -229,11 +231,11 @@ func (bs *Server) genesisStateRoot(ctx context.Context) ([]byte, error) {
func (bs *Server) finalizedStateRoot(ctx context.Context) ([]byte, error) {
cp, err := bs.BeaconDB.FinalizedCheckpoint(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get finalized checkpoint: %v", err)
return nil, errors.Wrap(err, "could not get finalized checkpoint")
}
b, err := bs.BeaconDB.Block(ctx, bytesutil.ToBytes32(cp.Root))
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get finalized block: %v", err)
return nil, errors.Wrap(err, "could not get finalized block")
}
if err := helpers.VerifyNilBeaconBlock(b); err != nil {
return nil, err
@@ -244,11 +246,11 @@ func (bs *Server) finalizedStateRoot(ctx context.Context) ([]byte, error) {
func (bs *Server) justifiedStateRoot(ctx context.Context) ([]byte, error) {
cp, err := bs.BeaconDB.JustifiedCheckpoint(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get justified checkpoint: %v", err)
return nil, errors.Wrap(err, "could not get justified checkpoint")
}
b, err := bs.BeaconDB.Block(ctx, bytesutil.ToBytes32(cp.Root))
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get justified block: %v", err)
return nil, errors.Wrap(err, "could not get justified block")
}
if err := helpers.VerifyNilBeaconBlock(b); err != nil {
return nil, err
@@ -261,35 +263,33 @@ func (bs *Server) stateRootByHex(ctx context.Context, stateId []byte) ([]byte, e
copy(stateRoot[:], stateId)
headState, err := bs.ChainInfoFetcher.HeadState(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get head state: %v", err)
return nil, errors.Wrap(err, "could not get head state")
}
for _, root := range headState.StateRoots() {
if bytes.Equal(root, stateRoot[:]) {
return stateRoot[:], nil
}
}
return nil, status.Errorf(
codes.NotFound,
"State not found in the last %d state roots in head state", len(headState.StateRoots()))
return nil, fmt.Errorf("state not found in the last %d state roots in head state", len(headState.StateRoots()))
}
func (bs *Server) stateRootBySlot(ctx context.Context, slot types.Slot) ([]byte, error) {
currentSlot := bs.GenesisTimeFetcher.CurrentSlot()
if slot > currentSlot {
return nil, status.Errorf(codes.Internal, "Slot cannot be in the future")
return nil, errors.New("slot cannot be in the future")
}
found, blks, err := bs.BeaconDB.BlocksBySlot(ctx, slot)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get blocks: %v", err)
return nil, errors.Wrap(err, "could not get blocks")
}
if !found {
return nil, status.Errorf(codes.NotFound, "No block exists")
return nil, errors.New("no block exists")
}
if len(blks) != 1 {
return nil, status.Errorf(codes.Internal, "Multiple blocks exist in same slot")
return nil, errors.New("multiple blocks exist in same slot")
}
if blks[0] == nil || blks[0].Block == nil {
return nil, status.Error(codes.Internal, "Nil block")
return nil, errors.New("nil block")
}
return blks[0].Block.StateRoot, nil
}
@@ -297,7 +297,7 @@ func (bs *Server) stateRootBySlot(ctx context.Context, slot types.Slot) ([]byte,
func (bs *Server) stateByHex(ctx context.Context, stateId []byte) (iface.BeaconState, error) {
headState, err := bs.ChainInfoFetcher.HeadState(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get head state: %v", err)
return nil, errors.Wrap(err, "could not get head state")
}
for i, root := range headState.StateRoots() {
if bytes.Equal(root, stateId) {
@@ -305,19 +305,17 @@ func (bs *Server) stateByHex(ctx context.Context, stateId []byte) (iface.BeaconS
return bs.StateGenService.StateByRoot(ctx, bytesutil.ToBytes32(blockRoot))
}
}
return nil, status.Errorf(
codes.NotFound,
"State not found in the last %d state roots in head state", len(headState.StateRoots()))
return nil, fmt.Errorf("state not found in the last %d state roots in head state", len(headState.StateRoots()))
}
func (bs *Server) stateBySlot(ctx context.Context, slot types.Slot) (iface.BeaconState, error) {
currentSlot := bs.GenesisTimeFetcher.CurrentSlot()
if slot > currentSlot {
return nil, status.Errorf(codes.Internal, "Slot cannot be in the future")
return nil, errors.New("slot cannot be in the future")
}
state, err := bs.StateGenService.StateBySlot(ctx, slot)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get state: %v", err)
return nil, errors.Wrap(err, "could not get state")
}
return state, nil
}

View File

@@ -214,7 +214,7 @@ func TestGetStateRoot(t *testing.T) {
_, err = s.GetStateRoot(ctx, &ethpb.StateRequest{
StateId: stateId,
})
assert.ErrorContains(t, fmt.Sprintf("State not found in the last %d state roots in head state", len(state.StateRoots())), err)
assert.ErrorContains(t, fmt.Sprintf("state not found in the last %d state roots in head state", len(state.StateRoots())), err)
})
t.Run("Slot", func(t *testing.T) {
@@ -251,7 +251,7 @@ func TestGetStateRoot(t *testing.T) {
_, err := s.GetStateRoot(ctx, &ethpb.StateRequest{
StateId: []byte("100"),
})
assert.ErrorContains(t, "Multiple blocks exist in same slot", err)
assert.ErrorContains(t, "multiple blocks exist in same slot", err)
})
t.Run("Slot too big", func(t *testing.T) {
@@ -263,7 +263,7 @@ func TestGetStateRoot(t *testing.T) {
_, err := s.GetStateRoot(ctx, &ethpb.StateRequest{
StateId: []byte(strconv.FormatUint(1, 10)),
})
assert.ErrorContains(t, "Slot cannot be in the future", err)
assert.ErrorContains(t, "slot cannot be in the future", err)
})
t.Run("Invalid state", func(t *testing.T) {
@@ -271,7 +271,7 @@ func TestGetStateRoot(t *testing.T) {
_, err := s.GetStateRoot(ctx, &ethpb.StateRequest{
StateId: []byte("foo"),
})
require.ErrorContains(t, "Invalid state ID: foo", err)
require.ErrorContains(t, "invalid state ID: foo", err)
})
}
@@ -431,7 +431,7 @@ func TestGetStateFork(t *testing.T) {
_, err = s.GetStateFork(ctx, &ethpb.StateRequest{
StateId: stateId,
})
require.ErrorContains(t, "State not found in the last 8192 state roots in head state", err)
require.ErrorContains(t, "state not found in the last 8192 state roots in head state", err)
})
t.Run("Slot", func(t *testing.T) {
@@ -461,7 +461,7 @@ func TestGetStateFork(t *testing.T) {
_, err := s.GetStateFork(ctx, &ethpb.StateRequest{
StateId: []byte(strconv.FormatUint(1, 10)),
})
assert.ErrorContains(t, "Slot cannot be in the future", err)
assert.ErrorContains(t, "slot cannot be in the future", err)
})
t.Run("Invalid state", func(t *testing.T) {
@@ -469,7 +469,7 @@ func TestGetStateFork(t *testing.T) {
_, err := s.GetStateFork(ctx, &ethpb.StateRequest{
StateId: []byte("foo"),
})
require.ErrorContains(t, "Invalid state ID: foo", err)
require.ErrorContains(t, "invalid state ID: foo", err)
})
}
@@ -658,7 +658,7 @@ func TestGetFinalityCheckpoints(t *testing.T) {
_, err = s.GetFinalityCheckpoints(ctx, &ethpb.StateRequest{
StateId: stateId,
})
require.ErrorContains(t, "State not found in the last 8192 state roots in head state", err)
require.ErrorContains(t, "state not found in the last 8192 state roots in head state", err)
})
t.Run("Slot", func(t *testing.T) {
@@ -691,7 +691,7 @@ func TestGetFinalityCheckpoints(t *testing.T) {
_, err := s.GetFinalityCheckpoints(ctx, &ethpb.StateRequest{
StateId: []byte(strconv.FormatUint(1, 10)),
})
assert.ErrorContains(t, "Slot cannot be in the future", err)
assert.ErrorContains(t, "slot cannot be in the future", err)
})
t.Run("Checkpoints not available", func(t *testing.T) {
@@ -725,6 +725,6 @@ func TestGetFinalityCheckpoints(t *testing.T) {
_, err := s.GetFinalityCheckpoints(ctx, &ethpb.StateRequest{
StateId: []byte("foo"),
})
require.ErrorContains(t, "Invalid state ID: foo", err)
require.ErrorContains(t, "invalid state ID: foo", err)
})
}

View File

@@ -131,7 +131,7 @@ func (ns *Server) ListPeers(ctx context.Context, req *ethpb.PeersRequest) (*ethp
for _, id := range allIds {
p, err := peerInfo(peerStatus, id)
if err != nil {
return nil, err
return nil, status.Errorf(codes.Internal, "Could not get peer info: %v", err)
}
allPeers = append(allPeers, p)
}
@@ -199,7 +199,7 @@ func (ns *Server) ListPeers(ctx context.Context, req *ethpb.PeersRequest) (*ethp
for _, id := range filteredIds {
p, err := peerInfo(peerStatus, id)
if err != nil {
return nil, err
return nil, status.Errorf(codes.Internal, "Could not get peer info: %v", err)
}
filteredPeers = append(filteredPeers, p)
}
@@ -298,23 +298,23 @@ func (ns *Server) handleEmptyFilters(req *ethpb.PeersRequest, peerStatus *peers.
func peerInfo(peerStatus *peers.Status, id peer.ID) (*ethpb.Peer, error) {
enr, err := peerStatus.ENR(id)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not obtain ENR: %v", err)
return nil, errors.Wrap(err, "could not obtain ENR")
}
serializedEnr, err := p2p.SerializeENR(enr)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not serialize ENR: %v", err)
return nil, errors.Wrap(err, "could not serialize ENR")
}
address, err := peerStatus.Address(id)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not obtain address: %v", err)
return nil, errors.Wrap(err, "could not obtain address")
}
connectionState, err := peerStatus.ConnectionState(id)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not obtain connection state: %v", err)
return nil, errors.Wrap(err, "could not obtain connection state")
}
direction, err := peerStatus.Direction(id)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not obtain direction: %v", err)
return nil, errors.Wrap(err, "could not obtain direction")
}
p := ethpb.Peer{
PeerId: id.Pretty(),