Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 10 commits into from
Mar 17, 2021
62 changes: 30 additions & 32 deletions beacon-chain/rpc/beaconv1/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand All @@ -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()
Expand All @@ -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{
Expand Down Expand Up @@ -137,15 +139,15 @@ 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)
} else {
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))
}
Expand All @@ -165,37 +167,37 @@ 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)
} else {
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))
}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -261,63 +263,59 @@ 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
}

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) {
blockRoot := headState.BlockRoots()[i]
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
}
Expand Down
20 changes: 10 additions & 10 deletions beacon-chain/rpc/beaconv1/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -263,15 +263,15 @@ 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) {
s := Server{}
_, 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)
})
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -461,15 +461,15 @@ 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) {
s := Server{}
_, 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)
})
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
})
}
14 changes: 7 additions & 7 deletions beacon-chain/rpc/nodev1/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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(),
Expand Down