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

Co-authored-by: Julien Robert <julien@rbrt.fr>
Co-authored-by: marbar3778 <marbar3778@yahoo.com>
This commit is contained in:
mergify[bot] 2023-10-06 08:34:40 +00:00 committed by GitHub
parent 53d228f4eb
commit a05fc0d1d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 7 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

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

View File

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

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