Skip to content

Commit

Permalink
Add a helper function for parse out msg responses (#5351)
Browse files Browse the repository at this point in the history
* add UnmarshalMsgResponse

* refactor

* refactor to right place

* chore: update UnmarshalMsgResponses api and usage

* lint: make lint-fix

---------

Co-authored-by: phamminh <[email protected]>
Co-authored-by: Đỗ Việt Hoàng <[email protected]>
Co-authored-by: Damian Nolan <[email protected]>
Co-authored-by: Cian Hatton <[email protected]>
  • Loading branch information
5 people authored Dec 14, 2023
1 parent 39e6073 commit 549fd90
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
4 changes: 2 additions & 2 deletions e2e/tests/wasm/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import (
"testing"
"time"

testifysuite "github.com/stretchr/testify/suite"

"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
"github.com/strangelove-ventures/interchaintest/v8/testutil"
testifysuite "github.com/stretchr/testify/suite"

upgradetypes "cosmossdk.io/x/upgrade/types"

authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

"github.com/cosmos/ibc-go/e2e/testsuite"
"github.com/cosmos/ibc-go/e2e/testvalues"
wasmtypes "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types"
Expand Down
19 changes: 2 additions & 17 deletions e2e/testsuite/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package testsuite
import (
"bytes"
"encoding/hex"
"fmt"

"github.com/cosmos/gogoproto/jsonpb"
"github.com/cosmos/gogoproto/proto"
Expand Down Expand Up @@ -35,6 +34,7 @@ import (
solomachine "github.com/cosmos/ibc-go/v8/modules/light-clients/06-solomachine"
ibctmtypes "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"
localhost "github.com/cosmos/ibc-go/v8/modules/light-clients/09-localhost"
ibctesting "github.com/cosmos/ibc-go/v8/testing"
simappparams "github.com/cosmos/ibc-go/v8/testing/simapp/params"
)

Expand Down Expand Up @@ -101,22 +101,7 @@ func UnmarshalMsgResponses(txResp sdk.TxResponse, msgs ...codec.ProtoMarshaler)
return err
}

var txMsgData sdk.TxMsgData
if err := cdc.Unmarshal(bz, &txMsgData); err != nil {
return err
}

if len(msgs) != len(txMsgData.MsgResponses) {
return fmt.Errorf("expected %d message responses but got %d", len(msgs), len(txMsgData.MsgResponses))
}

for i, msg := range msgs {
if err := cdc.Unmarshal(txMsgData.MsgResponses[i].Value, msg); err != nil {
return err
}
}

return nil
return ibctesting.UnmarshalMsgResponses(cdc, bz, msgs...)
}

// MustProtoMarshalJSON provides an auxiliary function to return Proto3 JSON encoded
Expand Down
24 changes: 24 additions & 0 deletions testing/utils.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package ibctesting

import (
"fmt"
"math/rand"
"testing"

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

abci "github.com/cometbft/cometbft/abci/types"
cmttypes "github.com/cometbft/cometbft/types"
)
Expand Down Expand Up @@ -33,3 +37,23 @@ func GenerateString(length uint) string {
}
return string(bytes)
}

// UnmarshalMsgResponses parse out msg responses from a transaction result
func UnmarshalMsgResponses(cdc codec.Codec, data []byte, msgs ...codec.ProtoMarshaler) error {
var txMsgData sdk.TxMsgData
if err := cdc.Unmarshal(data, &txMsgData); err != nil {
return err
}

if len(msgs) != len(txMsgData.MsgResponses) {
return fmt.Errorf("expected %d message responses but got %d", len(msgs), len(txMsgData.MsgResponses))
}

for i, msg := range msgs {
if err := cdc.Unmarshal(txMsgData.MsgResponses[i].Value, msg); err != nil {
return err
}
}

return nil
}

0 comments on commit 549fd90

Please sign in to comment.