Skip to content

Commit

Permalink
refactor(x/accounts): Skip Importing Unregistered Genesis Account Typ…
Browse files Browse the repository at this point in the history
…es (#20053)

Signed-off-by: Hwangjae Lee <meetrick@gmail.com>
  • Loading branch information
meetrick committed Apr 16, 2024
1 parent 9d6c6ef commit 2301e5e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
8 changes: 7 additions & 1 deletion x/accounts/genesis.go
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
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")
}

0 comments on commit 2301e5e

Please sign in to comment.