Skip to content
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

Using custom any resolver to StargateQuerier #1454

Closed
dadamu opened this issue Jun 16, 2023 · 4 comments
Closed

Using custom any resolver to StargateQuerier #1454

dadamu opened this issue Jun 16, 2023 · 4 comments

Comments

@dadamu
Copy link
Contributor

dadamu commented Jun 16, 2023

Context

Currently StargateQuerier is using default any resolver provided by codec.Codec, which unpacks Any type to the underlying structure then builds the JSON message for wasmvm. That makes parsing the proto message difficult since parsing underlying structure into Any requires a custom decoder inside contract.

The definition of default codec any resolver can be traced here:
https://github.com/cosmos/cosmos-sdk/blob/13e61bd0c5c7950d137bbdfb171f95d87179ed29/codec/proto_codec.go#L148
https://github.com/cosmos/cosmos-sdk/blob/13e61bd0c5c7950d137bbdfb171f95d87179ed29/codec/json.go#L16

For example, Desmos profiles is a Any proto message, the json pass to wasmvm is:

{
    "@type": "/desmos.profiles.v3.Profile",
    "dtag": "abc",
    "nickname": "abc",
    "bio": "abc",
    ...other fields
}

Instead of a Any JSON can be easily deserialized

{
    "@type": "/desmos.profiles.v3.Profile",
    "value": "<bas64 encoded of desmos Profile>"
}

As a result, it is not easy to deserialize the Any structure in Rust like:

pub struct Any {
     #[prost(string, tag = "1")]
    pub type_url: ::prost::alloc::string::String,
    #[prost(bytes = "vec", tag = "2")]
    pub value: ::prost::alloc::vec::Vec<u8>,
}

impl<'de> Deserialize<'de> for Any {
   fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
       logic of deserialize the JSON into Any
    }
}

Solution

The solution can be implementing a custom any resolver to unpack to Any JSON without underlying structure, it would be like the following commit, then the Any deserialization will be easy in contract side:
https://github.com/desmos-labs/wasmd/commit/b7700dfcc403b58c9702f0c45f4016b9668a287e

What do you think? I can create a pull request if you think it is worth to do.

Edited: 05:00 UTC +0, June 20, 2023

@webmaster128
Copy link
Member

webmaster128 commented Jun 19, 2023

Instead of a proper any JSON format like

This is only a smaller point of the whole problem, but please note that in https://protobuf.dev/programming-guides/proto3/#json the flattening of the Any in JSON is specified. This might not be the best way to use it in a contract, but is correct from the standardization point of view.

@dadamu
Copy link
Contributor Author

dadamu commented Jun 20, 2023

@webmaster128 Thanks for the reference! I see and fixed the content.

@alpe
Copy link
Contributor

alpe commented Jun 30, 2023

Changing the response encoding would be a breaking change for existing contracts. We can not simply do this without a lot of communication and coordination work. I do see your use case but the query plugin provides an extension point to set your own Stargate query handler and use that with your community. Our default is RejectStargateQuerier currently.

The AcceptListStargateQuerier function accepts any codec.Codec implementation. Why not use this to wrap your logic with a custom MarshalJSON implementation that makes use of your WasmAnyResolver. Simple, testable and no need to fork.
WDYT?

@dadamu
Copy link
Contributor Author

dadamu commented Jul 5, 2023

@alpe I think it the best solution for my usecase. Thanks for the wonderful solution!

@dadamu dadamu closed this as completed Jul 5, 2023
RiccardoM pushed a commit to desmos-labs/desmos that referenced this issue Jul 11, 2023
## Description

After discussing with wasmd team, the best way to have custom wasm any
for desmos-bindings is injecting a custom any resolver to the codec.
CosmWasm/wasmd#1454 (comment)

This PR creates a custom `InterfaceRegistry` with a custom any resolver
function `Resolve` in order to have a custom any JSON encoding in base64
for our contract SDK. As a result, we can use `wasmd` directly without
forking it.

In the other word, it moves the logic of [this
commit](https://github.com/desmos-labs/wasmd/commit/b7700dfcc403b58c9702f0c45f4016b9668a287e)
into Desmos.

<!-- Add a description of the changes that this PR introduces and the
files that
are the most critical to review. -->

---

### Author Checklist

*All items are required. Please add a note to the item if the item is
not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type
prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json)
in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR
Targeting](https://github.com/desmos-labs/desmos/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building
modules](https://docs.cosmos.network/v0.44/building-modules/intro.html)
- [ ] included the necessary unit and integration
[tests](https://github.com/desmos-labs/desmos/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go
code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable
and please add
your handle next to the items reviewed if you only reviewed selected
items.*

I have...

- [ ] confirmed the correct [type
prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json)
in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants