Skip to content

Optimizable isModuleAccount Implementation #775

@Hellobloc

Description

@Hellobloc

The current implementation of isModuleAccount relie on iterating through knownModules to determine whether an address is a module account.

func (k Keeper) isModuleAccount(ctx sdk.Context, addr sdk.AccAddress) bool {
for _, moduleName := range k.knownModules {
account := k.accountKeeper.GetModuleAccount(ctx, moduleName)
if account == nil {
continue
}
if account.GetAddress().Equals(addr) {
return true
}
}
return false
}

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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions