Skip to content

Commit

Permalink
Fix: exec internal messages decoding (#191)
Browse files Browse the repository at this point in the history
* Fix: exec internal messages decoding

* Fix: test
  • Loading branch information
aopoltorzhicky authored May 6, 2024
1 parent 4536f7f commit b9bd5e3
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
17 changes: 17 additions & 0 deletions pkg/indexer/decode/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,23 @@ func Message(
d.Msg.Type, d.Msg.Addresses, d.Msg.Grants, err = handle.MsgGrant(ctx, status, typedMsg)
case *authz.MsgExec:
d.Msg.Type, d.Msg.Addresses, d.Msg.InternalMsgs, err = handle.MsgExec(ctx, status, typedMsg)
if err != nil {
return d, err
}

msgs := make([]any, 0)
for i := range typedMsg.Msgs {
msg, err := cosmosTypes.GetMsgFromTypeURL(cfg.Codec, typedMsg.Msgs[i].TypeUrl)
if err != nil {
return d, err
}
if err := cfg.Codec.UnpackAny(typedMsg.Msgs[i], &msg); err != nil {
return d, err
}
msgs = append(msgs, structs.Map(msg))
}
d.Msg.Data["Msgs"] = msgs

case *authz.MsgRevoke:
d.Msg.Type, d.Msg.Addresses, d.Msg.Grants, err = handle.MsgRevoke(ctx, status, typedMsg)

Expand Down
12 changes: 11 additions & 1 deletion pkg/indexer/parser/events/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,17 @@ func handleExec(ctx *context.Context, events []storage.Event, msg *storage.Messa
return err
}
case "/cosmos.staking.v1beta1.MsgBeginRedelegate":
if err := processRedelegate(ctx, events, msg, idx); err != nil {
msgsAny, ok := msg.Data["Msgs"]
if !ok {
return errors.Errorf("can't find Msgs key in MsgExec: %##v", msg.Data)
}
msgs, ok := msgsAny.([]map[string]any)
if !ok {
return errors.Errorf("invalid type of Msgs in MsgExec: %T", msgsAny)
}
if err := processRedelegate(ctx, events, &storage.Message{
Data: msgs[i],
}, idx); err != nil {
return err
}
case "/cosmos.staking.v1beta1.MsgUndelegate":
Expand Down
51 changes: 51 additions & 0 deletions pkg/indexer/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ package parser

import (
"context"
"encoding/json"
"fmt"
"os"
"testing"
"time"

Expand All @@ -16,6 +19,7 @@ import (
"github.com/dipdup-net/indexer-sdk/pkg/modules"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
tmTypes "github.com/tendermint/tendermint/types"
)

Expand Down Expand Up @@ -260,3 +264,50 @@ func TestModule_OnParseError(t *testing.T) {
}
}
}

func getBlockByHeight(height uint64) (types.BlockData, error) {
blockFile, err := os.Open(fmt.Sprintf("../../../test/json/block_%d.json", height))
if err != nil {
return types.BlockData{}, err
}
defer blockFile.Close()

var block types.ResultBlock
if err := json.NewDecoder(blockFile).Decode(&block); err != nil {
return types.BlockData{}, err
}

blockResultsFile, err := os.Open(fmt.Sprintf("../../../test/json/results_%d.json", height))
if err != nil {
return types.BlockData{}, err
}
defer blockResultsFile.Close()

var blockResults types.ResultBlockResults
if err := json.NewDecoder(blockResultsFile).Decode(&blockResults); err != nil {
return types.BlockData{}, err
}

return types.BlockData{
ResultBlock: block,
ResultBlockResults: blockResults,
}, nil
}

func TestModule_1768659(t *testing.T) {
writerModule, writerOutputName, parserModule := createModules(t)

ctx, cancel := context.WithTimeout(context.Background(), time.Second*1)
defer cancel()

parserModule.Start(ctx)

block, err := getBlockByHeight(1768659)
require.NoError(t, err)
writerModule.MustOutput(writerOutputName).Push(block)

<-ctx.Done()

err = parserModule.Close()
require.NoError(t, err)
}
1 change: 1 addition & 0 deletions test/json/block_1768659.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions test/json/results_1768659.json

Large diffs are not rendered by default.

0 comments on commit b9bd5e3

Please sign in to comment.