Skip to content

Commit

Permalink
Launchpad Migration (#6829)
Browse files Browse the repository at this point in the history
* fix migration

* add 039 pkg

* lint

* Fix pkg name

* add v0.39 auth types

* remove file

* updates

* updates

* update godoc

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
  • Loading branch information
alexanderbez and fedekunze authored Jul 28, 2020
1 parent 919e906 commit 3ff3e58
Show file tree
Hide file tree
Showing 11 changed files with 580 additions and 74 deletions.
466 changes: 466 additions & 0 deletions x/auth/legacy/v0_39/types.go

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package v039
package v040

import (
"fmt"

v038auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_38"
v039auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_39"
)

// Migrate accepts exported x/auth genesis state from v0.38 and migrates it to
// v0.39 x/auth genesis state. The migration includes:
// Migrate accepts exported x/auth genesis state from v0.38/v0.39 and migrates
// it to v0.40 x/auth genesis state. The migration includes:
//
// - Removing coins from account encoding.
func Migrate(authGenState v038auth.GenesisState) v038auth.GenesisState {
func Migrate(authGenState v039auth.GenesisState) v039auth.GenesisState {
for _, account := range authGenState.Accounts {
// set coins to nil and allow the JSON encoding to omit coins
if err := account.SetCoins(nil); err != nil {
panic(fmt.Sprintf("failed to set account coins to nil: %s", err))
}
}

authGenState.Accounts = v038auth.SanitizeGenesisAccounts(authGenState.Accounts)
authGenState.Accounts = v039auth.SanitizeGenesisAccounts(authGenState.Accounts)
return authGenState
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v039_test
package v040_test

import (
"testing"
Expand All @@ -7,41 +7,39 @@ import (
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_34"
v038auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_38"
v039 "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_39"
v039auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_39"
v040 "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_40"

"github.com/stretchr/testify/require"
)

func TestMigrate(t *testing.T) {
v039Codec := codec.New()
cryptocodec.RegisterCrypto(v039Codec)
v038auth.RegisterCodec(v039Codec)
v039auth.RegisterCodec(v039Codec)

coins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50))
addr1, _ := sdk.AccAddressFromBech32("cosmos1xxkueklal9vejv9unqu80w9vptyepfa95pd53u")
acc1 := v038auth.NewBaseAccount(addr1, coins, nil, 1, 0)
acc1 := v039auth.NewBaseAccount(addr1, coins, nil, 1, 0)

addr2, _ := sdk.AccAddressFromBech32("cosmos15v50ymp6n5dn73erkqtmq0u8adpl8d3ujv2e74")
vaac := v038auth.NewContinuousVestingAccountRaw(
v038auth.NewBaseVestingAccount(
v038auth.NewBaseAccount(addr2, coins, nil, 1, 0), coins, nil, nil, 3160620846,
),
vaac := v039auth.NewContinuousVestingAccountRaw(
v039auth.NewBaseVestingAccount(v039auth.NewBaseAccount(addr2, coins, nil, 1, 0), coins, nil, nil, 3160620846),
1580309972,
)

gs := v038auth.GenesisState{
gs := v039auth.GenesisState{
Params: v0_34.Params{
MaxMemoCharacters: 10,
TxSigLimit: 10,
TxSizeCostPerByte: 10,
SigVerifyCostED25519: 10,
SigVerifyCostSecp256k1: 10,
},
Accounts: v038auth.GenesisAccounts{acc1, vaac},
Accounts: v039auth.GenesisAccounts{acc1, vaac},
}

migrated := v039.Migrate(gs)
migrated := v040.Migrate(gs)
expected := `{
"params": {
"max_memo_characters": "10",
Expand All @@ -55,18 +53,18 @@ func TestMigrate(t *testing.T) {
"type": "cosmos-sdk/BaseAccount",
"value": {
"address": "cosmos1xxkueklal9vejv9unqu80w9vptyepfa95pd53u",
"public_key": "",
"account_number": 1,
"sequence": 0
"public_key": null,
"account_number": "1",
"sequence": "0"
}
},
{
"type": "cosmos-sdk/ContinuousVestingAccount",
"value": {
"address": "cosmos15v50ymp6n5dn73erkqtmq0u8adpl8d3ujv2e74",
"public_key": "",
"account_number": 1,
"sequence": 0,
"public_key": null,
"account_number": "1",
"sequence": "0",
"original_vesting": [
{
"denom": "stake",
Expand All @@ -75,8 +73,8 @@ func TestMigrate(t *testing.T) {
],
"delegated_free": [],
"delegated_vesting": [],
"end_time": 3160620846,
"start_time": 1580309972
"end_time": "3160620846",
"start_time": "1580309972"
}
}
]
Expand Down
8 changes: 8 additions & 0 deletions x/auth/legacy/v0_40/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package v040

// DONTCOVER
// nolint

const (
ModuleName = "auth"
)
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package v039
package v040

import (
v038auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_38"
v039auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_39"
v038bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v0_38"
)

// Migrate accepts exported x/auth and x/bank genesis state from v0.38 and migrates
// it to v0.39 x/bank genesis state. The migration includes:
// Migrate accepts exported v0.39 x/auth and v0.38 x/bank genesis state and
// migrates it to v0.40 x/bank genesis state. The migration includes:
//
// - Moving balances from x/auth to x/bank genesis state.
func Migrate(bankGenState v038bank.GenesisState, authGenState v038auth.GenesisState) GenesisState {
func Migrate(bankGenState v038bank.GenesisState, authGenState v039auth.GenesisState) GenesisState {
balances := make([]Balance, len(authGenState.Accounts))
for i, acc := range authGenState.Accounts {
balances[i] = Balance{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v039_test
package v040_test

import (
"testing"
Expand All @@ -7,16 +7,17 @@ import (
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
v038auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_38"
v039auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_39"
v038bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v0_38"
v039bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v0_39"
v040bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v0_40"

"github.com/stretchr/testify/require"
)

func TestMigrate(t *testing.T) {
v039Codec := codec.New()
cryptocodec.RegisterCrypto(v039Codec)
v038auth.RegisterCodec(v039Codec)
v040Codec := codec.New()
cryptocodec.RegisterCrypto(v040Codec)
v039auth.RegisterCodec(v040Codec)

coins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50))
addr1, _ := sdk.AccAddressFromBech32("cosmos1xxkueklal9vejv9unqu80w9vptyepfa95pd53u")
Expand All @@ -33,11 +34,11 @@ func TestMigrate(t *testing.T) {
bankGenState := v038bank.GenesisState{
SendEnabled: true,
}
authGenState := v038auth.GenesisState{
Accounts: v038auth.GenesisAccounts{acc1, vaac},
authGenState := v039auth.GenesisState{
Accounts: v039auth.GenesisAccounts{acc1, vaac},
}

migrated := v039bank.Migrate(bankGenState, authGenState)
migrated := v040bank.Migrate(bankGenState, authGenState)
expected := `{
"send_enabled": true,
"balances": [
Expand All @@ -62,7 +63,7 @@ func TestMigrate(t *testing.T) {
]
}`

bz, err := v039Codec.MarshalJSONIndent(migrated, "", " ")
bz, err := v040Codec.MarshalJSONIndent(migrated, "", " ")
require.NoError(t, err)
require.Equal(t, expected, string(bz))
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v039
package v040

// DONTCOVER
// nolint
Expand Down
2 changes: 2 additions & 0 deletions x/genutil/client/cli/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
v036 "github.com/cosmos/cosmos-sdk/x/genutil/legacy/v0_36"
v038 "github.com/cosmos/cosmos-sdk/x/genutil/legacy/v0_38"
v039 "github.com/cosmos/cosmos-sdk/x/genutil/legacy/v0_39"
v040 "github.com/cosmos/cosmos-sdk/x/genutil/legacy/v0_40"
"github.com/cosmos/cosmos-sdk/x/genutil/types"
)

Expand All @@ -31,6 +32,7 @@ var migrationMap = types.MigrationMap{
"v0.36": v036.Migrate,
"v0.38": v038.Migrate, // NOTE: v0.37 and v0.38 are genesis compatible
"v0.39": v039.Migrate,
"v0.40": v040.Migrate,
}

// GetMigrationCallback returns a MigrationCallback for a given version.
Expand Down
4 changes: 1 addition & 3 deletions x/genutil/legacy/v0_38/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ func Migrate(appState types.AppMap) types.AppMap {

// Migrate relative source genesis application state and marshal it into
// the respective key.
appState[v038auth.ModuleName] = v038Codec.MustMarshalJSON(
v038auth.Migrate(authGenState, genAccountsGenState),
)
appState[v038auth.ModuleName] = v038Codec.MustMarshalJSON(v038auth.Migrate(authGenState, genAccountsGenState))
}

// migrate staking state
Expand Down
38 changes: 8 additions & 30 deletions x/genutil/legacy/v0_39/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,29 @@ import (
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
v038auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_38"
v039auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_39"
v038bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v0_38"
v039bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v0_39"
"github.com/cosmos/cosmos-sdk/x/genutil/types"
)

// Migrate migrates exported state from v0.38 to a v0.39 genesis state.
//
// NOTE: No actual migration occurs since the types do not change, but JSON
// serialization of accounts do change.
func Migrate(appState types.AppMap) types.AppMap {
v038Codec := codec.New()
cryptocodec.RegisterCrypto(v038Codec)
v038auth.RegisterCodec(v038Codec)

v039Codec := codec.New()
cryptocodec.RegisterCrypto(v039Codec)
v038auth.RegisterCodec(v039Codec)
v039auth.RegisterCodec(v039Codec)

// remove balances from existing accounts
// migrate x/auth state (JSON serialization only)
if appState[v038auth.ModuleName] != nil {
// unmarshal relative source genesis application state
var authGenState v038auth.GenesisState
v038Codec.MustUnmarshalJSON(appState[v038auth.ModuleName], &authGenState)

// delete deprecated x/auth genesis state
delete(appState, v038auth.ModuleName)

// Migrate relative source genesis application state and marshal it into
// the respective key.
appState[v039auth.ModuleName] = v039Codec.MustMarshalJSON(v039auth.Migrate(authGenState))
}

if appState[v038bank.ModuleName] != nil {
// unmarshal relative source genesis application state
var bankGenState v038bank.GenesisState
v038Codec.MustUnmarshalJSON(appState[v038bank.ModuleName], &bankGenState)

// unmarshal x/auth genesis state to retrieve all account balances
var authGenState v038auth.GenesisState
v038Codec.MustUnmarshalJSON(appState[v038auth.ModuleName], &authGenState)

// delete deprecated x/bank genesis state
delete(appState, v038bank.ModuleName)

// Migrate relative source genesis application state and marshal it into
// the respective key.
appState[v039bank.ModuleName] = v039Codec.MustMarshalJSON(
v039bank.Migrate(bankGenState, authGenState),
)
delete(appState, v038auth.ModuleName) // delete old key in case the name changed
appState[v039auth.ModuleName] = v039Codec.MustMarshalJSON(authGenState)
}

return appState
Expand Down
55 changes: 55 additions & 0 deletions x/genutil/legacy/v0_40/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package v040

import (
"github.com/cosmos/cosmos-sdk/codec"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
v039auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_39"
v040auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_40"
v038bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v0_38"
v040bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v0_40"
"github.com/cosmos/cosmos-sdk/x/genutil/types"
)

// Migrate migrates exported state from v0.39 to a v0.40 genesis state.
func Migrate(appState types.AppMap) types.AppMap {
v039Codec := codec.New()
cryptocodec.RegisterCrypto(v039Codec)
v039auth.RegisterCodec(v039Codec)

v040Codec := codec.New()
cryptocodec.RegisterCrypto(v040Codec)
v039auth.RegisterCodec(v040Codec)

// remove balances from existing accounts
if appState[v039auth.ModuleName] != nil {
// unmarshal relative source genesis application state
var authGenState v039auth.GenesisState
v039Codec.MustUnmarshalJSON(appState[v039auth.ModuleName], &authGenState)

// delete deprecated x/auth genesis state
delete(appState, v039auth.ModuleName)

// Migrate relative source genesis application state and marshal it into
// the respective key.
appState[v040auth.ModuleName] = v040Codec.MustMarshalJSON(v040auth.Migrate(authGenState))
}

if appState[v038bank.ModuleName] != nil {
// unmarshal relative source genesis application state
var bankGenState v038bank.GenesisState
v039Codec.MustUnmarshalJSON(appState[v038bank.ModuleName], &bankGenState)

// unmarshal x/auth genesis state to retrieve all account balances
var authGenState v039auth.GenesisState
v039Codec.MustUnmarshalJSON(appState[v039auth.ModuleName], &authGenState)

// delete deprecated x/bank genesis state
delete(appState, v038bank.ModuleName)

// Migrate relative source genesis application state and marshal it into
// the respective key.
appState[v040bank.ModuleName] = v040Codec.MustMarshalJSON(v040bank.Migrate(bankGenState, authGenState))
}

return appState
}

0 comments on commit 3ff3e58

Please sign in to comment.