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

94 lines
2.6 KiB
Go
Raw Normal View History

2020-03-04 19:16:27 +00:00
package committee
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/kava-labs/kava/x/committee/keeper"
"github.com/kava-labs/kava/x/committee/types"
)
// NewHandler creates an sdk.Handler for committee messages
func NewHandler(k keeper.Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
2020-03-12 00:47:25 +00:00
ctx = ctx.WithEventManager(sdk.NewEventManager())
2020-03-04 19:16:27 +00:00
switch msg := msg.(type) {
case types.MsgSubmitProposal:
2020-03-12 00:47:25 +00:00
return handleMsgSubmitProposal(ctx, k, msg)
2020-03-04 19:16:27 +00:00
case types.MsgVote:
2020-03-12 00:47:25 +00:00
return handleMsgVote(ctx, k, msg)
2020-03-04 19:16:27 +00:00
default:
errMsg := fmt.Sprintf("unrecognized %s msg type: %T", types.ModuleName, msg)
return sdk.ErrUnknownRequest(errMsg).Result()
}
}
}
func handleMsgSubmitProposal(ctx sdk.Context, k keeper.Keeper, msg types.MsgSubmitProposal) sdk.Result {
2020-03-12 00:47:25 +00:00
proposalID, err := k.SubmitProposal(ctx, msg.Proposer, msg.CommitteeID, msg.PubProposal)
2020-03-04 19:16:27 +00:00
if err != nil {
return err.Result()
}
2020-03-12 00:47:25 +00:00
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
2020-03-27 20:28:51 +00:00
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
2020-03-12 00:47:25 +00:00
sdk.NewAttribute(sdk.AttributeKeySender, msg.Proposer.String()),
),
)
return sdk.Result{
Data: GetKeyFromID(proposalID),
Events: ctx.EventManager().Events(),
}
2020-03-04 19:16:27 +00:00
}
func handleMsgVote(ctx sdk.Context, k keeper.Keeper, msg types.MsgVote) sdk.Result {
2020-03-27 20:28:51 +00:00
// get the proposal just to add fields to the event
proposal, found := k.GetProposal(ctx, msg.ProposalID)
if !found {
2020-03-29 19:43:25 +00:00
return ErrUnknownProposal(DefaultCodespace, msg.ProposalID).Result()
2020-03-27 20:28:51 +00:00
}
2020-03-12 00:47:25 +00:00
err := k.AddVote(ctx, msg.ProposalID, msg.Voter)
if err != nil {
return err.Result()
}
2020-03-11 19:27:36 +00:00
2020-03-26 20:17:49 +00:00
// Enact a proposal if it has enough votes
passes, err := k.GetProposalResult(ctx, msg.ProposalID)
if err != nil {
return err.Result()
}
if passes {
2020-03-27 20:28:51 +00:00
err = k.EnactProposal(ctx, msg.ProposalID)
outcome := types.AttributeValueProposalPassed
if err != nil {
outcome = types.AttributeValueProposalFailed
}
2020-03-26 20:17:49 +00:00
k.DeleteProposalAndVotes(ctx, msg.ProposalID)
2020-03-27 20:28:51 +00:00
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeProposalClose,
sdk.NewAttribute(types.AttributeKeyCommitteeID, fmt.Sprintf("%d", proposal.CommitteeID)),
sdk.NewAttribute(types.AttributeKeyProposalID, fmt.Sprintf("%d", proposal.ID)),
sdk.NewAttribute(types.AttributeKeyProposalCloseStatus, outcome),
),
)
2020-03-26 20:17:49 +00:00
}
2020-03-11 19:27:36 +00:00
2020-03-12 00:47:25 +00:00
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
2020-03-27 20:28:51 +00:00
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
2020-03-12 00:47:25 +00:00
sdk.NewAttribute(sdk.AttributeKeySender, msg.Voter.String()),
),
)
2020-03-04 19:16:27 +00:00
2020-03-12 00:47:25 +00:00
return sdk.Result{Events: ctx.EventManager().Events()}
2020-03-04 19:16:27 +00:00
}