From 7ee87f0b4cbfd644b7a4b6b73e9bb203596853df Mon Sep 17 00:00:00 2001 From: yihuang Date: Thu, 14 Dec 2023 17:07:40 +0800 Subject: [PATCH] Problem: don't support hardfork style upgrades (#1258) * Problem: don't support hardfork style upgrades * Update CHANGELOG.md Signed-off-by: yihuang * check chain-id --------- Signed-off-by: yihuang --- CHANGELOG.md | 4 ++++ app/app.go | 3 +++ app/forks.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 app/forks.go diff --git a/CHANGELOG.md b/CHANGELOG.md index f5ccea9e74..7f6e30d6ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## UNRELEASED + +- [#1258](https://github.com/crypto-org-chain/cronos/pull/1258) Support hard-fork style upgrades. + *December 11, 2023* ## v1.1.0-rc2 diff --git a/app/app.go b/app/app.go index 37fc8316d7..10b8fef7f9 100644 --- a/app/app.go +++ b/app/app.go @@ -175,6 +175,8 @@ const ( FlagBlockedAddresses = "blocked-addresses" ) +var Forks = []Fork{} + // this line is used by starport scaffolding # stargate/wasm/app/enabledProposals func getGovProposalHandlers() []govclient.ProposalHandler { @@ -983,6 +985,7 @@ func (app *App) PreBlocker(ctx sdk.Context, req abci.RequestBeginBlock) (sdk.Res // BeginBlocker application updates every begin block func (app *App) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock { + BeginBlockForks(ctx, app) return app.mm.BeginBlock(ctx, req) } diff --git a/app/forks.go b/app/forks.go new file mode 100644 index 0000000000..28e3b5c457 --- /dev/null +++ b/app/forks.go @@ -0,0 +1,32 @@ +package app + +import sdk "github.com/cosmos/cosmos-sdk/types" + +// Fork defines a struct containing the requisite fields for a non-software upgrade proposal +// Hard Fork at a given height to implement. +// There is one time code that can be added for the start of the Fork, in `BeginForkLogic`. +// Any other change in the code should be height-gated, if the goal is to have old and new binaries +// to be compatible prior to the upgrade height. +// +// Adapted from osmosis: https://github.com/osmosis-labs/osmosis/blob/057192c2c0949fde5673a5f314bf41816f808fd9/app/upgrades/types.go#L40 +type Fork struct { + // Upgrade version name, for the upgrade handler, e.g. `v7` + UpgradeName string + // height the upgrade occurs at + UpgradeHeight int64 + // chain-id the upgrade occurs at + UpgradeChainId string + + // Function that runs some custom state transition code at the beginning of a fork. + BeginForkLogic func(ctx sdk.Context, app *App) +} + +// BeginBlockForks is intended to be ran in a chain upgrade. +func BeginBlockForks(ctx sdk.Context, app *App) { + for _, fork := range Forks { + if ctx.BlockHeight() == fork.UpgradeHeight && ctx.ChainID() == fork.UpgradeChainId { + fork.BeginForkLogic(ctx, app) + return + } + } +}