From 867dd31d80fa86d80e5a0192e15512a8b2584321 Mon Sep 17 00:00:00 2001 From: dongsam Date: Sat, 12 Mar 2022 13:42:49 +0900 Subject: [PATCH 1/2] fix: init budgets to avoid nil slice on genesis --- x/budget/keeper/genesis.go | 10 +++++++++- x/budget/keeper/genesis_test.go | 26 +++++++++++++++++++++++++- x/budget/simulation/genesis.go | 2 +- x/budget/simulation/proposals.go | 2 +- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/x/budget/keeper/genesis.go b/x/budget/keeper/genesis.go index de6cecf..2b83b2a 100644 --- a/x/budget/keeper/genesis.go +++ b/x/budget/keeper/genesis.go @@ -13,6 +13,10 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) { if err := types.ValidateGenesis(genState); err != nil { panic(err) } + // init to prevent nil slice, []types.Budget(nil) + if genState.Params.Budgets == nil || len(genState.Params.Budgets) == 0 { + genState.Params.Budgets = []types.Budget{} + } k.SetParams(ctx, genState.Params) moduleAcc := k.accountKeeper.GetModuleAccount(ctx, types.ModuleName) @@ -28,8 +32,12 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) { // ExportGenesis returns the budget module's genesis state. func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { params := k.GetParams(ctx) - var budgetRecords []types.BudgetRecord + // init to prevent nil slice, []types.Budget(nil) + if params.Budgets == nil || len(params.Budgets) == 0 { + params.Budgets = []types.Budget{} + } + budgetRecords := make([]types.BudgetRecord, 0) k.IterateAllTotalCollectedCoins(ctx, func(record types.BudgetRecord) (stop bool) { budgetRecords = append(budgetRecords, record) return false diff --git a/x/budget/keeper/genesis_test.go b/x/budget/keeper/genesis_test.go index 6d2f120..c583fff 100644 --- a/x/budget/keeper/genesis_test.go +++ b/x/budget/keeper/genesis_test.go @@ -17,7 +17,7 @@ func (suite *KeeperTestSuite) TestInitGenesis() { suite.keeper.InitGenesis(suite.ctx, *emptyGenState) }) suite.Require().Equal(emptyGenState, suite.keeper.ExportGenesis(suite.ctx)) - suite.Require().Nil(emptyGenState.BudgetRecords) + suite.Require().EqualValues(emptyGenState.BudgetRecords, []types.BudgetRecord{}) err := suite.keeper.CollectBudgets(suite.ctx) suite.Require().NoError(err) @@ -35,3 +35,27 @@ func (suite *KeeperTestSuite) TestInitGenesis() { }) suite.Require().Equal(genState, suite.keeper.ExportGenesis(suite.ctx)) } + +func (s *KeeperTestSuite) TestDefaultGenesis() { + genState := *types.DefaultGenesisState() + + s.keeper.InitGenesis(s.ctx, genState) + got := s.keeper.ExportGenesis(s.ctx) + s.Require().Equal(genState, *got) +} + +func (s *KeeperTestSuite) TestImportExportGenesisEmpty() { + k, ctx := s.keeper, s.ctx + k.SetParams(ctx, types.DefaultParams()) + genState := k.ExportGenesis(ctx) + + bz := s.app.AppCodec().MustMarshalJSON(genState) + + var genState2 types.GenesisState + s.app.AppCodec().MustUnmarshalJSON(bz, &genState2) + k.InitGenesis(ctx, genState2) + genState3 := k.ExportGenesis(ctx) + + s.Require().Equal(*genState, genState2) + s.Require().Equal(genState2, *genState3) +} diff --git a/x/budget/simulation/genesis.go b/x/budget/simulation/genesis.go index 991c545..392c702 100644 --- a/x/budget/simulation/genesis.go +++ b/x/budget/simulation/genesis.go @@ -27,7 +27,7 @@ func GenEpochBlocks(r *rand.Rand) uint32 { } // GenBudgets returns randomized budgets. -func GenBudgets(r *rand.Rand, ctx sdk.Context, ak types.AccountKeeper, bk types.BankKeeper, accs []simtypes.Account) []types.Budget { +func GenBudgets(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) []types.Budget { ranBudgets := make([]types.Budget, 0) for i := 0; i < simtypes.RandIntBetween(r, 1, 20); i++ { diff --git a/x/budget/simulation/proposals.go b/x/budget/simulation/proposals.go index 7217a1d..58d17c7 100644 --- a/x/budget/simulation/proposals.go +++ b/x/budget/simulation/proposals.go @@ -32,7 +32,7 @@ func SimulateUpdateBudgetPlans(k keeper.Keeper) simtypes.ContentSimulatorFn { return func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) simtypes.Content { params := k.GetParams(ctx) - params.Budgets = GenBudgets(r, ctx, nil, nil, accs) + params.Budgets = GenBudgets(r, ctx, accs) params.EpochBlocks = GenEpochBlocks(r) // manually set params for simulation From 8afebb2aedb60e9e79c7105979e9b7e411c0cfd6 Mon Sep 17 00:00:00 2001 From: dongsam Date: Sat, 12 Mar 2022 13:47:14 +0900 Subject: [PATCH 2/2] docs: update change log for v1.1.0 --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 82e468c..29c536a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,13 @@ Ref: https://keepachangelog.com/en/1.0.0/ # Changelog +## v1.1.0 - 2022-03-12 + +* [\#110](https://github.com/tendermint/budget/pull/110) fix: improve budget validation logic and tests, spec doc +* [\#111](https://github.com/tendermint/budget/pull/111) build: bump cosmos-sdk to v0.44.5 +* [\#113](https://github.com/tendermint/budget/pull/113) fix: init budgets to avoid nil slice on genesis + + ## v1.0.0 - 2021-11-26 * [\#64](https://github.com/tendermint/budget/pull/64) docs: improve documentation for audit release