Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: init budgets to avoid nil slice on genesis #113

Merged
merged 2 commits into from
Mar 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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