Skip to content

Commit

Permalink
Move fork and upgrade logic into sub-directory structure (#680)
Browse files Browse the repository at this point in the history
* Move fork logic into sub-directory structure

* Move upgrade handlers into sub-directories

* Move upgrade logic to end of keeper initialization

* Add v4 migration test
  • Loading branch information
ValarDragon committed Jan 9, 2022
1 parent 4da4013 commit 66637c3
Show file tree
Hide file tree
Showing 20 changed files with 470 additions and 78 deletions.
42 changes: 39 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -128,7 +129,6 @@ import (
)

const appName = "OsmosisApp"
const v5UpgradeName = "v5"

var (
// DefaultNodeHome default home directories for the application daemon
Expand Down Expand Up @@ -348,6 +348,7 @@ func NewOsmosisApp(
app.BaseApp,
)

<<<<<<< HEAD
// this configures a no-op upgrade handler for the v4 upgrade,
// which improves the lockup module's store management.
app.UpgradeKeeper.SetUpgradeHandler(
Expand Down Expand Up @@ -437,6 +438,8 @@ func NewOsmosisApp(
app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &storeUpgrades))
}

=======
>>>>>>> 8dcbaf53... Move fork and upgrade logic into sub-directory structure (#680)
// Create IBC Keeper
app.IBCKeeper = ibckeeper.NewKeeper(
appCodec,
Expand Down Expand Up @@ -481,6 +484,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(
Expand Down Expand Up @@ -732,7 +737,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)
}

Expand Down Expand Up @@ -899,6 +904,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()
Expand Down
45 changes: 8 additions & 37 deletions app/forks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
27 changes: 0 additions & 27 deletions app/upgrade_test.go

This file was deleted.

11 changes: 11 additions & 0 deletions app/upgrades/README.md
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions app/upgrades/v3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# TODO Write stuff

Should include description about this version, compatibility with v1 until height {...}, and fork code here.
4 changes: 4 additions & 0 deletions app/upgrades/v3/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package v3

const UpgradeName = "v3"
const UpgradeHeight = 712000
46 changes: 46 additions & 0 deletions app/upgrades/v3/forks.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Empty file added app/upgrades/v4/README.md
Empty file.
3 changes: 3 additions & 0 deletions app/upgrades/v4/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package v4

const UpgradeName = "v4"
14 changes: 8 additions & 6 deletions app/prop12.go → app/upgrades/v4/prop12.go
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)

}
2 changes: 1 addition & 1 deletion app/prop12_data.go → app/upgrades/v4/prop12_data.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app
package v4

import (
"encoding/csv"
Expand Down
Loading

0 comments on commit 66637c3

Please sign in to comment.