Skip to content

Commit

Permalink
refactor: expedited proposals parameter migrations (#303)
Browse files Browse the repository at this point in the history
* migration to v4

* added value & migrated params

* Test additions and CI fixes

* additional fixes

* table mismatch fixes

* feedback implemented

* feedback implemented

* Apply suggestions from code review

Co-authored-by: Adam Tucker <[email protected]>
  • Loading branch information
xBalbinus and czarcas7ic authored Aug 8, 2022
1 parent 6fcd25f commit 02273b8
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 1 deletion.
6 changes: 6 additions & 0 deletions x/gov/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
v043 "github.com/cosmos/cosmos-sdk/x/gov/legacy/v043"
v3 "github.com/cosmos/cosmos-sdk/x/gov/legacy/v3"
v4 "github.com/cosmos/cosmos-sdk/x/gov/legacy/v4"
)

// Migrator is a struct for handling in-place store migrations.
Expand All @@ -25,3 +26,8 @@ func (m Migrator) Migrate1to2(ctx sdk.Context) error {
func (m Migrator) Migrate2to3(ctx sdk.Context) error {
return v3.MigrateStore(ctx, m.keeper.paramSpace)
}

// Migrate3to4 migrates from version 3 to 4.
func (m Migrator) Migrate3to4(ctx sdk.Context) error {
return v4.MigrateStore(ctx, m.keeper.paramSpace)
}
7 changes: 7 additions & 0 deletions x/gov/legacy/v4/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package v4

var (
MinExpeditedDeposit = minExpeditedDeposit
ExpeditedVotingPeriod = expeditedVotingPeriod
ExpeditedThreshold = expeditedThreshold
)
51 changes: 51 additions & 0 deletions x/gov/legacy/v4/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package v4

import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov/types"
)

// If expedited, the deposit to enter voting period will be
// increased to 5000 OSMO. The proposal will have 24 hours to achieve
// a two-thirds majority of all staked OSMO voting power voting YES.

var (
minExpeditedDeposit = sdk.NewCoins(sdk.NewCoin("uosmo", sdk.NewInt(5000 * 1_000_000)))
expeditedVotingPeriod = time.Duration(time.Hour * 24)
expeditedThreshold = sdk.NewDec(2).Quo(sdk.NewDec(3))
)

// MigrateStore performs in-place store migrations for consensus version 4
// in the gov module.
// The migration includes:
//
// - Setting the expedited proposals params in the paramstore.
func MigrateStore(ctx sdk.Context, paramstore types.ParamSubspace) error {
migrateParamsStore(ctx, paramstore)
return nil
}

func migrateParamsStore(ctx sdk.Context, paramstore types.ParamSubspace) {
var (
depositParams types.DepositParams
votingParams types.VotingParams
tallyParams types.TallyParams
)

// Set depositParams
paramstore.Get(ctx, types.ParamStoreKeyDepositParams, &depositParams)
depositParams.MinExpeditedDeposit = minExpeditedDeposit
paramstore.Set(ctx, types.ParamStoreKeyDepositParams, depositParams)

// Set votingParams
paramstore.Get(ctx, types.ParamStoreKeyVotingParams, &votingParams)
votingParams.ExpeditedVotingPeriod = expeditedVotingPeriod
paramstore.Set(ctx, types.ParamStoreKeyVotingParams, votingParams)

// Set tallyParams
paramstore.Get(ctx, types.ParamStoreKeyTallyParams, &tallyParams)
tallyParams.ExpeditedThreshold = expeditedThreshold
paramstore.Set(ctx, types.ParamStoreKeyTallyParams, tallyParams)
}
55 changes: 55 additions & 0 deletions x/gov/legacy/v4/store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package v4_test

import (
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
v4 "github.com/cosmos/cosmos-sdk/x/gov/legacy/v4"
"github.com/cosmos/cosmos-sdk/x/gov/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)

func TestGovStoreMigrationToV4ConsensusVersion(t *testing.T) {
encCfg := simapp.MakeTestEncodingConfig()
govKey := sdk.NewKVStoreKey("gov")
transientTestKey := sdk.NewTransientStoreKey("transient_test")
ctx := testutil.DefaultContext(govKey, transientTestKey)
paramstore := paramtypes.NewSubspace(encCfg.Marshaler, encCfg.Amino, govKey, transientTestKey, "gov")

paramstore = paramstore.WithKeyTable(types.ParamKeyTable())

// We assume that all deposit params are set besides the MinInitialDepositRatio
originalDepositParams := types.DefaultDepositParams()
originalDepositParams.MinExpeditedDeposit = sdk.NewCoins()
paramstore.Set(ctx, types.ParamStoreKeyDepositParams, originalDepositParams)

originalVotingParams := types.DefaultVotingParams()
originalVotingParams.ExpeditedVotingPeriod = time.Duration(0)
paramstore.Set(ctx, types.ParamStoreKeyVotingParams, originalVotingParams)

originalTallyParams := types.DefaultTallyParams()
originalTallyParams.ExpeditedThreshold = sdk.ZeroDec()
paramstore.Set(ctx, types.ParamStoreKeyTallyParams, originalTallyParams)

// Run migrations.
err := v4.MigrateStore(ctx, paramstore)
require.NoError(t, err)

// Make sure the new params are set.
var depositParams types.DepositParams
paramstore.Get(ctx, types.ParamStoreKeyDepositParams, &depositParams)
require.Equal(t, v4.MinExpeditedDeposit, depositParams.MinExpeditedDeposit)

var votingParams types.VotingParams
paramstore.Get(ctx, types.ParamStoreKeyVotingParams, &votingParams)
require.Equal(t, v4.ExpeditedVotingPeriod, votingParams.ExpeditedVotingPeriod)

var tallyParams types.TallyParams
paramstore.Get(ctx, types.ParamStoreKeyTallyParams, &tallyParams)
require.Equal(t, v4.ExpeditedThreshold, tallyParams.ExpeditedThreshold)
}
6 changes: 5 additions & 1 deletion x/gov/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
if err != nil {
panic(err)
}
cfg.RegisterMigration(types.ModuleName, 3, m.Migrate3to4)
if err != nil {
panic(err)
}
}

// InitGenesis performs genesis initialization for the gov module. It returns
Expand All @@ -184,7 +188,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 3 }
func (AppModule) ConsensusVersion() uint64 { return 4 }

// BeginBlock performs a no-op.
func (AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}
Expand Down

0 comments on commit 02273b8

Please sign in to comment.