From 5b36e017a7a2c818a589a4637d3dd78fa320372d Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 23 Feb 2024 13:02:12 +0100 Subject: [PATCH] fix: return correct types in wasm tests and add expected err (backport #5883) (#5887) * fix: return correct types in wasm tests and add expected err (#5883) Co-authored-by: Cian Hatton cian@interchain.io (cherry picked from commit 6b118957b44f65bcde91463babe4e8e4b40d661b) # Conflicts: # modules/light-clients/08-wasm/types/querier_test.go * fix conflicts * remove duplicate code * remove duplicate code * fix test * fix: address test failures --------- Co-authored-by: Damian Nolan Co-authored-by: Carlos Rodriguez --- .../08-wasm/types/querier_test.go | 34 +++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/modules/light-clients/08-wasm/types/querier_test.go b/modules/light-clients/08-wasm/types/querier_test.go index 51d66afbc17..f912480864b 100644 --- a/modules/light-clients/08-wasm/types/querier_test.go +++ b/modules/light-clients/08-wasm/types/querier_test.go @@ -131,6 +131,7 @@ func (suite *TypesTestSuite) TestStargateQuery() { testCases := []struct { name string malleate func() + expError error }{ { "success: custom query", @@ -141,7 +142,7 @@ func (suite *TypesTestSuite) TestStargateQuery() { ibcwasm.SetQueryPlugins(&querierPlugin) - suite.mockVM.RegisterQueryCallback(types.StatusMsg{}, func(_ wasmvm.Checksum, _ wasmvmtypes.Env, _ []byte, store wasmvm.KVStore, _ wasmvm.GoAPI, querier wasmvm.Querier, _ wasmvm.GasMeter, _ uint64, _ wasmvmtypes.UFraction) ([]byte, uint64, error) { + suite.mockVM.RegisterQueryCallback(types.TimestampAtHeightMsg{}, func(_ wasmvm.Checksum, _ wasmvmtypes.Env, _ []byte, store wasmvm.KVStore, _ wasmvm.GoAPI, querier wasmvm.Querier, _ wasmvm.GasMeter, _ uint64, _ wasmvmtypes.UFraction) ([]byte, uint64, error) { queryRequest := types.QueryChecksumsRequest{} bz, err := queryRequest.Marshal() suite.Require().NoError(err) @@ -165,9 +166,13 @@ func (suite *TypesTestSuite) TestStargateQuery() { store.Set(testKey, value) - return resp, wasmtesting.DefaultGasUsed, nil + result, err := json.Marshal(types.TimestampAtHeightResult{}) + suite.Require().NoError(err) + + return result, wasmtesting.DefaultGasUsed, nil }) }, + nil, }, { // The following test sets a mock proof key and value in the ibc store and registers a query callback on the Status msg. @@ -194,7 +199,7 @@ func (suite *TypesTestSuite) TestStargateQuery() { merklePath, err := commitmenttypes.ApplyPrefix(suite.chainA.GetPrefix(), merklePath) suite.Require().NoError(err) - suite.mockVM.RegisterQueryCallback(types.StatusMsg{}, func(_ wasmvm.Checksum, _ wasmvmtypes.Env, _ []byte, _ wasmvm.KVStore, _ wasmvm.GoAPI, querier wasmvm.Querier, _ wasmvm.GasMeter, _ uint64, _ wasmvmtypes.UFraction) ([]byte, uint64, error) { + suite.mockVM.RegisterQueryCallback(types.TimestampAtHeightMsg{}, func(_ wasmvm.Checksum, _ wasmvmtypes.Env, _ []byte, _ wasmvm.KVStore, _ wasmvm.GoAPI, querier wasmvm.Querier, _ wasmvm.GasMeter, _ uint64, _ wasmvmtypes.UFraction) ([]byte, uint64, error) { queryRequest := clienttypes.QueryVerifyMembershipRequest{ ClientId: endpoint.ClientID, Proof: proof, @@ -220,7 +225,10 @@ func (suite *TypesTestSuite) TestStargateQuery() { suite.Require().True(respData.Success) - return resp, wasmtesting.DefaultGasUsed, nil + result, err := json.Marshal(types.TimestampAtHeightResult{}) + suite.Require().NoError(err) + + return result, wasmtesting.DefaultGasUsed, nil }) suite.mockVM.RegisterSudoCallback(types.VerifyMembershipMsg{}, func(_ wasmvm.Checksum, _ wasmvmtypes.Env, sudoMsg []byte, store wasmvm.KVStore, @@ -247,11 +255,12 @@ func (suite *TypesTestSuite) TestStargateQuery() { return &wasmvmtypes.Response{Data: bz}, wasmtesting.DefaultGasUsed, nil }) }, + nil, }, { "failure: default querier", func() { - suite.mockVM.RegisterQueryCallback(types.StatusMsg{}, func(_ wasmvm.Checksum, _ wasmvmtypes.Env, _ []byte, store wasmvm.KVStore, _ wasmvm.GoAPI, querier wasmvm.Querier, _ wasmvm.GasMeter, _ uint64, _ wasmvmtypes.UFraction) ([]byte, uint64, error) { + suite.mockVM.RegisterQueryCallback(types.TimestampAtHeightMsg{}, func(_ wasmvm.Checksum, _ wasmvmtypes.Env, _ []byte, store wasmvm.KVStore, _ wasmvm.GoAPI, querier wasmvm.Querier, _ wasmvm.GasMeter, _ uint64, _ wasmvmtypes.UFraction) ([]byte, uint64, error) { queryRequest := types.QueryChecksumsRequest{} bz, err := queryRequest.Marshal() suite.Require().NoError(err) @@ -270,6 +279,7 @@ func (suite *TypesTestSuite) TestStargateQuery() { return nil, wasmtesting.DefaultGasUsed, err }) }, + wasmvmtypes.UnsupportedRequest{Kind: fmt.Sprintf("'%s' path is not allowed from the contract", typeURL)}, }, } @@ -286,7 +296,19 @@ func (suite *TypesTestSuite) TestStargateQuery() { clientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), endpoint.ClientID) clientState := endpoint.GetClientState() - clientState.Status(suite.chainA.GetContext(), clientStore, suite.chainA.App.AppCodec()) + + // NOTE: we register query callbacks against: types.TimestampAtHeightMsg{} + // in practise, this can against any client state msg, however registering against types.StatusMsg{} introduces recursive loops + // due to test case: "success: verify membership query" + _, err = clientState.GetTimestampAtHeight(suite.chainA.GetContext(), clientStore, suite.chainA.App.AppCodec(), clienttypes.NewHeight(1, 100)) + + expPass := tc.expError == nil + if expPass { + suite.Require().NoError(err) + } else { + // use error contains as wasmvm errors do not implement errors.Is method + suite.Require().ErrorContains(err, tc.expError.Error()) + } if expDiscardedState { suite.Require().False(clientStore.Has(testKey))