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

fix(x/gov): set default constitution in gov migration #17953

Merged
merged 2 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion collections/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (i Item[V]) Set(ctx context.Context, value V) error {
}

// Has reports whether the item exists in the store or not.
// Returns an error in case
// Returns an error in case encoding fails.
func (i Item[V]) Has(ctx context.Context) (bool, error) {
return (Map[noKey, V])(i).Has(ctx, noKey{})
}
Expand Down
2 changes: 1 addition & 1 deletion types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ func (m *Manager) ExportGenesisForModules(ctx sdk.Context, cdc codec.JSONCodec,
for moduleName := range channels {
res := <-channels[moduleName]
if res.err != nil {
return nil, res.err
return nil, fmt.Errorf("genesis export error in %s: %w", moduleName, res.err)
}

genesisData[moduleName] = res.bz
Expand Down
2 changes: 1 addition & 1 deletion x/gov/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ func (m Migrator) Migrate3to4(ctx sdk.Context) error {

// Migrate4to5 migrates from version 4 to 5.
func (m Migrator) Migrate4to5(ctx sdk.Context) error {
return v5.MigrateStore(ctx, m.keeper.storeService, m.keeper.cdc)
return v5.MigrateStore(ctx, m.keeper.storeService, m.keeper.cdc, m.keeper.Constitution)
}
25 changes: 21 additions & 4 deletions x/gov/migrations/v5/store.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package v5

import (
"cosmossdk.io/collections"
corestoretypes "cosmossdk.io/core/store"

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

// ParamsKey is the key of x/gov params
var ParamsKey = []byte{0x30}
var (
// ParamsKey is the key of x/gov params
ParamsKey = []byte{0x30}
// ConstitutionKey is the key of x/gov constitution
ConstitutionKey = collections.NewPrefix(49)
)

// MigrateStore performs in-place store migrations from v4 (v0.47) to v5 (v0.50). The
// migration includes:
//
// Addition of the new proposal expedited parameters that are set to 0 by default.
func MigrateStore(ctx sdk.Context, storeService corestoretypes.KVStoreService, cdc codec.BinaryCodec) error {
// Set of default chain constitution.
func MigrateStore(ctx sdk.Context, storeService corestoretypes.KVStoreService, cdc codec.BinaryCodec, constitutionCollection collections.Item[string]) error {
store := storeService.OpenKVStore(ctx)
paramsBz, err := store.Get(ParamsKey)
if err != nil {
Expand All @@ -40,5 +46,16 @@ func MigrateStore(ctx sdk.Context, storeService corestoretypes.KVStoreService, c
return err
}

return store.Set(ParamsKey, bz)
if err := store.Set(ParamsKey, bz); err != nil {
return err
}

// Set the default consisitution if it is not set
if ok, err := constitutionCollection.Has(ctx); !ok || err != nil {
if err := constitutionCollection.Set(ctx, "This chain has no constitution."); err != nil {
return err
}
}

return nil
}
12 changes: 10 additions & 2 deletions x/gov/migrations/v5/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/stretchr/testify/require"

"cosmossdk.io/collections"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/runtime"
Expand All @@ -22,6 +23,9 @@ func TestMigrateStore(t *testing.T) {
govKey := storetypes.NewKVStoreKey("gov")
ctx := testutil.DefaultContext(govKey, storetypes.NewTransientStoreKey("transient_test"))
store := ctx.KVStore(govKey)
storeService := runtime.NewKVStoreService(govKey)
sb := collections.NewSchemaBuilder(storeService)
constitutionCollection := collections.NewItem(sb, v5.ConstitutionKey, "constitution", collections.StringValue)

var params v1.Params
bz := store.Get(v5.ParamsKey)
Expand All @@ -31,8 +35,7 @@ func TestMigrateStore(t *testing.T) {
require.Equal(t, (*time.Duration)(nil), params.ExpeditedVotingPeriod)

// Run migrations.
storeService := runtime.NewKVStoreService(govKey)
err := v5.MigrateStore(ctx, storeService, cdc)
err := v5.MigrateStore(ctx, storeService, cdc, constitutionCollection)
require.NoError(t, err)

// Check params
Expand All @@ -42,4 +45,9 @@ func TestMigrateStore(t *testing.T) {
require.Equal(t, v1.DefaultParams().ExpeditedMinDeposit, params.ExpeditedMinDeposit)
require.Equal(t, v1.DefaultParams().ExpeditedThreshold, params.ExpeditedThreshold)
require.Equal(t, v1.DefaultParams().ExpeditedVotingPeriod, params.ExpeditedVotingPeriod)

// Check constitution
result, err := constitutionCollection.Get(ctx)
require.NoError(t, err)
require.Equal(t, "This chain has no constitution.", result)
}