Skip to content

Commit

Permalink
feat(logic): implement chain migration to v11
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Oct 23, 2024
1 parent ee6ec60 commit da93e81
Show file tree
Hide file tree
Showing 13 changed files with 5,132 additions and 30 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()

autocliv1.RegisterQueryServer(app.GRPCQueryRouter(), runtimeservices.NewAutoCLIQueryService(app.ModuleManager.Modules))
reflectionSvc, err := runtimeservices.NewReflectionService()
Expand Down
25 changes: 18 additions & 7 deletions app/upgrades.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
package app

import (
"context"
"fmt"

v7 "github.com/axone-protocol/axoned/v10/app/upgrades/v7"
upgradetypes "cosmossdk.io/x/upgrade/types"

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

// RegisterUpgradeHandlers registers the chain upgrade handlers.
func (app *App) RegisterUpgradeHandlers() {
app.UpgradeKeeper.SetUpgradeHandler(
v7.UpgradeName,
v7.CreateUpgradeHandler(app.ModuleManager, app.configurator),
)
var upgrades = []string{
"v11.0.0",
}

// registerUpgradeHandlers registers the chain upgrade handlers.
func (app *App) registerUpgradeHandlers() {
for _, upgrade := range upgrades {
app.UpgradeKeeper.SetUpgradeHandler(
upgrade,
func(ctx context.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
return app.ModuleManager.RunMigrations(ctx, app.configurator, vm)
},
)
}

upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk()
if err != nil {
Expand Down
20 changes: 0 additions & 20 deletions app/upgrades/v7/upgrade.go

This file was deleted.

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
70 changes: 70 additions & 0 deletions x/logic/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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 MigrateStoreV3ToV4(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
}
err = k.cdc.Unmarshal(bz, &params)

return params, err
}

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

var paramsTo types.Params

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

// Limits
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()
}

// GasPolicy
if v := paramsFrom.GasPolicy.WeightingFactor; v != nil {
paramsTo.GasPolicy.WeightingFactor = v.Uint64()
}
if v := paramsFrom.GasPolicy.DefaultPredicateCost; v != nil {
paramsTo.GasPolicy.DefaultPredicateCost = v.Uint64()
}
if v := paramsFrom.GasPolicy.PredicateCosts; v != nil {
for _, pc := range v {
paramsTo.GasPolicy.PredicateCosts = append(paramsTo.GasPolicy.PredicateCosts, types.PredicateCost{
Predicate: pc.Predicate,
Cost: pc.Cost.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: 42,
DefaultPredicateCost: 66,
PredicateCosts: []types.PredicateCost{
{
Predicate: "foo/1",
Cost: 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.MigrateStoreV3ToV4(*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 da93e81

Please sign in to comment.