add events

This commit is contained in:
rhuairahrighairigh 2020-03-27 20:28:51 +00:00
parent 57f4ca7c9a
commit e228aa6659
6 changed files with 83 additions and 40 deletions

View File

@ -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
})

View File

@ -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 (

View File

@ -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

View File

@ -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()),
),
)

View File

@ -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
}

View File

@ -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"
)