-
Notifications
You must be signed in to change notification settings - Fork 4
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
RiccardoM
wants to merge
15
commits into
main
Choose a base branch
from
riccardom/parse-events-instead-of-messages
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
dd36d9e
refactor: parse posts using events instead of messages
RiccardoM 26a7516
chore: added event logging
RiccardoM 1536990
fix: fixed module casting
RiccardoM 68a7791
feat: parse reports events instead of messages
RiccardoM 8eb6a71
docs: added missing comment
RiccardoM 994fca4
feat: add missing remove reason event parsing
RiccardoM 119d844
refactor: parse profiles events instead of messages
RiccardoM 657c821
chore: run lint
RiccardoM e50265a
chore: added missing events mapping
RiccardoM 36f532a
feat: parse reactions events instead of messages
RiccardoM 997e2cc
chore: added missing event filtering
RiccardoM 9c2f5cc
refactor: improve packages organization
RiccardoM a7b4804
feat: parse relationships event instead of messages
RiccardoM d8c79f2
refactor: parse subspaces instead of messages
RiccardoM fbc371b
chore: added changeset entry
RiccardoM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
.changeset/entries/fc893296400914d4ce8b2f9fd99eb1424adbf626bd7e5a8c0b2083ba8575ca19.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be better to add |
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can be also replaced by events.
See:
https://github.com/cosmos/cosmos-sdk/blob/release/v0.47.x/x/authz/keeper/keeper.go#L197
https://github.com/cosmos/cosmos-sdk/blob/release/v0.47.x/x/authz/keeper/keeper.go#L223