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

This commit is contained in:
Julien Robert 2023-10-06 10:16:25 +02:00 committed by GitHub
parent 12b45f3db6
commit 45b44e678c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 9 deletions

View File

@ -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{})
}

View File

@ -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

View File

@ -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)
}

View File

@ -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
}

View File

@ -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)
}