diff --git a/collections/item.go b/collections/item.go index 155cb729aa24..1b36bf46dad6 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 3d869a9157fe..5980b5b0a857 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 c8190275c02d..1b71107fd8db 100644 --- a/x/gov/keeper/migrations.go +++ b/x/gov/keeper/migrations.go @@ -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) } diff --git a/x/gov/migrations/v5/store.go b/x/gov/migrations/v5/store.go index 701492955432..cc37287f964c 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" @@ -8,14 +9,19 @@ import ( 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 { @@ -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 } diff --git a/x/gov/migrations/v5/store_test.go b/x/gov/migrations/v5/store_test.go index 41f13e510640..bd651674f4b7 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" @@ -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) @@ -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 @@ -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) }