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

Signed-off-by: Hwangjae Lee <meetrick@gmail.com>
This commit is contained in:
Hwangjae Lee 2024-04-17 05:48:21 +09:00 committed by GitHub
parent 9d6c6ef0ba
commit 2301e5e73a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 1 deletions

View File

@ -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

View File

@ -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")
}