diff --git a/x/accounts/genesis.go b/x/accounts/genesis.go index a760344ab0..bd0f9edc0b 100644 --- a/x/accounts/genesis.go +++ b/x/accounts/genesis.go @@ -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 diff --git a/x/accounts/genesis_test.go b/x/accounts/genesis_test.go index 0c5c25b133..5b143696cb 100644 --- a/x/accounts/genesis_test.go +++ b/x/accounts/genesis_test.go @@ -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) { @@ -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") +}