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

67 lines
1.9 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 (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)
func NewProposalHandler(k Keeper) govtypes.Handler {
return func(ctx sdk.Context, content govtypes.Content) sdk.Error {
switch c := content.(type) {
case CommitteeChangeProposal:
return handleCommitteeChangeProposal(ctx, k, c)
case CommitteeDeleteProposal:
return handleCommitteeDeleteProposal(ctx, k, c)
default:
errMsg := fmt.Sprintf("unrecognized %s proposal content type: %T", ModuleName, c)
return sdk.ErrUnknownRequest(errMsg)
}
}
}
func handleCommitteeChangeProposal(ctx sdk.Context, k Keeper, committeeProposal CommitteeChangeProposal) sdk.Error {
if err := committeeProposal.ValidateBasic(); err != nil {
2020-03-29 19:43:25 +00:00
return ErrInvalidCommittee(DefaultCodespace, err.Error())
2020-03-21 18:06:58 +00:00
}
// Remove all committee's ongoing proposals
var proposals []Proposal
k.IterateProposals(ctx, func(p Proposal) bool {
if p.CommitteeID == committeeProposal.NewCommittee.ID {
proposals = append(proposals, p)
}
return false
})
for _, p := range proposals { // split loops to avoid updating the db while iterating
k.DeleteProposalAndVotes(ctx, p.ID)
}
// update/create the committee
k.SetCommittee(ctx, committeeProposal.NewCommittee)
return nil
}
func handleCommitteeDeleteProposal(ctx sdk.Context, k Keeper, committeeProposal CommitteeDeleteProposal) sdk.Error {
if err := committeeProposal.ValidateBasic(); err != nil {
2020-03-29 19:43:25 +00:00
return ErrInvalidPubProposal(DefaultCodespace, err.Error())
2020-03-21 18:06:58 +00:00
}
// Remove all committee's ongoing proposals
var proposals []Proposal
k.IterateProposals(ctx, func(p Proposal) bool {
if p.CommitteeID == committeeProposal.CommitteeID {
proposals = append(proposals, p)
}
return false
})
for _, p := range proposals { // split loops to avoid updating the db while iterating
k.DeleteProposalAndVotes(ctx, p.ID)
}
k.DeleteCommittee(ctx, committeeProposal.CommitteeID)
return nil
}