Emit final proposal tally in close proposal event (#919)

* Committee types (#899)

* committee types

* refactor to committee interface

* include tokencommitee stringer method

* add members to BaseCommittee

* address revisions

* update querier

* update querier

* fix compilation errors, tests, etc.

* Update MsgVote with vote type (#900)

* add vote to msg

* update querier/rest

* update example cli vote msg

* remove incorrect comments

* address revisions

* update handler, stub keeper method

* add vote type to vote struct

* Committee module keeper logic for token holder governance (#902)

* fix keeper/test compilation errors

* fix keeper/test compilation errors pt 2

* add setters to committee interface

* fix sims compilation errors

* fix incentive tests compilation errors

* update types, expected keepers

* core keeper logic

* don't allow bond denom

* implement vote tallying

* query proposal polling status

* update module keepers in app.go

* register committee interface

* fix failing incentive test

* commitee types tests

* refactor GetProposalResult by committee types

* update invariants

* implement most proposal keeper tests

* add nulls to custom enums

* remove abstain vote type

* add test for close proposal

* remove outdated TODOs

* update ProcessProposals

* switch on committee type directly

* reintroduce Abstain votes and update vote tallying

* don't allow divide by 0 panics

* delete unused setters on committee interface

* clean up tally methods return values for querier

* update enum validation to catch negative ints

* reintroduce setters for sims compilation

* address revisions

* remove commented out test

* implement ProcessProposals test

* additional revisions

* Committee migrations (#909)

* add committee v14 legacy types

* update migration imports for compile

* addRegisterCodec() to committee v14 legacy types

* migrate committee genesis state from v14 to v15

* set stability committee permissions properly

* fix committee allowed params

* migration test, kava-7 sample data

* add concrete types to committees (#911)

* revisions: migrate + tests

* register msgs on legacy codec

* Prepare Committee module for migrations (#906)

* remove invariants

* edits

* fix abci test

* fix keeper querier tests

* use codec.Codec

* don't allow null vote types

* don't allow null tally option

* minor spelling fixes

* update example cli proposal

* fix cli tally query

* enable vote abstain from cli

* include vote options in cli help text

* call CloseProposal from handler

* custom enum marshaling

* emit final proposal tally

* marshal proposal tally to bytes
This commit is contained in:
Denali Marsh 2021-06-22 16:49:46 +02:00 committed by GitHub
parent cf16029e77
commit baf17b4ec8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 0 deletions

View File

@ -251,13 +251,37 @@ func (k Keeper) enactProposal(ctx sdk.Context, proposal types.Proposal) error {
// CloseProposal deletes proposals and their votes, emitting an event denoting the final status of the proposal // CloseProposal deletes proposals and their votes, emitting an event denoting the final status of the proposal
func (k Keeper) CloseProposal(ctx sdk.Context, proposal types.Proposal, outcome types.ProposalOutcome) { func (k Keeper) CloseProposal(ctx sdk.Context, proposal types.Proposal, outcome types.ProposalOutcome) {
proposalTally := types.ProposalPollingStatus{}
committee, found := k.GetCommittee(ctx, proposal.CommitteeID)
if found {
switch com := committee.(type) {
case types.MemberCommittee:
currVotes := k.TallyMemberCommitteeVotes(ctx, proposal.ID)
possibleVotes := sdk.NewDec(int64(len(com.Members)))
memberPollingStatus := types.NewProposalPollingStatus(proposal.ID, currVotes,
currVotes, possibleVotes, com.VoteThreshold, sdk.Dec{Int: nil})
proposalTally = memberPollingStatus
case types.TokenCommittee:
yesVotes, _, currVotes, possibleVotes := k.TallyTokenCommitteeVotes(ctx, proposal.ID, com.TallyDenom)
tokenPollingStatus := types.NewProposalPollingStatus(proposal.ID, yesVotes,
currVotes, possibleVotes, com.VoteThreshold, com.Quorum)
proposalTally = tokenPollingStatus
}
}
k.DeleteProposalAndVotes(ctx, proposal.ID) k.DeleteProposalAndVotes(ctx, proposal.ID)
bz, err := k.cdc.MarshalJSON(proposalTally)
if err != nil {
fmt.Println("error marshaling proposal tally to bytes:", proposalTally.String())
}
ctx.EventManager().EmitEvent( ctx.EventManager().EmitEvent(
sdk.NewEvent( sdk.NewEvent(
types.EventTypeProposalClose, types.EventTypeProposalClose,
sdk.NewAttribute(types.AttributeKeyCommitteeID, fmt.Sprintf("%d", proposal.CommitteeID)), sdk.NewAttribute(types.AttributeKeyCommitteeID, fmt.Sprintf("%d", proposal.CommitteeID)),
sdk.NewAttribute(types.AttributeKeyProposalID, fmt.Sprintf("%d", proposal.ID)), sdk.NewAttribute(types.AttributeKeyProposalID, fmt.Sprintf("%d", proposal.ID)),
sdk.NewAttribute(types.AttributeKeyProposalTally, fmt.Sprintf("%s", bz)),
sdk.NewAttribute(types.AttributeKeyProposalOutcome, outcome.String()), sdk.NewAttribute(types.AttributeKeyProposalOutcome, outcome.String()),
), ),
) )

View File

@ -818,8 +818,29 @@ func (suite *KeeperTestSuite) TestCloseProposal() {
// Confirm proposal exists // Confirm proposal exists
proposal, found := keeper.GetProposal(ctx, proposalID) proposal, found := keeper.GetProposal(ctx, proposalID)
suite.True(found) suite.True(found)
// Close proposal // Close proposal
keeper.CloseProposal(ctx, proposal, types.Passed) keeper.CloseProposal(ctx, proposal, types.Passed)
events := ctx.EventManager().Events()
event := events[0]
suite.Require().Equal("proposal_close", event.Type)
hasProposalTallyAttr := false
for _, attr := range event.Attributes {
if string(attr.GetKey()) == "proposal_tally" {
hasProposalTallyAttr = true
valueStr := string(attr.GetValue())
suite.Contains(valueStr, "proposal_id")
suite.Contains(valueStr, "yes_votes")
suite.Contains(valueStr, "current_votes")
suite.Contains(valueStr, "possible_votes")
suite.Contains(valueStr, "vote_threshold")
suite.Contains(valueStr, "quorum")
}
}
suite.Require().True(hasProposalTallyAttr)
// Confirm proposal doesn't exist // Confirm proposal doesn't exist
_, found = keeper.GetProposal(ctx, proposalID) _, found = keeper.GetProposal(ctx, proposalID)
suite.False(found) suite.False(found)

View File

@ -14,4 +14,5 @@ const (
AttributeKeyVoter = "voter" AttributeKeyVoter = "voter"
AttributeKeyVote = "vote" AttributeKeyVote = "vote"
AttributeKeyProposalOutcome = "proposal_outcome" AttributeKeyProposalOutcome = "proposal_outcome"
AttributeKeyProposalTally = "proposal_tally"
) )

View File

@ -1,6 +1,8 @@
package types package types
import ( import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
) )
@ -81,3 +83,16 @@ func NewProposalPollingStatus(proposalID uint64, yesVotes, currentVotes, possibl
Quorum: quorum, Quorum: quorum,
} }
} }
// String implements fmt.Stringer
func (p ProposalPollingStatus) String() string {
return fmt.Sprintf(`Proposal ID: %d
Yes votes: %d
Current votes: %d
Possible votes: %d
Vote threshold: %d
Quorum: %d`,
p.ProposalID, p.YesVotes, p.CurrentVotes,
p.PossibleVotes, p.VoteThreshold, p.Quorum,
)
}