Skip to content

Commit

Permalink
[Code Health] fix: session module gRPC status return errors (#960)
Browse files Browse the repository at this point in the history
## Summary

Ensure all session message and query handlers return gRPC status errors.

## Issue

- #860 

## Type of change

Select one or more from the following:

- [ ] New feature, functionality or library
- [x] Consensus breaking; add the `consensus-breaking` label if so. See
#791 for details
- [ ] Bug fix
- [x] Code health or cleanup
- [ ] Documentation
- [ ] Other (specify)

## Testing

- [ ] **Documentation**: `make docusaurus_start`; only needed if you
make doc changes
- [x] **Unit Tests**: `make go_develop_and_test`
- [ ] **LocalNet E2E Tests**: `make test_e2e`
- [x] **DevNet E2E Tests**: Add the `devnet-test-e2e` label to the PR.

## Sanity Checklist

- [x] I have tested my changes using the available tooling
- [ ] I have commented my code
- [x] I have performed a self-review of my own code; both comments &
source code
- [ ] I create and reference any new tickets, if applicable
- [ ] I have left TODOs throughout the codebase, if applicable
  • Loading branch information
bryanchriswhite authored Dec 2, 2024
1 parent 1c490ce commit 41a373b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
4 changes: 4 additions & 0 deletions x/proof/keeper/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package keeper

import (
"context"
"fmt"

cosmostypes "github.com/cosmos/cosmos-sdk/types"
"google.golang.org/grpc/status"

"github.com/pokt-network/poktroll/x/proof/types"
sessiontypes "github.com/pokt-network/poktroll/x/session/types"
Expand All @@ -30,6 +32,8 @@ func (k Keeper) queryAndValidateSessionHeader(
// session header is to be validated.
sessionRes, err := k.sessionKeeper.GetSession(ctx, sessionReq)
if err != nil {
// NB: Strip internal error status from error. An appropriate status will be associated by the caller.
err = fmt.Errorf("%s", status.Convert(err).Message())
return nil, err
}
onChainSession := sessionRes.GetSession()
Expand Down
19 changes: 16 additions & 3 deletions x/session/keeper/msg_update_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,34 @@ package keeper

import (
"context"
"fmt"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/pokt-network/poktroll/x/session/types"
)

func (k msgServer) UpdateParams(ctx context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
logger := k.Logger().With("method", "UpdateParams")

if err := req.ValidateBasic(); err != nil {
return nil, err
return nil, status.Error(codes.InvalidArgument, err.Error())
}

if k.GetAuthority() != req.Authority {
return nil, types.ErrSessionInvalidSigner.Wrapf("invalid authority; expected %s, got %s", k.GetAuthority(), req.Authority)
return nil, status.Error(
codes.PermissionDenied,
types.ErrSessionInvalidSigner.Wrapf(
"invalid authority; expected %s, got %s", k.GetAuthority(), req.Authority,
).Error(),
)
}

if err := k.SetParams(ctx, req.Params); err != nil {
return nil, err
err = fmt.Errorf("unable to set params: %w", err)
logger.Error(err.Error())
return nil, status.Error(codes.Internal, err.Error())
}

return &types.MsgUpdateParamsResponse{}, nil
Expand Down
7 changes: 5 additions & 2 deletions x/session/keeper/query_get_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
// GetSession should be deterministic and always return the same session for
// the same block height.
func (k Keeper) GetSession(ctx context.Context, req *types.QueryGetSessionRequest) (*types.QueryGetSessionResponse, error) {
logger := k.Logger().With("method", "GetSession")

if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
Expand All @@ -36,12 +38,13 @@ func (k Keeper) GetSession(ctx context.Context, req *types.QueryGetSessionReques
blockHeight = req.BlockHeight
}

k.Logger().Debug(fmt.Sprintf("Getting session for height: %d", blockHeight))
logger.Debug(fmt.Sprintf("Getting session for height: %d", blockHeight))

sessionHydrator := NewSessionHydrator(req.ApplicationAddress, req.ServiceId, blockHeight)
session, err := k.HydrateSession(ctx, sessionHydrator)
if err != nil {
return nil, err
logger.Error(err.Error())
return nil, status.Error(codes.Internal, err.Error())
}

res := &types.QueryGetSessionResponse{
Expand Down

0 comments on commit 41a373b

Please sign in to comment.