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

fix(feetiers): Reject malformed address in query #2653

Merged
merged 4 commits into from
Dec 13, 2024
Merged
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions protocol/x/feetiers/keeper/grpc_query.go
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@ package keeper
import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/x/feetiers/types"
"google.golang.org/grpc/codes"
@@ -42,6 +44,10 @@ func (k Keeper) UserFeeTier(
}

ctx := lib.UnwrapSDKContext(c, types.ModuleName)

if _, err := sdk.AccAddressFromBech32(req.User); err != nil {
return nil, status.Error(codes.InvalidArgument, "user address is valid bech32 address")
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix grammatical error in error message

The error message has incorrect grammar which could confuse users.

Apply this fix:

-		return nil, status.Error(codes.InvalidArgument, "user address is valid bech32 address")
+		return nil, status.Error(codes.InvalidArgument, "user address is not a valid bech32 address")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if _, err := sdk.AccAddressFromBech32(req.User); err != nil {
return nil, status.Error(codes.InvalidArgument, "user address is valid bech32 address")
}
if _, err := sdk.AccAddressFromBech32(req.User); err != nil {
return nil, status.Error(codes.InvalidArgument, "user address is not a valid bech32 address")
}

index, tier := k.getUserFeeTier(ctx, req.User)
return &types.QueryUserFeeTierResponse{
Index: index,
9 changes: 8 additions & 1 deletion protocol/x/feetiers/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
@@ -58,7 +58,7 @@ func TestUserFeeTier(t *testing.T) {
}{
"Success": {
req: &types.QueryUserFeeTierRequest{
User: "alice",
User: "dydx199tqg4wdlnu4qjlxchpd7seg454937hjrknju4",
},
res: &types.QueryUserFeeTierResponse{
Index: 0,
@@ -78,6 +78,13 @@ func TestUserFeeTier(t *testing.T) {
res: nil,
err: status.Error(codes.InvalidArgument, "invalid request"),
},
"Malformed address": {
req: &types.QueryUserFeeTierRequest{
User: "alice",
},
res: nil,
err: status.Error(codes.InvalidArgument, "user address is valid bech32 address"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the error message be saying the address is invalid?

},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Update error message to match implementation fix

Once the error message is fixed in the implementation, update this test case to match the new message.

Apply this fix after updating the implementation:

-			err: status.Error(codes.InvalidArgument, "user address is valid bech32 address"),
+			err: status.Error(codes.InvalidArgument, "user address is not a valid bech32 address"),
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"Malformed address": {
req: &types.QueryUserFeeTierRequest{
User: "alice",
},
res: nil,
err: status.Error(codes.InvalidArgument, "user address is valid bech32 address"),
},
"Malformed address": {
req: &types.QueryUserFeeTierRequest{
User: "alice",
},
res: nil,
err: status.Error(codes.InvalidArgument, "user address is not a valid bech32 address"),
},

} {
t.Run(name, func(t *testing.T) {
res, err := k.UserFeeTier(ctx, tc.req)