Created
July 5, 2022 01:01
-
-
Save zeroFruit/88fce993e3d7ae6a2f204c86d629956e to your computer and use it in GitHub Desktop.
Cosmos Dev Series: Cosmos-SDK-based Blockchain Upgrade - x/upgrade
This file contains hidden or 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
func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { | |
BeginBlocker(am.keeper, ctx, req) | |
} | |
func BeginBlocker(k keeper.Keeper, ctx sdk.Context, _ abci.RequestBeginBlock) { | |
plan, found := k.GetUpgradePlan(ctx) | |
... | |
if !found { | |
return | |
} | |
// Prepare shutdown if we don't have an upgrade handler for this upgrade name (meaning this software is out of date) | |
if !k.HasHandler(plan.Name) { | |
// Write the upgrade info to disk. The UpgradeStoreLoader uses this info to perform or skip | |
// store migrations. | |
err := k.DumpUpgradeInfoWithInfoToDisk(ctx.BlockHeight(), plan.Name, plan.Info) | |
if err != nil { | |
panic(fmt.Errorf("unable to write upgrade info to filesystem: %s", err.Error())) | |
} | |
upgradeMsg := BuildUpgradeNeededMsg(plan) | |
panic(upgradeMsg) | |
} | |
// To make sure clear upgrade is executed at the same block | |
if plan.ShouldExecute(ctx) { | |
... | |
k.ApplyUpgrade(ctx, plan) | |
return | |
} | |
} | |
// ApplyUpgrade will execute the handler associated with the Plan and mark the plan as done. | |
func (k Keeper) ApplyUpgrade(ctx sdk.Context, plan types.Plan) { | |
handler := k.upgradeHandlers[plan.Name] | |
if handler == nil { | |
panic("ApplyUpgrade should never be called without first checking HasHandler") | |
} | |
updatedVM, err := handler(ctx, plan, k.GetModuleVersionMap(ctx)) | |
if err != nil { | |
panic(err) | |
} | |
k.SetModuleVersionMap(ctx, updatedVM) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment