telescope icon indicating copy to clipboard operation
telescope copied to clipboard

gRPC wasm Clients

Open liangping opened this issue 2 years ago • 13 comments

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

liangping avatar May 25 '22 04:05 liangping

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

pyramation avatar May 25 '22 04:05 pyramation

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.

liangping avatar May 25 '22 04:05 liangping

gRPCFTW

sascha1337 avatar May 26 '22 19:05 sascha1337

@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?

pyramation avatar May 26 '22 19:05 pyramation

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 avatar May 27 '22 01:05 liangping

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 avatar May 27 '22 01:05 liangping

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

liangping avatar May 27 '22 01:05 liangping

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

pyramation avatar May 27 '22 22:05 pyramation

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.

liangping avatar May 29 '22 00:05 liangping

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

pyramation avatar Jun 01 '22 21:06 pyramation

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

pyramation avatar Jun 01 '22 23:06 pyramation

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 avatar Jul 30 '22 11:07 sascha1337

Also check efficiency // references here on bottom

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

sascha1337 avatar Jul 30 '22 11:07 sascha1337