Skip to content

Commit

Permalink
fix: init budgets to avoid nil slice on genesis
Browse files Browse the repository at this point in the history
  • Loading branch information
dongsam committed Mar 12, 2022
1 parent 63734a6 commit 867dd31
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
10 changes: 9 additions & 1 deletion x/budget/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
26 changes: 25 additions & 1 deletion x/budget/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion x/budget/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down
2 changes: 1 addition & 1 deletion x/budget/simulation/proposals.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 867dd31

Please sign in to comment.