-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add v3.0.0 upgrade handler with module account initialization (#812)
- Loading branch information
Showing
4 changed files
with
137 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package v_3_0_0 | ||
|
||
import ( | ||
store "github.com/cosmos/cosmos-sdk/store/types" | ||
"github.com/dydxprotocol/v4-chain/protocol/app/upgrades" | ||
) | ||
|
||
const ( | ||
UpgradeName = "v3.0.0" | ||
) | ||
|
||
var Upgrade = upgrades.Upgrade{ | ||
UpgradeName: UpgradeName, | ||
StoreUpgrades: store.StoreUpgrades{}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package v_3_0_0 | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/module" | ||
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" | ||
bridgemoduletypes "github.com/dydxprotocol/v4-chain/protocol/x/bridge/types" | ||
clobmoduletypes "github.com/dydxprotocol/v4-chain/protocol/x/clob/types" | ||
rewardsmoduletypes "github.com/dydxprotocol/v4-chain/protocol/x/rewards/types" | ||
satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types" | ||
vestmoduletypes "github.com/dydxprotocol/v4-chain/protocol/x/vest/types" | ||
) | ||
|
||
var ( | ||
// List of module accounts to check in state. | ||
// These include all dYdX custom module accounts. | ||
ModuleAccsToInitialize = []string{ | ||
bridgemoduletypes.ModuleName, | ||
satypes.ModuleName, | ||
clobmoduletypes.InsuranceFundName, | ||
rewardsmoduletypes.TreasuryAccountName, | ||
rewardsmoduletypes.VesterAccountName, | ||
vestmoduletypes.CommunityTreasuryAccountName, | ||
vestmoduletypes.CommunityVesterAccountName, | ||
} | ||
) | ||
|
||
// This module account initialization logic is copied from v2.0.0 upgrade handler. | ||
// Testnet is to be upgraded from v1.0.1 to v3.0.0 directly and needs this logic to fix some accounts. | ||
func InitializeModuleAccs(ctx sdk.Context, ak authkeeper.AccountKeeper) { | ||
for _, modAccName := range ModuleAccsToInitialize { | ||
// Get module account and relevant permissions from the accountKeeper. | ||
addr, perms := ak.GetModuleAddressAndPermissions(modAccName) | ||
if addr == nil { | ||
panic(fmt.Sprintf( | ||
"Did not find %v in `ak.GetModuleAddressAndPermissions`. This is not expected. Skipping.", | ||
modAccName, | ||
)) | ||
} | ||
|
||
// Try to get the account in state. | ||
acc := ak.GetAccount(ctx, addr) | ||
if acc != nil { | ||
// Account has been initialized. | ||
macc, isModuleAccount := acc.(authtypes.ModuleAccountI) | ||
if isModuleAccount { | ||
// Module account was correctly initialized. Skipping | ||
ctx.Logger().Info(fmt.Sprintf( | ||
"module account %+v was correctly initialized. No-op", | ||
macc, | ||
)) | ||
continue | ||
} | ||
// Module account has been initialized as a BaseAccount. Change to module account. | ||
// Note: We need to get the base account to retrieve its account number, and convert it | ||
// in place into a module account. | ||
baseAccount, ok := acc.(*authtypes.BaseAccount) | ||
if !ok { | ||
panic(fmt.Sprintf( | ||
"cannot cast %v into a BaseAccount, acc = %+v", | ||
modAccName, | ||
acc, | ||
)) | ||
} | ||
newModuleAccount := authtypes.NewModuleAccount( | ||
baseAccount, | ||
modAccName, | ||
perms..., | ||
) | ||
ak.SetModuleAccount(ctx, newModuleAccount) | ||
ctx.Logger().Info(fmt.Sprintf( | ||
"Successfully converted %v to module account in state: %+v", | ||
modAccName, | ||
newModuleAccount, | ||
)) | ||
continue | ||
} | ||
|
||
// Account has not been initialized at all. Initialize it as module. | ||
// Implementation taken from | ||
// https://github.com/dydxprotocol/cosmos-sdk/blob/bdf96fdd/x/auth/keeper/keeper.go#L213 | ||
newModuleAccount := authtypes.NewEmptyModuleAccount(modAccName, perms...) | ||
maccI := (ak.NewAccount(ctx, newModuleAccount)).(authtypes.ModuleAccountI) // this set the account number | ||
ak.SetModuleAccount(ctx, maccI) | ||
ctx.Logger().Info(fmt.Sprintf( | ||
"Successfully initialized module account in state: %+v", | ||
newModuleAccount, | ||
)) | ||
} | ||
} | ||
|
||
func CreateUpgradeHandler( | ||
mm *module.Manager, | ||
configurator module.Configurator, | ||
ak authkeeper.AccountKeeper, | ||
) upgradetypes.UpgradeHandler { | ||
return func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) { | ||
ctx.Logger().Info("Running %s Upgrade...", UpgradeName) | ||
InitializeModuleAccs(ctx, ak) | ||
return mm.RunMigrations(ctx, configurator, vm) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.