github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/upgrade/abci.go (about) 1 package upgrade 2 3 import ( 4 "fmt" 5 6 abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types" 7 8 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 9 ) 10 11 // BeginBlock will check if there is a scheduled plan and if it is ready to be executed. 12 // If the current height is in the provided set of heights to skip, it will skip and clear the upgrade plan. 13 // If it is ready, it will execute it if the handler is installed, and panic/abort otherwise. 14 // If the plan is not ready, it will ensure the handler is not registered too early (and abort otherwise). 15 // 16 // The purpose is to ensure the binary is switched EXACTLY at the desired block, and to allow 17 // a migration to be executed if needed upon this switch (migration defined in the new binary) 18 // skipUpgradeHeightArray is a set of block heights for which the upgrade must be skipped 19 func BeginBlocker(k Keeper, ctx sdk.Context, _ abci.RequestBeginBlock) { 20 plan, found := k.GetUpgradePlan(ctx) 21 if !found { 22 return 23 } 24 25 // To make sure clear upgrade is executed at the same block 26 if plan.ShouldExecute(ctx) { 27 // If skip upgrade has been set for current height, we clear the upgrade plan 28 if k.IsSkipHeight(ctx.BlockHeight()) { 29 skipUpgradeMsg := fmt.Sprintf("UPGRADE \"%s\" SKIPPED at %d: %s", plan.Name, plan.Height, plan.Info) 30 ctx.Logger().Info(skipUpgradeMsg) 31 32 // Clear the upgrade plan at current height 33 k.ClearUpgradePlan(ctx) 34 return 35 } 36 37 if !k.HasHandler(plan.Name) { 38 upgradeMsg := fmt.Sprintf("UPGRADE \"%s\" NEEDED at %s: %s", plan.Name, plan.DueAt(), plan.Info) 39 // We don't have an upgrade handler for this upgrade name, meaning this software is out of date so shutdown 40 ctx.Logger().Error(upgradeMsg) 41 panic(upgradeMsg) 42 } 43 // We have an upgrade handler for this upgrade name, so apply the upgrade 44 ctx.Logger().Info(fmt.Sprintf("applying upgrade \"%s\" at %s", plan.Name, plan.DueAt())) 45 ctx.SetBlockGasMeter(sdk.NewInfiniteGasMeter()) 46 k.ApplyUpgrade(ctx, plan) 47 return 48 } 49 50 // if we have a pending upgrade, but it is not yet time, make sure we did not 51 // set the handler already 52 if k.HasHandler(plan.Name) { 53 downgradeMsg := fmt.Sprintf("BINARY UPDATED BEFORE TRIGGER! UPGRADE \"%s\" - in binary but not executed on chain", plan.Name) 54 ctx.Logger().Error(downgradeMsg) 55 panic(downgradeMsg) 56 } 57 }