Skip to content

Commit

Permalink
re: add detailed validation logics for AddRequestProposal, UpdateRequ…
Browse files Browse the repository at this point in the history
…estProposal, DeleteRequestProposal
  • Loading branch information
jaybxyz committed Jul 28, 2021
1 parent 154b903 commit 672bb5a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
3 changes: 2 additions & 1 deletion x/farming/client/proposal_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
govclient "github.com/cosmos/cosmos-sdk/x/gov/client"

"github.com/tendermint/farming/x/farming/client/cli"
"github.com/tendermint/farming/x/farming/client/rest"
)

// ProposalHandler is the public plan creation handler.
var (
ProposalHandler = govclient.NewProposalHandler(cli.GetCmdSubmitPublicPlanProposal, nil)
ProposalHandler = govclient.NewProposalHandler(cli.GetCmdSubmitPublicPlanProposal, rest.ProposalRESTHandler)
)
24 changes: 24 additions & 0 deletions x/farming/client/rest/rest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package rest

import (
"net/http"

"github.com/cosmos/cosmos-sdk/client"
govrest "github.com/cosmos/cosmos-sdk/x/gov/client/rest"
)

// TODO: not implemented yet; although this legacy REST API may not be needed,
// it needs for ProposalHandler; otherwise it throws panic when starting the chain

// ProposalRESTHandler returns a ProposalRESTHandler that exposes the community pool spend REST handler with a given sub-route.
func ProposalRESTHandler(clientCtx client.Context) govrest.ProposalRESTHandler {
return govrest.ProposalRESTHandler{
SubRoute: "farming_plan",
Handler: postProposalHandlerFn(clientCtx),
}
}

func postProposalHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
}
}
12 changes: 0 additions & 12 deletions x/farming/keeper/proposal_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ func HandlePublicPlanProposal(ctx sdk.Context, k Keeper, proposal *types.PublicP
// AddPublicPlanProposal adds a new public plan once the governance proposal is passed.
func (k Keeper) AddPublicPlanProposal(ctx sdk.Context, proposals []*types.AddRequestProposal) error {
for _, p := range proposals {
if err := p.Validate(); err != nil {
return err
}

farmingPoolAddrAcc, err := sdk.AccAddressFromBech32(p.GetFarmingPoolAddress())
if err != nil {
return err
Expand Down Expand Up @@ -90,10 +86,6 @@ func (k Keeper) AddPublicPlanProposal(ctx sdk.Context, proposals []*types.AddReq
// UpdatePublicPlanProposal overwrites the plan with the new plan proposal once the governance proposal is passed.
func (k Keeper) UpdatePublicPlanProposal(ctx sdk.Context, proposals []*types.UpdateRequestProposal) error {
for _, proposal := range proposals {
if err := proposal.Validate(); err != nil {
return err
}

plan, found := k.GetPlan(ctx, proposal.GetPlanId())
if !found {
return sdkerrors.Wrapf(sdkerrors.ErrNotFound, "plan %d is not found", proposal.GetPlanId())
Expand Down Expand Up @@ -169,10 +161,6 @@ func (k Keeper) UpdatePublicPlanProposal(ctx sdk.Context, proposals []*types.Upd
// DeletePublicPlanProposal delets public plan proposal once the governance proposal is passed.
func (k Keeper) DeletePublicPlanProposal(ctx sdk.Context, proposals []*types.DeleteRequestProposal) error {
for _, p := range proposals {
if err := p.Validate(); err != nil {
return err
}

plan, found := k.GetPlan(ctx, p.GetPlanId())
if !found {
return sdkerrors.Wrapf(sdkerrors.ErrNotFound, "plan %d is not found", p.GetPlanId())
Expand Down
15 changes: 15 additions & 0 deletions x/farming/types/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ func (p *PublicPlanProposal) ValidateBasic() error {
if p.AddRequestProposals == nil && p.UpdateRequestProposals == nil && p.DeleteRequestProposals == nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "proposal request must not be empty")
}
for _, ap := range p.AddRequestProposals {
if err := ap.Validate(); err != nil {
return err
}
}
for _, up := range p.UpdateRequestProposals {
if err := up.Validate(); err != nil {
return err
}
}
for _, dp := range p.DeleteRequestProposals {
if err := dp.Validate(); err != nil {
return err
}
}
return gov.ValidateAbstract(p)
}

Expand Down

0 comments on commit 672bb5a

Please sign in to comment.