From 867dd31d80fa86d80e5a0192e15512a8b2584321 Mon Sep 17 00:00:00 2001 From: dongsam Date: Sat, 12 Mar 2022 13:42:49 +0900 Subject: [PATCH] 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