From b621cde958c17095f9fc00f379a161516efe820d Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 26 May 2023 20:19:31 +0200 Subject: [PATCH] feat(group): add group event tally result (backport #16191) (#16306) Co-authored-by: Jeancarlo Barrios Co-authored-by: marbar3778 Co-authored-by: Julien Robert --- CHANGELOG.md | 1 + x/group/events.pb.go | 58 +++++++------- x/group/keeper/genesis.go | 2 +- x/group/keeper/keeper.go | 17 ++++ x/group/keeper/keeper_test.go | 98 +++++++++++++++++++---- x/group/keeper/msg_server.go | 2 +- x/group/proto/cosmos/group/v1/query.proto | 6 +- 7 files changed, 133 insertions(+), 51 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d6ba98d3ebda..be4fb8114972 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (baseapp) [#16290](https://github.com/cosmos/cosmos-sdk/pull/16290) Add circuit breaker setter in baseapp. * [#16060](https://github.com/cosmos/cosmos-sdk/pull/16060) Support saving restoring snapshot locally. +* (x/group) [#16191](https://github.com/cosmos/cosmos-sdk/pull/16191) Add EventProposalPruned event to group module whenever a proposal is pruned. ### Improvements diff --git a/x/group/events.pb.go b/x/group/events.pb.go index 579fdfc2779d..dc869b695c66 100644 --- a/x/group/events.pb.go +++ b/x/group/events.pb.go @@ -544,35 +544,35 @@ func init() { func init() { proto.RegisterFile("cosmos/group/v1/events.proto", fileDescriptor_e8d753981546f032) } var fileDescriptor_e8d753981546f032 = []byte{ - // 433 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x93, 0x4f, 0x8b, 0xda, 0x40, - 0x18, 0xc6, 0x1d, 0x2b, 0x5a, 0xc7, 0x52, 0xcb, 0xf4, 0x0f, 0xd1, 0x4a, 0x14, 0x2f,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 0x0e, 0xce, 0xb7, 0x7a, 0x6a, 0xbd, 0xd5, 0x53, 0x37, - 0x5b, 0x3d, 0xf5, 0x5b, 0x65, 0xe5, 0xf8, 0x9f, 0xe1, 0x50, 0x73, 0x19, 0xbd, 0xb8, 0x49, 0x56, - 0xbe, 0xb4, 0x0f, 0x77, 0x01, 0x00, 0x00, 0xff, 0xff, 0x9e, 0x96, 0x2a, 0x36, 0xd2, 0x03, 0x00, - 0x00, + // 442 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x93, 0x4f, 0xef, 0xd2, 0x30, + 0x18, 0xc7, 0xe9, 0x4f, 0x02, 0x52, 0x8c, 0x98, 0xfa, 0x27, 0x03, 0xc9, 0x20, 0xc4, 0x44, 0x0e, + 0xb2, 0x05, 0x4c, 0xd4, 0x93, 0x44, 0x0c, 0x31, 0x24, 0x1c, 0xc8, 0xf0, 0x4f, 0xe2, 0x05, 0xc7, + 0xda, 0x8c, 0xc5, 0x41, 0x97, 0xb6, 0x9b, 0x70, 0xf4, 0x1d, 0xf8, 0x52, 0x3c, 0xf8, 0x22, 0x3c, + 0x12, 0x4f, 0x1e, 0x0d, 0xbc, 0x11, 0xb3, 0xae, 0x03, 0x82, 0x31, 0x23, 0xf9, 0x9d, 0x68, 0xfb, + 0xfd, 0x7c, 0xbf, 0x3c, 0x4f, 0x9f, 0x15, 0xd6, 0x1d, 0xca, 0x97, 0x94, 0x9b, 0x2e, 0xa3, 0x61, + 0x60, 0x46, 0x5d, 0x93, 0x44, 0x64, 0x25, 0xb8, 0x11, 0x30, 0x2a, 0x28, 0xaa, 0x24, 0xaa, 0x21, + 0x55, 0x23, 0xea, 0xd6, 0xaa, 0xc9, 0xc1, 0x4c, 0xca, 0xa6, 0x52, 0xe5, 0xa6, 0xf6, 0xf0, 0x3c, + 0x49, 0x6c, 0x02, 0xa2, 0xc4, 0x56, 0x07, 0xde, 0x19, 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7, 0x53, 0xb8, 0xba, 0x74, 0x0a, 0xdf, 0x81, 0x1a, 0x43, 0x5a, 0xdd, 0x84, 0x85, 0x2b, 0x82, + 0xb3, 0xfb, 0x7d, 0x0e, 0x0b, 0x5c, 0xd8, 0x22, 0xe4, 0xaa, 0xdf, 0xc6, 0x7f, 0xfb, 0x9d, 0x4a, + 0xcc, 0x52, 0x38, 0xea, 0xc3, 0x5b, 0xc2, 0xf6, 0xfd, 0xcd, 0x4c, 0x5d, 0x57, 0xdc, 0x6f, 0xb9, + 0x57, 0xff, 0xc7, 0xfe, 0x36, 0x86, 0xd4, 0x1d, 0x95, 0xc5, 0x71, 0x33, 0x78, 0xf9, 0x73, 0xa7, + 0x83, 0xed, 0x4e, 0x07, 0x7f, 0x76, 0x3a, 0xf8, 0xb6, 0xd7, 0x73, 0xdb, 0xbd, 0x9e, 0xfb, 0xbd, + 0xd7, 0x73, 0x1f, 0x1f, 0xb9, 0x9e, 0x58, 0x84, 0x73, 0xc3, 0xa1, 0x4b, 0xf5, 0x04, 0xd5, 0x4f, + 0x87, 0xe3, 0xcf, 0xe6, 0x3a, 0x79, 0x81, 0xf3, 0x82, 0x7c, 0x79, 0x4f, 0xff, 0x06, 0x00, 0x00, + 0xff, 0xff, 0xa5, 0x1a, 0x1c, 0xb9, 0xe2, 0x03, 0x00, 0x00, } func (m *EventCreateGroup) Marshal() (dAtA []byte, err error) { diff --git a/x/group/keeper/genesis.go b/x/group/keeper/genesis.go index 8c30ee85e71e..bfb1c68ea05d 100644 --- a/x/group/keeper/genesis.go +++ b/x/group/keeper/genesis.go @@ -45,7 +45,7 @@ func (k Keeper) InitGenesis(ctx context.Context, cdc codec.JSONCodec, data json. } // ExportGenesis returns the group module's exported genesis. -func (k Keeper) ExportGenesis(ctx context.Context, _ codec.JSONCodec) (*group.GenesisState, error) { +func (k Keeper) ExportGenesis(ctx types.Context, _ codec.JSONCodec) *group.GenesisState { genesisState := group.NewGenesisState() var groups []*group.GroupInfo diff --git a/x/group/keeper/keeper.go b/x/group/keeper/keeper.go index cca469e9620c..a18552ec8fe4 100644 --- a/x/group/keeper/keeper.go +++ b/x/group/keeper/keeper.go @@ -382,6 +382,15 @@ func (k Keeper) PruneProposals(ctx sdk.Context) error { if err != nil { return err } + // Emit event for proposal finalized with its result + if err := ctx.EventManager().EmitTypedEvent( + &group.EventProposalPruned{ + ProposalId: proposal.Id, + Status: proposal.Status, + TallyResult: &proposal.FinalTallyResult, + }); err != nil { + return err + } } return nil @@ -416,6 +425,14 @@ func (k Keeper) TallyProposalsAtVPEnd(ctx sdk.Context) error { if err := k.pruneVotes(ctx, proposalID); err != nil { return err } + // Emit event for proposal finalized with its result + if err := ctx.EventManager().EmitTypedEvent( + &group.EventProposalPruned{ + ProposalId: proposal.Id, + Status: proposal.Status, + }); err != nil { + return err + } } else if proposal.Status == group.PROPOSAL_STATUS_SUBMITTED { if err := k.doTallyAndUpdate(ctx, &proposal, electorate, policyInfo); err != nil { return sdkerrors.Wrap(err, "doTallyAndUpdate") diff --git a/x/group/keeper/keeper_test.go b/x/group/keeper/keeper_test.go index b37b2819fa6f..8c6e5d58fec3 100644 --- a/x/group/keeper/keeper_test.go +++ b/x/group/keeper/keeper_test.go @@ -8,6 +8,7 @@ import ( "github.com/golang/mock/gomock" "github.com/stretchr/testify/suite" + abci "github.com/tendermint/tendermint/abci/types" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" tmtime "github.com/tendermint/tendermint/types/time" @@ -33,6 +34,8 @@ import ( authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" ) +var EventProposalPruned = "cosmos.group.v1.EventProposalPruned" + const minExecutionPeriod = 5 * time.Second type TestSuite struct { @@ -1673,6 +1676,8 @@ func (s *TestSuite) TestSubmitProposal() { s.Require().Contains(fromBalances, sdk.NewInt64Coin("test", 9900)) toBalances := s.app.BankKeeper.GetAllBalances(sdkCtx, addr2) s.Require().Contains(toBalances, sdk.NewInt64Coin("test", 100)) + events := sdkCtx.EventManager().ABCIEvents() + s.Require().True(eventTypeFound(events, EventProposalPruned)) }, }, "with try exec, not enough yes votes for proposal to pass": { @@ -1761,6 +1766,7 @@ func (s *TestSuite) TestWithdrawProposal() { proposalId uint64 admin string expErrMsg string + postRun func(sdkCtx sdk.Context) }{ "wrong admin": { preRun: func(sdkCtx sdk.Context) uint64 { @@ -1768,6 +1774,7 @@ func (s *TestSuite) TestWithdrawProposal() { }, admin: addr5.String(), expErrMsg: "unauthorized", + postRun: func(sdkCtx sdk.Context) {}, }, "wrong proposalId": { preRun: func(sdkCtx sdk.Context) uint64 { @@ -1775,6 +1782,7 @@ func (s *TestSuite) TestWithdrawProposal() { }, admin: proposers[0], expErrMsg: "not found", + postRun: func(sdkCtx sdk.Context) {}, }, "happy case with proposer": { preRun: func(sdkCtx sdk.Context) uint64 { @@ -1782,6 +1790,7 @@ func (s *TestSuite) TestWithdrawProposal() { }, proposalId: proposalID, admin: proposers[0], + postRun: func(sdkCtx sdk.Context) {}, }, "already closed proposal": { preRun: func(sdkCtx sdk.Context) uint64 { @@ -1796,6 +1805,7 @@ func (s *TestSuite) TestWithdrawProposal() { proposalId: proposalID, admin: proposers[0], expErrMsg: "cannot withdraw a proposal with the status of PROPOSAL_STATUS_WITHDRAWN", + postRun: func(sdkCtx sdk.Context) {}, }, "happy case with group admin address": { preRun: func(sdkCtx sdk.Context) uint64 { @@ -1803,6 +1813,17 @@ func (s *TestSuite) TestWithdrawProposal() { }, proposalId: proposalID, admin: proposers[0], + postRun: func(sdkCtx sdk.Context) { + resp, err := s.keeper.Proposal(s.ctx, &group.QueryProposalRequest{ProposalId: proposalID}) + s.Require().NoError(err) + vpe := resp.Proposal.VotingPeriodEnd + timeDiff := vpe.Sub(s.sdkCtx.BlockTime()) + ctxVPE := sdkCtx.WithBlockTime(s.sdkCtx.BlockTime().Add(timeDiff).Add(time.Second * 1)) + s.Require().NoError(s.keeper.TallyProposalsAtVPEnd(ctxVPE)) + events := ctxVPE.EventManager().ABCIEvents() + + s.Require().True(eventTypeFound(events, EventProposalPruned)) + }, }, } for msg, spec := range specs { @@ -1825,6 +1846,7 @@ func (s *TestSuite) TestWithdrawProposal() { resp, err := s.keeper.Proposal(s.ctx, &group.QueryProposalRequest{ProposalId: pId}) s.Require().NoError(err) s.Require().Equal(resp.GetProposal().Status, group.PROPOSAL_STATUS_WITHDRAWN) + spec.postRun(s.sdkCtx) }) } } @@ -2287,6 +2309,7 @@ func (s *TestSuite) SetupTest() { expBalance bool expFromBalances sdk.Coin expToBalances sdk.Coin + postRun func(sdkCtx sdk.Context) }{ "proposal executed when accepted": { setupProposal: func(ctx context.Context) uint64 { @@ -2299,6 +2322,10 @@ func (s *TestSuite) SetupTest() { expBalance: true, expFromBalances: sdk.NewInt64Coin("test", 9900), expToBalances: sdk.NewInt64Coin("test", 100), + postRun: func(sdkCtx sdk.Context) { + events := sdkCtx.EventManager().ABCIEvents() + s.Require().True(eventTypeFound(events, EventProposalPruned)) + }, }, "proposal with multiple messages executed when accepted": { setupProposal: func(ctx context.Context) uint64 { @@ -2311,6 +2338,10 @@ func (s *TestSuite) SetupTest() { expBalance: true, expFromBalances: sdk.NewInt64Coin("test", 9800), expToBalances: sdk.NewInt64Coin("test", 200), + postRun: func(sdkCtx sdk.Context) { + events := sdkCtx.EventManager().ABCIEvents() + s.Require().True(eventTypeFound(events, EventProposalPruned)) + }, }, "proposal not executed when rejected": { setupProposal: func(ctx context.Context) uint64 { @@ -2320,6 +2351,7 @@ func (s *TestSuite) SetupTest() { srcBlockTime: s.blockTime.Add(minExecutionPeriod), // After min execution period end expProposalStatus: group.PROPOSAL_STATUS_REJECTED, expExecutorResult: group.PROPOSAL_EXECUTOR_RESULT_NOT_RUN, + postRun: func(sdkCtx sdk.Context) {}, }, "open proposal must not fail": { setupProposal: func(ctx context.Context) uint64 { @@ -2327,12 +2359,14 @@ func (s *TestSuite) SetupTest() { }, expProposalStatus: group.PROPOSAL_STATUS_SUBMITTED, expExecutorResult: group.PROPOSAL_EXECUTOR_RESULT_NOT_RUN, + postRun: func(sdkCtx sdk.Context) {}, }, "existing proposal required": { setupProposal: func(ctx context.Context) uint64 { return 9999 }, - expErr: true, + expErr: true, + postRun: func(sdkCtx sdk.Context) {}, }, "Decision policy also applied on exactly voting period end": { setupProposal: func(ctx context.Context) uint64 { @@ -2342,6 +2376,7 @@ func (s *TestSuite) SetupTest() { srcBlockTime: s.blockTime.Add(time.Second), // Voting period is 1s expProposalStatus: group.PROPOSAL_STATUS_REJECTED, expExecutorResult: group.PROPOSAL_EXECUTOR_RESULT_NOT_RUN, + postRun: func(sdkCtx sdk.Context) {}, }, "Decision policy also applied after voting period end": { setupProposal: func(ctx context.Context) uint64 { @@ -2351,6 +2386,7 @@ func (s *TestSuite) SetupTest() { srcBlockTime: s.blockTime.Add(time.Second).Add(time.Millisecond), // Voting period is 1s expProposalStatus: group.PROPOSAL_STATUS_REJECTED, expExecutorResult: group.PROPOSAL_EXECUTOR_RESULT_NOT_RUN, + postRun: func(sdkCtx sdk.Context) {}, }, "exec proposal before MinExecutionPeriod should fail": { setupProposal: func(ctx context.Context) uint64 { @@ -2360,6 +2396,7 @@ func (s *TestSuite) SetupTest() { srcBlockTime: s.blockTime.Add(4 * time.Second), // min execution date is 5s later after s.blockTime expProposalStatus: group.PROPOSAL_STATUS_ACCEPTED, expExecutorResult: group.PROPOSAL_EXECUTOR_RESULT_FAILURE, // Because MinExecutionPeriod has not passed + postRun: func(sdkCtx sdk.Context) {}, }, "exec proposal at exactly MinExecutionPeriod should pass": { setupProposal: func(ctx context.Context) uint64 { @@ -2369,6 +2406,10 @@ func (s *TestSuite) SetupTest() { srcBlockTime: s.blockTime.Add(5 * time.Second), // min execution date is 5s later after s.blockTime expProposalStatus: group.PROPOSAL_STATUS_ACCEPTED, expExecutorResult: group.PROPOSAL_EXECUTOR_RESULT_SUCCESS, + postRun: func(sdkCtx sdk.Context) { + events := sdkCtx.EventManager().ABCIEvents() + s.Require().True(eventTypeFound(events, EventProposalPruned)) + }, }, "prevent double execution when successful": { setupProposal: func(ctx context.Context) uint64 { @@ -2390,6 +2431,10 @@ func (s *TestSuite) SetupTest() { expBalance: true, expFromBalances: sdk.NewInt64Coin("test", 9900), expToBalances: sdk.NewInt64Coin("test", 100), + postRun: func(sdkCtx sdk.Context) { + events := sdkCtx.EventManager().ABCIEvents() + s.Require().True(eventTypeFound(events, EventProposalPruned)) + }, }, "rollback all msg updates on failure": { setupProposal: func(ctx context.Context) uint64 { @@ -2399,6 +2444,7 @@ func (s *TestSuite) SetupTest() { srcBlockTime: s.blockTime.Add(minExecutionPeriod), // After min execution period end expProposalStatus: group.PROPOSAL_STATUS_ACCEPTED, expExecutorResult: group.PROPOSAL_EXECUTOR_RESULT_FAILURE, + postRun: func(sdkCtx sdk.Context) {}, }, "executable when failed before": { setupProposal: func(ctx context.Context) uint64 { @@ -2415,6 +2461,10 @@ func (s *TestSuite) SetupTest() { srcBlockTime: s.blockTime.Add(minExecutionPeriod), // After min execution period end expProposalStatus: group.PROPOSAL_STATUS_ACCEPTED, expExecutorResult: group.PROPOSAL_EXECUTOR_RESULT_SUCCESS, + postRun: func(sdkCtx sdk.Context) { + events := sdkCtx.EventManager().ABCIEvents() + s.Require().True(eventTypeFound(events, EventProposalPruned)) + }, }, } err = policyReq.SetDecisionPolicy(policy) @@ -2427,18 +2477,16 @@ func (s *TestSuite) SetupTest() { policyRes, err := s.groupKeeper.CreateGroupPolicy(s.ctx, policyReq) s.Require().NoError(err) - addrbz, err := addressCodec.StringToBytes(policyRes.Address) - s.Require().NoError(err) - s.policy = policy - s.groupPolicyAddr = addrbz - s.groupPolicyStrAddr, err = addressCodec.BytesToString(s.groupPolicyAddr) - s.Require().NoError(err) - s.bankKeeper.EXPECT().MintCoins(s.sdkCtx, minttypes.ModuleName, sdk.Coins{sdk.NewInt64Coin("test", 100000)}).Return(nil).AnyTimes() - err = s.bankKeeper.MintCoins(s.sdkCtx, minttypes.ModuleName, sdk.Coins{sdk.NewInt64Coin("test", 100000)}) - s.Require().NoError(err) - s.bankKeeper.EXPECT().SendCoinsFromModuleToAccount(s.sdkCtx, minttypes.ModuleName, s.groupPolicyAddr, sdk.Coins{sdk.NewInt64Coin("test", 10000)}).Return(nil).AnyTimes() - err = s.bankKeeper.SendCoinsFromModuleToAccount(s.sdkCtx, minttypes.ModuleName, s.groupPolicyAddr, sdk.Coins{sdk.NewInt64Coin("test", 10000)}) - s.Require().NoError(err) + if spec.expBalance { + fromBalances := s.app.BankKeeper.GetAllBalances(sdkCtx, s.groupPolicyAddr) + s.Require().Contains(fromBalances, spec.expFromBalances) + toBalances := s.app.BankKeeper.GetAllBalances(sdkCtx, addr2) + s.Require().Contains(toBalances, spec.expToBalances) + } + + spec.postRun(sdkCtx) + }) + } } func (s *TestSuite) TestExecPrunedProposalsAndVotes() { @@ -2571,10 +2619,15 @@ func (s *TestSuite) TestExecPrunedProposalsAndVotes() { } s.Require().NoError(err) - groupPolicyAccBumpAccountNumber, err := authtypes.NewBaseAccountWithPubKey(ac) - s.Require().NoError(err) - err = groupPolicyAccBumpAccountNumber.SetAccountNumber(nextAccVal) - s.Require().NoError(err) + if spec.expExecutorResult == group.PROPOSAL_EXECUTOR_RESULT_SUCCESS { + // Make sure proposal is deleted from state + _, err := s.keeper.Proposal(ctx, &group.QueryProposalRequest{ProposalId: proposalID}) + s.Require().Contains(err.Error(), spec.expErrMsg) + res, err := s.keeper.VotesByProposal(ctx, &group.QueryVotesByProposalRequest{ProposalId: proposalID}) + s.Require().NoError(err) + s.Require().Empty(res.GetVotes()) + events := sdkCtx.EventManager().ABCIEvents() + s.Require().True(eventTypeFound(events, EventProposalPruned)) s.accountKeeper.EXPECT().GetAccount(gomock.Any(), sdk.AccAddress(ac.Address())).Return(nil).AnyTimes() s.accountKeeper.EXPECT().NewAccount(gomock.Any(), groupPolicyAcc).Return(groupPolicyAccBumpAccountNumber).AnyTimes() @@ -3134,3 +3187,14 @@ func (s *TestSuite) TestTallyProposalsAtVPEnd_GroupMemberLeaving() { s.Require().NoError(s.app.GroupKeeper.TallyProposalsAtVPEnd(ctx)) s.NotPanics(func() { module.EndBlocker(ctx, s.app.GroupKeeper) }) } + +func eventTypeFound(events []abci.Event, eventType string) bool { + eventTypeFound := false + for _, e := range events { + if e.Type == eventType { + eventTypeFound = true + break + } + } + return eventTypeFound +} diff --git a/x/group/keeper/msg_server.go b/x/group/keeper/msg_server.go index a306b823ab6c..4cb8039d7e85 100644 --- a/x/group/keeper/msg_server.go +++ b/x/group/keeper/msg_server.go @@ -888,7 +888,7 @@ func (k Keeper) Exec(ctx context.Context, msg *group.MsgExec) (*group.MsgExecRes } // Emit event for proposal finalized with its result - if err := k.EventService.EventManager(ctx).Emit( + if err := ctx.EventManager().EmitTypedEvent( &group.EventProposalPruned{ ProposalId: proposal.Id, Status: proposal.Status, diff --git a/x/group/proto/cosmos/group/v1/query.proto b/x/group/proto/cosmos/group/v1/query.proto index 7fd79c472af8..322f4b6f0d8e 100644 --- a/x/group/proto/cosmos/group/v1/query.proto +++ b/x/group/proto/cosmos/group/v1/query.proto @@ -85,7 +85,7 @@ service Query { }; // Groups queries all groups in state. - // + // // Since: cosmos-sdk 0.47.1 rpc Groups(QueryGroupsRequest) returns (QueryGroupsResponse) { option (google.api.http).get = "/cosmos/group/v1/groups"; @@ -317,7 +317,7 @@ message QueryGroupsResponse { } // QueryGroupsRequest is the Query/Groups request type. -// +// // Since: cosmos-sdk 0.47.1 message QueryGroupsRequest { @@ -326,7 +326,7 @@ message QueryGroupsRequest { } // QueryGroupsResponse is the Query/Groups response type. -// +// // Since: cosmos-sdk 0.47.1 message QueryGroupsResponse { // `groups` is all the groups present in state.