From f0c298b716d3576fed208588259042c49d5f1d76 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Mon, 20 Mar 2023 18:07:34 +0800 Subject: [PATCH 1/4] fix missing supply for add-genesis-account --- module/cmd/gravity/cmd/genaccounts.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/module/cmd/gravity/cmd/genaccounts.go b/module/cmd/gravity/cmd/genaccounts.go index 6b23dc9c0..5bc11f003 100644 --- a/module/cmd/gravity/cmd/genaccounts.go +++ b/module/cmd/gravity/cmd/genaccounts.go @@ -151,7 +151,7 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa bankGenState := banktypes.GetGenesisStateFromAppState(depCdc, appState) bankGenState.Balances = append(bankGenState.Balances, balances) bankGenState.Balances = banktypes.SanitizeGenesisBalances(bankGenState.Balances) - + bankGenState.Supply = bankGenState.Supply.Add(balances.Coins...) bankGenStateBz, err := cdc.MarshalJSON(bankGenState) if err != nil { return fmt.Errorf("failed to marshal bank genesis state: %w", err) From e13619be45e187cc830aae94fc6b3276590535f3 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Mon, 20 Mar 2023 23:27:53 +0800 Subject: [PATCH 2/4] add supply test --- module/cmd/gravity/cmd/genaccounts_test.go | 28 ++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/module/cmd/gravity/cmd/genaccounts_test.go b/module/cmd/gravity/cmd/genaccounts_test.go index c11eab509..c8d01c12b 100644 --- a/module/cmd/gravity/cmd/genaccounts_test.go +++ b/module/cmd/gravity/cmd/genaccounts_test.go @@ -1,8 +1,10 @@ -package cmd_test +package cmd import ( "context" + "encoding/json" "fmt" + "os" "testing" "github.com/spf13/viper" @@ -11,13 +13,16 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/simapp" - simcmd "github.com/cosmos/cosmos-sdk/simapp/simd/cmd" "github.com/cosmos/cosmos-sdk/testutil/testdata" "github.com/cosmos/cosmos-sdk/types/module" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/cosmos/cosmos-sdk/x/genutil" genutiltest "github.com/cosmos/cosmos-sdk/x/genutil/client/testutil" + tmjson "github.com/tendermint/tendermint/libs/json" + tmtypes "github.com/tendermint/tendermint/types" ) var testMbm = module.NewBasicManager(genutil.AppModuleBasic{}) @@ -45,7 +50,7 @@ func TestAddGenesisAccountCmd(t *testing.T) { { name: "multiple denoms", addr: addr1.String(), - denom: "1000atom, 2000stake", + denom: "1000atom,2000stake", expectErr: false, }, } @@ -69,7 +74,7 @@ func TestAddGenesisAccountCmd(t *testing.T) { ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx) ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx) - cmd := simcmd.AddGenesisAccountCmd(home) + cmd := AddGenesisAccountCmd(home) cmd.SetArgs([]string{ tc.addr, tc.denom, @@ -80,6 +85,21 @@ func TestAddGenesisAccountCmd(t *testing.T) { require.Error(t, cmd.ExecuteContext(ctx)) } else { require.NoError(t, cmd.ExecuteContext(ctx)) + config := serverCtx.Config + genFile := config.GenesisFile() + bytes, err := os.ReadFile(genFile) + require.NoError(t, err) + var genDoc tmtypes.GenesisDoc + tmjson.Unmarshal(bytes, &genDoc) + var appState map[string]json.RawMessage + err = json.Unmarshal(genDoc.AppState, &appState) + require.NoError(t, err) + bankGenStateBz := appState[banktypes.ModuleName] + var bankGenState banktypes.GenesisState + depCdc := clientCtx.Codec + cdc := depCdc.(codec.Codec) + cdc.MustUnmarshalJSON(bankGenStateBz, &bankGenState) + require.Equal(t, bankGenState.Supply.String(), tc.denom) } }) } From 603824515074d796a6a43be7b42ea36e928d6ee3 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Wed, 22 Mar 2023 08:34:03 +0800 Subject: [PATCH 3/4] Update module/cmd/gravity/cmd/genaccounts_test.go Co-authored-by: Collin --- module/cmd/gravity/cmd/genaccounts_test.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/module/cmd/gravity/cmd/genaccounts_test.go b/module/cmd/gravity/cmd/genaccounts_test.go index c8d01c12b..176864163 100644 --- a/module/cmd/gravity/cmd/genaccounts_test.go +++ b/module/cmd/gravity/cmd/genaccounts_test.go @@ -85,20 +85,22 @@ func TestAddGenesisAccountCmd(t *testing.T) { require.Error(t, cmd.ExecuteContext(ctx)) } else { require.NoError(t, cmd.ExecuteContext(ctx)) - config := serverCtx.Config - genFile := config.GenesisFile() + + genFile := serverCtx.Config.GenesisFile() bytes, err := os.ReadFile(genFile) require.NoError(t, err) + var genDoc tmtypes.GenesisDoc tmjson.Unmarshal(bytes, &genDoc) + var appState map[string]json.RawMessage err = json.Unmarshal(genDoc.AppState, &appState) require.NoError(t, err) - bankGenStateBz := appState[banktypes.ModuleName] + var bankGenState banktypes.GenesisState - depCdc := clientCtx.Codec - cdc := depCdc.(codec.Codec) - cdc.MustUnmarshalJSON(bankGenStateBz, &bankGenState) + bankGenStateBz := appState[banktypes.ModuleName] + clientCtx.Codec.MustUnmarshalJSON(bankGenStateBz, &bankGenState) + require.Equal(t, bankGenState.Supply.String(), tc.denom) } }) From 50a797c7ba68596d19649f322e537ad97a749cd2 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Wed, 22 Mar 2023 08:35:22 +0800 Subject: [PATCH 4/4] Apply suggestions from code review --- module/cmd/gravity/cmd/genaccounts_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/module/cmd/gravity/cmd/genaccounts_test.go b/module/cmd/gravity/cmd/genaccounts_test.go index 176864163..b2de1eefc 100644 --- a/module/cmd/gravity/cmd/genaccounts_test.go +++ b/module/cmd/gravity/cmd/genaccounts_test.go @@ -13,7 +13,6 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/simapp" "github.com/cosmos/cosmos-sdk/testutil/testdata"