-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add migrations for balances with zero coins (#9664)
<!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Closes: #9653 <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) (cherry picked from commit d56c8cd) # Conflicts: # x/bank/legacy/v043/json.go # x/bank/legacy/v043/json_test.go # x/bank/legacy/v043/keys.go # x/bank/legacy/v043/store_test.go
- Loading branch information
Showing
6 changed files
with
238 additions
and
10 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
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 { | ||
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