-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
feat: add migrations for balances with zero coins #9664
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
56c639d
WIP: zero coin migrations
atheeshp 03864eb
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/pr…
atheeshp d31cf4e
prune zero coins supply
atheeshp 283f1e3
add tests
atheeshp ea470a8
address review changes
atheeshp 2262c4d
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/pr…
atheeshp 18fd61c
Merge branch 'master' into atheesh/prune-zero-coin-migrations
alexanderbez e2c7fea
address review changess
atheeshp 52136fe
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/pr…
atheeshp 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package v043 | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/bank/types" | ||
) | ||
|
||
// pruneZeroBalancesJSON removes the zero balance addresses from exported genesis. | ||
func pruneZeroBalancesJSON(oldBalances []types.Balance) []types.Balance { | ||
var balances []types.Balance | ||
|
||
for _, b := range oldBalances { | ||
if !b.Coins.IsZero() { | ||
b.Coins = sdk.NewCoins(b.Coins...) // prunes zero denom. | ||
balances = append(balances, b) | ||
} | ||
} | ||
|
||
return balances | ||
} | ||
|
||
// MigrateJSON accepts exported v0.40 x/bank genesis state and migrates it to | ||
// v0.43 x/bank genesis state. The migration includes: | ||
// - Prune balances & supply with zero coins (ref: https://github.com/cosmos/cosmos-sdk/pull/9229) | ||
func MigrateJSON(oldState *types.GenesisState) *types.GenesisState { | ||
atheeshp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return &types.GenesisState{ | ||
Params: oldState.Params, | ||
Balances: pruneZeroBalancesJSON(oldState.Balances), | ||
Supply: sdk.NewCoins(oldState.Supply...), // NewCoins used here to remove zero coin denoms from supply. | ||
DenomMetadata: oldState.DenomMetadata, | ||
} | ||
} |
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,93 @@ | ||
package v043_test | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/simapp" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
v043bank "github.com/cosmos/cosmos-sdk/x/bank/migrations/v043" | ||
"github.com/cosmos/cosmos-sdk/x/bank/types" | ||
) | ||
|
||
func TestMigrateJSON(t *testing.T) { | ||
encodingConfig := simapp.MakeTestEncodingConfig() | ||
clientCtx := client.Context{}. | ||
WithInterfaceRegistry(encodingConfig.InterfaceRegistry). | ||
WithTxConfig(encodingConfig.TxConfig). | ||
WithCodec(encodingConfig.Codec) | ||
|
||
voter, err := sdk.AccAddressFromBech32("cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh") | ||
require.NoError(t, err) | ||
bankGenState := &types.GenesisState{ | ||
Balances: []types.Balance{ | ||
{ | ||
Address: voter.String(), | ||
Coins: sdk.Coins{ | ||
sdk.NewCoin("foo", sdk.NewInt(10)), | ||
sdk.NewCoin("bar", sdk.NewInt(20)), | ||
sdk.NewCoin("foobar", sdk.NewInt(0)), | ||
}, | ||
}, | ||
}, | ||
Supply: sdk.Coins{ | ||
sdk.NewCoin("foo", sdk.NewInt(10)), | ||
sdk.NewCoin("bar", sdk.NewInt(20)), | ||
sdk.NewCoin("foobar", sdk.NewInt(0)), | ||
sdk.NewCoin("barfoo", sdk.NewInt(0)), | ||
}, | ||
} | ||
|
||
migrated := v043bank.MigrateJSON(bankGenState) | ||
|
||
bz, err := clientCtx.Codec.MarshalJSON(migrated) | ||
require.NoError(t, err) | ||
|
||
// Indent the JSON bz correctly. | ||
var jsonObj map[string]interface{} | ||
err = json.Unmarshal(bz, &jsonObj) | ||
require.NoError(t, err) | ||
indentedBz, err := json.MarshalIndent(jsonObj, "", "\t") | ||
require.NoError(t, err) | ||
|
||
// Make sure about: | ||
// - zero coin balances pruned. | ||
// - zero supply denoms pruned. | ||
expected := `{ | ||
"balances": [ | ||
{ | ||
"address": "cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh", | ||
"coins": [ | ||
{ | ||
"amount": "20", | ||
"denom": "bar" | ||
}, | ||
{ | ||
"amount": "10", | ||
"denom": "foo" | ||
} | ||
] | ||
} | ||
], | ||
"denom_metadata": [], | ||
"params": { | ||
"default_send_enabled": false, | ||
"send_enabled": [] | ||
}, | ||
"supply": [ | ||
{ | ||
"amount": "20", | ||
"denom": "bar" | ||
}, | ||
{ | ||
"amount": "10", | ||
"denom": "foo" | ||
} | ||
] | ||
}` | ||
|
||
require.Equal(t, expected, string(indentedBz)) | ||
} |
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,12 @@ | ||
package v043 | ||
|
||
const ( | ||
// ModuleName is the name of the module | ||
ModuleName = "bank" | ||
) | ||
|
||
// KVStore keys | ||
var ( | ||
BalancesPrefix = []byte{0x02} | ||
SupplyKey = []byte{0x00} | ||
) |
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
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.
Why modify
b
and not justbalances = append(balances, sdk.NewCoins(b.Coins...))
?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.
if we do this, we may miss
b.Address
.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 don't follow. We don't use
b.Address
. AreoldBalances
being mutated?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.
yes, we need to remove some zero denoms from balance of an address (occures when few denoms set to zero but not all)
ex:
we have to remove
foobar
here.