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

chore(x/feegrant): use cosmossdk.io/core/codec instead of github.com/cosmos/cosmos-sdk/codec #23297

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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/feegrant/migrations/v2/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"context"

"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/codec"
"cosmossdk.io/core/store"
"cosmossdk.io/store/prefix"
"cosmossdk.io/x/feegrant"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/runtime"
)

Expand Down
8 changes: 6 additions & 2 deletions x/feegrant/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
"google.golang.org/grpc"

"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/codec"
"cosmossdk.io/core/registry"
"cosmossdk.io/errors"
"cosmossdk.io/x/feegrant"
"cosmossdk.io/x/feegrant/client/cli"
"cosmossdk.io/x/feegrant/keeper"

sdkclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/types/module"
)
Expand Down Expand Up @@ -103,7 +103,11 @@ func (am AppModule) RegisterMigrations(mr appmodule.MigrationRegistrar) error {

// DefaultGenesis returns default genesis state as raw bytes for the feegrant module.
func (am AppModule) DefaultGenesis() json.RawMessage {
return am.cdc.MustMarshalJSON(feegrant.DefaultGenesisState())
data, err := am.cdc.MarshalJSON(feegrant.DefaultGenesisState())
if err != nil {
panic(err)
}
return data
Comment on lines +106 to +110
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

Consider using error propagation instead of panic.

While the error handling is more explicit now, using panic in production code is generally discouraged. Consider propagating the error up the call stack by updating the interface to return an error.

-func (am AppModule) DefaultGenesis() json.RawMessage {
+func (am AppModule) DefaultGenesis() (json.RawMessage, error) {
 	data, err := am.cdc.MarshalJSON(feegrant.DefaultGenesisState())
 	if err != nil {
-		panic(err)
+		return nil, fmt.Errorf("failed to marshal default genesis state: %w", err)
 	}
-	return data
+	return data, nil
}

Committable suggestion skipped: line range outside the PR's diff.

}

// ValidateGenesis performs genesis state validation for the feegrant module.
Expand Down
10 changes: 7 additions & 3 deletions x/feegrant/simulation/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"bytes"
"fmt"

"cosmossdk.io/core/codec"
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 import ordering using gci.

The imports need to be reordered according to the project's guidelines using gci with the following configuration:

gci --skip-generated -s standard -s default -s prefix(cosmossdk.io) -s prefix(github.com/cosmos/cosmos-sdk)

"cosmossdk.io/x/feegrant"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/kv"
)

Expand All @@ -17,8 +17,12 @@ func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string {
switch {
case bytes.Equal(kvA.Key[:1], feegrant.FeeAllowanceKeyPrefix):
var grantA, grantB feegrant.Grant
cdc.MustUnmarshal(kvA.Value, &grantA)
cdc.MustUnmarshal(kvB.Value, &grantB)
if err := cdc.Unmarshal(kvA.Value, &grantA); err != nil {
panic(err)
}
if err := cdc.Unmarshal(kvB.Value, &grantB); err != nil {
panic(err)
}
Comment on lines +20 to +25
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

Consider using error propagation instead of panic.

While the error handling is more explicit now, using panic in production code is generally discouraged. Consider propagating the error up the call stack instead.

-func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string {
+func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) (string, error) {
-	return func(kvA, kvB kv.Pair) string {
+	return func(kvA, kvB kv.Pair) (string, error) {
 		switch {
 		case bytes.Equal(kvA.Key[:1], feegrant.FeeAllowanceKeyPrefix):
 			var grantA, grantB feegrant.Grant
 			if err := cdc.Unmarshal(kvA.Value, &grantA); err != nil {
-				panic(err)
+				return "", fmt.Errorf("failed to unmarshal grant A: %w", err)
 			}
 			if err := cdc.Unmarshal(kvB.Value, &grantB); err != nil {
-				panic(err)
+				return "", fmt.Errorf("failed to unmarshal grant B: %w", err)
 			}
-			return fmt.Sprintf("%v\n%v", grantA, grantB)
+			return fmt.Sprintf("%v\n%v", grantA, grantB), nil
 		default:
-			panic(fmt.Sprintf("invalid feegrant key %X", kvA.Key))
+			return "", fmt.Errorf("invalid feegrant key %X", kvA.Key)
 		}
 	}
 }
📝 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 := cdc.Unmarshal(kvA.Value, &grantA); err != nil {
panic(err)
}
if err := cdc.Unmarshal(kvB.Value, &grantB); err != nil {
panic(err)
}
func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) (string, error) {
return func(kvA, kvB kv.Pair) (string, error) {
switch {
case bytes.Equal(kvA.Key[:1], feegrant.FeeAllowanceKeyPrefix):
var grantA, grantB feegrant.Grant
if err := cdc.Unmarshal(kvA.Value, &grantA); err != nil {
return "", fmt.Errorf("failed to unmarshal grant A: %w", err)
}
if err := cdc.Unmarshal(kvB.Value, &grantB); err != nil {
return "", fmt.Errorf("failed to unmarshal grant B: %w", err)
}
return fmt.Sprintf("%v\n%v", grantA, grantB), nil
default:
return "", fmt.Errorf("invalid feegrant key %X", kvA.Key)
}
}
}

return fmt.Sprintf("%v\n%v", grantA, grantB)
default:
panic(fmt.Sprintf("invalid feegrant key %X", kvA.Key))
Expand Down
Loading