0g-chain/x/committee/proposal_handler.go

53 lines
1.6 KiB
Go
Raw Normal View History

2020-03-04 19:16:27 +00:00
package committee
2020-03-04 16:55:12 +00:00
2020-03-21 18:06:58 +00:00
import (
sdk "github.com/cosmos/cosmos-sdk/types"
2020-04-27 18:19:05 +00:00
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
2020-03-21 18:06:58 +00:00
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)
func NewProposalHandler(k Keeper) govtypes.Handler {
2020-04-27 18:19:05 +00:00
return func(ctx sdk.Context, content govtypes.Content) error {
2020-03-21 18:06:58 +00:00
switch c := content.(type) {
case CommitteeChangeProposal:
return handleCommitteeChangeProposal(ctx, k, c)
case CommitteeDeleteProposal:
return handleCommitteeDeleteProposal(ctx, k, c)
default:
2020-04-27 18:19:05 +00:00
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized %s proposal content type: %T", ModuleName, c)
2020-03-21 18:06:58 +00:00
}
}
}
2020-04-27 18:19:05 +00:00
func handleCommitteeChangeProposal(ctx sdk.Context, k Keeper, committeeProposal CommitteeChangeProposal) error {
2020-03-21 18:06:58 +00:00
if err := committeeProposal.ValidateBasic(); err != nil {
2020-04-27 18:19:05 +00:00
return sdkerrors.Wrap(ErrInvalidPubProposal, err.Error())
2020-03-21 18:06:58 +00:00
}
// Remove all committee's ongoing proposals
proposals := k.GetProposalsByCommittee(ctx, committeeProposal.NewCommittee.ID)
for _, p := range proposals {
2020-04-27 14:37:25 +00:00
k.DeleteProposalAndVotes(ctx, p.ID)
}
2020-03-21 18:06:58 +00:00
// update/create the committee
k.SetCommittee(ctx, committeeProposal.NewCommittee)
return nil
}
2020-04-27 18:19:05 +00:00
func handleCommitteeDeleteProposal(ctx sdk.Context, k Keeper, committeeProposal CommitteeDeleteProposal) error {
2020-03-21 18:06:58 +00:00
if err := committeeProposal.ValidateBasic(); err != nil {
2020-04-27 18:19:05 +00:00
return sdkerrors.Wrap(ErrInvalidPubProposal, err.Error())
2020-03-21 18:06:58 +00:00
}
// Remove all committee's ongoing proposals
proposals := k.GetProposalsByCommittee(ctx, committeeProposal.CommitteeID)
for _, p := range proposals {
2020-04-27 14:37:25 +00:00
k.DeleteProposalAndVotes(ctx, p.ID)
}
2020-03-21 18:06:58 +00:00
k.DeleteCommittee(ctx, committeeProposal.CommitteeID)
return nil
}