-
Notifications
You must be signed in to change notification settings - Fork 657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add custom queries to wasm module #5261
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e548d6b
feat: add custom queries to wasm module
aeryz 75d7a9c
linter stuff as per usual.
DimitrisJim 7dc57e9
Merge branch 'main' into feat/wasm-custom-queries
crodriguezvega 6ae37b7
review comments
aeryz 3c87e16
lint
aeryz 8d62994
Merge branch 'main' into feat/wasm-custom-queries
DimitrisJim 827f7cb
Merge branch 'main' into feat/wasm-custom-queries
crodriguezvega 718b01f
Use wasmvm.Querier.
DimitrisJim e2368bd
Lint this bad boy
DimitrisJim 7164508
Small test nits.
DimitrisJim d27e469
add default querier and update documentation
crodriguezvega File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package types_test | ||
|
||
import ( | ||
"encoding/json" | ||
"math" | ||
|
||
wasmvm "github.com/CosmWasm/wasmvm" | ||
wasmvmtypes "github.com/CosmWasm/wasmvm/types" | ||
|
||
"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/internal/ibcwasm" | ||
wasmtesting "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/testing" | ||
"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types" | ||
"github.com/cosmos/ibc-go/v8/modules/core/exported" | ||
) | ||
|
||
type CustomQuery struct { | ||
Echo *QueryEcho `json:"echo,omitempty"` | ||
} | ||
|
||
type QueryEcho struct { | ||
Data string `json:"data"` | ||
} | ||
|
||
type CustomQueryHandler struct{} | ||
|
||
func (*CustomQueryHandler) GasConsumed() uint64 { | ||
return 0 | ||
} | ||
|
||
func (*CustomQueryHandler) Query(request wasmvmtypes.QueryRequest, gasLimit uint64) ([]byte, error) { | ||
var customQuery CustomQuery | ||
err := json.Unmarshal([]byte(request.Custom), &customQuery) | ||
if err != nil { | ||
return nil, wasmtesting.ErrMockContract | ||
} | ||
|
||
if customQuery.Echo != nil { | ||
data, err := json.Marshal(customQuery.Echo.Data) | ||
return data, err | ||
} | ||
|
||
return nil, wasmtesting.ErrMockContract | ||
} | ||
|
||
func (suite *TypesTestSuite) TestCustomQuery() { | ||
testCases := []struct { | ||
name string | ||
malleate func() | ||
}{ | ||
{ | ||
"success: custom query", | ||
func() { | ||
suite.mockVM.RegisterQueryCallback(types.StatusMsg{}, func(checksum wasmvm.Checksum, env wasmvmtypes.Env, queryMsg []byte, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) ([]byte, uint64, error) { | ||
echo := CustomQuery{ | ||
Echo: &QueryEcho{ | ||
Data: "hello world", | ||
}, | ||
} | ||
echoJSON, err := json.Marshal(echo) | ||
suite.Require().NoError(err) | ||
|
||
resp, err := querier.Query(wasmvmtypes.QueryRequest{ | ||
Custom: json.RawMessage(echoJSON), | ||
}, math.MaxUint64) | ||
suite.Require().NoError(err) | ||
|
||
var respData string | ||
err = json.Unmarshal(resp, &respData) | ||
suite.Require().NoError(err) | ||
suite.Require().Equal("hello world", respData) | ||
|
||
resp, err = json.Marshal(types.StatusResult{Status: exported.Active.String()}) | ||
suite.Require().NoError(err) | ||
|
||
return resp, wasmtesting.DefaultGasUsed, nil | ||
}) | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
suite.Run(tc.name, func() { | ||
suite.SetupWasmWithMockVM() | ||
|
||
endpoint := wasmtesting.NewWasmEndpoint(suite.chainA) | ||
err := endpoint.CreateClient() | ||
suite.Require().NoError(err) | ||
|
||
tc.malleate() | ||
|
||
clientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), endpoint.ClientID) | ||
clientState := endpoint.GetClientState() | ||
|
||
ibcwasm.SetQuerier(&CustomQueryHandler{}) | ||
clientState.Status(suite.chainA.GetContext(), clientStore, suite.chainA.App.AppCodec()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simon from Confio has warned us against
nil
queriers and they will not be allowed in the future, so it would be good to add a check here thatwasmQuerier
is not nil and also inNewKeeperWithVM
, just as we do now for the vm.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense but also, isn't it a better API for end users to pass
nil
toNewKeeperWithVM
and in the setter function, do:This way we could hide the default type and users won't have to give that to the
NewKeeperWithVM
function.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, basically this comment. Sorry lol: #5261 (comment)