Skip to content

Commit

Permalink
Merge pull request #113 from tendermint/112-avoid-nil-states
Browse files Browse the repository at this point in the history
fix: init budgets to avoid nil slice on genesis
  • Loading branch information
dongsam authored Mar 12, 2022
2 parents 63734a6 + 8afebb2 commit 460d16b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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 460d16b

Please sign in to comment.