From a05fc0d1d61ceeb4bcd7ddd1807d39bc37cef613 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 6 Oct 2023 08:34:40 +0000 Subject: [PATCH] fix(x/gov): set default constitution in gov migration (backport #17953) (#17983) Co-authored-by: Julien Robert Co-authored-by: marbar3778 --- collections/item.go | 2 +- types/module/module.go | 2 +- x/gov/keeper/migrations.go | 2 +- x/gov/migrations/v5/store.go | 22 ++++++++++++++++++++-- x/gov/migrations/v5/store_test.go | 12 ++++++++++-- 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/collections/item.go b/collections/item.go index 155cb729aa..1b36bf46da 100644 --- a/collections/item.go +++ b/collections/item.go @@ -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{}) } diff --git a/types/module/module.go b/types/module/module.go index 83b9b63760..57bd493076 100644 --- a/types/module/module.go +++ b/types/module/module.go @@ -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 diff --git a/x/gov/keeper/migrations.go b/x/gov/keeper/migrations.go index af12c2e5a8..508d8292b9 100644 --- a/x/gov/keeper/migrations.go +++ b/x/gov/keeper/migrations.go @@ -40,5 +40,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) } diff --git a/x/gov/migrations/v5/store.go b/x/gov/migrations/v5/store.go index ddfa20f529..9451ab3b45 100644 --- a/x/gov/migrations/v5/store.go +++ b/x/gov/migrations/v5/store.go @@ -1,6 +1,7 @@ package v5 import ( + "cosmossdk.io/collections" corestoretypes "cosmossdk.io/core/store" "github.com/cosmos/cosmos-sdk/codec" @@ -9,11 +10,19 @@ import ( govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" ) +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(v4.ParamsKey) if err != nil { @@ -38,7 +47,16 @@ func MigrateStore(ctx sdk.Context, storeService corestoretypes.KVStoreService, c return err } - store.Set(v4.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 } diff --git a/x/gov/migrations/v5/store_test.go b/x/gov/migrations/v5/store_test.go index e4e4ba6832..5040f10b8e 100644 --- a/x/gov/migrations/v5/store_test.go +++ b/x/gov/migrations/v5/store_test.go @@ -6,6 +6,7 @@ import ( "github.com/stretchr/testify/require" + "cosmossdk.io/collections" storetypes "cosmossdk.io/store/types" "github.com/cosmos/cosmos-sdk/runtime" @@ -23,6 +24,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(v4.ParamsKey) @@ -32,8 +36,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 @@ -43,4 +46,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) }