diff --git a/x/bank/client/cli/cli_test.go b/x/bank/client/cli/cli_test.go index 0666b3e75..2041ec2d9 100644 --- a/x/bank/client/cli/cli_test.go +++ b/x/bank/client/cli/cli_test.go @@ -1,7 +1,6 @@ package cli_test import ( - "bytes" "context" "fmt" "testing" @@ -10,6 +9,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/testutil" "github.com/cosmos/cosmos-sdk/testutil/network" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" @@ -43,12 +43,7 @@ func (s *IntegrationTestSuite) TearDownSuite() { } func (s *IntegrationTestSuite) TestGetBalancesCmd() { - buf := new(bytes.Buffer) val := s.network.Validators[0] - clientCtx := val.ClientCtx.WithOutput(buf) - - ctx := context.Background() - ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx) testCases := []struct { name string @@ -95,11 +90,15 @@ func (s *IntegrationTestSuite) TestGetBalancesCmd() { tc := tc s.Run(tc.name, func() { - buf.Reset() - cmd := cli.GetBalancesCmd() - cmd.SetErr(buf) - cmd.SetOut(buf) + _, out := testutil.ApplyMockIO(cmd) + + clientCtx := val.ClientCtx.WithOutput(out) + + ctx := context.Background() + ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx) + + out.Reset() cmd.SetArgs(tc.args) err := cmd.ExecuteContext(ctx) @@ -107,7 +106,7 @@ func (s *IntegrationTestSuite) TestGetBalancesCmd() { s.Require().Error(err) } else { s.Require().NoError(err) - s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(buf.Bytes(), tc.respType)) + s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(out.Bytes(), tc.respType)) s.Require().Equal(tc.expected.String(), tc.respType.String()) } }) @@ -115,12 +114,7 @@ func (s *IntegrationTestSuite) TestGetBalancesCmd() { } func (s *IntegrationTestSuite) TestGetCmdQueryTotalSupply() { - buf := new(bytes.Buffer) val := s.network.Validators[0] - clientCtx := val.ClientCtx.WithOutput(buf) - - ctx := context.Background() - ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx) testCases := []struct { name string @@ -165,11 +159,15 @@ func (s *IntegrationTestSuite) TestGetCmdQueryTotalSupply() { tc := tc s.Run(tc.name, func() { - buf.Reset() - cmd := cli.GetCmdQueryTotalSupply() - cmd.SetErr(buf) - cmd.SetOut(buf) + _, out := testutil.ApplyMockIO(cmd) + + clientCtx := val.ClientCtx.WithOutput(out) + + ctx := context.Background() + ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx) + + out.Reset() cmd.SetArgs(tc.args) err := cmd.ExecuteContext(ctx) @@ -177,7 +175,7 @@ func (s *IntegrationTestSuite) TestGetCmdQueryTotalSupply() { s.Require().Error(err) } else { s.Require().NoError(err) - s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(buf.Bytes(), tc.respType), buf.String()) + s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(out.Bytes(), tc.respType), out.String()) s.Require().Equal(tc.expected.String(), tc.respType.String()) } }) @@ -185,9 +183,7 @@ func (s *IntegrationTestSuite) TestGetCmdQueryTotalSupply() { } func (s *IntegrationTestSuite) TestNewSendTxCmd() { - buf := new(bytes.Buffer) val := s.network.Validators[0] - clientCtx := val.ClientCtx.WithOutput(buf) testCases := []struct { name string @@ -274,19 +270,17 @@ func (s *IntegrationTestSuite) TestNewSendTxCmd() { tc := tc s.Run(tc.name, func() { - buf.Reset() + clientCtx := val.ClientCtx - cmd := cli.NewSendTxCmd() - cmd.SetErr(buf) - cmd.SetOut(buf) - cmd.SetArgs(tc.args) + ctx := context.Background() + ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx) - out, err := banktestutil.MsgSendExec(clientCtx, tc.from, tc.to, tc.amount, tc.args...) + bz, err := banktestutil.MsgSendExec(clientCtx, tc.from, tc.to, tc.amount, tc.args...) if tc.expectErr { s.Require().Error(err) } else { s.Require().NoError(err) - s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(out, tc.respType), string(out)) + s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(bz, tc.respType), string(bz)) txResp := tc.respType.(*sdk.TxResponse) s.Require().Equal(tc.expectedCode, txResp.Code) diff --git a/x/bank/genesis.go b/x/bank/keeper/genesis.go similarity index 52% rename from x/bank/genesis.go rename to x/bank/keeper/genesis.go index 45c30b536..19034bdbe 100644 --- a/x/bank/genesis.go +++ b/x/bank/keeper/genesis.go @@ -1,26 +1,25 @@ -package bank +package keeper import ( "fmt" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/bank/keeper" "github.com/cosmos/cosmos-sdk/x/bank/types" ) // InitGenesis initializes the bank module's state from a given genesis state. -func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, genState types.GenesisState) { - keeper.SetParams(ctx, genState.Params) +func (k BaseKeeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) { + k.SetParams(ctx, genState.Params) var totalSupply sdk.Coins genState.Balances = types.SanitizeGenesisBalances(genState.Balances) for _, balance := range genState.Balances { - if err := keeper.ValidateBalance(ctx, balance.Address); err != nil { + if err := k.ValidateBalance(ctx, balance.Address); err != nil { panic(err) } - if err := keeper.SetBalances(ctx, balance.Address, balance.Coins); err != nil { + if err := k.SetBalances(ctx, balance.Address, balance.Coins); err != nil { panic(fmt.Errorf("error on setting balances %w", err)) } @@ -31,14 +30,14 @@ func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, genState types.GenesisSt genState.Supply = totalSupply } - keeper.SetSupply(ctx, types.NewSupply(genState.Supply)) + k.SetSupply(ctx, types.NewSupply(genState.Supply)) } // ExportGenesis returns the bank module's genesis state. -func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) types.GenesisState { +func (k BaseKeeper) ExportGenesis(ctx sdk.Context) types.GenesisState { balancesSet := make(map[string]sdk.Coins) - keeper.IterateAllBalances(ctx, func(addr sdk.AccAddress, balance sdk.Coin) bool { + k.IterateAllBalances(ctx, func(addr sdk.AccAddress, balance sdk.Coin) bool { balancesSet[addr.String()] = balancesSet[addr.String()].Add(balance) return false }) @@ -57,14 +56,5 @@ func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) types.GenesisState { }) } - return types.NewGenesisState(keeper.GetParams(ctx), balances, keeper.GetSupply(ctx).GetTotal()) -} - -// ValidateGenesis performs basic validation of supply genesis data returning an -// error for any failed validation criteria. -func ValidateGenesis(data types.GenesisState) error { - if err := data.Params.Validate(); err != nil { - return err - } - return types.NewSupply(data.Supply).ValidateBasic() + return types.NewGenesisState(k.GetParams(ctx), balances, k.GetSupply(ctx).GetTotal()) } diff --git a/x/bank/keeper/keeper.go b/x/bank/keeper/keeper.go index f4e257eeb..f4d539aea 100644 --- a/x/bank/keeper/keeper.go +++ b/x/bank/keeper/keeper.go @@ -24,6 +24,9 @@ var _ Keeper = (*BaseKeeper)(nil) type Keeper interface { SendKeeper + InitGenesis(sdk.Context, types.GenesisState) + ExportGenesis(sdk.Context) types.GenesisState + GetSupply(ctx sdk.Context) exported.SupplyI SetSupply(ctx sdk.Context, supply exported.SupplyI) diff --git a/x/bank/module.go b/x/bank/module.go index ed2081ecf..6719d364c 100644 --- a/x/bank/module.go +++ b/x/bank/module.go @@ -6,7 +6,6 @@ import ( "math/rand" "github.com/gogo/protobuf/grpc" - "github.com/gorilla/mux" "github.com/spf13/cobra" abci "github.com/tendermint/tendermint/abci/types" @@ -55,7 +54,7 @@ func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, bz json.RawMessag return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) } - return ValidateGenesis(data) + return types.ValidateGenesis(data) } // RegisterRESTRoutes registers the REST routes for the bank module. @@ -127,14 +126,14 @@ func (am AppModule) NewQuerierHandler() sdk.Querier { func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, data json.RawMessage) []abci.ValidatorUpdate { var genesisState types.GenesisState cdc.MustUnmarshalJSON(data, &genesisState) - InitGenesis(ctx, am.keeper, genesisState) + am.keeper.InitGenesis(ctx, genesisState) return []abci.ValidatorUpdate{} } // ExportGenesis returns the exported genesis state as raw bytes for the bank // module. func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONMarshaler) json.RawMessage { - gs := ExportGenesis(ctx, am.keeper) + gs := am.keeper.ExportGenesis(ctx) return cdc.MustMarshalJSON(gs) } diff --git a/x/bank/types/genesis.go b/x/bank/types/genesis.go index 77b6cb6c5..5f36425f4 100644 --- a/x/bank/types/genesis.go +++ b/x/bank/types/genesis.go @@ -49,6 +49,16 @@ func SanitizeGenesisBalances(balances []Balance) []Balance { return balances } +// ValidateGenesis performs basic validation of supply genesis data returning an +// error for any failed validation criteria. +func ValidateGenesis(data GenesisState) error { + if err := data.Params.Validate(); err != nil { + return err + } + + return NewSupply(data.Supply).ValidateBasic() +} + // NewGenesisState creates a new genesis state. func NewGenesisState(params Params, balances []Balance, supply sdk.Coins) GenesisState { return GenesisState{ diff --git a/x/crisis/client/cli/cli_test.go b/x/crisis/client/cli/cli_test.go index 722ad65e7..4d93a0a3a 100644 --- a/x/crisis/client/cli/cli_test.go +++ b/x/crisis/client/cli/cli_test.go @@ -1,7 +1,6 @@ package cli_test import ( - "bytes" "context" "fmt" "testing" @@ -10,6 +9,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/testutil" "github.com/cosmos/cosmos-sdk/testutil/network" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/crisis/client/cli" @@ -41,12 +41,7 @@ func (s *IntegrationTestSuite) TearDownSuite() { } func (s *IntegrationTestSuite) TestNewMsgVerifyInvariantTxCmd() { - buf := new(bytes.Buffer) val := s.network.Validators[0] - clientCtx := val.ClientCtx.WithOutput(buf) - - ctx := context.Background() - ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx) testCases := []struct { name string @@ -94,11 +89,15 @@ func (s *IntegrationTestSuite) TestNewMsgVerifyInvariantTxCmd() { tc := tc s.Run(tc.name, func() { - buf.Reset() - cmd := cli.NewMsgVerifyInvariantTxCmd() - cmd.SetErr(buf) - cmd.SetOut(buf) + _, out := testutil.ApplyMockIO(cmd) + + clientCtx := val.ClientCtx.WithOutput(out) + + ctx := context.Background() + ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx) + + out.Reset() cmd.SetArgs(tc.args) err := cmd.ExecuteContext(ctx) @@ -106,7 +105,7 @@ func (s *IntegrationTestSuite) TestNewMsgVerifyInvariantTxCmd() { s.Require().Error(err) } else { s.Require().NoError(err) - s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(buf.Bytes(), tc.respType), buf.String()) + s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(out.Bytes(), tc.respType), out.String()) txResp := tc.respType.(*sdk.TxResponse) s.Require().Equal(tc.expectedCode, txResp.Code) diff --git a/x/crisis/genesis.go b/x/crisis/genesis.go deleted file mode 100644 index 4d9ea3568..000000000 --- a/x/crisis/genesis.go +++ /dev/null @@ -1,18 +0,0 @@ -package crisis - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/crisis/keeper" - "github.com/cosmos/cosmos-sdk/x/crisis/types" -) - -// new crisis genesis -func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, data types.GenesisState) { - keeper.SetConstantFee(ctx, data.ConstantFee) -} - -// ExportGenesis returns a GenesisState for a given context and keeper. -func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) types.GenesisState { - constantFee := keeper.GetConstantFee(ctx) - return types.NewGenesisState(constantFee) -} diff --git a/x/crisis/keeper/genesis.go b/x/crisis/keeper/genesis.go new file mode 100644 index 000000000..2426217ed --- /dev/null +++ b/x/crisis/keeper/genesis.go @@ -0,0 +1,17 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/crisis/types" +) + +// new crisis genesis +func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) { + k.SetConstantFee(ctx, data.ConstantFee) +} + +// ExportGenesis returns a GenesisState for a given context and keeper. +func (k Keeper) ExportGenesis(ctx sdk.Context) types.GenesisState { + constantFee := k.GetConstantFee(ctx) + return types.NewGenesisState(constantFee) +} diff --git a/x/crisis/module.go b/x/crisis/module.go index d281ac064..54b6d40ea 100644 --- a/x/crisis/module.go +++ b/x/crisis/module.go @@ -116,8 +116,7 @@ func (am AppModule) RegisterQueryService(grpc.Server) {} func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, data json.RawMessage) []abci.ValidatorUpdate { var genesisState types.GenesisState cdc.MustUnmarshalJSON(data, &genesisState) - InitGenesis(ctx, *am.keeper, genesisState) - + am.keeper.InitGenesis(ctx, genesisState) am.keeper.AssertInvariants(ctx) return []abci.ValidatorUpdate{} } @@ -125,7 +124,7 @@ func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, data j // ExportGenesis returns the exported genesis state as raw bytes for the crisis // module. func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONMarshaler) json.RawMessage { - gs := ExportGenesis(ctx, *am.keeper) + gs := am.keeper.ExportGenesis(ctx) return cdc.MustMarshalJSON(gs) } diff --git a/x/gov/genesis_test.go b/x/gov/genesis_test.go index f369ec31c..0d1a0d5a9 100644 --- a/x/gov/genesis_test.go +++ b/x/gov/genesis_test.go @@ -12,7 +12,6 @@ import ( "github.com/cosmos/cosmos-sdk/simapp" "github.com/cosmos/cosmos-sdk/x/auth" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - "github.com/cosmos/cosmos-sdk/x/bank" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/cosmos/cosmos-sdk/x/gov" "github.com/cosmos/cosmos-sdk/x/gov/types" @@ -52,7 +51,7 @@ func TestImportExportQueues(t *testing.T) { require.True(t, proposal2.Status == types.StatusVotingPeriod) authGenState := auth.ExportGenesis(ctx, app.AccountKeeper) - bankGenState := bank.ExportGenesis(ctx, app.BankKeeper) + bankGenState := app.BankKeeper.ExportGenesis(ctx) // export the state and import it into a new app govGenState := gov.ExportGenesis(ctx, app.GovKeeper)