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

refactor(x/accounts): Skip Importing Unregistered Genesis Account Types #20053

Merged
merged 1 commit into from
Apr 16, 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
8 changes: 7 additions & 1 deletion x/accounts/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ func (k Keeper) ImportState(ctx context.Context, genState *v1.GenesisState) erro
}

func (k Keeper) importAccount(ctx context.Context, acc *v1.GenesisAccount) error {
// TODO: maybe check if impl exists?
// Check if the account type exists in the registered accounts
_, ok := k.accounts[acc.AccountType]
if !ok {
// If the account type does not exist, return an error
return fmt.Errorf("account type %s not found in the registered accounts", acc.AccountType)
}

addrBytes, err := k.addressCodec.StringToBytes(acc.Address)
if err != nil {
return err
Expand Down
26 changes: 26 additions & 0 deletions x/accounts/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"cosmossdk.io/collections/colltest"
"cosmossdk.io/x/accounts/internal/implementation"
v1 "cosmossdk.io/x/accounts/v1"
)

func TestGenesis(t *testing.T) {
Expand Down Expand Up @@ -48,3 +49,28 @@ func TestGenesis(t *testing.T) {
require.NoError(t, err)
require.Equal(t, &types.UInt64Value{Value: 20}, resp)
}

func TestImportAccountError(t *testing.T) {
// Initialize the keeper and context for testing
k, ctx := newKeeper(t, func(deps implementation.Dependencies) (string, implementation.Account, error) {
acc, err := NewTestAccount(deps)
return "test", acc, err
})

// Define a mock GenesisAccount with a non-existent account type
acc := &v1.GenesisAccount{
Address: "test-address",
AccountType: "non-existent-type",
AccountNumber: 1,
State: nil,
}

// Attempt to import the mock GenesisAccount into the state
err := k.importAccount(ctx, acc)

// Assert that an error is returned
require.Error(t, err)

// Assert that the error message contains the expected substring
require.Contains(t, err.Error(), "account type non-existent-type not found in the registered accounts")
}
Loading