Skip to content

Commit

Permalink
fix upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Jan 4, 2024
1 parent 0050a33 commit a2194a8
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 130 deletions.
9 changes: 9 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,9 @@ func New(
supply.NewAppModule(app.SupplyKeeper),
nft.NewAppModule(appCodec, app.NFTKeeper, app.AccountKeeper, app.BankKeeper),
)
app.mm.SetOrderPreBlockers(
upgradetypes.ModuleName,
)

// During begin block slashing happens after distr.BeginBlocker so that
// there is nothing left over in the validator fee pool, so as to keep the
Expand Down Expand Up @@ -696,6 +699,7 @@ func New(

// initialize BaseApp
app.SetInitChainer(app.InitChainer)
app.SetPreBlocker(app.PreBlocker)
app.SetBeginBlocker(app.BeginBlocker)
anteHandler, err := NewAnteHandler(
HandlerOptions{
Expand Down Expand Up @@ -768,6 +772,11 @@ func (app *ChainApp) setPostHandler() {
// Name returns the name of the App
func (app *ChainApp) Name() string { return app.BaseApp.Name() }

// PreBlocker updates every pre begin block
func (app *ChainApp) PreBlocker(ctx sdk.Context, req abci.RequestBeginBlock) (sdk.ResponsePreBlock, error) {
return app.mm.PreBlock(ctx, req)
}

// BeginBlocker application updates every begin block
func (app *ChainApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
// backport: https://github.com/cosmos/cosmos-sdk/pull/16639
Expand Down
91 changes: 0 additions & 91 deletions app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,18 @@ import (
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
"github.com/cosmos/cosmos-sdk/x/group"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
ica "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts"
icacontrollertypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/controller/types"
icahosttypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/host/types"
icatypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/types"
ibcfeetypes "github.com/cosmos/ibc-go/v7/modules/apps/29-fee/types"
ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
clientkeeper "github.com/cosmos/ibc-go/v7/modules/core/02-client/keeper"
"github.com/cosmos/ibc-go/v7/modules/core/exported"
ibctmmigrations "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint/migrations"
icaauthmoduletypes "github.com/crypto-org-chain/chain-main/v4/x/icaauth/types"
nfttransfertypes "github.com/crypto-org-chain/chain-main/v4/x/nft-transfer/types"
)

func (app *ChainApp) RegisterUpgradeHandlers(cdc codec.BinaryCodec, clientKeeper clientkeeper.Keeper) {
Expand Down Expand Up @@ -82,107 +77,21 @@ func (app *ChainApp) RegisterUpgradeHandlers(cdc codec.BinaryCodec, clientKeeper

// Migrate Tendermint consensus parameters from x/params module to a dedicated x/consensus module.
baseapp.MigrateParams(ctx, baseAppLegacySS, &app.ConsensusParamsKeeper)

Check warning on line 79 in app/upgrades.go

View check run for this annotation

Codecov / codecov/patch

app/upgrades.go#L78-L79

Added lines #L78 - L79 were not covered by tests

// the minimal commission rate of 5% (0.05)
// (default is needed to be set because of SDK store migrations that set the param)
stakingtypes.DefaultMinCommissionRate = sdk.NewDecWithPrec(5, 2)

app.StakingKeeper.IterateValidators(ctx, func(index int64, val stakingtypes.ValidatorI) (stop bool) {
if val.GetCommission().LT(stakingtypes.DefaultMinCommissionRate) {
validator, found := app.StakingKeeper.GetValidator(ctx, val.GetOperator())
if !found {
ctx.Logger().Error("validator not found", val)
return true
}
ctx.Logger().Info("update validator's commission rate to a minimal one", val)
validator.Commission.Rate = stakingtypes.DefaultMinCommissionRate
if validator.Commission.MaxRate.LT(stakingtypes.DefaultMinCommissionRate) {
validator.Commission.MaxRate = stakingtypes.DefaultMinCommissionRate
}
app.StakingKeeper.SetValidator(ctx, validator)
}
return false
})

icaModule := app.mm.Modules[icatypes.ModuleName].(ica.AppModule)

// set the ICS27 consensus version so InitGenesis is not run
fromVM[icatypes.ModuleName] = icaModule.ConsensusVersion()

// create ICS27 Controller submodule params
controllerParams := icacontrollertypes.Params{
ControllerEnabled: false,
}

// create ICS27 Host submodule params
hostParams := icahosttypes.Params{
HostEnabled: false,
AllowMessages: []string{
"/cosmos.authz.v1beta1.MsgExec",
"/cosmos.authz.v1beta1.MsgGrant",
"/cosmos.authz.v1beta1.MsgRevoke",
"/cosmos.bank.v1beta1.MsgSend",
"/cosmos.bank.v1beta1.MsgMultiSend",
"/cosmos.distribution.v1beta1.MsgSetWithdrawAddress",
"/cosmos.distribution.v1beta1.MsgWithdrawValidatorCommission",
"/cosmos.distribution.v1beta1.MsgFundCommunityPool",
"/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward",
"/cosmos.gov.v1beta1.MsgVoteWeighted",
"/cosmos.gov.v1beta1.MsgSubmitProposal",
"/cosmos.gov.v1beta1.MsgDeposit",
"/cosmos.gov.v1beta1.MsgVote",
"/cosmos.staking.v1beta1.MsgCreateValidator",
"/cosmos.staking.v1beta1.MsgEditValidator",
"/cosmos.staking.v1beta1.MsgDelegate",
"/cosmos.staking.v1beta1.MsgUndelegate",
"/cosmos.staking.v1beta1.MsgBeginRedelegate",
"/cosmos.staking.v1beta1.MsgCancelUnbondingDelegation",
"/cosmos.slashing.v1beta1.MsgUnjail",
"/ibc.applications.transfer.v1.MsgTransfer",
"/chainmain.nft_transfer.v1.MsgTransfer",
"/chainmain.nft.v1.MsgBurnNFT",
"/chainmain.nft.v1.MsgEditNFT",
"/chainmain.nft.v1.MsgIssueDenom",
"/chainmain.nft.v1.MsgMintNFT",
"/chainmain.nft.v1.MsgTransferNFT",
},
}

ctx.Logger().Info("start to init interchain account module...")

// initialize ICS27 module
icaModule.InitModule(ctx, controllerParams, hostParams)

ctx.Logger().Info("start to run module migrations...")

return app.mm.RunMigrations(ctx, app.configurator, fromVM)
})

// testnets need to do a coordinated upgrade to keep in sync with current mainnet version
testnetPlanName := "v4.2.7-testnet"
app.UpgradeKeeper.SetUpgradeHandler(testnetPlanName, func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
return app.mm.RunMigrations(ctx, app.configurator, fromVM)
})

upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk()
if err != nil {
panic(fmt.Sprintf("failed to read upgrade info from disk %s", err))
}

if upgradeInfo.Name == planName && !app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) {
storeUpgrades := storetypes.StoreUpgrades{
Added: []string{
group.ModuleName,
icacontrollertypes.StoreKey,
icahosttypes.StoreKey,
icaauthmoduletypes.StoreKey,
ibcfeetypes.StoreKey,
nfttransfertypes.StoreKey,
consensusparamtypes.StoreKey,
crisistypes.StoreKey,

Check warning on line 92 in app/upgrades.go

View check run for this annotation

Codecov / codecov/patch

app/upgrades.go#L91-L92

Added lines #L91 - L92 were not covered by tests
},
}

// configure store loader that checks if version == upgradeHeight and applies store upgrades
app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &storeUpgrades))
}
Expand Down
50 changes: 26 additions & 24 deletions integration_tests/cosmoscli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,33 @@ def gov_propose_legacy(self, proposer, kind, proposal, no_validate=False, **kwar
mode = kwargs.get("broadcast_mode", "block")
event_query_tx = mode != "block"
if kind == "software-upgrade":
rsp = json.loads(
self.raw(
"tx",
"gov",
"submit-legacy-proposal",
kind,
proposal["name"],
"-y",
"--no-validate" if no_validate else None,
from_=proposer,
# content
title=proposal.get("title"),
description=proposal.get("description"),
upgrade_height=proposal.get("upgrade-height"),
upgrade_time=proposal.get("upgrade-time"),
upgrade_info=proposal.get("upgrade-info"),
deposit=proposal.get("deposit"),
# basic
home=self.data_dir,
node=self.node_rpc,
keyring_backend="test",
chain_id=self.chain_id,
**kwargs,
)
raw = self.raw(
"tx",
"gov",
"submit-legacy-proposal",
kind,
proposal["name"],
"-y",
"--no-validate" if no_validate else None,
from_=proposer,
# content
title=proposal.get("title"),
description=proposal.get("description"),
upgrade_height=proposal.get("upgrade-height"),
upgrade_time=proposal.get("upgrade-time"),
upgrade_info=proposal.get("upgrade-info", "info"),
deposit=proposal.get("deposit"),
# basic
home=self.data_dir,
node=self.node_rpc,
keyring_backend="test",
chain_id=self.chain_id,
**kwargs,
)
# skip success log info that breaks json.loads
brace_index = raw.index(b"{")
json_part = raw[brace_index:]
rsp = json.loads(json_part)
if rsp["code"] == 0 and event_query_tx:
rsp = self.event_query_tx_for(rsp["txhash"])
return rsp
Expand Down
27 changes: 13 additions & 14 deletions integration_tests/test_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,12 @@ def test_cosmovisor(cosmovisor_cluster):

def propose_and_pass(cluster, kind, proposal, cosmos_sdk_47=True, **kwargs):
if cosmos_sdk_47:
kwargs.setdefault("output", "json")
rsp = cluster.gov_propose_legacy(

Check failure on line 148 in integration_tests/test_upgrade.py

View workflow job for this annotation

GitHub Actions / test-upgrade

test_cancel_upgrade TypeError: integration_tests.cosmoscli.ClusterCLI.gov_propose_legacy() got multiple values for keyword argument 'no_validate'
"community",
kind,
proposal,
broadcast_mode="sync",
no_validate=True,
**kwargs,
)
else:
Expand All @@ -169,10 +170,9 @@ def propose_and_pass(cluster, kind, proposal, cosmos_sdk_47=True, **kwargs):
proposal = cluster.query_proposal(proposal_id)
print("mm-proposal", proposal)
assert proposal["status"] == "PROPOSAL_STATUS_VOTING_PERIOD", proposal
wait = cosmos_sdk_47
rsp = cluster.gov_vote("validator", proposal_id, "yes", event_query_tx=wait)
rsp = cluster.gov_vote("validator", proposal_id, "yes", event_query_tx=False)
assert rsp["code"] == 0, rsp["raw_log"]
rsp = cluster.gov_vote("validator", proposal_id, "yes", i=1, event_query_tx=wait)
rsp = cluster.gov_vote("validator", proposal_id, "yes", i=1, event_query_tx=False)
assert rsp["code"] == 0, rsp["raw_log"]

proposal = cluster.query_proposal(proposal_id)
Expand All @@ -187,7 +187,6 @@ def propose_and_pass(cluster, kind, proposal, cosmos_sdk_47=True, **kwargs):

def upgrade(cluster, plan_name, target_height, cosmos_sdk_47=True):
print("upgrade height", target_height)

propose_and_pass(
cluster,
"software-upgrade",
Expand Down Expand Up @@ -217,14 +216,14 @@ def upgrade(cluster, plan_name, target_height, cosmos_sdk_47=True):
)

# check upgrade-info.json file is written
assert (
json.load((cluster.home(0) / "data/upgrade-info.json").open())
== json.load((cluster.home(1) / "data/upgrade-info.json").open())
== {
"name": plan_name,
"height": target_height,
}
)
js1 = json.load((cluster.home(0) / "data/upgrade-info.json").open())
js2 = json.load((cluster.home(1) / "data/upgrade-info.json").open())
expected = {
"name": plan_name,
"height": target_height,
}
assert js1 == js2
assert expected.items() <= js1.items()

# use the upgrade-test binary
edit_chain_program(
Expand Down Expand Up @@ -373,7 +372,7 @@ def assert_commission(adr, expected):
}, ev

target_height = cluster.block_height() + 30
upgrade(cluster, "v4.0.0", target_height, cosmos_sdk_47=False)
upgrade(cluster, "v4.2.0", target_height, cosmos_sdk_47=False)

cli = cluster.cosmos_cli()

Expand Down
2 changes: 1 addition & 1 deletion integration_tests/upgrade-test.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ pkgs.linkFarm "upgrade-test-package" [
{ name = "genesis"; path = released; }
{ name = "v2.0.0"; path = released2; }
{ name = "v3.0.0"; path = released3; }
{ name = "v4.0.0"; path = released4; }
{ name = "v4.2.0"; path = released4; }
{ name = "sdk47-upgrade"; path = current; }
]

0 comments on commit a2194a8

Please sign in to comment.