-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
alexanderbez
merged 2 commits into
bez/5519-adr-004-bank-balances-ii
from
bez/5519-adr-004-bank-balances-gen-migration
Jan 30, 2020
Merged
ADR 004 Geneis Migration #5589
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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 | ||
} |
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,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)) | ||
} |
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,8 @@ | ||
package v039 | ||
|
||
// DONTCOVER | ||
// nolint | ||
|
||
const ( | ||
ModuleName = "auth" | ||
) |
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,14 @@ | ||
package v038 | ||
|
||
// DONTCOVER | ||
// nolint | ||
|
||
const ( | ||
ModuleName = "bank" | ||
) | ||
|
||
type ( | ||
GenesisState struct { | ||
SendEnabled bool `json:"send_enabled" yaml:"send_enabled"` | ||
} | ||
) |
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,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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to ensure that this |
||
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) | ||
} |
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,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)) | ||
} |
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,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 | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like this move!