Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADR 004 Geneis Migration #5589

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions x/auth/legacy/v0_38/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func Migrate(authGenState v036auth.GenesisState, genAccountsGenState v036genacco
accounts[i] = genAccount
}

accounts = sanitizeGenesisAccounts(accounts)
accounts = SanitizeGenesisAccounts(accounts)

if err := validateGenAccounts(accounts); err != nil {
if err := ValidateGenAccounts(accounts); err != nil {
panic(err)
}

Expand Down
12 changes: 6 additions & 6 deletions x/auth/legacy/v0_38/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ type (

BaseAccount struct {
Address sdk.AccAddress `json:"address" yaml:"address"`
Coins sdk.Coins `json:"coins" yaml:"coins"`
Coins sdk.Coins `json:"coins,omitempty" yaml:"coins,omitempty"`
Copy link
Contributor Author

@alexanderbez alexanderbez Jan 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've decided to add omitempty here to avoid having to literally copy the entire file solely to remove a single field.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like this move!

PubKey crypto.PubKey `json:"public_key" yaml:"public_key"`
AccountNumber uint64 `json:"account_number" yaml:"account_number"`
Sequence uint64 `json:"sequence" yaml:"sequence"`
}

baseAccountPretty struct {
Address sdk.AccAddress `json:"address" yaml:"address"`
Coins sdk.Coins `json:"coins" yaml:"coins"`
Coins sdk.Coins `json:"coins,omitempty" yaml:"coins,omitempty"`
PubKey string `json:"public_key" yaml:"public_key"`
AccountNumber uint64 `json:"account_number" yaml:"account_number"`
Sequence uint64 `json:"sequence" yaml:"sequence"`
Expand All @@ -72,7 +72,7 @@ type (

vestingAccountPretty struct {
Address sdk.AccAddress `json:"address" yaml:"address"`
Coins sdk.Coins `json:"coins" yaml:"coins"`
Coins sdk.Coins `json:"coins,omitempty" yaml:"coins,omitempty"`
PubKey string `json:"public_key" yaml:"public_key"`
AccountNumber uint64 `json:"account_number" yaml:"account_number"`
Sequence uint64 `json:"sequence" yaml:"sequence"`
Expand Down Expand Up @@ -104,7 +104,7 @@ type (

moduleAccountPretty struct {
Address sdk.AccAddress `json:"address" yaml:"address"`
Coins sdk.Coins `json:"coins" yaml:"coins"`
Coins sdk.Coins `json:"coins,omitempty" yaml:"coins,omitempty"`
PubKey string `json:"public_key" yaml:"public_key"`
AccountNumber uint64 `json:"account_number" yaml:"account_number"`
Sequence uint64 `json:"sequence" yaml:"sequence"`
Expand Down Expand Up @@ -486,7 +486,7 @@ func validatePermissions(permissions ...string) error {
return nil
}

func sanitizeGenesisAccounts(genAccounts GenesisAccounts) GenesisAccounts {
func SanitizeGenesisAccounts(genAccounts GenesisAccounts) GenesisAccounts {
sort.Slice(genAccounts, func(i, j int) bool {
return genAccounts[i].GetAccountNumber() < genAccounts[j].GetAccountNumber()
})
Expand All @@ -500,7 +500,7 @@ func sanitizeGenesisAccounts(genAccounts GenesisAccounts) GenesisAccounts {
return genAccounts
}

func validateGenAccounts(genAccounts GenesisAccounts) error {
func ValidateGenAccounts(genAccounts GenesisAccounts) error {
addrMap := make(map[string]bool, len(genAccounts))
for _, acc := range genAccounts {

Expand Down
23 changes: 23 additions & 0 deletions x/auth/legacy/v0_39/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package v039

import (
"fmt"

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

// Migrate accepts exported x/auth genesis state from v0.38 and migrates it to
// v0.39 x/auth genesis state. The migration includes:
//
// - Removing coins from account encoding.
func Migrate(authGenState v038auth.GenesisState) v038auth.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)
return authGenState
}
87 changes: 87 additions & 0 deletions x/auth/legacy/v0_39/migrate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package v039_test

import (
"testing"

"github.com/cosmos/cosmos-sdk/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"

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

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

coins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50))
addr1, _ := sdk.AccAddressFromBech32("cosmos1xxkueklal9vejv9unqu80w9vptyepfa95pd53u")
acc1 := v038auth.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,
),
1580309972,
)

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

migrated := v039.Migrate(gs)
expected := `{
"params": {
"max_memo_characters": "10",
"tx_sig_limit": "10",
"tx_size_cost_per_byte": "10",
"sig_verify_cost_ed25519": "10",
"sig_verify_cost_secp256k1": "10"
},
"accounts": [
{
"type": "cosmos-sdk/Account",
"value": {
"address": "cosmos1xxkueklal9vejv9unqu80w9vptyepfa95pd53u",
"public_key": "",
"account_number": 1,
"sequence": 0
}
},
{
"type": "cosmos-sdk/ContinuousVestingAccount",
"value": {
"address": "cosmos15v50ymp6n5dn73erkqtmq0u8adpl8d3ujv2e74",
"public_key": "",
"account_number": 1,
"sequence": 0,
"original_vesting": [
{
"denom": "stake",
"amount": "50"
}
],
"delegated_free": [],
"delegated_vesting": [],
"end_time": 3160620846,
"start_time": 1580309972
}
}
]
}`

bz, err := v039Codec.MarshalJSONIndent(migrated, "", " ")
require.NoError(t, err)
require.Equal(t, expected, string(bz))
}
8 changes: 8 additions & 0 deletions x/auth/legacy/v0_39/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package v039

// DONTCOVER
// nolint

const (
ModuleName = "auth"
)
14 changes: 14 additions & 0 deletions x/bank/legacy/v0_38/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package v038

// DONTCOVER
// nolint

const (
ModuleName = "bank"
)

type (
GenesisState struct {
SendEnabled bool `json:"send_enabled" yaml:"send_enabled"`
}
)
22 changes: 22 additions & 0 deletions x/bank/legacy/v0_39/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package v039

import (
v038auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_38"
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:
//
// - Moving balances from x/auth to x/bank genesis state.
func Migrate(bankGenState v038bank.GenesisState, authGenState v038auth.GenesisState) GenesisState {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to ensure that this Migrate runs before auth.Migrate? Is there anything that needs to be done to ensure that?

balances := make([]Balance, len(authGenState.Accounts))
for i, acc := range authGenState.Accounts {
balances[i] = Balance{
Address: acc.GetAddress(),
Coins: acc.GetCoins(),
}
}

return NewGenesisState(bankGenState.SendEnabled, balances)
}
67 changes: 67 additions & 0 deletions x/bank/legacy/v0_39/migrate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package v039_test

import (
"testing"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
v038auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v0_38"
v038bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v0_38"
v039bank "github.com/cosmos/cosmos-sdk/x/bank/legacy/v0_39"

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

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

coins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50))
addr1, _ := sdk.AccAddressFromBech32("cosmos1xxkueklal9vejv9unqu80w9vptyepfa95pd53u")
acc1 := v038auth.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,
),
1580309972,
)

bankGenState := v038bank.GenesisState{
SendEnabled: true,
}
authGenState := v038auth.GenesisState{
Accounts: v038auth.GenesisAccounts{acc1, vaac},
}

migrated := v039bank.Migrate(bankGenState, authGenState)
expected := `{
"send_enabled": true,
"balances": [
{
"address": "cosmos1xxkueklal9vejv9unqu80w9vptyepfa95pd53u",
"coins": [
{
"denom": "stake",
"amount": "50"
}
]
},
{
"address": "cosmos15v50ymp6n5dn73erkqtmq0u8adpl8d3ujv2e74",
"coins": [
{
"denom": "stake",
"amount": "50"
}
]
}
]
}`

bz, err := v039Codec.MarshalJSONIndent(migrated, "", " ")
require.NoError(t, err)
require.Equal(t, expected, string(bz))
}
43 changes: 43 additions & 0 deletions x/bank/legacy/v0_39/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package v039

// DONTCOVER
// nolint

import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

const (
ModuleName = "bank"
)

var _ GenesisBalance = (*Balance)(nil)

type (
GenesisBalance interface {
GetAddress() sdk.AccAddress
GetCoins() sdk.Coins
}

GenesisState struct {
SendEnabled bool `json:"send_enabled" yaml:"send_enabled"`
Balances []Balance `json:"balances" yaml:"balances"`
}

Balance struct {
Address sdk.AccAddress `json:"address" yaml:"address"`
Coins sdk.Coins `json:"coins" yaml:"coins"`
}
)

func NewGenesisState(sendEnabled bool, balances []Balance) GenesisState {
return GenesisState{SendEnabled: sendEnabled, Balances: balances}
}

func (b Balance) GetAddress() sdk.AccAddress {
return b.Address
}

func (b Balance) GetCoins() sdk.Coins {
return b.Coins
}
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 (
extypes "github.com/cosmos/cosmos-sdk/x/genutil"
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"
)

const (
Expand All @@ -29,6 +30,7 @@ const (
var migrationMap = extypes.MigrationMap{
"v0.36": v036.Migrate,
"v0.38": v038.Migrate, // NOTE: v0.37 and v0.38 are genesis compatible
"v0.39": v039.Migrate,
}

// GetMigrationCallback returns a MigrationCallback for a given version.
Expand Down
Loading