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

feat: allow to handle MsgExec instances properly #75

Merged
merged 4 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Unreleased
### Changes
- ([\#74](https://github.com/forbole/juno/pull/74)) Added database block count to prometheus to improve alert monitoring
- ([\#75](https://github.com/forbole/juno/pull/75)) Allow modules to handle MsgExec inner messages

## v3.4.0
### Changes
Expand Down
13 changes: 12 additions & 1 deletion modules/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"strings"

"github.com/cosmos/cosmos-sdk/x/authz"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/go-co-op/gocron"
tmctypes "github.com/tendermint/tendermint/rpc/core/types"
Expand Down Expand Up @@ -91,9 +93,18 @@ type TransactionModule interface {

type MessageModule interface {
// HandleMsg handles a single message.
// For convenience of usa, the index of the message inside the transaction and the transaction itself
// For convenience of use, the index of the message inside the transaction and the transaction itself
// are passed as well.
// NOTE. The returned error will be logged using the MsgError method. All other modules' handlers
// will still be called.
HandleMsg(index int, msg sdk.Msg, tx *types.Tx) error
}

type AuthzMessageModule interface {
// HandleMsgExec handles a single message that is contained within an authz.MsgExec instance.
// For convenience of use, the index of the message inside the transaction and the transaction itself
// are passed as well.
// NOTE. The returned error will be logged using the MsgError method. All other modules' handlers
// will still be called.
HandleMsgExec(index int, msgExec *authz.MsgExec, authzMsgIndex int, executedMsg sdk.Msg, tx *types.Tx) error
}
23 changes: 23 additions & 0 deletions parser/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"time"

"github.com/cosmos/cosmos-sdk/x/authz"

"github.com/forbole/juno/v3/logging"

"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -291,6 +293,7 @@ func (w Worker) handleTx(tx *types.Tx) {
// handleMessage accepts the transaction and handles messages contained
// inside the transaction.
func (w Worker) handleMessage(index int, msg sdk.Msg, tx *types.Tx) {
// Allow modules to handle the message
for _, module := range w.modules {
if messageModule, ok := module.(modules.MessageModule); ok {
err := messageModule.HandleMsg(index, msg, tx)
Expand All @@ -299,6 +302,26 @@ func (w Worker) handleMessage(index int, msg sdk.Msg, tx *types.Tx) {
}
}
}

// If it's a MsgExecute, we need to make sure the included messages are handled as well
if msgExec, ok := msg.(*authz.MsgExec); ok {
for authzIndex, msgAny := range msgExec.Msgs {
var executedMsg sdk.Msg
err := w.codec.UnpackAny(msgAny, &executedMsg)
if err != nil {
w.logger.Error("unable to unpack MsgExec inner message", "index", authzIndex, "error", err)
}

for _, module := range w.modules {
if messageModule, ok := module.(modules.AuthzMessageModule); ok {
err = messageModule.HandleMsgExec(index, msgExec, authzIndex, executedMsg, tx)
if err != nil {
w.logger.MsgError(module, tx, executedMsg, err)
}
}
}
}
}
}

// ExportTxs accepts a slice of transactions and persists then inside the database.
Expand Down