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

[Code Health] fix: gateway module gRPC status error returns #955

Merged
merged 4 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion x/gateway/keeper/msg_server_stake_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (k msgServer) StakeGateway(
if err != nil {
// TODO_TECHDEBT(#384): determine whether to continue using cosmos logger for debug level.
logger.Error(fmt.Sprintf("could not escrowed %v coins from %q to %q module account due to %v", coinsToEscrow, gatewayAddress, types.ModuleName, err))
return nil, err
return nil, status.Error(codes.Internal, err.Error())
}

// Update the Gateway in the store
Expand Down
18 changes: 12 additions & 6 deletions x/gateway/keeper/msg_server_unstake_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,36 @@ func (k msgServer) UnstakeGateway(
logger.Info(fmt.Sprintf("About to unstake gateway with msg: %v", msg))

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

// Check if the gateway already exists or not
var err error
gateway, isGatewayFound := k.GetGateway(ctx, msg.Address)
if !isGatewayFound {
logger.Info(fmt.Sprintf("Gateway not found. Cannot unstake address %s", msg.Address))
return nil, types.ErrGatewayNotFound
return nil, status.Error(
codes.NotFound,
types.ErrGatewayNotFound.Wrapf(
"gateway with address %s", msg.Address,
).Error(),
)
}
logger.Info(fmt.Sprintf("Gateway found. Unstaking gateway for address %s", msg.Address))

// Retrieve the address of the gateway
gatewayAddress, err := sdk.AccAddressFromBech32(msg.Address)
if err != nil {
logger.Error(fmt.Sprintf("could not parse address %s", msg.Address))
return nil, err
logger.Info(fmt.Sprintf("could not parse address %s", msg.Address))
return nil, status.Error(codes.InvalidArgument, err.Error())
}

// Send the coins from the gateway pool back to the gateway
err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, gatewayAddress, []sdk.Coin{*gateway.Stake})
if err != nil {
logger.Error(fmt.Sprintf("could not send %v coins from %s module to %s account due to %v", gateway.Stake, gatewayAddress, types.ModuleName, err))
return nil, err
err = fmt.Errorf("could not send %v coins from %s module to %s account due to %v", gateway.Stake, gatewayAddress, types.ModuleName, err)
logger.Error(err.Error())
return nil, status.Error(codes.Internal, err.Error())
}

// Update the Gateway in the store
Expand Down
2 changes: 1 addition & 1 deletion x/gateway/keeper/msg_server_unstake_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestMsgServer_UnstakeGateway_FailIfNotStaked(t *testing.T) {
unstakeMsg := &types.MsgUnstakeGateway{Address: addr}
_, err := srv.UnstakeGateway(ctx, unstakeMsg)
require.Error(t, err)
require.ErrorIs(t, err, types.ErrGatewayNotFound)
require.ErrorContains(t, err, types.ErrGatewayNotFound.Error())

_, isGatewayFound = k.GetGateway(ctx, addr)
require.False(t, isGatewayFound)
Expand Down
18 changes: 15 additions & 3 deletions x/gateway/keeper/msg_update_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package keeper

import (
"context"
"fmt"

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

"github.com/pokt-network/poktroll/x/gateway/types"
)
Expand All @@ -12,17 +15,26 @@ func (k msgServer) UpdateParams(
goCtx 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.ErrGatewayInvalidSigner.Wrapf("invalid authority; expected %s, got %s", k.GetAuthority(), req.Authority)
return nil, status.Error(
codes.PermissionDenied,
types.ErrGatewayInvalidSigner.Wrapf(
"invalid authority; expected %s, got %s", k.GetAuthority(), req.Authority,
).Error(),
)
}

ctx := sdk.UnwrapSDKContext(goCtx)
// NOTE(#322): Omitted parameters will be set to their zero value.
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())
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
}

return &types.MsgUpdateParamsResponse{}, nil
Expand Down
5 changes: 4 additions & 1 deletion x/gateway/keeper/query_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
)

func (k Keeper) AllGateways(ctx context.Context, req *types.QueryAllGatewaysRequest) (*types.QueryAllGatewaysResponse, error) {
logger := k.Logger().With("method", "AllGateways")

if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
Expand All @@ -26,7 +28,8 @@ func (k Keeper) AllGateways(ctx context.Context, req *types.QueryAllGatewaysRequ
pageRes, err := query.Paginate(gatewayStore, req.Pagination, func(key []byte, value []byte) error {
var gateway types.Gateway
if err := k.cdc.Unmarshal(value, &gateway); err != nil {
return err
logger.Error(fmt.Sprintf("unmarshaling gateway with key (hex): %x: %+v", key, err))
return status.Error(codes.Internal, err.Error())
}

gateways = append(gateways, gateway)
Expand Down
Loading