Skip to content

Commit

Permalink
fix: return correct types in wasm tests and add expected err (#5883)
Browse files Browse the repository at this point in the history
Co-authored-by: Cian Hatton cian@interchain.io

(cherry picked from commit 6b11895)

# Conflicts:
#	modules/light-clients/08-wasm/types/querier_test.go
  • Loading branch information
damiannolan authored and mergify[bot] committed Feb 21, 2024
1 parent 57fcdb9 commit cfaa309
Showing 1 changed file with 120 additions and 0 deletions.
120 changes: 120 additions & 0 deletions modules/light-clients/08-wasm/types/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func (suite *TypesTestSuite) TestStargateQuery() {
testCases := []struct {
name string
malleate func()
expError error
}{
{
"success: custom query",
Expand Down Expand Up @@ -153,11 +154,107 @@ func (suite *TypesTestSuite) TestStargateQuery() {
suite.Require().Len(respData.Checksums, 1)
suite.Require().Equal(expChecksum, respData.Checksums[0])

<<<<<<< HEAD
return resp, wasmtesting.DefaultGasUsed, nil
=======
store.Set(testKey, value)

result, err := json.Marshal(types.TimestampAtHeightResult{})
suite.Require().NoError(err)

return result, wasmtesting.DefaultGasUsed, nil
>>>>>>> 6b118957 (fix: return correct types in wasm tests and add expected err (#5883))
})
},
nil,
},
{
<<<<<<< HEAD
=======
// The following test sets a mock proof key and value in the ibc store and registers a query callback on the Status msg.
// The Status handler will then perform the QueryVerifyMembershipRequest using the wasmvm.Querier.
// As the VerifyMembership query rpc will call directly back into the same client, we also register a callback for VerifyMembership.
// Here we decode the proof and verify the mock proof key and value are set in the ibc store.
// This exercises the full flow through the grpc handler and into the light client for verification, handling encoding and routing.
// Furthermore we write a test key and assert that the state changes made by this handler were discarded by the cachedCtx at the grpc handler.
"success: verify membership query",
func() {
querierPlugin := types.QueryPlugins{
Stargate: types.AcceptListStargateQuerier([]string{""}),
}

ibcwasm.SetQueryPlugins(&querierPlugin)

store := suite.chainA.GetContext().KVStore(GetSimApp(suite.chainA).GetKey(exported.StoreKey))
store.Set(proofKey, value)

suite.coordinator.CommitBlock(suite.chainA)
proof, proofHeight := endpoint.QueryProofAtHeight(proofKey, uint64(suite.chainA.GetContext().BlockHeight()))

merklePath := commitmenttypes.NewMerklePath(string(proofKey))
merklePath, err := commitmenttypes.ApplyPrefix(suite.chainA.GetPrefix(), merklePath)
suite.Require().NoError(err)

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,
ProofHeight: proofHeight,
MerklePath: merklePath,
Value: value,
}

bz, err := queryRequest.Marshal()
suite.Require().NoError(err)

resp, err := querier.Query(wasmvmtypes.QueryRequest{
Stargate: &wasmvmtypes.StargateQuery{
Path: "/ibc.core.client.v1.Query/VerifyMembership",
Data: bz,
},
}, math.MaxUint64)
suite.Require().NoError(err)

var respData clienttypes.QueryVerifyMembershipResponse
err = respData.Unmarshal(resp)
suite.Require().NoError(err)

suite.Require().True(respData.Success)

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,
_ wasmvm.GoAPI, _ wasmvm.Querier, _ wasmvm.GasMeter, _ uint64, _ wasmvmtypes.UFraction,
) (*wasmvmtypes.Response, uint64, error) {
var payload types.SudoMsg
err := json.Unmarshal(sudoMsg, &payload)
suite.Require().NoError(err)

var merkleProof commitmenttypes.MerkleProof
err = suite.chainA.Codec.Unmarshal(payload.VerifyMembership.Proof, &merkleProof)
suite.Require().NoError(err)

root := commitmenttypes.NewMerkleRoot(suite.chainA.App.LastCommitID().Hash)
err = merkleProof.VerifyMembership(commitmenttypes.GetSDKSpecs(), root, merklePath, payload.VerifyMembership.Value)
suite.Require().NoError(err)

bz, err := json.Marshal(types.EmptyResult{})
suite.Require().NoError(err)

expDiscardedState = true
store.Set(testKey, value)

return &wasmvmtypes.Response{Data: bz}, wasmtesting.DefaultGasUsed, nil
})
},
nil,
},
{
>>>>>>> 6b118957 (fix: return correct types in wasm tests and add expected err (#5883))
"failure: default querier",
func() {
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) {
Expand All @@ -177,6 +274,7 @@ func (suite *TypesTestSuite) TestStargateQuery() {
return nil, wasmtesting.DefaultGasUsed, err
})
},
wasmvmtypes.UnsupportedRequest{Kind: fmt.Sprintf("'%s' path is not allowed from the contract", typeURL)},
},
}

Expand All @@ -192,7 +290,29 @@ func (suite *TypesTestSuite) TestStargateQuery() {

clientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), endpoint.ClientID)
clientState := endpoint.GetClientState()
<<<<<<< HEAD
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))
} else {
suite.Require().True(clientStore.Has(testKey))
}
>>>>>>> 6b118957 (fix: return correct types in wasm tests and add expected err (#5883))

// reset query plugins after each test
ibcwasm.SetQueryPlugins(types.NewDefaultQueryPlugins())
Expand Down

0 comments on commit cfaa309

Please sign in to comment.