neutron-org / neutron

Smart Contract platform secured by Cosmos Hub
https://neutron.org
Apache License 2.0
119 stars 95 forks source link

Optimizable `isModuleAccount` Implementation #775

Open Hellobloc opened 1 day ago

Hellobloc commented 1 day ago

The current implementation of isModuleAccount relie on iterating through knownModules to determine whether an address is a module account. https://github.com/neutron-org/neutron/blob/a6525cb3375631175a69086bdc3314f6f637c0ac/x/tokenfactory/keeper/bankactions.go#L92-L105

However, this approach has two notable drawbacks. First, the use of loop iteration for validation can be resource-intensive. Second, since knownModules is a configuration parameter initialized with the keeper, any updates introducing new modules might lead to omissions.

Given these considerations, it would be more efficient and reliable to determine whether an address is a module account by directly checking its account type.

func (k Keeper) isModuleAccount(ctx sdk.Context, addr sdk.AccAddress) bool {
    acc := k.authKeeper.GetAccount(ctx, addr)
    if acc == nil {
        return false
    }
    _, ok := acc.(authtypes.ModuleAccountI)
    return ok
}
pr0n00gler commented 1 day ago

Hey-hey, seems like it's a decent change. What do you think of opening a PR?