Skip to content

Commit a5f405b

Browse files
committed
fix v3 migrate
1 parent 11343e6 commit a5f405b

File tree

6 files changed

+97
-23
lines changed

6 files changed

+97
-23
lines changed

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,6 @@ cloud.google.com/go/workflows v1.7.0/go.mod h1:JhSrZuVZWuiDfKEFxU0/F1PQjmpnpcoIS
614614
cloud.google.com/go/workflows v1.8.0/go.mod h1:ysGhmEajwZxGn1OhGOGKsTXc5PyxOc0vfKf5Af+to4M=
615615
cloud.google.com/go/workflows v1.9.0/go.mod h1:ZGkj1aFIOd9c8Gerkjjq7OW7I5+l6cSvT3ujaO/WwSA=
616616
cloud.google.com/go/workflows v1.10.0/go.mod h1:fZ8LmRmZQWacon9UCX1r/g/DfAXx5VcPALq2CxzdePw=
617-
cosmossdk.io/api v1.0.0-rc.1 h1:KwZHIMveoeg6YVwvKZxJLp7be5uk6qmnqNAar2tPxVU=
618-
cosmossdk.io/api v1.0.0-rc.1/go.mod h1:8YOT+XjVFb9eZJk62YqjFILOm8MlLhbnkC9/jxIYri8=
619617
cosmossdk.io/collections v1.3.1 h1:09e+DUId2brWsNOQ4nrk+bprVmMUaDH9xvtZkeqIjVw=
620618
cosmossdk.io/collections v1.3.1/go.mod h1:ynvkP0r5ruAjbmedE+vQ07MT6OtJ0ZIDKrtJHK7Q/4c=
621619
cosmossdk.io/core v1.1.0-rc.1 h1:VhF5xd4uJZt/lQzbl8qT1W3Pcrklp4RSnugcWQZyf5M=

x/gov/types/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package types
22

3-
// Config is a config struct used for initialising the gov module to avoid using globals.
3+
// Config is a config struct used for initializing the gov module to avoid using globals.
44
type Config struct {
55
// MaxMetadataLen defines the maximum proposal metadata length.
66
MaxMetadataLen uint64

x/mint/keeper/migrator.go

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package keeper
22

33
import (
4-
"context"
5-
64
sdk "github.com/cosmos/cosmos-sdk/types"
75
"github.com/cosmos/cosmos-sdk/x/mint/exported"
86
v2 "github.com/cosmos/cosmos-sdk/x/mint/migrations/v2"
9-
"github.com/cosmos/cosmos-sdk/x/mint/types"
7+
v3 "github.com/cosmos/cosmos-sdk/x/mint/migrations/v3"
108
)
119

1210
// Migrator is a struct for handling in-place state migrations.
@@ -33,21 +31,6 @@ func (m Migrator) Migrate1to2(ctx sdk.Context) error {
3331

3432
// Migrate2to3 migrates the x/mint module state from the consensus version 2 to
3533
// version 3.
36-
func (m Migrator) Migrate2to3(ctx context.Context) error {
37-
params, err := m.keeper.Params.Get(ctx)
38-
if err != nil {
39-
return err
40-
}
41-
42-
// Initialize the new MaxSupply parameter with the default value
43-
defaultParams := types.DefaultParams()
44-
params.MaxSupply = defaultParams.MaxSupply
45-
46-
// Set the updated params
47-
err = m.keeper.Params.Set(ctx, params)
48-
if err != nil {
49-
return err
50-
}
51-
52-
return nil
34+
func (m Migrator) Migrate2to3(ctx sdk.Context) error {
35+
return v3.Migrate(ctx, m.keeper.storeService.OpenKVStore(ctx), m.keeper.cdc, m.keeper.Params)
5336
}

x/mint/migrations/v3/migrate.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package v3
2+
3+
import (
4+
"cosmossdk.io/collections"
5+
storetypes "cosmossdk.io/core/store"
6+
"cosmossdk.io/math"
7+
8+
"github.com/cosmos/cosmos-sdk/codec"
9+
sdk "github.com/cosmos/cosmos-sdk/types"
10+
"github.com/cosmos/cosmos-sdk/x/mint/types"
11+
)
12+
13+
const (
14+
ModuleName = "mint"
15+
)
16+
17+
var ParamsKey = []byte{0x01}
18+
19+
// Migrate migrates the x/mint module state from the consensus version 2 to
20+
// version 3.
21+
func Migrate(
22+
ctx sdk.Context,
23+
store storetypes.KVStore,
24+
cdc codec.BinaryCodec,
25+
params collections.Item[types.Params],
26+
) error {
27+
currParams, err := params.Get(ctx)
28+
if err != nil {
29+
return err
30+
}
31+
32+
currParams.MaxSupply = math.NewInt(0)
33+
if err := currParams.Validate(); err != nil {
34+
return err
35+
}
36+
37+
bz := cdc.MustMarshal(&currParams)
38+
return store.Set(ParamsKey, bz)
39+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package v3_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
8+
"cosmossdk.io/collections"
9+
storetypes "cosmossdk.io/store/types"
10+
11+
"github.com/cosmos/cosmos-sdk/codec"
12+
"github.com/cosmos/cosmos-sdk/runtime"
13+
"github.com/cosmos/cosmos-sdk/testutil"
14+
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
15+
"github.com/cosmos/cosmos-sdk/x/mint"
16+
v3 "github.com/cosmos/cosmos-sdk/x/mint/migrations/v3"
17+
"github.com/cosmos/cosmos-sdk/x/mint/types"
18+
)
19+
20+
type mockSubspace struct {
21+
ps types.Params
22+
}
23+
24+
func newMockSubspace(ps types.Params) mockSubspace {
25+
return mockSubspace{ps: ps}
26+
}
27+
28+
func TestMigrate(t *testing.T) {
29+
encCfg := moduletestutil.MakeTestEncodingConfig(mint.AppModuleBasic{})
30+
cdc := encCfg.Codec
31+
32+
storeKey := storetypes.NewKVStoreKey(v3.ModuleName)
33+
tKey := storetypes.NewTransientStoreKey("transient_test")
34+
ctx := testutil.DefaultContext(storeKey, tKey)
35+
kvStoreService := runtime.NewKVStoreService(storeKey)
36+
store := kvStoreService.OpenKVStore(ctx)
37+
38+
sb := collections.NewSchemaBuilder(kvStoreService)
39+
params := collections.NewItem(sb, v3.ParamsKey, "params", codec.CollValue[types.Params](cdc))
40+
41+
dp := newMockSubspace(types.DefaultParams())
42+
require.NoError(t, params.Set(ctx, dp.ps))
43+
require.NoError(t, v3.Migrate(ctx, store, cdc, params))
44+
45+
var res types.Params
46+
bz, err := store.Get(v3.ParamsKey)
47+
require.NoError(t, err)
48+
require.NoError(t, cdc.Unmarshal(bz, &res))
49+
require.Equal(t, dp.ps, res)
50+
}

x/mint/module.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
133133
if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {
134134
panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", types.ModuleName, err))
135135
}
136+
137+
if err := cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3); err != nil {
138+
panic(fmt.Sprintf("failed to migrate x/%s from version 2 to 3: %v", types.ModuleName, err))
139+
}
136140
}
137141

138142
// InitGenesis performs genesis initialization for the mint module. It returns

0 commit comments

Comments
 (0)