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

Add new StargateMsg to return arbitrary protobuf messages and handle arbitrary queries #694

Closed
ethanfrey opened this issue Jan 5, 2021 · 3 comments · Fixed by #706
Closed
Assignees
Milestone

Comments

@ethanfrey
Copy link
Member

ethanfrey commented Jan 5, 2021

Stargate (plans to?) offer backwards compatible message codecs. We can start exposing this power to contract developers. Use with care, as it may break in a poorly done chain upgrade. But it will allow you to quickly call any native functionality exposed by a message, even an custom one in your app (not the cosmos sdk)

Updated

Add one new CosmosMsg variant as suggested by @webmaster128 in #694 (comment)

pub enum CosmosMsg {
    /// A Stargate message encoded the same way as a protobof [Any](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/any.proto).
    /// This is the same structure as messages in `TxBody` from [ADR-020](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-020-protobuf-transaction-encoding.md)
    #[cfg(feature="stargate")]
    Stargate { type: String, data: Binary },
    // ....
}

We also add a generic query format, which allows us to call the gRPC services for the query handler in any module. Type-safety of parsing the return value requires the caller to properly use the service files (tooling for that out of scope of this PR)

pub enum QueryRequest {
    /// A Stargate message encoded the same way as a protobof [Any](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/any.proto).
    /// The format is defined in [ADR-21](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-021-protobuf-query-encoding.md)
    #[cfg(feature="stargate")]
    Stargate { type: String, data: Binary },
    // ....
}

To understand the query format, look at some example query services.

I hide this all under a stargate feature flag, so contracts are rejected on upload if they run on another chain. Also, you should only assume the standard cosmos-sdk modules are available unless you require another chain-specific feature flag. Example, to use cyber congress link module via raw commands, the contract should require both stargate and cybercongress features so we can do proper static checks on upload (and ensure it doesn't fail on some key feature well after people have sent funds to it)


Original proposal (to make sense of the comments):

pub enum CosmosMsg{
    // ...
    Stargate(StargateMsg),
}

pub enum StargateMsg {
    // this is a protobuf-encoded message in stargate format
   Proto(Binary),
   // this is a json-encoded message in stargate format
   JSON(Binary),
}

The messages would be unmarshalled in wasmd and if properly parsed into sdk.Msg, dispatched into the handler.

@ethanfrey ethanfrey added this to the 0.14.0 milestone Jan 5, 2021
@webmaster128
Copy link
Member

The JSON representation is Stargate messages an Animo JSON compatibility layer, right? Do we really want to add Amino JSON support? This creates all kind of type registries to convert between Amino JSON and protobuf (which we do in CosmJS) and it is rather painful. I think we should prepare for the Amino JSON to fade out.

What about going protobuf only like this

pub enum CosmosMsg {
    /// A Stargate message encoded the same way as a protobof [Any](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/any.proto).
    /// This is the same structure as messages in `TxBody` from [ADR-020](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-020-protobuf-transaction-encoding.md)
    Any { type: String, data: Binary },
}

which can be created via

#[derive(ProtobufSerializationStuff)]
struct MyMsg {
   /// ...
}

impl From<MyMsg> for CosmosMsg {
    fn from(original: MyMsg) -> Self  {
        Self::Any {
            type: "/cosmos.bank.v1beta1.MsgMy",
            data: proto_encode(original),
        }
    }
}

At the end of the day this encodes the same as StargateMsg::Proto(Binary), but avoids two nesting levels.

@ethanfrey
Copy link
Member Author

I like your format (type string, binary data) which is protobuf style. However, we should name it Stargate as this does not work on all runtime (someone could connect cosmwasm 0.14 with cosmos sdk launchpad). And keep it under a Stargate feature flag, like staking is flagged.

It does avoid nesting, which is nice

@ethanfrey
Copy link
Member Author

After the discussion in #629 I realized we will want the query equivalent as well. I will update the issue accordingly

@ethanfrey ethanfrey changed the title Add new StargateMsg to return arbitrary protobuf messages Add new StargateMsg to return arbitrary protobuf messages and handle arbitrary queries Jan 7, 2021
@ethanfrey ethanfrey self-assigned this Jan 11, 2021
@mergify mergify bot closed this as completed in #706 Jan 12, 2021
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

Successfully merging a pull request may close this issue.

2 participants