Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: added upgrade handler for new token emission #2176

Merged
merged 8 commits into from
Aug 1, 2023
19 changes: 19 additions & 0 deletions app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/umee-network/umee/v5/x/incentive"
leveragekeeper "github.com/umee-network/umee/v5/x/leverage/keeper"
leveragetypes "github.com/umee-network/umee/v5/x/leverage/types"
"github.com/umee-network/umee/v5/x/metoken"
oraclekeeper "github.com/umee-network/umee/v5/x/oracle/keeper"
oracletypes "github.com/umee-network/umee/v5/x/oracle/types"
"github.com/umee-network/umee/v5/x/ugov"
Expand Down Expand Up @@ -55,6 +56,24 @@ func (app UmeeApp) RegisterUpgradeHandlers(bool) {
app.registerUpgrade("v5.0", upgradeInfo, ugov.ModuleName, wasm.ModuleName)
app.registerUpgrade5_1(upgradeInfo)
app.registerUpgrade6(upgradeInfo)
app.registerNewTokenEmissionUpgrade(upgradeInfo)
}

// TODO: this upgrade registration is just for testing purpose, once we finalize the release for new token emission
// then we need to change planName and storeUpgrades
func (app *UmeeApp) registerNewTokenEmissionUpgrade(upgradeInfo upgradetypes.Plan) {
// TODO:finalize the name
planName := "token_emission"
app.UpgradeKeeper.SetUpgradeHandler(planName,
func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
return app.mm.RunMigrations(ctx, app.configurator, fromVM)
},
)

app.storeUpgrade(planName, upgradeInfo, storetypes.StoreUpgrades{
Added: []string{metoken.ModuleName},
gsk967 marked this conversation as resolved.
Show resolved Hide resolved
Deleted: []string{"gravity"},
})
}

func (app *UmeeApp) registerUpgrade6(upgradeInfo upgradetypes.Plan) {
Expand Down
2 changes: 1 addition & 1 deletion x/ugov/inflation_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func DefaultInflationParams() InflationParams {
return InflationParams{
MaxSupply: coin.New(appparams.BondDenom, 21_000000000_000000), // 21 Billion Maximum
InflationCycle: time.Hour * 24 * 365, // 2 years for default inflation cycle
InflationCycle: time.Hour * 24 * 365 * 2, // 2 years for default inflation cycle
InflationReductionRate: bpmath.FixedBP(2500), // 25% reduction rate for inflation cyle
}
}
Expand Down
9 changes: 0 additions & 9 deletions x/ugov/keeper/keys.go

This file was deleted.

24 changes: 24 additions & 0 deletions x/ugov/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"

v2 "github.com/umee-network/umee/v5/x/ugov/migrations/v2"
)

// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
kb Builder
}

// NewMigrator returns a new Migrator instance.
func NewMigrator(kb Builder) Migrator {
return Migrator{
kb: kb,
}
}

// Migrate1to2 migrates from version 1 to 2.
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
return v2.MigrateStore(ctx, m.kb.storeKey)
}
18 changes: 9 additions & 9 deletions x/ugov/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,44 @@ import (
)

func (k Keeper) SetMinGasPrice(p sdk.DecCoin) error {
return store.SetValue(k.store, keyMinGasPrice, &p, "gas_price")
return store.SetValue(k.store, ugov.KeyMinGasPrice, &p, "gas_price")
}

func (k Keeper) MinGasPrice() sdk.DecCoin {
gp := store.GetValue[*sdk.DecCoin](k.store, keyMinGasPrice, "gas_price")
gp := store.GetValue[*sdk.DecCoin](k.store, ugov.KeyMinGasPrice, "gas_price")
if gp == nil {
return coin.Umee0dec
}
return *gp
}

func (k Keeper) SetEmergencyGroup(p sdk.AccAddress) {
store.SetAddress(k.store, keyEmergencyGroup, p)
store.SetAddress(k.store, ugov.KeyEmergencyGroup, p)
}

func (k Keeper) EmergencyGroup() sdk.AccAddress {
return store.GetAddress(k.store, keyEmergencyGroup)
return store.GetAddress(k.store, ugov.KeyEmergencyGroup)
}

func (k Keeper) SetInflationParams(ip ugov.InflationParams) error {
return store.SetValue(k.store, keyInflationParams, &ip, "inflation_params")
return store.SetValue(k.store, ugov.KeyInflationParams, &ip, "inflation_params")
}

func (k Keeper) InflationParams() ugov.InflationParams {
ip := store.GetValue[*ugov.InflationParams](k.store, keyInflationParams, "inflation_params")
ip := store.GetValue[*ugov.InflationParams](k.store, ugov.KeyInflationParams, "inflation_params")
if ip == nil {
return ugov.InflationParams{}
}
return *ip
}

func (k Keeper) SetInflationCycleEnd(startTime time.Time) error {
store.SetTimeMs(k.store, keyInflationCycleEnd, startTime)
func (k Keeper) SetInflationCycleEnd(cycleEnd time.Time) error {
store.SetTimeMs(k.store, ugov.KeyInflationCycleEnd, cycleEnd)
return nil
}

// Returns zero unix time if the inflation cycle was not set.
func (k Keeper) GetInflationCycleEnd() time.Time {
t, _ := store.GetTimeMs(k.store, keyInflationCycleEnd)
t, _ := store.GetTimeMs(k.store, ugov.KeyInflationCycleEnd)
return t
}
8 changes: 8 additions & 0 deletions x/ugov/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ const (
// StoreKey defines the primary module store key
StoreKey = ModuleName
)

// store key prefixes
var (
KeyMinGasPrice = []byte{0x01}
KeyEmergencyGroup = []byte{0x02}
KeyInflationParams = []byte{0x03}
KeyInflationCycleEnd = []byte{0x04}
)
23 changes: 23 additions & 0 deletions x/ugov/migrations/v2/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package v2

import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/umee-network/umee/v5/util/store"
"github.com/umee-network/umee/v5/x/ugov"
)

// MigrateStore performs in-place store migrations from 1 to 2
func MigrateStore(ctx sdk.Context, key storetypes.StoreKey) error {
kvStore := ctx.KVStore(key)

ip := ugov.DefaultInflationParams()
if err := store.SetValue(kvStore, ugov.KeyInflationParams, &ip, "inflation_params"); err != nil {
return err
}

cycleEnd := ctx.BlockTime().Add(ip.InflationCycle)
gsk967 marked this conversation as resolved.
Show resolved Hide resolved
store.SetTimeMs(kvStore, ugov.KeyInflationCycleEnd, cycleEnd)

return nil
}
44 changes: 44 additions & 0 deletions x/ugov/migrations/v2/store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package v2_test

import (
"testing"
"time"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
"gotest.tools/v3/assert"

"github.com/umee-network/umee/v5/tests/tsdk"
"github.com/umee-network/umee/v5/util/store"
"github.com/umee-network/umee/v5/x/ugov"
v2 "github.com/umee-network/umee/v5/x/ugov/migrations/v2"
)

func TestMigrateStore(t *testing.T) {
storeKey := storetypes.NewKVStoreKey(ugov.ModuleName)
sdkContext, _ := tsdk.NewCtx(t, []storetypes.StoreKey{storeKey}, []storetypes.StoreKey{})
kvStore := sdkContext.KVStore(storeKey)

getInflationParams := func() *ugov.InflationParams {
return store.GetValue[*ugov.InflationParams](kvStore, ugov.KeyInflationParams, "ip")
}

getInflationCycleEnd := func() (time.Time, bool) {
return store.GetTimeMs(kvStore, ugov.KeyInflationCycleEnd)
}

// before migration
_, ok := getInflationCycleEnd()
assert.Equal(t, ok, false)
ip := getInflationParams()
assert.DeepEqual(t, 0, ip.Size())

// after migration
err := v2.MigrateStore(sdkContext, storeKey)
assert.NilError(t, err)
ip = getInflationParams()
assert.DeepEqual(t, ugov.DefaultInflationParams(), *ip)

cycleEnd, ok := getInflationCycleEnd()
assert.Equal(t, ok, true)
assert.DeepEqual(t, sdkContext.BlockTime().Add(ip.InflationCycle), cycleEnd)
}
11 changes: 10 additions & 1 deletion x/ugov/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import (
"github.com/umee-network/umee/v5/x/ugov/keeper"
)

const (
consensusVersion uint64 = 2
)

var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
Expand Down Expand Up @@ -111,7 +115,7 @@ func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.

// ConsensusVersion implements module.AppModule
func (AppModule) ConsensusVersion() uint64 {
return 1
return consensusVersion
}

// RegisterInvariants implements module.AppModule
Expand All @@ -121,6 +125,11 @@ func (AppModule) RegisterInvariants(sdk.InvariantRegistry) {}
func (am AppModule) RegisterServices(cfg module.Configurator) {
ugov.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServer(am.kb))
ugov.RegisterQueryServer(cfg.QueryServer(), keeper.NewQuerier(am.kb))

m := keeper.NewMigrator(am.kb)
if err := cfg.RegisterMigration(ugov.ModuleName, 1, m.Migrate1to2); err != nil {
panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", ugov.ModuleName, err))
}
}

// BeginBlock executes all ABCI BeginBlock logic respective to the x/uibc module.
Expand Down
Loading