2020-03-04 19:16:27 +00:00
|
|
|
package committee
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
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-04 19:16:27 +00:00
|
|
|
|
|
|
|
"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 {
|
2020-04-27 18:19:05 +00:00
|
|
|
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
|
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:
|
2020-04-27 18:19:05 +00:00
|
|
|
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized %s message type: %T", ModuleName, msg)
|
2020-03-04 19:16:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 18:19:05 +00:00
|
|
|
func handleMsgSubmitProposal(ctx sdk.Context, k keeper.Keeper, msg types.MsgSubmitProposal) (*sdk.Result, error) {
|
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 {
|
2020-04-27 18:19:05 +00:00
|
|
|
return nil, err
|
2020-03-04 19:16:27 +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.Proposer.String()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2020-04-27 18:19:05 +00:00
|
|
|
return &sdk.Result{
|
2020-03-12 00:47:25 +00:00
|
|
|
Data: GetKeyFromID(proposalID),
|
|
|
|
Events: ctx.EventManager().Events(),
|
2020-04-27 18:19:05 +00:00
|
|
|
}, nil
|
2020-03-04 19:16:27 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 18:19:05 +00:00
|
|
|
func handleMsgVote(ctx sdk.Context, k keeper.Keeper, msg types.MsgVote) (*sdk.Result, error) {
|
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-04-27 18:19:05 +00:00
|
|
|
return nil, sdkerrors.Wrapf(ErrUnknownProposal, "%d", msg.ProposalID)
|
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 {
|
2020-04-27 18:19:05 +00:00
|
|
|
return nil, err
|
2020-03-12 00:47:25 +00:00
|
|
|
}
|
2020-04-28 00:51:53 +00:00
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeySender, msg.Voter.String()),
|
|
|
|
),
|
|
|
|
)
|
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 {
|
2020-04-27 18:19:05 +00:00
|
|
|
return nil, err
|
2020-03-26 20:17:49 +00:00
|
|
|
}
|
2020-04-28 00:51:53 +00:00
|
|
|
if !passes {
|
|
|
|
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
|
|
|
|
}
|
2020-03-27 20:28:51 +00:00
|
|
|
|
2020-04-28 00:51:53 +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
|
|
|
}
|
2020-03-11 19:27:36 +00:00
|
|
|
|
2020-04-28 00:51:53 +00:00
|
|
|
k.DeleteProposalAndVotes(ctx, msg.ProposalID)
|
|
|
|
|
2020-03-12 00:47:25 +00:00
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
2020-04-28 00:51:53 +00:00
|
|
|
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-12 00:47:25 +00:00
|
|
|
),
|
|
|
|
)
|
2020-03-04 19:16:27 +00:00
|
|
|
|
2020-04-27 18:19:05 +00:00
|
|
|
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
|
2020-03-04 19:16:27 +00:00
|
|
|
}
|