-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
146 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package keeper | ||
|
||
import ( | ||
"github.com/OmniFlix/onft/exported" | ||
v2 "github.com/OmniFlix/onft/migrations/v2" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// Migrator is a struct for handling in-place state migrations. | ||
type Migrator struct { | ||
keeper Keeper | ||
legacySubspace exported.Subspace | ||
} | ||
|
||
func NewMigrator(k Keeper, ss exported.Subspace) Migrator { | ||
return Migrator{ | ||
keeper: k, | ||
legacySubspace: ss, | ||
} | ||
} | ||
|
||
// Migrate1to2 migrates the onft module state from the consensus version 1 to | ||
// version 2. Specifically, it takes the parameters that are currently stored | ||
// and managed by the x/params modules and stores them directly into the onft | ||
// module state. | ||
func (m Migrator) Migrate1to2(ctx sdk.Context) error { | ||
return v2.Migrate(ctx, ctx.KVStore(m.keeper.storeKey), m.legacySubspace, m.keeper.cdc) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package v2 | ||
|
||
import ( | ||
"github.com/OmniFlix/onft/exported" | ||
"github.com/OmniFlix/onft/types" | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
const ( | ||
ModuleName = "onft" | ||
) | ||
|
||
var ParamsKey = []byte{0x07} | ||
|
||
// Migrate migrates the onft module state from the consensus version 1 to | ||
// version 2. Specifically, it takes the parameters that are currently stored | ||
// and managed by the x/params modules and stores them directly into the onft | ||
// module state. | ||
func Migrate( | ||
ctx sdk.Context, | ||
store sdk.KVStore, | ||
legacySubspace exported.Subspace, | ||
cdc codec.BinaryCodec, | ||
) error { | ||
var currParams types.Params | ||
legacySubspace.GetParamSet(ctx, &currParams) | ||
|
||
if err := currParams.ValidateBasic(); err != nil { | ||
return err | ||
} | ||
|
||
bz := cdc.MustMarshal(&currParams) | ||
store.Set(ParamsKey, bz) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package v2_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/OmniFlix/onft" | ||
"github.com/OmniFlix/onft/exported" | ||
v2 "github.com/OmniFlix/onft/migrations/v2" | ||
"github.com/OmniFlix/onft/types" | ||
"github.com/cosmos/cosmos-sdk/testutil" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" | ||
) | ||
|
||
type mockSubspace struct { | ||
ps types.Params | ||
} | ||
|
||
func newMockSubspace(ps types.Params) mockSubspace { | ||
return mockSubspace{ps: ps} | ||
} | ||
|
||
func (ms mockSubspace) GetParamSet(ctx sdk.Context, ps exported.ParamSet) { | ||
*ps.(*types.Params) = ms.ps | ||
} | ||
|
||
func TestMigrate(t *testing.T) { | ||
encCfg := moduletestutil.MakeTestEncodingConfig(onft.AppModuleBasic{}) | ||
cdc := encCfg.Codec | ||
|
||
storeKey := sdk.NewKVStoreKey(v2.ModuleName) | ||
tKey := sdk.NewTransientStoreKey("transient_test") | ||
ctx := testutil.DefaultContext(storeKey, tKey) | ||
store := ctx.KVStore(storeKey) | ||
|
||
legacySubspace := newMockSubspace(types.DefaultParams()) | ||
require.NoError(t, v2.Migrate(ctx, store, legacySubspace, cdc)) | ||
|
||
var res types.Params | ||
bz := store.Get(v2.ParamsKey) | ||
require.NoError(t, cdc.Unmarshal(bz, &res)) | ||
require.Equal(t, legacySubspace.ps, res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters