cosmology-tech / telescope

A TypeScript Transpiler for Cosmos Protobufs ⚛️
https://cosmology.zone/products/telescope
Apache License 2.0
146 stars 43 forks source link

gRPC wasm Clients #102

Open liangping opened 2 years ago

liangping commented 2 years ago

NOTE: This might not strongly related to telescope.

Currently, most of web apps have to use Javascript/Typescript to connect with light client server, since most web explorers don’t support gRPC and http2.

It's very useful to build a gRPC client with Web assembly and http2. Once it is built, developers can easily invoke gRPC services underlying the Rest services. and it is smaller, faster, laser resources consumed.

With Telescope, This could be much earlier.

List all services

% ./grpcurl grpc-osmosis-ia.notional.ventures:443 list
bech32ibc.bech32ibc.v1beta1.Query
cosmos.auth.v1beta1.Query
cosmos.authz.v1beta1.Query
cosmos.bank.v1beta1.Query
cosmos.base.reflection.v1beta1.ReflectionService
cosmos.base.reflection.v2alpha1.ReflectionService
cosmos.base.tendermint.v1beta1.Service
cosmos.distribution.v1beta1.Query
cosmos.evidence.v1beta1.Query
cosmos.gov.v1beta1.Query
cosmos.params.v1beta1.Query
cosmos.slashing.v1beta1.Query
cosmos.staking.v1beta1.Query
cosmos.tx.v1beta1.Service
cosmos.upgrade.v1beta1.Query
cosmwasm.wasm.v1.Query
grpc.reflection.v1alpha.ServerReflection
ibc.applications.transfer.v1.Query
ibc.core.channel.v1.Query
ibc.core.client.v1.Query
ibc.core.connection.v1.Query
ibc.core.port.v1.Query
osmosis.claim.v1beta1.Query
osmosis.epochs.v1beta1.Query
osmosis.gamm.v1beta1.Query
osmosis.incentives.Query
osmosis.lockup.Query
osmosis.mint.v1beta1.Query
osmosis.poolincentives.v1beta1.Query
osmosis.superfluid.Query
osmosis.txfees.v1beta1.Query
testdata.Query

List Methods of a service:

% ./grpcurl grpc-osmosis-ia.notional.ventures:443 list cosmos.base.tendermint.v1beta1.Service
cosmos.base.tendermint.v1beta1.Service.GetBlockByHeight
cosmos.base.tendermint.v1beta1.Service.GetLatestBlock
cosmos.base.tendermint.v1beta1.Service.GetLatestValidatorSet
cosmos.base.tendermint.v1beta1.Service.GetNodeInfo
cosmos.base.tendermint.v1beta1.Service.GetSyncing
cosmos.base.tendermint.v1beta1.Service.GetValidatorSetByHeight

invoke gRPC calls:

Call cosmos.base.tendermint.v1beta1.Service.GetLatestBlock return proto message.

Therefore, We can call all services directly via gRPC

pyramation commented 2 years ago

thanks @liangping ! will be integrating this into our gRPC client class. Appreciate the help!

liangping commented 2 years ago

thanks @liangping ! will be integrating this into our gRPC client class. Appreciate the help!

Awesome! If so i would like to use this instead of the current LCD endpoint on ping.pub.

sascha1337 commented 2 years ago

gRPCFTW

pyramation commented 2 years ago

@sascha1337 @liangping does this look like a good reference implementation?

https://github.com/cosmos/cosmjs/blob/main/packages/stargate/src/modules/bank/queries.ts#L20-L59

It looks like they were actually using this? https://github.com/confio/cosmjs-types/blob/main/src/cosmos/bank/v1beta1/query.ts#L1079

I think we can improve it a bit?

Some ideas:

  1. class methods could be camelCase not SuperCase (this could also be configurable per dev's config).
  2. we can combine what is inside of QueryClientImpl here with how it's used here to make one cohesive class.

    Any feedback or thoughts you may have on how we should implement this?

liangping commented 2 years ago

On protobuf, It's good reference.

It might be almost same, except this one:

const rpc = createProtobufRpcClient(base);

gRPC client need Http2(almost all web explorer(chrome, firefox) don't support it so far), we need implement it with WASM.

liangping commented 2 years ago

look at

export function createProtobufRpcClient(base: QueryClient): ProtobufRpcClient {
  return {
    request: (service: string, method: string, data: Uint8Array): Promise<Uint8Array> => {
      const path = `/${service}/${method}`;
      return base.queryUnverified(path, data);
    },
  };
}

seems that we can just simply re-implement this method.

public async queryUnverified(path: string, request: Uint8Array): Promise<Uint8Array> {
    const response = await this.tmClient.abciQuery({
      path: path,
      data: request,
      prove: false,
    });

    if (response.code) {
      throw new Error(`Query failed with (${response.code}): ${response.log}`);
    }

    return response.value;
  }
liangping commented 2 years ago

I confirm that tmclient is implemented based on http, not http2 https://github.com/cosmos/cosmjs/blob/b63075aca18538a0c9f312dfaf43be271f5d39aa/packages/tendermint-rpc/src/tendermint34/tendermint34client.ts#L17

pyramation commented 2 years ago

thank you @liangping 🙌🏻

this is great to have all the information we need for this issue in one place.

I confirm that tmclient is implemented based on http, not http2 https://github.com/cosmos/cosmjs/blob/b63075aca18538a0c9f312dfaf43be271f5d39aa/packages/tendermint-rpc/src/tendermint34/tendermint34client.ts#L17

I think secret.js uses this for http2 https://github.com/improbable-eng/grpc-web

Should we look at what secret is doing? https://github.com/scrtlabs/secret.js/blob/master/src/secret_network_client.ts#L636-L723

liangping commented 2 years ago

I think we are almost there. Secret did amazing works. We can improve it. In this implementation, we need a gRPC proxy which is not friendly to users. That's why I want to use WASM.

pyramation commented 2 years ago

Looking at more clients out there, found this one: https://github.com/deeplay-io/nice-grpc

pyramation commented 2 years ago

https://github.com/cosmos/cosmjs/issues/885

sascha1337 commented 2 years ago

Sometimes GitHub ui sucks, using mobile GitHub app now, i see meantion and comments that completely were kinda hidden on desktop 🤦‍♂️

I've worked a lot on grpc / introspection methods and solutions lately, if combined with cosmology repo // chain registry api's, this is really powerful stuff, need and built this for myself, so time to get that wrapped and packed to npm sers.

sascha1337 commented 2 years ago

Also check efficiency // references here on bottom

https://github.com/cosmos/cosmos-sdk/pull/10045