Skip to content

Commit

Permalink
feat: allow to handle MsgExec instances properly (#75)
Browse files Browse the repository at this point in the history
## Description
This PR adds the possibility of handling `MsgExec` inner messages. 

Currently when a `MsgExec` is executed, the inner messages are not parsed correctly by the various modules. With this PR a new `AuthzMessageModule` interface is added. Thanks to this, modules will be able to properly handle `MsgExec` inner messages and parse their data accordingly.

## Checklist
- [x] Targeted PR against correct branch.
- [ ] Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
- [ ] Wrote unit tests.  
- [x] Re-reviewed `Files changed` in the Github PR explorer.
  • Loading branch information
RiccardoM committed Sep 15, 2022
1 parent 7d0a7c4 commit da81533
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
@@ -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

0 comments on commit da81533

Please sign in to comment.