Skip to content

Commit

Permalink
feat: allow to handle MsgExec instances properly
Browse files Browse the repository at this point in the history
  • Loading branch information
RiccardoM committed Sep 15, 2022
1 parent a9dfa4b commit 765d0ec
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
### Changes
- Updated cosmos/cosmos-sdk to `v0.45.8`
- Use `sqlx` instead of `sql` while dealing with a PostgreSQL database
- ([\#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

0 comments on commit 765d0ec

Please sign in to comment.