-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR #5589: ADR 004 Geneis Migration
- Loading branch information
1 parent
69a3808
commit 59592cf
Showing
11 changed files
with
329 additions
and
8 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,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 { | ||
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.