Skip to content

Commit

Permalink
feat(x/gov): Emit VoterAddr (backport cosmos#17354) (cosmos#17357)
Browse files Browse the repository at this point in the history
Co-authored-by: Devon Bear <itsdevbear@berachain.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
  • Loading branch information
3 people authored and JeancarloBarrios committed Sep 28, 2024
1 parent a17fe86 commit f613d15
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

* (x/gov) [#17354](https://github.com/cosmos/cosmos-sdk/issues/17354) Emit `VoterAddr` in `proposal_vote` event.
* (x/genutil) [#17296](https://github.com/cosmos/cosmos-sdk/pull/17296) Add `MigrateHandler` to allow reuse migrate genesis related function.
* In v0.46, v0.47 this function is additive to the `genesis migrate` command. However in v0.50+, adding custom migrations to the `genesis migrate` command is directly possible.

Expand Down
44 changes: 42 additions & 2 deletions x/gov/keeper/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,48 @@ func (k Keeper) AddVote(ctx context.Context, proposalID uint64, voterAddr sdk.Ac
return errors.Wrapf(types.ErrInactiveProposal, "%d", proposalID)
}

if err := k.assertMetadataLength(metadata); err != nil {
return err
vote := v1.NewVote(proposalID, voterAddr, options, metadata)
keeper.SetVote(ctx, vote)

// called after a vote on a proposal is cast
keeper.AfterProposalVote(ctx, proposalID, voterAddr)

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeProposalVote,
sdk.NewAttribute(types.AttributeKeyVoter, voterAddr.String()),
sdk.NewAttribute(types.AttributeKeyOption, options.String()),
sdk.NewAttribute(types.AttributeKeyProposalID, fmt.Sprintf("%d", proposalID)),
),
)

return nil
}

// GetAllVotes returns all the votes from the store
func (keeper Keeper) GetAllVotes(ctx sdk.Context) (votes v1.Votes) {
keeper.IterateAllVotes(ctx, func(vote v1.Vote) bool {
votes = append(votes, &vote)
return false
})
return
}

// GetVotes returns all the votes from a proposal
func (keeper Keeper) GetVotes(ctx sdk.Context, proposalID uint64) (votes v1.Votes) {
keeper.IterateVotes(ctx, proposalID, func(vote v1.Vote) bool {
votes = append(votes, &vote)
return false
})
return
}

// GetVote gets the vote from an address on a specific proposal
func (keeper Keeper) GetVote(ctx sdk.Context, proposalID uint64, voterAddr sdk.AccAddress) (vote v1.Vote, found bool) {
store := ctx.KVStore(keeper.storeKey)
bz := store.Get(types.VoteKey(proposalID, voterAddr))
if bz == nil {
return vote, false
}

store := ctx.KVStore(keeper.storeKey)
Expand Down
36 changes: 14 additions & 22 deletions x/gov/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,18 @@ const (
EventTypeActiveProposal = "active_proposal"
EventTypeCancelProposal = "cancel_proposal"

AttributeKeyProposalResult = "proposal_result"
AttributeKeyVoter = "voter"
AttributeKeyOption = "option"
AttributeKeyProposalID = "proposal_id"
AttributeKeyDepositor = "depositor"
AttributeKeyProposalMessages = "proposal_messages" // Msg type_urls in the proposal
AttributeKeyVotingPeriodStart = "voting_period_start"
AttributeKeyProposalLog = "proposal_log" // log of proposal execution
AttributeKeyProposalDepositError = "proposal_deposit_error" // error on proposal deposit refund/burn
AttributeKeyProposalProposer = "proposal_proposer" // account address of the proposer

AttributeValueProposalDropped = "proposal_dropped" // didn't meet min deposit
AttributeValueProposalPassed = "proposal_passed" // met vote quorum
AttributeValueProposalRejected = "proposal_rejected" // didn't meet vote quorum
AttributeValueExpeditedProposalRejected = "expedited_proposal_rejected" // didn't meet expedited vote quorum
AttributeValueOptimisticProposalRejected = "optimistic_proposal_rejected" // didn't meet optimistic vote quorum
AttributeValueProposalFailed = "proposal_failed" // error on proposal handler
AttributeValueProposalCanceled = "proposal_canceled" // error on proposal handler

AttributeKeyProposalType = "proposal_type"
AttributeSignalTitle = "signal_title"
AttributeSignalDescription = "signal_description"
AttributeKeyProposalResult = "proposal_result"
AttributeKeyVoter = "voter"
AttributeKeyOption = "option"
AttributeKeyProposalID = "proposal_id"
AttributeKeyProposalMessages = "proposal_messages" // Msg type_urls in the proposal
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
AttributeKeyProposalType = "proposal_type"
AttributeSignalTitle = "signal_title"
AttributeSignalDescription = "signal_description"
)

0 comments on commit f613d15

Please sign in to comment.