diff --git a/proto/cosmos/group/v1/events.proto b/proto/cosmos/group/v1/events.proto index c2cfe8728f72..2b98ec9abc32 100644 --- a/proto/cosmos/group/v1/events.proto +++ b/proto/cosmos/group/v1/events.proto @@ -79,3 +79,16 @@ message EventLeaveGroup { // address is the account address of the group member. string address = 2 [(cosmos_proto.scalar) = "cosmos.AddressString"]; } + +// EventProposalPruned is an event emitted when a proposal is pruned. +message EventProposalPruned { + + // proposal_id is the unique ID of the proposal. + uint64 proposal_id = 1; + + // status is the proposal status (UNSPECIFIED, SUBMITTED, ACCEPTED, REJECTED, ABORTED, WITHDRAWN). + ProposalStatus status = 2; + + // tally_result is the proposal tally result (when applicable). + TallyResult tally_result = 3; +} diff --git a/proto/cosmos/group/v1/types.proto b/proto/cosmos/group/v1/types.proto index 837271542c63..d975d6b62da6 100644 --- a/proto/cosmos/group/v1/types.proto +++ b/proto/cosmos/group/v1/types.proto @@ -14,7 +14,6 @@ import "google/protobuf/any.proto"; // Member represents a group member with an account address, // non-zero weight, metadata and added_at timestamp. message Member { - // address is the member's account address. string address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"]; @@ -32,7 +31,6 @@ message Member { // Contrary to `Member`, it doesn't have any `added_at` field // since this field cannot be set as part of requests. message MemberRequest { - // address is the member's account address. string address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"]; @@ -120,7 +118,6 @@ enum VoteOption { // GroupInfo represents the high-level on-chain information for a group. message GroupInfo { - // id is the unique ID of the group. uint64 id = 1; @@ -145,7 +142,6 @@ message GroupInfo { // GroupMember represents the relationship between a group and a member. message GroupMember { - // group_id is the unique ID of the group. uint64 group_id = 1; @@ -299,7 +295,6 @@ message TallyResult { // Vote represents a vote for a proposal. message Vote { - // proposal is the unique ID of the proposal. uint64 proposal_id = 1; diff --git a/x/group/events.pb.go b/x/group/events.pb.go index 3cbcb2d491d6..c724b905da7c 100644 --- a/x/group/events.pb.go +++ b/x/group/events.pb.go @@ -464,6 +464,70 @@ func (m *EventLeaveGroup) GetAddress() string { return "" } +// EventProposalPruned is an event emitted when a proposal is pruned. +type EventProposalPruned struct { + // proposal_id is the unique ID of the proposal. + ProposalId uint64 `protobuf:"varint,1,opt,name=proposal_id,json=proposalId,proto3" json:"proposal_id,omitempty"` + // status is the proposal status (UNSPECIFIED, SUBMITTED, ACCEPTED, REJECTED, ABORTED, WITHDRAWN). + Status ProposalStatus `protobuf:"varint,2,opt,name=status,proto3,enum=cosmos.group.v1.ProposalStatus" json:"status,omitempty"` + // tally_result is the proposal tally result (when applicable). + TallyResult *TallyResult `protobuf:"bytes,3,opt,name=tally_result,json=tallyResult,proto3" json:"tally_result,omitempty"` +} + +func (m *EventProposalPruned) Reset() { *m = EventProposalPruned{} } +func (m *EventProposalPruned) String() string { return proto.CompactTextString(m) } +func (*EventProposalPruned) ProtoMessage() {} +func (*EventProposalPruned) Descriptor() ([]byte, []int) { + return fileDescriptor_e8d753981546f032, []int{9} +} +func (m *EventProposalPruned) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *EventProposalPruned) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_EventProposalPruned.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *EventProposalPruned) XXX_Merge(src proto.Message) { + xxx_messageInfo_EventProposalPruned.Merge(m, src) +} +func (m *EventProposalPruned) XXX_Size() int { + return m.Size() +} +func (m *EventProposalPruned) XXX_DiscardUnknown() { + xxx_messageInfo_EventProposalPruned.DiscardUnknown(m) +} + +var xxx_messageInfo_EventProposalPruned proto.InternalMessageInfo + +func (m *EventProposalPruned) GetProposalId() uint64 { + if m != nil { + return m.ProposalId + } + return 0 +} + +func (m *EventProposalPruned) GetStatus() ProposalStatus { + if m != nil { + return m.Status + } + return PROPOSAL_STATUS_UNSPECIFIED +} + +func (m *EventProposalPruned) GetTallyResult() *TallyResult { + if m != nil { + return m.TallyResult + } + return nil +} + func init() { proto.RegisterType((*EventCreateGroup)(nil), "cosmos.group.v1.EventCreateGroup") proto.RegisterType((*EventUpdateGroup)(nil), "cosmos.group.v1.EventUpdateGroup") @@ -474,36 +538,41 @@ func init() { proto.RegisterType((*EventVote)(nil), "cosmos.group.v1.EventVote") proto.RegisterType((*EventExec)(nil), "cosmos.group.v1.EventExec") proto.RegisterType((*EventLeaveGroup)(nil), "cosmos.group.v1.EventLeaveGroup") + proto.RegisterType((*EventProposalPruned)(nil), "cosmos.group.v1.EventProposalPruned") } func init() { proto.RegisterFile("cosmos/group/v1/events.proto", fileDescriptor_e8d753981546f032) } var fileDescriptor_e8d753981546f032 = []byte{ - // 382 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x49, 0xce, 0x2f, 0xce, - 0xcd, 0x2f, 0xd6, 0x4f, 0x2f, 0xca, 0x2f, 0x2d, 0xd0, 0x2f, 0x33, 0xd4, 0x4f, 0x2d, 0x4b, 0xcd, - 0x2b, 0x29, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x87, 0xc8, 0xea, 0x81, 0x65, 0xf5, - 0xca, 0x0c, 0xa5, 0x24, 0x21, 0x02, 0xf1, 0x60, 0x69, 0x7d, 0xa8, 0x2c, 0x98, 0x23, 0x25, 0x8d, - 0x6e, 0x52, 0x49, 0x65, 0x41, 0x2a, 0x54, 0x52, 0x49, 0x97, 0x4b, 0xc0, 0x15, 0x64, 0xb0, 0x73, - 0x51, 0x6a, 0x62, 0x49, 0xaa, 0x3b, 0x48, 0x89, 0x90, 0x24, 0x17, 0x07, 0x58, 0x6d, 0x7c, 0x66, - 0x8a, 0x04, 0xa3, 0x02, 0xa3, 0x06, 0x4b, 0x10, 0x3b, 0x98, 0xef, 0x99, 0x02, 0x57, 0x1e, 0x5a, - 0x90, 0x42, 0x8c, 0x72, 0x1f, 0x2e, 0x31, 0x74, 0xd3, 0x03, 0xf2, 0x73, 0x32, 0x93, 0x2b, 0x85, - 0x8c, 0xb8, 0xd8, 0x13, 0x53, 0x52, 0x8a, 0x52, 0x8b, 0x8b, 0xc1, 0x7a, 0x38, 0x9d, 0x24, 0x2e, - 0x6d, 0xd1, 0x15, 0x81, 0xba, 0xdb, 0x11, 0x22, 0x13, 0x5c, 0x52, 0x94, 0x99, 0x97, 0x1e, 0x04, - 0x53, 0x08, 0x37, 0x0d, 0xc9, 0x72, 0x0a, 0x4c, 0x33, 0xe3, 0x12, 0x06, 0x9b, 0x16, 0x5c, 0x9a, - 0x94, 0x9b, 0x59, 0x12, 0x50, 0x94, 0x5f, 0x90, 0x5f, 0x9c, 0x98, 0x23, 0x24, 0xcf, 0xc5, 0x5d, - 0x00, 0x65, 0x23, 0x3c, 0xc4, 0x05, 0x13, 0xf2, 0x4c, 0x51, 0xb2, 0xe0, 0x12, 0x05, 0xeb, 0x0b, - 0xcf, 0x2c, 0xc9, 0x48, 0x29, 0x4a, 0x2c, 0x27, 0x5e, 0xa7, 0x0e, 0x17, 0x27, 0x58, 0x67, 0x58, - 0x7e, 0x49, 0x2a, 0x61, 0xd5, 0x8d, 0x8c, 0x50, 0xe5, 0xae, 0x15, 0xa9, 0xc9, 0x04, 0x95, 0x0b, - 0xd9, 0x73, 0xb1, 0x15, 0xa5, 0x16, 0x97, 0xe6, 0x94, 0x48, 0x30, 0x29, 0x30, 0x6a, 0xf0, 0x19, - 0xa9, 0xeb, 0xa1, 0x25, 0x11, 0x3d, 0x98, 0x43, 0x41, 0xe6, 0x95, 0x96, 0xe4, 0x17, 0x05, 0x81, - 0x95, 0x07, 0x41, 0xb5, 0x09, 0x09, 0x71, 0xb1, 0xe4, 0xe4, 0xa7, 0x17, 0x4b, 0x30, 0x83, 0x02, - 0x30, 0x08, 0xcc, 0x56, 0x4a, 0xe0, 0xe2, 0x07, 0x3b, 0xc1, 0x27, 0x35, 0xb1, 0x8c, 0x60, 0x6c, - 0x23, 0xc7, 0x02, 0x13, 0x91, 0xb1, 0xe0, 0x64, 0x77, 0xe2, 0x91, 0x1c, 0xe3, 0x85, 0x47, 0x72, - 0x8c, 0x0f, 0x1e, 0xc9, 0x31, 0x4e, 0x78, 0x2c, 0xc7, 0x70, 0xe1, 0xb1, 0x1c, 0xc3, 0x8d, 0xc7, - 0x72, 0x0c, 0x51, 0x2a, 0xe9, 0x99, 0x25, 0x19, 0xa5, 0x49, 0x7a, 0xc9, 0xf9, 0xb9, 0xd0, 0xf4, - 0x0c, 0xa5, 0x74, 0x8b, 0x53, 0xb2, 0xf5, 0x2b, 0x20, 0xc9, 0x39, 0x89, 0x0d, 0x9c, 0x8c, 0x8d, - 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0x5b, 0xc0, 0x98, 0xf8, 0x2f, 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, 0xc6, 0xc1, 0xaf, 0x19, 0xb1, 0x05, 0x79, + 0x13, 0x23, 0xa8, 0x0a, 0x6f, 0x4a, 0x76, 0xe6, 0x61, 0x0d, 0x34, 0x41, 0x3b, 0x6f, 0x15, 0xe5, + 0x7e, 0x84, 0x0f, 0xf8, 0xbb, 0x00, 0x5f, 0x82, 0x8f, 0xe1, 0x83, 0xf3, 0xf4, 0x09, 0xf5, 0x3d, + 0x67, 0x83, 0x7a, 0xb0, 0x68, 0x63, 0xcc, 0x08, 0xe7, 0xd2, 0x53, 0x1a, 0x68, 0xbf, 0x7e, 0x74, + 0xee, 0xa9, 0xba, 0x5f, 0x25, 0xca, 0x54, 0x30, 0x6f, 0xe5, 0x5a, 0x29, 0x78, 0x48, 0x3b, 0xf9, + 0xf3, 0x6b, 0xa4, 0x3d, 0x83, 0x77, 0x65, 0xda, 0x34, 0x9c, 0x2f, 0x3d, 0x31, 0x61, 0x34, 0xa0, + 0xdc, 0xf6, 0x51, 0x03, 0x96, 0x03, 0xb5, 0x3e, 0x36, 0x04, 0xd3, 0xa3, 0x11, 0x6e, 0xbd, 0x80, + 0xf7, 0xa5, 0xef, 0x83, 0x27, 0x16, 0x98, 0xd9, 0x5f, 0x2e, 0x77, 0x3e, 0x81, 0x25, 0xe9, 0x7c, + 0x4f, 0x05, 0xc9, 0xa6, 0xbf, 0x02, 0x85, 0x0f, 0xd7, 0xc4, 0xc9, 0xc4, 0x51, 0x1f, 0x16, 0x18, + 0xe1, 0xa1, 0x2f, 0xb4, 0xab, 0x26, 0x68, 0xdf, 0xee, 0x3d, 0x36, 0xce, 0x3e, 0x11, 0x23, 0x2d, + 0x34, 0xce, 0x0b, 0x05, 0x65, 0x96, 0xc4, 0x2d, 0x65, 0x43, 0x08, 0xe6, 0x7d, 0xea, 0x72, 0xed, + 0x46, 0x7c, 0x81, 0x96, 0x5c, 0xb7, 0x3e, 0xc1, 0x8a, 0x2c, 0x61, 0x4c, 0xec, 0x28, 0x73, 0xda, + 0xa7, 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) { @@ -781,6 +850,51 @@ func (m *EventLeaveGroup) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } +func (m *EventProposalPruned) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *EventProposalPruned) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *EventProposalPruned) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.TallyResult != nil { + { + size, err := m.TallyResult.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintEvents(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x1a + } + if m.Status != 0 { + i = encodeVarintEvents(dAtA, i, uint64(m.Status)) + i-- + dAtA[i] = 0x10 + } + if m.ProposalId != 0 { + i = encodeVarintEvents(dAtA, i, uint64(m.ProposalId)) + i-- + dAtA[i] = 0x8 + } + return len(dAtA) - i, nil +} + func encodeVarintEvents(dAtA []byte, offset int, v uint64) int { offset -= sovEvents(v) base := offset @@ -913,6 +1027,25 @@ func (m *EventLeaveGroup) Size() (n int) { return n } +func (m *EventProposalPruned) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.ProposalId != 0 { + n += 1 + sovEvents(uint64(m.ProposalId)) + } + if m.Status != 0 { + n += 1 + sovEvents(uint64(m.Status)) + } + if m.TallyResult != nil { + l = m.TallyResult.Size() + n += 1 + l + sovEvents(uint64(l)) + } + return n +} + func sovEvents(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 } @@ -1649,6 +1782,130 @@ func (m *EventLeaveGroup) Unmarshal(dAtA []byte) error { } return nil } +func (m *EventProposalPruned) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowEvents + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: EventProposalPruned: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: EventProposalPruned: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field ProposalId", wireType) + } + m.ProposalId = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowEvents + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.ProposalId |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 2: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Status", wireType) + } + m.Status = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowEvents + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Status |= ProposalStatus(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field TallyResult", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowEvents + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthEvents + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthEvents + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.TallyResult == nil { + m.TallyResult = &TallyResult{} + } + if err := m.TallyResult.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipEvents(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthEvents + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func skipEvents(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 diff --git a/x/group/keeper/genesis.go b/x/group/keeper/genesis.go index 2e0766ed716e..8043b52acce0 100644 --- a/x/group/keeper/genesis.go +++ b/x/group/keeper/genesis.go @@ -42,7 +42,8 @@ func (k Keeper) InitGenesis(ctx types.Context, cdc codec.JSONCodec, data json.Ra return []abci.ValidatorUpdate{} } -func (k Keeper) ExportGenesis(ctx types.Context, cdc codec.JSONCodec) *group.GenesisState { +// ExportGenesis returns the group module's exported genesis. +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 210ce6014c58..02bd70d1206e 100644 --- a/x/group/keeper/keeper.go +++ b/x/group/keeper/keeper.go @@ -367,6 +367,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 @@ -401,6 +410,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 56a748018965..e9c9aa9539cc 100644 --- a/x/group/keeper/keeper_test.go +++ b/x/group/keeper/keeper_test.go @@ -9,6 +9,7 @@ import ( "time" "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" @@ -23,6 +24,8 @@ import ( "github.com/cosmos/cosmos-sdk/x/group/module" ) +var EventProposalPruned = "cosmos.group.v1.EventProposalPruned" + const minExecutionPeriod = 5 * time.Second type TestSuite struct { @@ -1663,6 +1666,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": { @@ -1751,6 +1756,7 @@ func (s *TestSuite) TestWithdrawProposal() { proposalId uint64 admin string expErrMsg string + postRun func(sdkCtx sdk.Context) }{ "wrong admin": { preRun: func(sdkCtx sdk.Context) uint64 { @@ -1758,6 +1764,7 @@ func (s *TestSuite) TestWithdrawProposal() { }, admin: addr5.String(), expErrMsg: "unauthorized", + postRun: func(sdkCtx sdk.Context) {}, }, "wrong proposalId": { preRun: func(sdkCtx sdk.Context) uint64 { @@ -1765,6 +1772,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 { @@ -1772,6 +1780,7 @@ func (s *TestSuite) TestWithdrawProposal() { }, proposalId: proposalID, admin: proposers[0], + postRun: func(sdkCtx sdk.Context) {}, }, "already closed proposal": { preRun: func(sdkCtx sdk.Context) uint64 { @@ -1786,6 +1795,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 { @@ -1793,6 +1803,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 { @@ -1815,6 +1836,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) }) } } @@ -2316,6 +2338,7 @@ func (s *TestSuite) TestExecProposal() { expBalance bool expFromBalances sdk.Coin expToBalances sdk.Coin + postRun func(sdkCtx sdk.Context) }{ "proposal executed when accepted": { setupProposal: func(ctx context.Context) uint64 { @@ -2328,6 +2351,10 @@ func (s *TestSuite) TestExecProposal() { 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 { @@ -2340,6 +2367,10 @@ func (s *TestSuite) TestExecProposal() { 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 { @@ -2349,6 +2380,7 @@ func (s *TestSuite) TestExecProposal() { 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 { @@ -2356,12 +2388,14 @@ func (s *TestSuite) TestExecProposal() { }, 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 { @@ -2371,6 +2405,7 @@ func (s *TestSuite) TestExecProposal() { 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 { @@ -2380,6 +2415,7 @@ func (s *TestSuite) TestExecProposal() { 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 { @@ -2389,6 +2425,7 @@ func (s *TestSuite) TestExecProposal() { 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 { @@ -2398,6 +2435,10 @@ func (s *TestSuite) TestExecProposal() { 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 { @@ -2419,6 +2460,10 @@ func (s *TestSuite) TestExecProposal() { 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 { @@ -2428,6 +2473,7 @@ func (s *TestSuite) TestExecProposal() { 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 { @@ -2444,6 +2490,10 @@ func (s *TestSuite) TestExecProposal() { 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)) + }, }, } for msg, spec := range specs { @@ -2487,6 +2537,8 @@ func (s *TestSuite) TestExecProposal() { toBalances := s.app.BankKeeper.GetAllBalances(sdkCtx, addr2) s.Require().Contains(toBalances, spec.expToBalances) } + + spec.postRun(sdkCtx) }) } } @@ -2629,6 +2681,8 @@ func (s *TestSuite) TestExecPrunedProposalsAndVotes() { 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)) } else { // Check that proposal and votes exists @@ -3191,3 +3245,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 3947f0adad50..d4ffcdbbcffc 100644 --- a/x/group/keeper/msg_server.go +++ b/x/group/keeper/msg_server.go @@ -702,6 +702,7 @@ func (k Keeper) doTallyAndUpdate(ctx sdk.Context, p *group.Proposal, electorate } else { p.Status = group.PROPOSAL_STATUS_REJECTED } + } return nil @@ -773,6 +774,16 @@ func (k Keeper) Exec(goCtx context.Context, req *group.MsgExec) (*group.MsgExecR if err := k.pruneProposal(ctx, proposal.Id); err != nil { return nil, 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 nil, err + } } else { store := ctx.KVStore(k.key) if err := k.proposalTable.Update(store, id, &proposal); err != nil { diff --git a/x/group/spec/04_events.md b/x/group/spec/04_events.md index ca4e2fdf20bb..1f064009d7bc 100644 --- a/x/group/spec/04_events.md +++ b/x/group/spec/04_events.md @@ -70,3 +70,12 @@ The group module emits the following events: | message | action | /cosmos.group.v1.Msg/LeaveGroup | | cosmos.group.v1.EventLeaveGroup | proposal_id | {proposalId} | | cosmos.group.v1.EventLeaveGroup | address | {address} | + +### EventProposalPruned + +| Type | Attribute Key | Attribute Value | +|-------------------------------------|---------------|---------------------------------| +| message | action | /cosmos.group.v1.Msg/LeaveGroup | +| cosmos.group.v1.EventProposalPruned | proposal_id | {proposalId} | +| cosmos.group.v1.EventProposalPruned | status | {ProposalStatus} | +| cosmos.group.v1.EventProposalPruned | tally_result | {TallyResult} | \ No newline at end of file