From e228aa6659d1021f6d11e27c7c36aa9d97d86bd8 Mon Sep 17 00:00:00 2001 From: rhuairahrighairigh Date: Fri, 27 Mar 2020 20:28:51 +0000 Subject: [PATCH] add events --- x/committee/abci.go | 12 +++++ x/committee/alias.go | 50 ++++++++++++--------- x/committee/client/common/query_proposer.go | 2 +- x/committee/handler.go | 26 +++++++++-- x/committee/keeper/proposal.go | 10 ++++- x/committee/types/events.go | 23 +++++----- 6 files changed, 83 insertions(+), 40 deletions(-) diff --git a/x/committee/abci.go b/x/committee/abci.go index 7b56d99e..3dc45499 100644 --- a/x/committee/abci.go +++ b/x/committee/abci.go @@ -1,6 +1,8 @@ package committee import ( + "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" abci "github.com/tendermint/tendermint/abci/types" @@ -13,7 +15,17 @@ func BeginBlocker(ctx sdk.Context, _ abci.RequestBeginBlock, k Keeper) { // Close all expired proposals k.IterateProposals(ctx, func(proposal types.Proposal) bool { if proposal.HasExpiredBy(ctx.BlockTime()) { + k.DeleteProposalAndVotes(ctx, proposal.ID) + + 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, types.AttributeValueProposalTimeout), + ), + ) } return false }) diff --git a/x/committee/alias.go b/x/committee/alias.go index 1fe2eab5..01d8188f 100644 --- a/x/committee/alias.go +++ b/x/committee/alias.go @@ -9,27 +9,35 @@ import ( ) const ( - AttributeKeyProposalID = types.AttributeKeyProposalID - DefaultCodespace = types.DefaultCodespace - DefaultNextProposalID = types.DefaultNextProposalID - DefaultParamspace = types.DefaultParamspace - EventTypeSubmitProposal = types.EventTypeSubmitProposal - MaxDescriptionLength = types.MaxDescriptionLength - ModuleName = types.ModuleName - ProposalTypeCommitteeChange = types.ProposalTypeCommitteeChange - ProposalTypeCommitteeDelete = types.ProposalTypeCommitteeDelete - QuerierRoute = types.QuerierRoute - QueryCommittee = types.QueryCommittee - QueryCommittees = types.QueryCommittees - QueryProposal = types.QueryProposal - QueryProposals = types.QueryProposals - QueryTally = types.QueryTally - QueryVote = types.QueryVote - QueryVotes = types.QueryVotes - RouterKey = types.RouterKey - StoreKey = types.StoreKey - TypeMsgSubmitProposal = types.TypeMsgSubmitProposal - TypeMsgVote = types.TypeMsgVote + AttributeKeyCommitteeID = types.AttributeKeyCommitteeID + AttributeKeyProposalCloseStatus = types.AttributeKeyProposalCloseStatus + AttributeKeyProposalID = types.AttributeKeyProposalID + AttributeValueCategory = types.AttributeValueCategory + AttributeValueProposalFailed = types.AttributeValueProposalFailed + AttributeValueProposalPassed = types.AttributeValueProposalPassed + AttributeValueProposalTimeout = types.AttributeValueProposalTimeout + DefaultCodespace = types.DefaultCodespace + DefaultNextProposalID = types.DefaultNextProposalID + DefaultParamspace = types.DefaultParamspace + EventTypeProposalClose = types.EventTypeProposalClose + EventTypeProposalSubmit = types.EventTypeProposalSubmit + EventTypeProposalVote = types.EventTypeProposalVote + MaxCommitteeDescriptionLength = types.MaxCommitteeDescriptionLength + ModuleName = types.ModuleName + ProposalTypeCommitteeChange = types.ProposalTypeCommitteeChange + ProposalTypeCommitteeDelete = types.ProposalTypeCommitteeDelete + QuerierRoute = types.QuerierRoute + QueryCommittee = types.QueryCommittee + QueryCommittees = types.QueryCommittees + QueryProposal = types.QueryProposal + QueryProposals = types.QueryProposals + QueryTally = types.QueryTally + QueryVote = types.QueryVote + QueryVotes = types.QueryVotes + RouterKey = types.RouterKey + StoreKey = types.StoreKey + TypeMsgSubmitProposal = types.TypeMsgSubmitProposal + TypeMsgVote = types.TypeMsgVote ) var ( diff --git a/x/committee/client/common/query_proposer.go b/x/committee/client/common/query_proposer.go index 0508f53d..5a44be42 100644 --- a/x/committee/client/common/query_proposer.go +++ b/x/committee/client/common/query_proposer.go @@ -34,7 +34,7 @@ func (p Proposer) String() string { func QueryProposer(cliCtx context.CLIContext, proposalID uint64) (Proposer, error) { events := []string{ fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, types.TypeMsgSubmitProposal), - fmt.Sprintf("%s.%s='%s'", types.EventTypeSubmitProposal, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", proposalID))), + fmt.Sprintf("%s.%s='%s'", types.EventTypeProposalSubmit, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", proposalID))), } // NOTE: SearchTxs is used to facilitate the txs query which does not currently diff --git a/x/committee/handler.go b/x/committee/handler.go index e52989f7..7233dbca 100644 --- a/x/committee/handler.go +++ b/x/committee/handler.go @@ -35,7 +35,7 @@ func handleMsgSubmitProposal(ctx sdk.Context, k keeper.Keeper, msg types.MsgSubm ctx.EventManager().EmitEvent( sdk.NewEvent( sdk.EventTypeMessage, - // TODO sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), sdk.NewAttribute(sdk.AttributeKeySender, msg.Proposer.String()), ), ) @@ -47,6 +47,12 @@ func handleMsgSubmitProposal(ctx sdk.Context, k keeper.Keeper, msg types.MsgSubm } func handleMsgVote(ctx sdk.Context, k keeper.Keeper, msg types.MsgVote) sdk.Result { + // get the proposal just to add fields to the event + proposal, found := k.GetProposal(ctx, msg.ProposalID) + if !found { + return sdk.ErrInternal("proposal not found").Result() + } + err := k.AddVote(ctx, msg.ProposalID, msg.Voter) if err != nil { return err.Result() @@ -58,15 +64,27 @@ func handleMsgVote(ctx sdk.Context, k keeper.Keeper, msg types.MsgVote) sdk.Resu return err.Result() } if passes { - _ = k.EnactProposal(ctx, msg.ProposalID) - // log err + err = k.EnactProposal(ctx, msg.ProposalID) + outcome := types.AttributeValueProposalPassed + if err != nil { + outcome = types.AttributeValueProposalFailed + } k.DeleteProposalAndVotes(ctx, msg.ProposalID) + + 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), + ), + ) } ctx.EventManager().EmitEvent( sdk.NewEvent( sdk.EventTypeMessage, - // TODO sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), sdk.NewAttribute(sdk.AttributeKeySender, msg.Voter.String()), ), ) diff --git a/x/committee/keeper/proposal.go b/x/committee/keeper/proposal.go index ad0e3ced..d0218777 100644 --- a/x/committee/keeper/proposal.go +++ b/x/committee/keeper/proposal.go @@ -38,7 +38,8 @@ func (k Keeper) SubmitProposal(ctx sdk.Context, proposer sdk.AccAddress, committ ctx.EventManager().EmitEvent( sdk.NewEvent( - types.EventTypeSubmitProposal, + types.EventTypeProposalSubmit, + sdk.NewAttribute(types.AttributeKeyCommitteeID, fmt.Sprintf("%d", com.ID)), sdk.NewAttribute(types.AttributeKeyProposalID, fmt.Sprintf("%d", proposalID)), ), ) @@ -66,6 +67,13 @@ func (k Keeper) AddVote(ctx sdk.Context, proposalID uint64, voter sdk.AccAddress // Store vote, overwriting any prior vote k.SetVote(ctx, types.Vote{ProposalID: proposalID, Voter: voter}) + ctx.EventManager().EmitEvent( + sdk.NewEvent( + types.EventTypeProposalVote, + sdk.NewAttribute(types.AttributeKeyCommitteeID, fmt.Sprintf("%d", com.ID)), + sdk.NewAttribute(types.AttributeKeyProposalID, fmt.Sprintf("%d", pr.ID)), + ), + ) return nil } diff --git a/x/committee/types/events.go b/x/committee/types/events.go index 5037f606..8ccbf667 100644 --- a/x/committee/types/events.go +++ b/x/committee/types/events.go @@ -2,18 +2,15 @@ package types // Module event types const ( - EventTypeSubmitProposal = "submit_proposal" - // EventTypeProposalVote = "proposal_vote" - // EventTypeInactiveProposal = "inactive_proposal" - // EventTypeActiveProposal = "active_proposal" + EventTypeProposalSubmit = "proposal_submit" + EventTypeProposalClose = "proposal_close" + EventTypeProposalVote = "proposal_vote" - // AttributeKeyProposalResult = "proposal_result" - // AttributeKeyOption = "option" - AttributeKeyProposalID = "proposal_id" - // AttributeKeyVotingPeriodStart = "voting_period_start" - // AttributeValueCategory = "governance" - // AttributeValueProposalDropped = "proposal_dropped" // didn't meet min deposit - // AttributeValueProposalPassed = "proposal_passed" // met vote quorum - // AttributeValueProposalRejected = "proposal_rejected" // didn't meet vote quorum - // AttributeValueProposalFailed = "proposal_failed" // error on proposal handler + AttributeValueCategory = "committee" + AttributeKeyCommitteeID = "committee_id" + AttributeKeyProposalID = "proposal_id" + AttributeKeyProposalCloseStatus = "status" + AttributeValueProposalPassed = "proposal_passed" + AttributeValueProposalTimeout = "proposal_timeout" + AttributeValueProposalFailed = "proposal_failed" )