Skip to content

Commit

Permalink
feat(logic): implement store migration from v10 to v11
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Oct 22, 2024
1 parent 53a0091 commit 01b84c2
Show file tree
Hide file tree
Showing 13 changed files with 5,104 additions and 9 deletions.
4 changes: 2 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -814,9 +814,9 @@ func New(
panic(err)
}

// RegisterUpgradeHandlers is used for registering any on-chain upgrades.
// registerUpgradeHandlers is used for registering any on-chain upgrades.
// Make sure it's called after `app.ModuleManager` and `app.configurator` are set.
app.RegisterUpgradeHandlers()
app.registerUpgradeHandlers()

Check warning on line 819 in app/app.go

View check run for this annotation

Codecov / codecov/patch

app/app.go#L819

Added line #L819 was not covered by tests

autocliv1.RegisterQueryServer(app.GRPCQueryRouter(), runtimeservices.NewAutoCLIQueryService(app.ModuleManager.Modules))
reflectionSvc, err := runtimeservices.NewReflectionService()
Expand Down
10 changes: 5 additions & 5 deletions app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package app
import (
"fmt"

v7 "github.com/axone-protocol/axoned/v10/app/upgrades/v7"
v11 "github.com/axone-protocol/axoned/v10/app/upgrades/v11"
)

// RegisterUpgradeHandlers registers the chain upgrade handlers.
func (app *App) RegisterUpgradeHandlers() {
// registerUpgradeHandlers registers the chain upgrade handlers.
func (app *App) registerUpgradeHandlers() {

Check warning on line 10 in app/upgrades.go

View check run for this annotation

Codecov / codecov/patch

app/upgrades.go#L10

Added line #L10 was not covered by tests
app.UpgradeKeeper.SetUpgradeHandler(
v7.UpgradeName,
v7.CreateUpgradeHandler(app.ModuleManager, app.configurator),
v11.UpgradeName,
v11.CreateUpgradeHandler(app.ModuleManager, app.configurator),

Check warning on line 13 in app/upgrades.go

View check run for this annotation

Codecov / codecov/patch

app/upgrades.go#L12-L13

Added lines #L12 - L13 were not covered by tests
)

upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk()
Expand Down
4 changes: 2 additions & 2 deletions app/upgrades/v7/upgrade.go → app/upgrades/v11/upgrade.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v7
package v11

import (
"context"
Expand All @@ -8,7 +8,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/module"
)

var UpgradeName = "v7.0.0"
var UpgradeName = "v11.0.0"

func CreateUpgradeHandler(
mm *module.Manager,
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ require (
github.com/huandu/xstrings v1.5.0
github.com/hyperledger/aries-framework-go v0.3.2
github.com/ignite/cli v0.27.2
github.com/jinzhu/copier v0.3.5
github.com/muesli/reflow v0.3.0
github.com/nuts-foundation/go-did v0.15.0
github.com/piprate/json-gold v0.5.1-0.20230111113000-6ddbe6e6f19f
Expand Down
54 changes: 54 additions & 0 deletions x/logic/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package keeper

import (
"github.com/jinzhu/copier"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"

v1beta2types "github.com/axone-protocol/axoned/v10/x/logic/legacy/v1beta2/types"
"github.com/axone-protocol/axoned/v10/x/logic/types"
)

func MigrateStoreV10ToV11(k Keeper) module.MigrationHandler {
getParams := func(ctx sdk.Context) (params v1beta2types.Params, err error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ParamsKey)
if bz == nil {
return params, nil
}

Check warning on line 19 in x/logic/keeper/migrations.go

View check run for this annotation

Codecov / codecov/patch

x/logic/keeper/migrations.go#L18-L19

Added lines #L18 - L19 were not covered by tests
err = k.cdc.Unmarshal(bz, &params)

return params, err
}

return func(ctx sdk.Context) error {
paramsFrom, err := getParams(ctx)
if err != nil {
return err
}

Check warning on line 29 in x/logic/keeper/migrations.go

View check run for this annotation

Codecov / codecov/patch

x/logic/keeper/migrations.go#L28-L29

Added lines #L28 - L29 were not covered by tests

var paramsTo types.Params

if err := copier.Copy(&paramsTo.Interpreter, paramsFrom.Interpreter); err != nil {
return err
}

Check warning on line 35 in x/logic/keeper/migrations.go

View check run for this annotation

Codecov / codecov/patch

x/logic/keeper/migrations.go#L34-L35

Added lines #L34 - L35 were not covered by tests
if err := copier.Copy(&paramsTo.GasPolicy, paramsFrom.GasPolicy); err != nil {
return err
}

Check warning on line 38 in x/logic/keeper/migrations.go

View check run for this annotation

Codecov / codecov/patch

x/logic/keeper/migrations.go#L37-L38

Added lines #L37 - L38 were not covered by tests
if v := paramsFrom.Limits.MaxSize; v != nil {
paramsTo.Limits.MaxSize = v.Uint64()
}
if v := paramsFrom.Limits.MaxResultCount; v != nil {
paramsTo.Limits.MaxResultCount = v.Uint64()
}
if v := paramsFrom.Limits.MaxUserOutputSize; v != nil {
paramsTo.Limits.MaxUserOutputSize = v.Uint64()
}
if v := paramsFrom.Limits.MaxVariables; v != nil {
paramsTo.Limits.MaxVariables = v.Uint64()
}

return k.SetParams(ctx, paramsTo)
}
}
166 changes: 166 additions & 0 deletions x/logic/keeper/migrations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package keeper_test

import (
gocontext "context"
"fmt"
"io/fs"
"testing"

"github.com/golang/mock/gomock"
"github.com/samber/lo"

. "github.com/smartystreets/goconvey/convey"

sdkmath "cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/testutil"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

"github.com/axone-protocol/axoned/v10/x/logic"
"github.com/axone-protocol/axoned/v10/x/logic/keeper"
v1beta2types "github.com/axone-protocol/axoned/v10/x/logic/legacy/v1beta2/types"
logictestutil "github.com/axone-protocol/axoned/v10/x/logic/testutil"
"github.com/axone-protocol/axoned/v10/x/logic/types"
)

func TestMigrateStoreV10ToV11(t *testing.T) {
Convey("Given a test cases", t, func() {
cases := []struct {
params v1beta2types.Params
expect types.Params
}{
{
params: v1beta2types.Params{
Interpreter: v1beta2types.Interpreter{
PredicatesFilter: v1beta2types.Filter{},
Bootstrap: "",
VirtualFilesFilter: v1beta2types.Filter{},
},
GasPolicy: v1beta2types.GasPolicy{},
Limits: v1beta2types.Limits{},
},
expect: types.Params{
Interpreter: types.Interpreter{
PredicatesFilter: types.Filter{},
Bootstrap: "",
VirtualFilesFilter: types.Filter{},
},
GasPolicy: types.GasPolicy{},
Limits: types.Limits{},
},
},
{
params: v1beta2types.Params{
Interpreter: v1beta2types.Interpreter{
PredicatesFilter: v1beta2types.Filter{
Whitelist: []string{"foo/1", "bar/2"},
Blacklist: []string{"baz/3"},
},
Bootstrap: "foo(bar).",
VirtualFilesFilter: v1beta2types.Filter{
Whitelist: []string{"foo://bar"},
Blacklist: []string{"bar://baz"},
},
},
GasPolicy: v1beta2types.GasPolicy{
WeightingFactor: lo.ToPtr(sdkmath.NewUint(42)),
DefaultPredicateCost: lo.ToPtr(sdkmath.NewUint(66)),
PredicateCosts: []v1beta2types.PredicateCost{
{
Predicate: "foo/1",
Cost: lo.ToPtr(sdkmath.NewUint(99)),
},
},
},
Limits: v1beta2types.Limits{
MaxSize: lo.ToPtr(sdkmath.NewUint(100)),
MaxResultCount: lo.ToPtr(sdkmath.NewUint(10)),
MaxUserOutputSize: lo.ToPtr(sdkmath.NewUint(50)),
MaxVariables: lo.ToPtr(sdkmath.NewUint(5)),
},
},
expect: types.Params{
Interpreter: types.Interpreter{
PredicatesFilter: types.Filter{
Whitelist: []string{"foo/1", "bar/2"},
Blacklist: []string{"baz/3"},
},
Bootstrap: "foo(bar).",
VirtualFilesFilter: types.Filter{
Whitelist: []string{"foo://bar"},
Blacklist: []string{"bar://baz"},
},
},
GasPolicy: types.GasPolicy{
WeightingFactor: lo.ToPtr(sdkmath.NewUint(42)),
DefaultPredicateCost: lo.ToPtr(sdkmath.NewUint(66)),
PredicateCosts: []types.PredicateCost{
{
Predicate: "foo/1",
Cost: lo.ToPtr(sdkmath.NewUint(99)),
},
},
},
Limits: types.Limits{
MaxSize: 100,
MaxResultCount: 10,
MaxUserOutputSize: 50,
MaxVariables: 5,
},
},
},
}
for nc, tc := range cases {
Convey(fmt.Sprintf("Given a mocked logic keeper for test case %d", nc), func() {
encCfg := moduletestutil.MakeTestEncodingConfig(logic.AppModuleBasic{})
key := storetypes.NewKVStoreKey(types.StoreKey)
testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test"))

ctrl := gomock.NewController(t)
accountKeeper := logictestutil.NewMockAccountKeeper(ctrl)
authQueryService := logictestutil.NewMockAuthQueryService(ctrl)
bankKeeper := logictestutil.NewMockBankKeeper(ctrl)
fsProvider := logictestutil.NewMockFS(ctrl)

logicKeeper := keeper.NewKeeper(
encCfg.Codec,
encCfg.InterfaceRegistry,
key,
key,
authtypes.NewModuleAddress(govtypes.ModuleName),
accountKeeper,
authQueryService,
bankKeeper,
func(_ gocontext.Context) fs.FS {
return fsProvider
})
So(logicKeeper, ShouldNotBeNil)

Convey("Given a store with v10 params", func() {
store := testCtx.Ctx.KVStore(key)
bz, err := encCfg.Codec.Marshal(&tc.params)
So(err, ShouldBeNil)

store.Set(types.ParamsKey, bz)

Convey("When migrating store from v10 to v11", func() {
migrateHandler := keeper.MigrateStoreV10ToV11(*logicKeeper)
So(migrateHandler, ShouldNotBeNil)

err := migrateHandler(testCtx.Ctx)
So(err, ShouldBeNil)

Convey("Then the store should have the expected v11 params", func() {
params := logicKeeper.GetParams(testCtx.Ctx)
So(err, ShouldBeNil)
So(params, ShouldResemble, tc.expect)
})
})
})
})
}
})
}
Loading

0 comments on commit 01b84c2

Please sign in to comment.