From 1f34bcdf91374a6d039b932000c822760e37a92e Mon Sep 17 00:00:00 2001 From: ValarDragon Date: Sun, 19 Dec 2021 21:04:38 -0600 Subject: [PATCH 1/4] Move fork logic into sub-directory structure --- app/app.go | 2 +- app/forks.go | 45 +++++++---------------------------- app/upgrades/README.md | 11 +++++++++ app/upgrades/v3/README.md | 3 +++ app/upgrades/v3/constants.go | 4 ++++ app/upgrades/v3/forks.go | 46 ++++++++++++++++++++++++++++++++++++ app/upgrades/v4/README.md | 0 app/upgrades/v6/README.md | 1 + app/upgrades/v6/constants.go | 4 ++++ app/upgrades/v6/forks.go | 11 +++++++++ 10 files changed, 89 insertions(+), 38 deletions(-) create mode 100644 app/upgrades/README.md create mode 100644 app/upgrades/v3/README.md create mode 100644 app/upgrades/v3/constants.go create mode 100644 app/upgrades/v3/forks.go create mode 100644 app/upgrades/v4/README.md create mode 100644 app/upgrades/v6/README.md create mode 100644 app/upgrades/v6/constants.go create mode 100644 app/upgrades/v6/forks.go diff --git a/app/app.go b/app/app.go index e5528bb9256..2a9b9d68fd1 100644 --- a/app/app.go +++ b/app/app.go @@ -732,7 +732,7 @@ func (app *OsmosisApp) Name() string { return app.BaseApp.Name() } // BeginBlocker application updates every begin block func (app *OsmosisApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock { - forks(ctx, app) + BeginBlockForks(ctx, app) return app.mm.BeginBlock(ctx, req) } diff --git a/app/forks.go b/app/forks.go index 0273d798136..17e8e81cbf3 100644 --- a/app/forks.go +++ b/app/forks.go @@ -2,48 +2,19 @@ package app import ( sdk "github.com/cosmos/cosmos-sdk/types" + v3 "github.com/osmosis-labs/osmosis/app/upgrades/v3" + v6 "github.com/osmosis-labs/osmosis/app/upgrades/v6" ) -func forks(ctx sdk.Context, app *OsmosisApp) { +// BeginBlockForks is intended to be ran in +func BeginBlockForks(ctx sdk.Context, app *OsmosisApp) { switch ctx.BlockHeight() { - case 712000: - fix_min_deposit_denom(ctx, app) - fix_min_commision_rate(ctx, app) - case 2464000: - ctx.Logger().Info("Applying emergency hard fork for v6, allows IBC to create new channels.") + case v3.UpgradeHeight: + v3.RunForkLogic(ctx, &app.GovKeeper, &app.StakingKeeper) + case v6.UpgradeHeight: + v6.RunForkLogic(ctx) default: // do nothing return } } - -// Fixes an error where minimum deposit was set to "500 osmo" -// This denom does not exist, which makes it impossible for a proposal to go to a vote -func fix_min_deposit_denom(ctx sdk.Context, app *OsmosisApp) { - var params = app.GovKeeper.GetDepositParams(ctx) - params.MinDeposit = sdk.NewCoins(sdk.NewCoin("uosmo", sdk.NewInt(500000000))) - app.GovKeeper.SetDepositParams(ctx, params) -} - -// Fixes an error where validators can be created with a commission rate -// less than the network minimum rate. -func fix_min_commision_rate(ctx sdk.Context, app *OsmosisApp) { - // Upgrade every validators min-commission rate - validators := app.StakingKeeper.GetAllValidators(ctx) - minCommissionRate := app.StakingKeeper.GetParams(ctx).MinCommissionRate - for _, v := range validators { - if v.Commission.Rate.LT(minCommissionRate) { - comm, err := app.StakingKeeper.MustUpdateValidatorCommission( - ctx, v, minCommissionRate) - if err != nil { - panic(err) - } - v.Commission = comm - - // call the before-modification hook since we're about to update the commission - app.StakingKeeper.BeforeValidatorModified(ctx, v.GetOperator()) - - app.StakingKeeper.SetValidator(ctx, v) - } - } -} diff --git a/app/upgrades/README.md b/app/upgrades/README.md new file mode 100644 index 00000000000..90635cb7382 --- /dev/null +++ b/app/upgrades/README.md @@ -0,0 +1,11 @@ +# Osmosis Upgrades + +This folder contains logic for every osmosis upgrade. (Both state migrations, and hard forks) + +* v1 - Initial version +* v3 - short blurb on prop19 and the fork +* v4 - Berylium State Migration +* v5 - Boron State migration +* v6 - hard fork for IBC bug fix + +## TODO: Make a fork-upgrade struct and a state-migration upgrade struct \ No newline at end of file diff --git a/app/upgrades/v3/README.md b/app/upgrades/v3/README.md new file mode 100644 index 00000000000..7187aa1376a --- /dev/null +++ b/app/upgrades/v3/README.md @@ -0,0 +1,3 @@ +# TODO Write stuff + +Should include description about this version, compatibility with v1 until height {...}, and fork code here. \ No newline at end of file diff --git a/app/upgrades/v3/constants.go b/app/upgrades/v3/constants.go new file mode 100644 index 00000000000..8a0e9c62217 --- /dev/null +++ b/app/upgrades/v3/constants.go @@ -0,0 +1,4 @@ +package v3 + +const UpgradeName = "v3" +const UpgradeHeight = 712000 diff --git a/app/upgrades/v3/forks.go b/app/upgrades/v3/forks.go new file mode 100644 index 00000000000..a22955ac55f --- /dev/null +++ b/app/upgrades/v3/forks.go @@ -0,0 +1,46 @@ +package v3 + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper" + stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" +) + +func RunForkLogic(ctx sdk.Context, gov *govkeeper.Keeper, staking *stakingkeeper.Keeper) { + ctx.Logger().Info("Applying Osmosis v3 upgrade." + + " Fixing governance deposit so proposals can be voted upon," + + " and fixing validator min commission rate.") + FixMinDepositDenom(ctx, gov) + FixMinCommisionRate(ctx, staking) +} + +// Fixes an error where minimum deposit was set to "500 osmo" +// This denom does not exist, which makes it impossible for a proposal to go to a vote +func FixMinDepositDenom(ctx sdk.Context, gov *govkeeper.Keeper) { + var params = gov.GetDepositParams(ctx) + params.MinDeposit = sdk.NewCoins(sdk.NewCoin("uosmo", sdk.NewInt(500000000))) + gov.SetDepositParams(ctx, params) +} + +// Fixes an error where validators can be created with a commission rate +// less than the network minimum rate. +func FixMinCommisionRate(ctx sdk.Context, staking *stakingkeeper.Keeper) { + // Upgrade every validators min-commission rate + validators := staking.GetAllValidators(ctx) + minCommissionRate := staking.GetParams(ctx).MinCommissionRate + for _, v := range validators { + if v.Commission.Rate.LT(minCommissionRate) { + comm, err := staking.MustUpdateValidatorCommission( + ctx, v, minCommissionRate) + if err != nil { + panic(err) + } + v.Commission = comm + + // call the before-modification hook since we're about to update the commission + staking.BeforeValidatorModified(ctx, v.GetOperator()) + + staking.SetValidator(ctx, v) + } + } +} diff --git a/app/upgrades/v4/README.md b/app/upgrades/v4/README.md new file mode 100644 index 00000000000..e69de29bb2d diff --git a/app/upgrades/v6/README.md b/app/upgrades/v6/README.md new file mode 100644 index 00000000000..2b4e83e6ead --- /dev/null +++ b/app/upgrades/v6/README.md @@ -0,0 +1 @@ +# TODO: Describe bug etc here. \ No newline at end of file diff --git a/app/upgrades/v6/constants.go b/app/upgrades/v6/constants.go new file mode 100644 index 00000000000..57b8681e912 --- /dev/null +++ b/app/upgrades/v6/constants.go @@ -0,0 +1,4 @@ +package v6 + +const UpgradeName = "v6" +const UpgradeHeight = 2464000 diff --git a/app/upgrades/v6/forks.go b/app/upgrades/v6/forks.go new file mode 100644 index 00000000000..cdaa5cd4425 --- /dev/null +++ b/app/upgrades/v6/forks.go @@ -0,0 +1,11 @@ +package v6 + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func RunForkLogic(ctx sdk.Context) { + // All the height gated fork logic is actually in our ibc-go fork. + // See: https://github.com/osmosis-labs/ibc-go/releases/tag/v2.0.2-osmo + ctx.Logger().Info("Applying emergency hard fork for v6, allows IBC to create new channels.") +} From 533217c10934359b21d1ac3c57e6f7c9f0ac5aac Mon Sep 17 00:00:00 2001 From: ValarDragon Date: Sun, 19 Dec 2021 21:34:19 -0600 Subject: [PATCH 2/4] Move upgrade handlers into sub-directories --- app/app.go | 83 +++----------------- app/upgrades/v4/constants.go | 3 + app/{ => upgrades/v4}/prop12.go | 14 ++-- app/{ => upgrades/v4}/prop12_data.go | 2 +- app/upgrades/v4/upgrades.go | 40 ++++++++++ app/upgrades/v5/constants.go | 3 + app/upgrades/v5/upgrades.go | 75 ++++++++++++++++++ app/{ => upgrades/v5}/whitelist_feetokens.go | 7 +- 8 files changed, 145 insertions(+), 82 deletions(-) create mode 100644 app/upgrades/v4/constants.go rename app/{ => upgrades/v4}/prop12.go (61%) rename app/{ => upgrades/v4}/prop12_data.go (99%) create mode 100644 app/upgrades/v4/upgrades.go create mode 100644 app/upgrades/v5/constants.go create mode 100644 app/upgrades/v5/upgrades.go rename app/{ => upgrades/v5}/whitelist_feetokens.go (92%) diff --git a/app/app.go b/app/app.go index 2a9b9d68fd1..7b0456acae3 100644 --- a/app/app.go +++ b/app/app.go @@ -18,7 +18,6 @@ import ( ibcclient "github.com/cosmos/ibc-go/v2/modules/core/02-client" ibcclienttypes "github.com/cosmos/ibc-go/v2/modules/core/02-client/types" - ibcconnectiontypes "github.com/cosmos/ibc-go/v2/modules/core/03-connection/types" "github.com/cosmos/cosmos-sdk/x/authz" paramproposal "github.com/cosmos/cosmos-sdk/x/params/types/proposal" @@ -93,6 +92,8 @@ import ( "github.com/gorilla/mux" appparams "github.com/osmosis-labs/osmosis/app/params" + v4 "github.com/osmosis-labs/osmosis/app/upgrades/v4" + v5 "github.com/osmosis-labs/osmosis/app/upgrades/v5" _ "github.com/osmosis-labs/osmosis/client/docs/statik" "github.com/osmosis-labs/osmosis/x/claim" claimkeeper "github.com/osmosis-labs/osmosis/x/claim/keeper" @@ -128,7 +129,6 @@ import ( ) const appName = "OsmosisApp" -const v5UpgradeName = "v5" var ( // DefaultNodeHome default home directories for the application daemon @@ -351,84 +351,23 @@ func NewOsmosisApp( // this configures a no-op upgrade handler for the v4 upgrade, // which improves the lockup module's store management. app.UpgradeKeeper.SetUpgradeHandler( - "v4", func(ctx sdk.Context, _plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) { - // // Upgrade all of the lock storages - // locks, err := app.LockupKeeper.GetLegacyPeriodLocks(ctx) - // if err != nil { - // panic(err) - // } - // // clear all lockup module locking / unlocking queue items - // app.LockupKeeper.ClearAllLockRefKeys(ctx) - // app.LockupKeeper.ClearAllAccumulationStores(ctx) - - // // reset all lock and references - // if err := app.LockupKeeper.ResetAllLocks(ctx, locks); err != nil { - // panic(err) - // } - - // // configure upgrade for gamm module's pool creation fee param add - // app.GAMMKeeper.SetParams(ctx, gammtypes.NewParams(sdk.Coins{sdk.NewInt64Coin("uosmo", 1)})) // 1 uOSMO - // // execute prop12. See implementation in - // prop12(ctx, app) - return vm, nil - }) + v4.UpgradeName, v4.CreateUpgradeHandler( + app.mm, app.configurator, + &app.BankKeeper, &app.DistrKeeper, &app.GAMMKeeper)) app.UpgradeKeeper.SetUpgradeHandler( - v5UpgradeName, - func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) { - // Set IBC updates from {inside SDK} to v1 - // https://github.com/cosmos/ibc-go/blob/main/docs/migrations/ibc-migration-043.md#in-place-store-migrations - app.IBCKeeper.ConnectionKeeper.SetParams(ctx, ibcconnectiontypes.DefaultParams()) - - totalLiquidity := app.GAMMKeeper.GetLegacyTotalLiquidity(ctx) - app.GAMMKeeper.DeleteLegacyTotalLiquidity(ctx) - app.GAMMKeeper.SetTotalLiquidity(ctx, totalLiquidity) - - // Set all modules "old versions" to 1. - // Then the run migrations logic will handle running their upgrade logics - fromVM := make(map[string]uint64) - for moduleName := range app.mm.Modules { - fromVM[moduleName] = 1 - } - // EXCEPT Auth needs to run _after_ staking (https://github.com/cosmos/cosmos-sdk/issues/10591), - // and it seems bank as well (https://github.com/provenance-io/provenance/blob/407c89a7d73854515894161e1526f9623a94c368/app/upgrades.go#L86-L122). - // So we do this by making auth run last. - // This is done by setting auth's consensus version to 2, running RunMigrations, - // then setting it back to 1, and then running migrations again. - fromVM[authtypes.ModuleName] = 2 - - // override versions for authz & bech32ibctypes module as to not skip their InitGenesis - // for txfees module, we will override txfees ourselves. - delete(fromVM, authz.ModuleName) - delete(fromVM, bech32ibctypes.ModuleName) - - newVM, err := app.mm.RunMigrations(ctx, app.configurator, fromVM) - if err != nil { - return nil, err - } - - // Override txfees genesis here - ctx.Logger().Info("Setting txfees module genesis with actual v5 desired genesis") - feeTokens := initialWhitelistedFeetokens(ctx, app) - txfees.InitGenesis(ctx, app.TxFeesKeeper, txfeestypes.GenesisState{ - Basedenom: app.StakingKeeper.BondDenom(ctx), - Feetokens: feeTokens, - }) - - // now update auth version back to v1, to run auth migration last - newVM[authtypes.ModuleName] = 1 - - ctx.Logger().Info("Now running migrations just for auth, to get auth migration to be last. " + - "(CC https://github.com/cosmos/cosmos-sdk/issues/10591)") - return app.mm.RunMigrations(ctx, app.configurator, newVM) - }) + v5.UpgradeName, + v5.CreateUpgradeHandler( + app.mm, app.configurator, + &app.IBCKeeper.ConnectionKeeper, &app.TxFeesKeeper, + &app.GAMMKeeper, &app.StakingKeeper)) upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk() if err != nil { panic(fmt.Sprintf("failed to read upgrade info from disk %s", err)) } - if upgradeInfo.Name == v5UpgradeName && !app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) { + if upgradeInfo.Name == v5.UpgradeName && !app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) { storeUpgrades := store.StoreUpgrades{ Added: []string{authz.ModuleName, txfees.ModuleName, bech32ibctypes.ModuleName}, } diff --git a/app/upgrades/v4/constants.go b/app/upgrades/v4/constants.go new file mode 100644 index 00000000000..ada73308424 --- /dev/null +++ b/app/upgrades/v4/constants.go @@ -0,0 +1,3 @@ +package v4 + +const UpgradeName = "v4" diff --git a/app/prop12.go b/app/upgrades/v4/prop12.go similarity index 61% rename from app/prop12.go rename to app/upgrades/v4/prop12.go index 9dde1f1c413..5bce369e110 100644 --- a/app/prop12.go +++ b/app/upgrades/v4/prop12.go @@ -1,14 +1,16 @@ -package app +package v4 import ( "strconv" "strings" sdk "github.com/cosmos/cosmos-sdk/types" -) -func prop12(ctx sdk.Context, app *OsmosisApp) { + bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" + distrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper" +) +func Prop12(ctx sdk.Context, bank *bankkeeper.Keeper, distr *distrkeeper.Keeper) { payments := GetProp12Payments() var total = int64(0) @@ -23,15 +25,15 @@ func prop12(ctx sdk.Context, app *OsmosisApp) { panic(err) } coins := sdk.NewCoins(sdk.NewInt64Coin("uosmo", amount)) - if err := app.BankKeeper.SendCoinsFromModuleToAccount(ctx, "distribution", addr, coins); err != nil { + if err := (*bank).SendCoinsFromModuleToAccount(ctx, "distribution", addr, coins); err != nil { panic(err) } total += amount } //deduct from the feePool tracker - feePool := app.DistrKeeper.GetFeePool(ctx) + feePool := distr.GetFeePool(ctx) feePool.CommunityPool = feePool.CommunityPool.Sub(sdk.NewDecCoins(sdk.NewInt64DecCoin("uosmo", total))) - app.DistrKeeper.SetFeePool(ctx, feePool) + distr.SetFeePool(ctx, feePool) } diff --git a/app/prop12_data.go b/app/upgrades/v4/prop12_data.go similarity index 99% rename from app/prop12_data.go rename to app/upgrades/v4/prop12_data.go index 482c304368d..a6f8845c774 100644 --- a/app/prop12_data.go +++ b/app/upgrades/v4/prop12_data.go @@ -1,4 +1,4 @@ -package app +package v4 import ( "encoding/csv" diff --git a/app/upgrades/v4/upgrades.go b/app/upgrades/v4/upgrades.go new file mode 100644 index 00000000000..f6ebc736edc --- /dev/null +++ b/app/upgrades/v4/upgrades.go @@ -0,0 +1,40 @@ +package v4 + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + + bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" + distrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper" + gammkeeper "github.com/osmosis-labs/osmosis/x/gamm/keeper" + + upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" + gammtypes "github.com/osmosis-labs/osmosis/x/gamm/types" +) + +func CreateUpgradeHandler(mm *module.Manager, configurator module.Configurator, + bank *bankkeeper.Keeper, + distr *distrkeeper.Keeper, + gamm *gammkeeper.Keeper) upgradetypes.UpgradeHandler { + return func(ctx sdk.Context, _plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) { + // // Upgrade all of the lock storages + // locks, err := app.LockupKeeper.GetLegacyPeriodLocks(ctx) + // if err != nil { + // panic(err) + // } + // // clear all lockup module locking / unlocking queue items + // app.LockupKeeper.ClearAllLockRefKeys(ctx) + // app.LockupKeeper.ClearAllAccumulationStores(ctx) + + // // reset all lock and references + // if err := app.LockupKeeper.ResetAllLocks(ctx, locks); err != nil { + // panic(err) + // } + + // configure upgrade for gamm module's pool creation fee param add + gamm.SetParams(ctx, gammtypes.NewParams(sdk.Coins{sdk.NewInt64Coin("uosmo", 1)})) // 1 uOSMO + // execute prop12. See implementation in + Prop12(ctx, bank, distr) + return vm, nil + } +} diff --git a/app/upgrades/v5/constants.go b/app/upgrades/v5/constants.go new file mode 100644 index 00000000000..aa1f5f8f8df --- /dev/null +++ b/app/upgrades/v5/constants.go @@ -0,0 +1,3 @@ +package v5 + +const UpgradeName = "v5" diff --git a/app/upgrades/v5/upgrades.go b/app/upgrades/v5/upgrades.go new file mode 100644 index 00000000000..02c0230d2f6 --- /dev/null +++ b/app/upgrades/v5/upgrades.go @@ -0,0 +1,75 @@ +package v5 + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + "github.com/cosmos/cosmos-sdk/x/authz" + + upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" + + stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" + connectionkeeper "github.com/cosmos/ibc-go/v2/modules/core/03-connection/keeper" + gammkeeper "github.com/osmosis-labs/osmosis/x/gamm/keeper" + txfeeskeeper "github.com/osmosis-labs/osmosis/x/txfees/keeper" + + "github.com/osmosis-labs/osmosis/x/txfees" + + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + ibcconnectiontypes "github.com/cosmos/ibc-go/v2/modules/core/03-connection/types" + bech32ibctypes "github.com/osmosis-labs/bech32-ibc/x/bech32ibc/types" + txfeestypes "github.com/osmosis-labs/osmosis/x/txfees/types" +) + +func CreateUpgradeHandler(mm *module.Manager, configurator module.Configurator, + ibcConnections *connectionkeeper.Keeper, + txFeesKeeper *txfeeskeeper.Keeper, + gamm *gammkeeper.Keeper, + staking *stakingkeeper.Keeper) upgradetypes.UpgradeHandler { + return func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) { + // Set IBC updates from {inside SDK} to v1 + // https://github.com/cosmos/ibc-go/blob/main/docs/migrations/ibc-migration-043.md#in-place-store-migrations + ibcConnections.SetParams(ctx, ibcconnectiontypes.DefaultParams()) + + totalLiquidity := gamm.GetLegacyTotalLiquidity(ctx) + gamm.DeleteLegacyTotalLiquidity(ctx) + gamm.SetTotalLiquidity(ctx, totalLiquidity) + + // Set all modules "old versions" to 1. + // Then the run migrations logic will handle running their upgrade logics + fromVM := make(map[string]uint64) + for moduleName := range mm.Modules { + fromVM[moduleName] = 1 + } + // EXCEPT Auth needs to run _after_ staking (https://github.com/cosmos/cosmos-sdk/issues/10591), + // and it seems bank as well (https://github.com/provenance-io/provenance/blob/407c89a7d73854515894161e1526f9623a94c368/app/upgrades.go#L86-L122). + // So we do this by making auth run last. + // This is done by setting auth's consensus version to 2, running RunMigrations, + // then setting it back to 1, and then running migrations again. + fromVM[authtypes.ModuleName] = 2 + + // override versions for authz & bech32ibctypes module as to not skip their InitGenesis + // for txfees module, we will override txfees ourselves. + delete(fromVM, authz.ModuleName) + delete(fromVM, bech32ibctypes.ModuleName) + + newVM, err := mm.RunMigrations(ctx, configurator, fromVM) + if err != nil { + return nil, err + } + + // Override txfees genesis here + ctx.Logger().Info("Setting txfees module genesis with actual v5 desired genesis") + feeTokens := InitialWhitelistedFeetokens(ctx, gamm) + txfees.InitGenesis(ctx, *txFeesKeeper, txfeestypes.GenesisState{ + Basedenom: staking.BondDenom(ctx), + Feetokens: feeTokens, + }) + + // now update auth version back to v1, to run auth migration last + newVM[authtypes.ModuleName] = 1 + + ctx.Logger().Info("Now running migrations just for auth, to get auth migration to be last. " + + "(CC https://github.com/cosmos/cosmos-sdk/issues/10591)") + return mm.RunMigrations(ctx, configurator, newVM) + } +} diff --git a/app/whitelist_feetokens.go b/app/upgrades/v5/whitelist_feetokens.go similarity index 92% rename from app/whitelist_feetokens.go rename to app/upgrades/v5/whitelist_feetokens.go index 2411c35b163..fabeb1e3cc4 100644 --- a/app/whitelist_feetokens.go +++ b/app/upgrades/v5/whitelist_feetokens.go @@ -1,4 +1,4 @@ -package app +package v5 import ( "encoding/csv" @@ -6,6 +6,7 @@ import ( "strings" sdk "github.com/cosmos/cosmos-sdk/types" + gammkeeper "github.com/osmosis-labs/osmosis/x/gamm/keeper" "github.com/osmosis-labs/osmosis/x/txfees/types" ) @@ -37,7 +38,7 @@ med,ibc/3BCCC93AD5DF58D11A6F8A05FA8BC801CBA0BA61A981F57E91B8B598BF8061CB,586 boot,ibc/FE2CD1E6828EC0FAB8AF39BAC45BC25B965BA67CCBC50C13A14BD610B0D1E2C4,597 ` -func initialWhitelistedFeetokens(ctx sdk.Context, app *OsmosisApp) []types.FeeToken { +func InitialWhitelistedFeetokens(ctx sdk.Context, gamm *gammkeeper.Keeper) []types.FeeToken { r := csv.NewReader(strings.NewReader(feetoken_whitelist_data)) assets, err := r.ReadAll() if err != nil { @@ -54,7 +55,7 @@ func initialWhitelistedFeetokens(ctx sdk.Context, app *OsmosisApp) []types.FeeTo panic(err) } - pool, poolExistsErr := app.GAMMKeeper.GetPool(ctx, poolId) + pool, poolExistsErr := gamm.GetPool(ctx, poolId) if poolExistsErr != nil { continue } From a68d0dea81a61e6fd80bd8b1ef27e2c7eb93a752 Mon Sep 17 00:00:00 2001 From: ValarDragon Date: Sun, 19 Dec 2021 21:43:41 -0600 Subject: [PATCH 3/4] Move upgrade logic to end of keeper initialization --- app/app.go | 61 +++++++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/app/app.go b/app/app.go index 7b0456acae3..26f3149c385 100644 --- a/app/app.go +++ b/app/app.go @@ -348,34 +348,6 @@ func NewOsmosisApp( app.BaseApp, ) - // this configures a no-op upgrade handler for the v4 upgrade, - // which improves the lockup module's store management. - app.UpgradeKeeper.SetUpgradeHandler( - v4.UpgradeName, v4.CreateUpgradeHandler( - app.mm, app.configurator, - &app.BankKeeper, &app.DistrKeeper, &app.GAMMKeeper)) - - app.UpgradeKeeper.SetUpgradeHandler( - v5.UpgradeName, - v5.CreateUpgradeHandler( - app.mm, app.configurator, - &app.IBCKeeper.ConnectionKeeper, &app.TxFeesKeeper, - &app.GAMMKeeper, &app.StakingKeeper)) - - upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk() - if err != nil { - panic(fmt.Sprintf("failed to read upgrade info from disk %s", err)) - } - - if upgradeInfo.Name == v5.UpgradeName && !app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) { - storeUpgrades := store.StoreUpgrades{ - Added: []string{authz.ModuleName, txfees.ModuleName, bech32ibctypes.ModuleName}, - } - - // configure store loader that checks if version == upgradeHeight and applies store upgrades - app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &storeUpgrades)) - } - // Create IBC Keeper app.IBCKeeper = ibckeeper.NewKeeper( appCodec, @@ -420,6 +392,8 @@ func NewOsmosisApp( app.ClaimKeeper = claimkeeper.NewKeeper(appCodec, keys[claimtypes.StoreKey], app.AccountKeeper, app.BankKeeper, stakingKeeper, app.DistrKeeper) + app.setupUpgrades() + // register the staking hooks // NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks app.StakingKeeper = *stakingKeeper.SetHooks( @@ -838,6 +812,37 @@ func (app *OsmosisApp) RegisterTendermintService(clientCtx client.Context) { tmservice.RegisterTendermintService(app.BaseApp.GRPCQueryRouter(), clientCtx, app.interfaceRegistry) } +// RegisterTendermintService implements the Application.RegisterTendermintService method. +func (app *OsmosisApp) setupUpgrades() { + // this configures a no-op upgrade handler for the v4 upgrade, + // which improves the lockup module's store management. + app.UpgradeKeeper.SetUpgradeHandler( + v4.UpgradeName, v4.CreateUpgradeHandler( + app.mm, app.configurator, + &app.BankKeeper, &app.DistrKeeper, &app.GAMMKeeper)) + + app.UpgradeKeeper.SetUpgradeHandler( + v5.UpgradeName, + v5.CreateUpgradeHandler( + app.mm, app.configurator, + &app.IBCKeeper.ConnectionKeeper, &app.TxFeesKeeper, + &app.GAMMKeeper, &app.StakingKeeper)) + + upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk() + if err != nil { + panic(fmt.Sprintf("failed to read upgrade info from disk %s", err)) + } + + if upgradeInfo.Name == v5.UpgradeName && !app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) { + storeUpgrades := store.StoreUpgrades{ + Added: []string{authz.ModuleName, txfees.ModuleName, bech32ibctypes.ModuleName}, + } + + // configure store loader that checks if version == upgradeHeight and applies store upgrades + app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &storeUpgrades)) + } +} + // RegisterSwaggerAPI registers swagger route with API Server func RegisterSwaggerAPI(ctx client.Context, rtr *mux.Router) { statikFS, err := fs.New() From 1243268162e0c43cb2b667fed02156e1ea1b67c8 Mon Sep 17 00:00:00 2001 From: ValarDragon Date: Sat, 25 Dec 2021 22:38:42 -0600 Subject: [PATCH 4/4] Add v4 migration test --- app/upgrade_test.go | 27 ------- app/upgrades/v4/upgrade_test.go | 128 ++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 27 deletions(-) delete mode 100644 app/upgrade_test.go create mode 100644 app/upgrades/v4/upgrade_test.go diff --git a/app/upgrade_test.go b/app/upgrade_test.go deleted file mode 100644 index 4f42ce4cbe5..00000000000 --- a/app/upgrade_test.go +++ /dev/null @@ -1,27 +0,0 @@ -package app_test - -import ( - "testing" - "time" - - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/osmosis-labs/osmosis/app" - "github.com/stretchr/testify/suite" - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" -) - -type UpgradeTestSuite struct { - suite.Suite - - ctx sdk.Context - app *app.OsmosisApp -} - -func (suite *UpgradeTestSuite) SetupTest() { - suite.app = app.Setup(false) - suite.ctx = suite.app.BaseApp.NewContext(false, tmproto.Header{Height: 1, ChainID: "osmosis-1", Time: time.Now().UTC()}) -} - -func TestKeeperTestSuite(t *testing.T) { - suite.Run(t, new(UpgradeTestSuite)) -} diff --git a/app/upgrades/v4/upgrade_test.go b/app/upgrades/v4/upgrade_test.go new file mode 100644 index 00000000000..e70bf821014 --- /dev/null +++ b/app/upgrades/v4/upgrade_test.go @@ -0,0 +1,128 @@ +package v4_test + +import ( + "fmt" + "strconv" + "strings" + "testing" + "time" + + sdk "github.com/cosmos/cosmos-sdk/types" + upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" + "github.com/osmosis-labs/osmosis/app" + v4 "github.com/osmosis-labs/osmosis/app/upgrades/v4" + "github.com/stretchr/testify/suite" + abci "github.com/tendermint/tendermint/abci/types" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" +) + +type UpgradeTestSuite struct { + suite.Suite + + ctx sdk.Context + app *app.OsmosisApp +} + +func (suite *UpgradeTestSuite) SetupTest() { + suite.app = app.Setup(false) + suite.ctx = suite.app.BaseApp.NewContext(false, tmproto.Header{Height: 1, ChainID: "osmosis-1", Time: time.Now().UTC()}) +} + +func TestKeeperTestSuite(t *testing.T) { + suite.Run(t, new(UpgradeTestSuite)) +} + +const dummyUpgradeHeight = 5 + +func (suite *UpgradeTestSuite) TestUpgradePayments() { + testCases := []struct { + msg string + pre_update func() + update func() + post_update func() + expPass bool + }{ + { + "Test community pool payouts for Prop 12", + func() { + // mint coins to distribution module / feepool.communitypool + + var bal = int64(1000000000000) + coin := sdk.NewInt64Coin("uosmo", bal) + coins := sdk.NewCoins(coin) + err := suite.app.BankKeeper.MintCoins(suite.ctx, "mint", coins) + suite.Require().NoError(err) + err = suite.app.BankKeeper.SendCoinsFromModuleToModule(suite.ctx, "mint", "distribution", coins) + suite.Require().NoError(err) + feePool := suite.app.DistrKeeper.GetFeePool(suite.ctx) + feePool.CommunityPool = feePool.CommunityPool.Add(sdk.NewDecCoinFromCoin(coin)) + suite.app.DistrKeeper.SetFeePool(suite.ctx, feePool) + + }, + func() { + // run upgrade + suite.ctx = suite.ctx.WithBlockHeight(dummyUpgradeHeight - 1) + plan := upgradetypes.Plan{Name: "v4", Height: dummyUpgradeHeight} + err := suite.app.UpgradeKeeper.ScheduleUpgrade(suite.ctx, plan) + suite.Require().NoError(err) + plan, exists := suite.app.UpgradeKeeper.GetUpgradePlan(suite.ctx) + suite.Require().True(exists) + + suite.ctx = suite.ctx.WithBlockHeight(dummyUpgradeHeight) + suite.Require().NotPanics(func() { + beginBlockRequest := abci.RequestBeginBlock{} + suite.app.BeginBlocker(suite.ctx, beginBlockRequest) + }) + }, + func() { + var total = int64(0) + + // check that each account got the payment expected + payments := v4.GetProp12Payments() + for _, payment := range payments { + addr, err := sdk.AccAddressFromBech32(payment[0]) + suite.Require().NoError(err) + amount, err := strconv.ParseInt(strings.TrimSpace(payment[1]), 10, 64) + suite.Require().NoError(err) + coin := sdk.NewInt64Coin("uosmo", amount) + + accBal := suite.app.BankKeeper.GetBalance(suite.ctx, addr, "uosmo") + suite.Require().Equal(coin, accBal) + + total += amount + } + + //check that the total paid out was as expected + suite.Require().Equal(total, int64(367926557424)) + + expectedBal := 1000000000000 - total + + // check that distribution module account balance has been reduced correctly + distAddr := suite.app.AccountKeeper.GetModuleAddress("distribution") + distBal := suite.app.BankKeeper.GetBalance(suite.ctx, distAddr, "uosmo") + suite.Require().Equal(distBal, sdk.NewInt64Coin("uosmo", expectedBal)) + + // check that feepool.communitypool has been reduced correctly + feePool := suite.app.DistrKeeper.GetFeePool(suite.ctx) + suite.Require().Equal(feePool.GetCommunityPool(), sdk.NewDecCoins(sdk.NewInt64DecCoin("uosmo", expectedBal))) + + // Check that gamm Minimum Fee has been set correctly + gammParams := suite.app.GAMMKeeper.GetParams(suite.ctx) + expectedCreationFee := sdk.NewCoins(sdk.NewCoin("uosmo", sdk.OneInt())) + suite.Require().Equal(gammParams.PoolCreationFee, expectedCreationFee) + }, + true, + }, + } + + for _, tc := range testCases { + suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { + suite.SetupTest() // reset + + tc.pre_update() + tc.update() + tc.post_update() + + }) + } +}