Skip to content

Commit

Permalink
fix from comment & fix bug prune with no tally result
Browse files Browse the repository at this point in the history
  • Loading branch information
colmazia committed Dec 4, 2023
1 parent c3e1bf1 commit 99fa143
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 30 deletions.
13 changes: 13 additions & 0 deletions hooks/emitter/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,19 @@ func (h *Hook) AfterDeliverTx(ctx sdk.Context, req abci.RequestDeliverTx, res ab

// AfterEndBlock specify actions need to do after end block period (app.Hook interface).
func (h *Hook) AfterEndBlock(ctx sdk.Context, req abci.RequestEndBlock, res abci.ResponseEndBlock) {
// update group proposals when voting period is end
timeBytes := sdk.FormatTimeBytes(ctx.BlockTime().UTC())
lenTimeByte := byte(len(timeBytes))
prefix := []byte{groupkeeper.ProposalsByVotingPeriodEndPrefix}

iterator := ctx.KVStore(h.groupStoreKey).
Iterator(prefix, sdk.PrefixEndBytes(append(append(prefix, lenTimeByte), timeBytes...)))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
proposalID, _ := splitKeyWithTime(iterator.Key())
h.doUpdateGroupProposal(ctx, proposalID)
}

for _, event := range res.Events {
h.handleBeginBlockEndBlockEvent(ctx, event)
}
Expand Down
52 changes: 35 additions & 17 deletions hooks/emitter/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ import (
proto "github.com/cosmos/gogoproto/proto"
)

func extractStringFromEventMap(evMap common.EvMap, event string, topic string) string {
return strings.Trim(evMap[event+"."+topic][0], `"`)
}

// handleGroupMsgCreateGroup implements emitter handler for Group's MsgCreateGroup.
func (h *Hook) handleGroupMsgCreateGroup(
ctx sdk.Context, evMap common.EvMap,
) {
groupId := uint64(
common.Atoi(strings.Trim(evMap[proto.MessageName(&group.EventCreateGroup{})+".group_id"][0], `"`)),
common.Atoi(extractStringFromEventMap(evMap, proto.MessageName(&group.EventCreateGroup{}), "group_id")),
)
groupInfoResponse, _ := h.groupKeeper.GroupInfo(
sdk.WrapSDKContext(ctx),
Expand All @@ -38,7 +42,7 @@ func (h *Hook) handleGroupMsgCreateGroup(
func (h *Hook) handleGroupMsgCreateGroupPolicy(
ctx sdk.Context, evMap common.EvMap,
) {
policyAddress := strings.Trim(evMap[proto.MessageName(&group.EventCreateGroupPolicy{})+".address"][0], `"`)
policyAddress := extractStringFromEventMap(evMap, proto.MessageName(&group.EventCreateGroupPolicy{}), "address")
groupPolicyResponse, _ := h.groupKeeper.GroupPolicyInfo(
sdk.WrapSDKContext(ctx),
&group.QueryGroupPolicyInfoRequest{
Expand Down Expand Up @@ -72,7 +76,7 @@ func (h *Hook) handleGroupMsgSubmitProposal(
ctx sdk.Context, evMap common.EvMap,
) {
proposalId := uint64(
common.Atoi(strings.Trim(evMap[proto.MessageName(&group.EventSubmitProposal{})+".proposal_id"][0], `"`)),
common.Atoi(extractStringFromEventMap(evMap, proto.MessageName(&group.EventSubmitProposal{}), "proposal_id")),
)
proposalResponse, _ := h.groupKeeper.Proposal(
sdk.WrapSDKContext(ctx),
Expand Down Expand Up @@ -114,7 +118,7 @@ func (h *Hook) handleGroupMsgVote(
ctx sdk.Context, msg *group.MsgVote, evMap common.EvMap,
) {
proposalId := uint64(
common.Atoi(strings.Trim(evMap[proto.MessageName(&group.EventVote{})+".proposal_id"][0], `"`)),
common.Atoi(extractStringFromEventMap(evMap, proto.MessageName(&group.EventVote{}), "proposal_id")),
)
voteResponse, err := h.groupKeeper.VoteByProposalVoter(
sdk.WrapSDKContext(ctx),
Expand All @@ -141,9 +145,9 @@ func (h *Hook) handleGroupMsgLeaveGroup(
ctx sdk.Context, evMap common.EvMap,
) {
groupId := uint64(
common.Atoi(strings.Trim(evMap[proto.MessageName(&group.EventLeaveGroup{})+".group_id"][0], `"`)),
common.Atoi(extractStringFromEventMap(evMap, proto.MessageName(&group.EventLeaveGroup{}), "group_id")),
)
address := strings.Trim(evMap[proto.MessageName(&group.EventLeaveGroup{})+".address"][0], `"`)
address := extractStringFromEventMap(evMap, proto.MessageName(&group.EventLeaveGroup{}), "address")
h.doUpdateGroup(ctx, groupId)
h.Write("REMOVE_GROUP_MEMBER", common.JsDict{
"group_id": groupId,
Expand All @@ -156,7 +160,7 @@ func (h *Hook) handleGroupMsgUpdateGroupAdmin(
ctx sdk.Context, evMap common.EvMap,
) {
groupId := uint64(
common.Atoi(strings.Trim(evMap[proto.MessageName(&group.EventUpdateGroup{})+".group_id"][0], `"`)),
common.Atoi(extractStringFromEventMap(evMap, proto.MessageName(&group.EventUpdateGroup{}), "group_id")),
)
h.doUpdateGroup(ctx, groupId)
}
Expand All @@ -177,7 +181,7 @@ func (h *Hook) handleGroupMsgUpdateGroupMetadata(
ctx sdk.Context, evMap common.EvMap,
) {
groupId := uint64(
common.Atoi(strings.Trim(evMap[proto.MessageName(&group.EventUpdateGroup{})+".group_id"][0], `"`)),
common.Atoi(extractStringFromEventMap(evMap, proto.MessageName(&group.EventUpdateGroup{}), "group_id")),
)
h.doUpdateGroup(ctx, groupId)
}
Expand All @@ -186,23 +190,35 @@ func (h *Hook) handleGroupMsgUpdateGroupMetadata(
func (h *Hook) handleGroupMsgUpdateGroupPolicyAdmin(
ctx sdk.Context, evMap common.EvMap,
) {
groupPolicyAddress := strings.Trim(evMap[proto.MessageName(&group.EventUpdateGroupPolicy{})+".address"][0], `"`)
groupPolicyAddress := extractStringFromEventMap(
evMap,
proto.MessageName(&group.EventUpdateGroupPolicy{}),
"address",
)
h.doUpdateGroupPolicy(ctx, groupPolicyAddress)
}

// handleGroupMsgUpdateGroupPolicyDecisionPolicy implements emitter handler for Group's MsgUpdateGroupPolicyDecisionPolicy.
func (h *Hook) handleGroupMsgUpdateGroupPolicyDecisionPolicy(
ctx sdk.Context, evMap common.EvMap,
) {
groupPolicyAddress := strings.Trim(evMap[proto.MessageName(&group.EventUpdateGroupPolicy{})+".address"][0], `"`)
groupPolicyAddress := extractStringFromEventMap(
evMap,
proto.MessageName(&group.EventUpdateGroupPolicy{}),
"address",
)
h.doUpdateGroupPolicy(ctx, groupPolicyAddress)
}

// handleGroupMsgUpdateGroupPolicyMetadata implements emitter handler for Group's MsgUpdateGroupPolicyMetadata.
func (h *Hook) handleGroupMsgUpdateGroupPolicyMetadata(
ctx sdk.Context, evMap common.EvMap,
) {
groupPolicyAddress := strings.Trim(evMap[proto.MessageName(&group.EventUpdateGroupPolicy{})+".address"][0], `"`)
groupPolicyAddress := extractStringFromEventMap(
evMap,
proto.MessageName(&group.EventUpdateGroupPolicy{}),
"address",
)
h.doUpdateGroupPolicy(ctx, groupPolicyAddress)
}

Expand All @@ -211,7 +227,9 @@ func (h *Hook) handleGroupMsgWithdrawProposal(
ctx sdk.Context, evMap common.EvMap,
) {
proposalId := uint64(
common.Atoi(strings.Trim(evMap[proto.MessageName(&group.EventWithdrawProposal{})+".proposal_id"][0], `"`)),
common.Atoi(
extractStringFromEventMap(evMap, proto.MessageName(&group.EventWithdrawProposal{}), "proposal_id"),
),
)
h.doUpdateGroupProposal(ctx, proposalId)
}
Expand All @@ -224,9 +242,9 @@ func (h *Hook) handleGroupEventExec(
return
}
proposalId := uint64(
common.Atoi(strings.Trim(evMap[proto.MessageName(&group.EventExec{})+".proposal_id"][0], `"`)),
common.Atoi(extractStringFromEventMap(evMap, proto.MessageName(&group.EventExec{}), "proposal_id")),
)
executorResult := strings.Trim(evMap[proto.MessageName(&group.EventExec{})+".result"][0], `"`)
executorResult := extractStringFromEventMap(evMap, proto.MessageName(&group.EventExec{}), "result")
h.Write("UPDATE_GROUP_PROPOSAL_BY_ID", common.JsDict{
"id": proposalId,
"executor_result": executorResult,
Expand All @@ -243,10 +261,10 @@ func (h *Hook) handleGroupEventProposalPruned(
return
}
proposalId := uint64(
common.Atoi(strings.Trim(evMap[proto.MessageName(&group.EventProposalPruned{})+".proposal_id"][0], `"`)),
common.Atoi(extractStringFromEventMap(evMap, proto.MessageName(&group.EventProposalPruned{}), "proposal_id")),
)
proposalStatus := strings.Trim(evMap[proto.MessageName(&group.EventProposalPruned{})+".status"][0], `"`)
var tallyResult group.TallyResult
proposalStatus := extractStringFromEventMap(evMap, proto.MessageName(&group.EventProposalPruned{}), "status")
tallyResult := group.DefaultTallyResult()
json.Unmarshal([]byte(evMap[proto.MessageName(&group.EventProposalPruned{})+".tally_result"][0]), &tallyResult)
h.Write("UPDATE_GROUP_PROPOSAL_BY_ID", common.JsDict{
"id": proposalId,
Expand Down
13 changes: 0 additions & 13 deletions hooks/emitter/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
"github.com/cosmos/cosmos-sdk/x/group"
groupkeeper "github.com/cosmos/cosmos-sdk/x/group/keeper"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
proto "github.com/cosmos/gogoproto/proto"
Expand Down Expand Up @@ -159,18 +158,6 @@ func (h *Hook) handleMsg(ctx sdk.Context, txHash []byte, msg sdk.Msg, log sdk.AB
}

func (h *Hook) handleBeginBlockEndBlockEvent(ctx sdk.Context, event abci.Event) {
timeBytes := sdk.FormatTimeBytes(ctx.BlockTime().UTC())
lenTimeByte := byte(len(timeBytes))
prefix := []byte{groupkeeper.ProposalsByVotingPeriodEndPrefix}

iterator := ctx.KVStore(h.groupStoreKey).
Iterator(prefix, sdk.PrefixEndBytes(append(append(prefix, lenTimeByte), timeBytes...)))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
proposalID, _ := splitKeyWithTime(iterator.Key())
h.doUpdateGroupProposal(ctx, proposalID)
}

events := sdk.StringifyEvents([]abci.Event{event})
evMap := parseEvents(events)
switch event.Type {
Expand Down

0 comments on commit 99fa143

Please sign in to comment.