From 549fd90639086a60e57f3287ea72116860cce7a7 Mon Sep 17 00:00:00 2001 From: Pham Anh Minh <93236465+phamminh0811@users.noreply.github.com> Date: Thu, 14 Dec 2023 16:41:33 +0700 Subject: [PATCH] Add a helper function for parse out msg responses (#5351) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add UnmarshalMsgResponse * refactor * refactor to right place * chore: update UnmarshalMsgResponses api and usage * lint: make lint-fix --------- Co-authored-by: phamminh Co-authored-by: Đỗ Việt Hoàng Co-authored-by: Damian Nolan Co-authored-by: Cian Hatton --- e2e/tests/wasm/upgrade_test.go | 4 ++-- e2e/testsuite/codec.go | 19 ++----------------- testing/utils.go | 24 ++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/e2e/tests/wasm/upgrade_test.go b/e2e/tests/wasm/upgrade_test.go index 03b4e7f2d6c..10d8f75c5ce 100644 --- a/e2e/tests/wasm/upgrade_test.go +++ b/e2e/tests/wasm/upgrade_test.go @@ -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" diff --git a/e2e/testsuite/codec.go b/e2e/testsuite/codec.go index 65010d1e0a8..b9240aeb326 100644 --- a/e2e/testsuite/codec.go +++ b/e2e/testsuite/codec.go @@ -3,7 +3,6 @@ package testsuite import ( "bytes" "encoding/hex" - "fmt" "github.com/cosmos/gogoproto/jsonpb" "github.com/cosmos/gogoproto/proto" @@ -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" ) @@ -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 diff --git a/testing/utils.go b/testing/utils.go index 16bf2492248..bc023cec6fe 100644 --- a/testing/utils.go +++ b/testing/utils.go @@ -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" ) @@ -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 +}