Skip to content

Commit

Permalink
Migration implementation
Browse files Browse the repository at this point in the history
See PR #277
  • Loading branch information
RiccardoM authored Oct 5, 2020
1 parent 914fe4c commit 53442e1
Show file tree
Hide file tree
Showing 50 changed files with 67,574 additions and 146,688 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Version 0.12.2
## Changes
- Added the migration command from v0.10.0 to v0.12.0

# Version 0.12.1
## Bug fixes
- Fixed an upgrade migration bug
Expand Down
8 changes: 0 additions & 8 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,14 +369,6 @@ func NewDesmosApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest
app.mm.RegisterInvariants(&app.CrisisKeeper)
app.mm.RegisterRoutes(app.Router(), app.QueryRouter())

// Register the upgrade handler for the september update
app.upgradeKeeper.SetUpgradeHandler("september-upgrade", func(ctx sdk.Context, plan upgrade.Plan) {
// Migrate the posts from v0.10.0 to v0.12.0
if err := app.postsKeeper.MigratePostsFrom0100To0120(ctx); err != nil {
panic(err)
}
})

// create the simulation manager and define the order of the modules for deterministic simulations
//
// NOTE: this is not required apps that don't use the simulator for fuzz testing
Expand Down
6 changes: 4 additions & 2 deletions cli_test/cli_relationships_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
package clitest

import (
"testing"

"github.com/cosmos/cosmos-sdk/tests"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/desmos-labs/desmos/x/relationships/types"
"github.com/stretchr/testify/require"
"testing"

"github.com/desmos-labs/desmos/x/relationships/types"
)

func TestDesmosCLICreateRelationship(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions x/genutil/client/cli/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strconv"
"time"

v0120 "github.com/desmos-labs/desmos/x/genutil/legacy/v0.12.0"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
Expand All @@ -24,6 +26,7 @@ import (
var migrationMap = map[string]types.MigrationCallback{
"v0.8.0": v080.Migrate,
"v0.10.0": v0100.Migrate,
"v0.12.0": v0120.Migrate,
}

const (
Expand Down
4 changes: 2 additions & 2 deletions x/genutil/legacy/v0.10.0/migrate.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v090
package v0100

import (
"time"
Expand All @@ -13,7 +13,7 @@ import (
v080posts "github.com/desmos-labs/desmos/x/posts/legacy/v0.8.0"
)

// Migrate migrates exported state from v0.8.0 to a v0.9.0 genesis state.
// Migrate migrates exported state from v0.8.0 to a v0.10.0 genesis state.
func Migrate(appState genutil.AppMap, values ...interface{}) genutil.AppMap {
// Perform Cosmos v0.39 migration
appState = v039.Migrate(appState)
Expand Down
41 changes: 41 additions & 0 deletions x/genutil/legacy/v0.12.0/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package v0120

import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/genutil"

"github.com/desmos-labs/desmos/x/relationships"
relationshipsTypes "github.com/desmos-labs/desmos/x/relationships/types"

v0100posts "github.com/desmos-labs/desmos/x/posts/legacy/v0.10.0"
v0120posts "github.com/desmos-labs/desmos/x/posts/legacy/v0.12.0"
v060posts "github.com/desmos-labs/desmos/x/posts/legacy/v0.6.0"
)

// Migrate migrates exported state from v0.10.0 to a v0.12.0 genesis state.
func Migrate(appState genutil.AppMap, values ...interface{}) genutil.AppMap {
v0100Codec := codec.New()
codec.RegisterCrypto(v0100Codec)

v0120Codec := codec.New()
codec.RegisterCrypto(v0120Codec)

// Migrate posts state
if appState[v060posts.ModuleName] != nil {
var genDocs v0100posts.GenesisState
v0120Codec.MustUnmarshalJSON(appState[v060posts.ModuleName], &genDocs)

appState[v060posts.ModuleName] = v0120Codec.MustMarshalJSON(
v0120posts.Migrate(genDocs),
)
}

// Add the relationships default state
if appState[relationships.ModuleName] == nil {
appState[relationships.ModuleName] = v0120Codec.MustMarshalJSON(
relationshipsTypes.DefaultGenesisState(),
)
}

return appState
}
58 changes: 58 additions & 0 deletions x/genutil/legacy/v0.12.0/migrate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package v0120_test

import (
"testing"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/stretchr/testify/require"
tm "github.com/tendermint/tendermint/types"

v0120 "github.com/desmos-labs/desmos/x/genutil/legacy/v0.12.0"
v0100posts "github.com/desmos-labs/desmos/x/posts/legacy/v0.10.0"
v0120posts "github.com/desmos-labs/desmos/x/posts/legacy/v0.12.0"
v060posts "github.com/desmos-labs/desmos/x/posts/legacy/v0.6.0"
"github.com/desmos-labs/desmos/x/relationships"
)

func TestMigrate0100(t *testing.T) {
cdc := codec.New()
codec.RegisterCrypto(cdc)

config := sdk.GetConfig()
config.SetBech32PrefixForAccount("desmos", "desmos"+sdk.PrefixPublic)
config.Seal()

// Read the genesis
genesis, err := tm.GenesisDocFromFile("v0100state.json")
require.NoError(t, err)

// Read the whole app state
var v010state genutil.AppMap
err = cdc.UnmarshalJSON(genesis.AppState, &v010state)
require.NoError(t, err)

// Migrate everything
v0120state := v0120.Migrate(v010state)

// Make sure that all the posts are migrated
var v010postsState v0100posts.GenesisState
err = cdc.UnmarshalJSON(v010state[v060posts.ModuleName], &v010postsState)

var v0120postsState v0120posts.GenesisState
err = cdc.UnmarshalJSON(v0120state[v060posts.ModuleName], &v0120postsState)
require.Equal(t, len(v0120postsState.Posts), len(v010postsState.Posts))

// Make sure that all the posts' polls are migrated correctly
for index, post := range v010postsState.Posts {
if post.PollData == nil {
require.Nil(t, v010postsState.Posts[index].PollData)
} else {
require.NotNil(t, v010postsState.Posts[index].PollData)
}
}

// Make sure the relationships genesis state is not nil
require.NotNil(t, v0120state[relationships.ModuleName])
}
Loading

0 comments on commit 53442e1

Please sign in to comment.