From 717dcdfa8f86133a159617abb8ec9ea41857f7d1 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 15 Oct 2021 13:00:29 +0200 Subject: [PATCH] refactor!: remove gov keeper.MustMarshal (#10373) * chore: remove gov keeper.MustMarshal * added changelog and comments * Update CHANGELOG.md Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com> Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com> --- CHANGELOG.md | 6 ++++-- x/gov/keeper/proposal.go | 39 ++++++++++++++++----------------------- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72c497c989b2..245b287483bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -97,7 +97,9 @@ Ref: https://keepachangelog.com/en/1.0.0/ * Replace `baseapp.SetAnteHandler` with `baseapp.SetTxHandler`. * Move Msg routers from BaseApp to middlewares. * Move Baseapp panic recovery into a middleware. - * Rename simulation helper methods `baseapp.{Check,Deliver}` to `baseapp.Sim{Check,Deliver}`. + * Rename simulation helper methods `baseapp.{Check,Deliver}` to `baseapp.Sim{Check,Deliver}**. +* (x/gov) [\#10373](https://github.com/cosmos/cosmos-sdk/pull/10373) Removed gov `keeper.{MustMarshal, MustUnmarshal}`. + ### Client Breaking Changes @@ -132,7 +134,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes -* (client) [#10226](https://github.com/cosmos/cosmos-sdk/pull/10226) Fix --home flag parsing. +* (client) [#10226](https://github.com/cosmos/cosmos-sdk/pull/10226) Fix --home flag parsing. * [#10180](https://github.com/cosmos/cosmos-sdk/issues/10180) Documentation: make references to Cosmos SDK consistent * (x/genutil) [#10104](https://github.com/cosmos/cosmos-sdk/pull/10104) Ensure the `init` command reads the `--home` flag value correctly. * [\#9651](https://github.com/cosmos/cosmos-sdk/pull/9651) Change inconsistent limit of `0` to `MaxUint64` on InfiniteGasMeter and add GasRemaining func to GasMeter. diff --git a/x/gov/keeper/proposal.go b/x/gov/keeper/proposal.go index 216596932fad..f7c58cf095e6 100644 --- a/x/gov/keeper/proposal.go +++ b/x/gov/keeper/proposal.go @@ -54,7 +54,8 @@ func (keeper Keeper) SubmitProposal(ctx sdk.Context, content types.Content) (typ return proposal, nil } -// GetProposal get proposal from store by ProposalID +// GetProposal get proposal from store by ProposalID. +// Panics if can't unmarshal the proposal. func (keeper Keeper) GetProposal(ctx sdk.Context, proposalID uint64) (types.Proposal, bool) { store := ctx.KVStore(keeper.storeKey) @@ -64,21 +65,27 @@ func (keeper Keeper) GetProposal(ctx sdk.Context, proposalID uint64) (types.Prop } var proposal types.Proposal - keeper.MustUnmarshalProposal(bz, &proposal) + if err := keeper.UnmarshalProposal(bz, &proposal); err != nil { + panic(err) + } return proposal, true } -// SetProposal set a proposal to store +// SetProposal set a proposal to store. +// Panics if can't marshal the proposal. func (keeper Keeper) SetProposal(ctx sdk.Context, proposal types.Proposal) { - store := ctx.KVStore(keeper.storeKey) - - bz := keeper.MustMarshalProposal(proposal) + bz, err := keeper.MarshalProposal(proposal) + if err != nil { + panic(err) + } + store := ctx.KVStore(keeper.storeKey) store.Set(types.ProposalKey(proposal.ProposalId), bz) } -// DeleteProposal deletes a proposal from store +// DeleteProposal deletes a proposal from store. +// Panics if the proposal doesn't exist. func (keeper Keeper) DeleteProposal(ctx sdk.Context, proposalID uint64) { store := ctx.KVStore(keeper.storeKey) proposal, ok := keeper.GetProposal(ctx, proposalID) @@ -90,7 +97,8 @@ func (keeper Keeper) DeleteProposal(ctx sdk.Context, proposalID uint64) { store.Delete(types.ProposalKey(proposalID)) } -// IterateProposals iterates over the all the proposals and performs a callback function +// IterateProposals iterates over the all the proposals and performs a callback function. +// Panics when the iterator encounters a proposal which can't be unmarshaled. func (keeper Keeper) IterateProposals(ctx sdk.Context, cb func(proposal types.Proposal) (stop bool)) { store := ctx.KVStore(keeper.storeKey) @@ -209,18 +217,3 @@ func (keeper Keeper) UnmarshalProposal(bz []byte, proposal *types.Proposal) erro } return nil } - -func (keeper Keeper) MustMarshalProposal(proposal types.Proposal) []byte { - bz, err := keeper.MarshalProposal(proposal) - if err != nil { - panic(err) - } - return bz -} - -func (keeper Keeper) MustUnmarshalProposal(bz []byte, proposal *types.Proposal) { - err := keeper.UnmarshalProposal(bz, proposal) - if err != nil { - panic(err) - } -}