Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: parse events instead of messages #395

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: feat
module: none
pull_request: 395
description: Updated the parsing of data to rely on emitted events instead of messages
backward_compatible: true
date: 2024-01-23T08:06:35.693359413Z
16 changes: 16 additions & 0 deletions utils/events/attributes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package events

import (
abci "github.com/cometbft/cometbft/abci/types"
subspacestypes "github.com/desmos-labs/desmos/v6/x/subspaces/types"
juno "github.com/forbole/juno/v5/types"
)

// GetSubspaceIDFromEvent returns the subspace ID from the given event
func GetSubspaceIDFromEvent(event abci.Event) (uint64, error) {
attribute, err := juno.FindAttributeByKey(event, subspacestypes.AttributeKeySubspaceID)
if err != nil {
return 0, err
}
return subspacestypes.ParseSubspaceID(attribute.Value)
}
32 changes: 32 additions & 0 deletions utils/transactions/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package transactions

import (
abci "github.com/cometbft/cometbft/abci/types"
juno "github.com/forbole/juno/v5/types"
"github.com/rs/zerolog/log"

"github.com/desmos-labs/athena/x/filters"
)

// ParseTxEvents parses the given events using the given parsers
func ParseTxEvents(tx *juno.Tx, eventsParsers map[string]func(tx *juno.Tx, event abci.Event) error) error {
for _, event := range tx.Events {
if !filters.ShouldEventBeParsed(event) {
continue
}

parseEvent, canBeParsed := eventsParsers[event.Type]
if !canBeParsed {
continue
}

err := parseEvent(tx, event)
if err != nil {
return err
}

log.Debug().Str("event", event.Type).Msg("handled event")
}

return nil
}
2 changes: 1 addition & 1 deletion x/authz/handle_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// HandleMsg implements modules.MessageModule
func (m *Module) HandleMsg(index int, msg sdk.Msg, tx *juno.Tx) error {
func (m *Module) HandleMsg(_ int, msg sdk.Msg, tx *juno.Tx) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if len(tx.Logs) == 0 {
return nil
}
Expand Down
25 changes: 25 additions & 0 deletions x/filters/messages.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package filters

import (
abci "github.com/cometbft/cometbft/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
subspacestypes "github.com/desmos-labs/desmos/v6/x/subspaces/types"
"github.com/forbole/juno/v5/types"
"github.com/forbole/juno/v5/types/config"
)

Expand All @@ -11,6 +13,29 @@ var (
cfg *Config
)

// ShouldEventBeParsed tells whether the given event should be parsed
func ShouldEventBeParsed(event abci.Event) bool {
parseCfg()

// Search either a 'subspace' or 'subspace_id' attribute
attribute, err := types.FindAttributeByKey(event, "subspace_id")
if err != nil {
attribute, err = types.FindAttributeByKey(event, "subspace")
}

if err != nil {
return false
}

// Parse the subspace id
subspaceID, err := subspacestypes.ParseSubspaceID(attribute.Value)
if err != nil {
return false
}

return cfg.isSubspaceSupported(subspaceID)
}

// ShouldMsgBeParsed tells whether the given subspace is currently supported and its messages should be parsed
func ShouldMsgBeParsed(msg sdk.Msg) bool {
parseCfg()
Expand Down
131 changes: 0 additions & 131 deletions x/posts/handle_msg.go

This file was deleted.

160 changes: 160 additions & 0 deletions x/posts/handle_tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package posts

import (
abci "github.com/cometbft/cometbft/abci/types"
poststypes "github.com/desmos-labs/desmos/v6/x/posts/types"
juno "github.com/forbole/juno/v5/types"

"github.com/desmos-labs/athena/utils/events"
"github.com/desmos-labs/athena/utils/transactions"

"github.com/desmos-labs/athena/types"
)

// HandleTx handles the transaction events
func (m *Module) HandleTx(tx *juno.Tx) error {
return transactions.ParseTxEvents(tx, map[string]func(tx *juno.Tx, event abci.Event) error{
poststypes.EventTypeCreatePost: m.parseCreatePostEvent,
poststypes.EventTypeEditPost: m.parseEditPostEvent,
poststypes.EventTypeDeletePost: m.parseDeletePostEvent,
poststypes.EventTypeAddPostAttachment: m.parseAddPostAttachmentEvent,
poststypes.EventTypeRemovePostAttachment: m.parseRemovePostAttachmentEvent,
poststypes.EventTypeAnswerPoll: m.parseAnswerPollEvent,
})
}

// -------------------------------------------------------------------------------------------------------------------

// parseCreatePostEvent handles the creation of a new post
func (m *Module) parseCreatePostEvent(tx *juno.Tx, event abci.Event) error {
subspaceID, err := events.GetSubspaceIDFromEvent(event)
if err != nil {
return err
}

postID, err := GetPostIDFromEvent(event)
if err != nil {
return err
}

// Update the post
err = m.updatePost(tx.Height, subspaceID, postID)
if err != nil {
return err
}

// Update the post attachments
err = m.updatePostAttachments(tx.Height, subspaceID, postID)
if err != nil {
return err
}

// Save the related transaction
return m.db.SavePostTx(types.NewPostTransaction(subspaceID, postID, tx.TxHash))
}

// parseEditPostEvent handles the edition of an existing post
func (m *Module) parseEditPostEvent(tx *juno.Tx, event abci.Event) error {
subspaceID, err := events.GetSubspaceIDFromEvent(event)
if err != nil {
return err
}

postID, err := GetPostIDFromEvent(event)
if err != nil {
return err
}

// Update the post
err = m.updatePost(tx.Height, subspaceID, postID)
if err != nil {
return err
}

// Save the related transaction
return m.db.SavePostTx(types.NewPostTransaction(subspaceID, postID, tx.TxHash))
}

// parseDeletePostEvent handles the deletion of an existing post
func (m *Module) parseDeletePostEvent(tx *juno.Tx, event abci.Event) error {
subspaceID, err := events.GetSubspaceIDFromEvent(event)
if err != nil {
return err
}

postID, err := GetPostIDFromEvent(event)
if err != nil {
return err
}

return m.db.DeletePost(tx.Height, subspaceID, postID)
}

// parseAddPostReactionEvent handles the addition of a reaction to an existing post
func (m *Module) parseAddPostAttachmentEvent(tx *juno.Tx, event abci.Event) error {
subspaceID, err := events.GetSubspaceIDFromEvent(event)
if err != nil {
return err
}

postID, err := GetPostIDFromEvent(event)
if err != nil {
return err
}

// Update the attachments
err = m.updatePostAttachments(tx.Height, subspaceID, postID)
if err != nil {
return err
}

// Store the related post transaction
return m.db.SavePostTx(types.NewPostTransaction(subspaceID, postID, tx.TxHash))
}

// parseRemovePostAttachmentEvent handles the removal of a reaction from an existing post
func (m *Module) parseRemovePostAttachmentEvent(tx *juno.Tx, event abci.Event) error {
subspaceID, err := events.GetSubspaceIDFromEvent(event)
if err != nil {
return err
}

postID, err := GetPostIDFromEvent(event)
if err != nil {
return err
}

attachmentID, err := GetAttachmentIDFromEvent(event)
if err != nil {
return err
}

// Delete the attachment
err = m.db.DeletePostAttachment(tx.Height, subspaceID, postID, attachmentID)
if err != nil {
return err
}

// Store the related post transaction
return m.db.SavePostTx(types.NewPostTransaction(subspaceID, postID, tx.TxHash))
}

// parseAnswerPollEvent handles the answer to a poll
func (m *Module) parseAnswerPollEvent(tx *juno.Tx, event abci.Event) error {
subspaceID, err := events.GetSubspaceIDFromEvent(event)
if err != nil {
return err
}

postID, err := GetPostIDFromEvent(event)
if err != nil {
return err
}

pollID, err := GetPollIDFromEvent(event)
if err != nil {
return err
}

return m.updatePollAnswers(tx.Height, subspaceID, postID, pollID)
Comment on lines +143 to +159
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to add answer_indexes and answerer attribute to Desmos core in the future so it can be updated directly rather than updating all the poll answers. What do you think?

}
Loading
Loading