Skip to content

Commit

Permalink
feat(app)!: migrate pre-initialized module accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
haiyizxx committed Nov 5, 2024
1 parent d8e91da commit 1ab2c43
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 9 deletions.
14 changes: 5 additions & 9 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,18 +477,14 @@ func (app *AxelarApp) setUpgradeBehaviour(configurator module.Configurator, keep
upgradeKeeper.SetUpgradeHandler(
upgradeName(app.Version()),
func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
updatedVM, err := app.mm.RunMigrations(ctx, configurator, fromVM)
err := MigratePreInitializedModuleAccounts(ctx, *GetKeeper[authkeeper.AccountKeeper](keepers), []string{nexusTypes.ModuleName})

Check warning on line 480 in app/app.go

View check run for this annotation

Codecov / codecov/patch

app/app.go#L480

Added line #L480 was not covered by tests
if err != nil {
return updatedVM, err
return nil, err

Check warning on line 482 in app/app.go

View check run for this annotation

Codecov / codecov/patch

app/app.go#L482

Added line #L482 was not covered by tests
}

// TODO: remove after v35 upgrade
// Override wasm module default params
if upgradeName(app.Version()) == "v0.35" && IsWasmEnabled() {
GetKeeper[wasm.Keeper](keepers).SetParams(ctx, wasmtypes.Params{
CodeUploadAccess: wasmtypes.AllowNobody,
InstantiateDefaultPermission: wasmtypes.AccessTypeNobody,
})
updatedVM, err := app.mm.RunMigrations(ctx, configurator, fromVM)
if err != nil {
return updatedVM, err

Check warning on line 487 in app/app.go

View check run for this annotation

Codecov / codecov/patch

app/app.go#L485-L487

Added lines #L485 - L487 were not covered by tests
}

return updatedVM, err
Expand Down
66 changes: 66 additions & 0 deletions app/migrate_module_account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package app

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)

// MigratePreInitializedModuleAccounts migrates module accounts that were pre-initialized as BaseAccounts to ModuleAccounts.
func MigratePreInitializedModuleAccounts(
ctx sdk.Context,
ak authkeeper.AccountKeeper,
moduleAccountsToInitialize []string,
) error {
for _, module := range moduleAccountsToInitialize {
addr, perms := ak.GetModuleAddressAndPermissions(module)
if addr == nil {
return fmt.Errorf(
"failed to get module address and permissions for module %s",
module,
)
}

Check warning on line 24 in app/migrate_module_account.go

View check run for this annotation

Codecov / codecov/patch

app/migrate_module_account.go#L16-L24

Added lines #L16 - L24 were not covered by tests

acc := ak.GetAccount(ctx, addr)
if acc == nil {
ctx.Logger().Info(fmt.Sprintf(
"account for module %s has not been initialized yet, skipping",
module,
))
continue

Check warning on line 32 in app/migrate_module_account.go

View check run for this annotation

Codecov / codecov/patch

app/migrate_module_account.go#L26-L32

Added lines #L26 - L32 were not covered by tests
}

_, isModuleAccount := acc.(authtypes.ModuleAccountI)
if isModuleAccount {
ctx.Logger().Info(fmt.Sprintf(
"account for module %s was correctly initialized, skipping",
module,
))
continue

Check warning on line 41 in app/migrate_module_account.go

View check run for this annotation

Codecov / codecov/patch

app/migrate_module_account.go#L35-L41

Added lines #L35 - L41 were not covered by tests
}

// Migrate from base account to module account
baseAccount, ok := acc.(*authtypes.BaseAccount)
if !ok {
panic(fmt.Sprintf("account %s must be a base account", acc.GetAddress()))

Check warning on line 47 in app/migrate_module_account.go

View check run for this annotation

Codecov / codecov/patch

app/migrate_module_account.go#L45-L47

Added lines #L45 - L47 were not covered by tests
}

newModuleAccount := authtypes.NewModuleAccount(
baseAccount,
module,
perms...,
)
ak.SetModuleAccount(ctx, newModuleAccount)

ctx.Logger().Info(fmt.Sprintf(
"Successfully migrated from base account %+v to module account %+v for module %s",
baseAccount,
newModuleAccount,
module,
))

Check warning on line 62 in app/migrate_module_account.go

View check run for this annotation

Codecov / codecov/patch

app/migrate_module_account.go#L50-L62

Added lines #L50 - L62 were not covered by tests
}

return nil

Check warning on line 65 in app/migrate_module_account.go

View check run for this annotation

Codecov / codecov/patch

app/migrate_module_account.go#L65

Added line #L65 was not covered by tests
}

0 comments on commit 1ab2c43

Please sign in to comment.