Skip to content

Instantly share code, notes, and snippets.

@zeroFruit
Created July 5, 2022 01:01
Show Gist options
  • Save zeroFruit/88fce993e3d7ae6a2f204c86d629956e to your computer and use it in GitHub Desktop.
Save zeroFruit/88fce993e3d7ae6a2f204c86d629956e to your computer and use it in GitHub Desktop.
Cosmos Dev Series: Cosmos-SDK-based Blockchain Upgrade - x/upgrade
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