From 179c819a9d542cb56c236454dc75d7120591438c Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Mon, 14 Jun 2021 02:56:25 -0700 Subject: [PATCH] fix: testnet cli command update genesis supply (#9497) ## Description closes: #9372 ### This PR makes the `testnet` command update the bank genesis supply. When using the `testnet` cli command, it creates nodes and balances, but does **not** update the supply. When using this in conjunction with `add-genesis-account` which **does** update the supply, it creates an invalid genesis file. This PR updates the testnet command to properly set the supply. --- ### 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... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [x] 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 - [x] 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) --- simapp/simd/cmd/testnet.go | 5 +++- simapp/simd/cmd/testnet_test.go | 50 +++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 simapp/simd/cmd/testnet_test.go diff --git a/simapp/simd/cmd/testnet.go b/simapp/simd/cmd/testnet.go index abf6cb3cd..071d9364d 100644 --- a/simapp/simd/cmd/testnet.go +++ b/simapp/simd/cmd/testnet.go @@ -289,7 +289,10 @@ func initGenFiles( var bankGenState banktypes.GenesisState clientCtx.Codec.MustUnmarshalJSON(appGenState[banktypes.ModuleName], &bankGenState) - bankGenState.Balances = genBalances + bankGenState.Balances = banktypes.SanitizeGenesisBalances(genBalances) + for _, bal := range bankGenState.Balances { + bankGenState.Supply = bankGenState.Supply.Add(bal.Coins...) + } appGenState[banktypes.ModuleName] = clientCtx.Codec.MustMarshalJSON(&bankGenState) appGenStateJSON, err := json.MarshalIndent(appGenState, "", " ") diff --git a/simapp/simd/cmd/testnet_test.go b/simapp/simd/cmd/testnet_test.go new file mode 100644 index 000000000..da58fd454 --- /dev/null +++ b/simapp/simd/cmd/testnet_test.go @@ -0,0 +1,50 @@ +package cmd + +import ( + "context" + "fmt" + "testing" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/server" + "github.com/cosmos/cosmos-sdk/simapp" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + genutiltest "github.com/cosmos/cosmos-sdk/x/genutil/client/testutil" + genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" + "github.com/spf13/viper" + "github.com/stretchr/testify/require" + "github.com/tendermint/tendermint/libs/log" +) + +func Test_TestnetCmd(t *testing.T) { + home := t.TempDir() + encodingConfig := simapp.MakeTestEncodingConfig() + logger := log.NewNopLogger() + cfg, err := genutiltest.CreateDefaultTendermintConfig(home) + require.NoError(t, err) + + err = genutiltest.ExecInitCmd(simapp.ModuleBasics, home, encodingConfig.Marshaler) + require.NoError(t, err) + + serverCtx := server.NewContext(viper.New(), cfg, logger) + clientCtx := client.Context{}. + WithCodec(encodingConfig.Marshaler). + WithHomeDir(home). + WithTxConfig(encodingConfig.TxConfig) + + ctx := context.Background() + ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx) + ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx) + cmd := testnetCmd(simapp.ModuleBasics, banktypes.GenesisBalancesIterator{}) + cmd.SetArgs([]string{fmt.Sprintf("--%s=test", flags.FlagKeyringBackend), fmt.Sprintf("--output-dir=%s", home)}) + err = cmd.ExecuteContext(ctx) + require.NoError(t, err) + + genFile := cfg.GenesisFile() + appState, _, err := genutiltypes.GenesisStateFromGenFile(genFile) + require.NoError(t, err) + + bankGenState := banktypes.GetGenesisStateFromAppState(encodingConfig.Marshaler, appState) + require.NotEmpty(t, bankGenState.Supply.String()) +}